Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5bfd33e8 authored by Justin Ghan's avatar Justin Ghan
Browse files

Add Layout#getLineBottom with includeLineSpacing parameter

Bug: 247609599
Test: atest android.text.cts.StaticLayoutTest
Test: atest android.text.LayoutGetRangeForRectTest
Change-Id: I7363fe3974d712f992bca5aaefa5ca76499656d2
parent 78975396
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -45192,6 +45192,7 @@ package android.text {
    method public final int getLineAscent(int);
    method public final int getLineAscent(int);
    method public final int getLineBaseline(int);
    method public final int getLineBaseline(int);
    method public final int getLineBottom(int);
    method public final int getLineBottom(int);
    method public int getLineBottom(int, boolean);
    method public int getLineBounds(int, android.graphics.Rect);
    method public int getLineBounds(int, android.graphics.Rect);
    method public abstract boolean getLineContainsTab(int);
    method public abstract boolean getLineContainsTab(int);
    method public abstract int getLineCount();
    method public abstract int getLineCount();
+15 −11
Original line number Original line Diff line number Diff line
@@ -1841,7 +1841,7 @@ public abstract class Layout {
        // Find the first line whose vertical center is below the top of the area.
        // Find the first line whose vertical center is below the top of the area.
        int startLine = getLineForVertical((int) area.top);
        int startLine = getLineForVertical((int) area.top);
        int startLineTop = getLineTop(startLine);
        int startLineTop = getLineTop(startLine);
        int startLineBottom = getLineBottomWithoutSpacing(startLine);
        int startLineBottom = getLineBottom(startLine, /* includeLineSpacing= */ false);
        if (area.top > (startLineTop + startLineBottom) / 2f) {
        if (area.top > (startLineTop + startLineBottom) / 2f) {
            startLine++;
            startLine++;
            if (startLine >= getLineCount()) {
            if (startLine >= getLineCount()) {
@@ -1854,7 +1854,7 @@ public abstract class Layout {
        // Find the last line whose vertical center is above the bottom of the area.
        // Find the last line whose vertical center is above the bottom of the area.
        int endLine = getLineForVertical((int) area.bottom);
        int endLine = getLineForVertical((int) area.bottom);
        int endLineTop = getLineTop(endLine);
        int endLineTop = getLineTop(endLine);
        int endLineBottom = getLineBottomWithoutSpacing(endLine);
        int endLineBottom = getLineBottom(endLine, /* includeLineSpacing= */ false);
        if (area.bottom < (endLineTop + endLineBottom) / 2f) {
        if (area.bottom < (endLineTop + endLineBottom) / 2f) {
            endLine--;
            endLine--;
        }
        }
@@ -2229,18 +2229,22 @@ public abstract class Layout {
     * Return the vertical position of the bottom of the specified line.
     * Return the vertical position of the bottom of the specified line.
     */
     */
    public final int getLineBottom(int line) {
    public final int getLineBottom(int line) {
        return getLineTop(line + 1);
        return getLineBottom(line, /* includeLineSpacing= */ true);
    }
    }


    /**
    /**
     * Return the vertical position of the bottom of the specified line without the line spacing
     * Return the vertical position of the bottom of the specified line.
     * added.
     *
     *
     * @hide
     * @param line index of the line
     * @param includeLineSpacing whether to include the line spacing
     */
     */
    public final int getLineBottomWithoutSpacing(int line) {
    public int getLineBottom(int line, boolean includeLineSpacing) {
        if (includeLineSpacing) {
            return getLineTop(line + 1);
        } else {
            return getLineTop(line + 1) - getLineExtra(line);
            return getLineTop(line + 1) - getLineExtra(line);
        }
        }
    }


    /**
    /**
     * Return the vertical position of the baseline of the specified line.
     * Return the vertical position of the baseline of the specified line.
@@ -2394,7 +2398,7 @@ public abstract class Layout {


        int line = getLineForOffset(point);
        int line = getLineForOffset(point);
        int top = getLineTop(line);
        int top = getLineTop(line);
        int bottom = getLineBottomWithoutSpacing(line);
        int bottom = getLineBottom(line, /* includeLineSpacing= */ false);


        boolean clamped = shouldClampCursor(line);
        boolean clamped = shouldClampCursor(line);
        float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
        float h1 = getPrimaryHorizontal(point, clamped) - 0.5f;
@@ -2530,7 +2534,7 @@ public abstract class Layout {
        final int endline = getLineForOffset(end);
        final int endline = getLineForOffset(end);


        int top = getLineTop(startline);
        int top = getLineTop(startline);
        int bottom = getLineBottomWithoutSpacing(endline);
        int bottom = getLineBottom(endline, /* includeLineSpacing= */ false);


        if (startline == endline) {
        if (startline == endline) {
            addSelection(startline, start, end, top, bottom, consumer);
            addSelection(startline, start, end, top, bottom, consumer);
@@ -2559,7 +2563,7 @@ public abstract class Layout {
            }
            }


            top = getLineTop(endline);
            top = getLineTop(endline);
            bottom = getLineBottomWithoutSpacing(endline);
            bottom = getLineBottom(endline, /* includeLineSpacing= */ false);


            addSelection(endline, getLineStart(endline), end, top, bottom, consumer);
            addSelection(endline, getLineStart(endline), end, top, bottom, consumer);


+18 −14
Original line number Original line Diff line number Diff line
@@ -572,8 +572,8 @@ public class Editor {


        final Layout layout = mTextView.getLayout();
        final Layout layout = mTextView.getLayout();
        final int line = layout.getLineForOffset(mTextView.getSelectionStart());
        final int line = layout.getLineForOffset(mTextView.getSelectionStart());
        final int sourceHeight =
        final int sourceHeight = layout.getLineBottom(line, /* includeLineSpacing= */ false)
            layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line);
                - layout.getLineTop(line);
        final int height = (int)(sourceHeight * zoom);
        final int height = (int)(sourceHeight * zoom);
        final int width = (int)(aspectRatio * Math.max(sourceHeight, mMinLineHeightForMagnifier));
        final int width = (int)(aspectRatio * Math.max(sourceHeight, mMinLineHeightForMagnifier));


@@ -2340,7 +2340,7 @@ public class Editor {
        final int offset = mTextView.getSelectionStart();
        final int offset = mTextView.getSelectionStart();
        final int line = layout.getLineForOffset(offset);
        final int line = layout.getLineForOffset(offset);
        final int top = layout.getLineTop(line);
        final int top = layout.getLineTop(line);
        final int bottom = layout.getLineBottomWithoutSpacing(line);
        final int bottom = layout.getLineBottom(line, /* includeLineSpacing= */ false);


        final boolean clamped = layout.shouldClampCursor(line);
        final boolean clamped = layout.shouldClampCursor(line);
        updateCursorPosition(top, bottom, layout.getPrimaryHorizontal(offset, clamped));
        updateCursorPosition(top, bottom, layout.getPrimaryHorizontal(offset, clamped));
@@ -3443,7 +3443,7 @@ public class Editor {
        @Override
        @Override
        protected int getVerticalLocalPosition(int line) {
        protected int getVerticalLocalPosition(int line) {
            final Layout layout = mTextView.getLayout();
            final Layout layout = mTextView.getLayout();
            return layout.getLineBottomWithoutSpacing(line);
            return layout.getLineBottom(line, /* includeLineSpacing= */ false);
        }
        }


        @Override
        @Override
@@ -4109,7 +4109,8 @@ public class Editor {
        @Override
        @Override
        protected int getVerticalLocalPosition(int line) {
        protected int getVerticalLocalPosition(int line) {
            final Layout layout = mTextView.getLayout();
            final Layout layout = mTextView.getLayout();
            return layout.getLineBottomWithoutSpacing(line) - mContainerMarginTop;
            return layout.getLineBottom(line, /* includeLineSpacing= */ false)
                    - mContainerMarginTop;
        }
        }


        @Override
        @Override
@@ -4706,7 +4707,8 @@ public class Editor {
                                + viewportToContentVerticalOffset;
                                + viewportToContentVerticalOffset;
                        final float insertionMarkerBaseline = layout.getLineBaseline(line)
                        final float insertionMarkerBaseline = layout.getLineBaseline(line)
                                + viewportToContentVerticalOffset;
                                + viewportToContentVerticalOffset;
                        final float insertionMarkerBottom = layout.getLineBottomWithoutSpacing(line)
                        final float insertionMarkerBottom =
                                layout.getLineBottom(line, /* includeLineSpacing= */ false)
                                        + viewportToContentVerticalOffset;
                                        + viewportToContentVerticalOffset;
                        final boolean isTopVisible = mTextView
                        final boolean isTopVisible = mTextView
                                .isPositionVisible(insertionMarkerX, insertionMarkerTop);
                                .isPositionVisible(insertionMarkerX, insertionMarkerTop);
@@ -5137,7 +5139,7 @@ public class Editor {


                mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX
                mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX
                        - getHorizontalOffset() + getCursorOffset();
                        - getHorizontalOffset() + getCursorOffset();
                mPositionY = layout.getLineBottomWithoutSpacing(line);
                mPositionY = layout.getLineBottom(line, /* includeLineSpacing= */ false);


                // Take TextView's padding and scroll into account.
                // Take TextView's padding and scroll into account.
                mPositionX += mTextView.viewportToContentHorizontalOffset();
                mPositionX += mTextView.viewportToContentHorizontalOffset();
@@ -5233,8 +5235,8 @@ public class Editor {
            if (mNewMagnifierEnabled) {
            if (mNewMagnifierEnabled) {
                Layout layout = mTextView.getLayout();
                Layout layout = mTextView.getLayout();
                final int line = layout.getLineForOffset(getCurrentCursorOffset());
                final int line = layout.getLineForOffset(getCurrentCursorOffset());
                return layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line)
                return layout.getLineBottom(line, /* includeLineSpacing= */ false)
                        >= mMaxLineHeightForMagnifier;
                        - layout.getLineTop(line) >= mMaxLineHeightForMagnifier;
            }
            }
            final float magnifierContentHeight = Math.round(
            final float magnifierContentHeight = Math.round(
                    mMagnifierAnimator.mMagnifier.getHeight()
                    mMagnifierAnimator.mMagnifier.getHeight()
@@ -5389,7 +5391,8 @@ public class Editor {


            // Vertically snap to middle of current line.
            // Vertically snap to middle of current line.
            showPosInView.y = ((mTextView.getLayout().getLineTop(lineNumber)
            showPosInView.y = ((mTextView.getLayout().getLineTop(lineNumber)
                    + mTextView.getLayout().getLineBottomWithoutSpacing(lineNumber)) / 2.0f
                    + mTextView.getLayout()
                            .getLineBottom(lineNumber, /* includeLineSpacing= */ false)) / 2.0f
                    + mTextView.getTotalPaddingTop() - mTextView.getScrollY()) * mTextViewScaleY;
                    + mTextView.getTotalPaddingTop() - mTextView.getScrollY()) * mTextViewScaleY;
            return true;
            return true;
        }
        }
@@ -5473,7 +5476,8 @@ public class Editor {
                        updateCursorPosition();
                        updateCursorPosition();
                    }
                    }
                    final int lineHeight =
                    final int lineHeight =
                            layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line);
                            layout.getLineBottom(line, /* includeLineSpacing= */ false)
                                    - layout.getLineTop(line);
                    float zoom = mInitialZoom;
                    float zoom = mInitialZoom;
                    if (lineHeight < mMinLineHeightForMagnifier) {
                    if (lineHeight < mMinLineHeightForMagnifier) {
                        zoom = zoom * mMinLineHeightForMagnifier / lineHeight;
                        zoom = zoom * mMinLineHeightForMagnifier / lineHeight;
@@ -5823,8 +5827,8 @@ public class Editor {
        private MotionEvent transformEventForTouchThrough(MotionEvent ev) {
        private MotionEvent transformEventForTouchThrough(MotionEvent ev) {
            final Layout layout = mTextView.getLayout();
            final Layout layout = mTextView.getLayout();
            final int line = layout.getLineForOffset(getCurrentCursorOffset());
            final int line = layout.getLineForOffset(getCurrentCursorOffset());
            final int textHeight =
            final int textHeight = layout.getLineBottom(line, /* includeLineSpacing= */ false)
                    layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line);
                    - layout.getLineTop(line);
            // Transforms the touch events to screen coordinates.
            // Transforms the touch events to screen coordinates.
            // And also shift up to make the hit point is on the text.
            // And also shift up to make the hit point is on the text.
            // Note:
            // Note:
+3 −3
Original line number Original line Diff line number Diff line
@@ -9341,7 +9341,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        PointF point = convertFromScreenToContentCoordinates(gesture.getInsertionPoint());
        PointF point = convertFromScreenToContentCoordinates(gesture.getInsertionPoint());
        int line = mLayout.getLineForVertical((int) point.y);
        int line = mLayout.getLineForVertical((int) point.y);
        if (point.y < mLayout.getLineTop(line)
        if (point.y < mLayout.getLineTop(line)
                || point.y > mLayout.getLineBottomWithoutSpacing(line)) {
                || point.y > mLayout.getLineBottom(line, /* includeLineSpacing= */ false)) {
            return handleGestureFailure(gesture);
            return handleGestureFailure(gesture);
        }
        }
        if (point.x < mLayout.getLineLeft(line) || point.x > mLayout.getLineRight(line)) {
        if (point.x < mLayout.getLineLeft(line) || point.x > mLayout.getLineRight(line)) {
@@ -9369,7 +9369,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            // Both points are above the top of the first line.
            // Both points are above the top of the first line.
            return handleGestureFailure(gesture);
            return handleGestureFailure(gesture);
        }
        }
        if (yMin > mLayout.getLineBottomWithoutSpacing(line)) {
        if (yMin > mLayout.getLineBottom(line, /* includeLineSpacing= */ false)) {
            if (line == mLayout.getLineCount() - 1 || yMax < mLayout.getLineTop(line + 1)) {
            if (line == mLayout.getLineCount() - 1 || yMax < mLayout.getLineTop(line + 1)) {
                // The points are below the last line, or they are between two lines.
                // The points are below the last line, or they are between two lines.
                return handleGestureFailure(gesture);
                return handleGestureFailure(gesture);
@@ -9423,7 +9423,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        int line = mLayout.getLineForVertical((int) point.y);
        int line = mLayout.getLineForVertical((int) point.y);
        if (point.y < mLayout.getLineTop(line)
        if (point.y < mLayout.getLineTop(line)
                || point.y > mLayout.getLineBottomWithoutSpacing(line)) {
                || point.y > mLayout.getLineBottom(line, /* includeLineSpacing= */ false)) {
            return handleGestureFailure(gesture);
            return handleGestureFailure(gesture);
        }
        }
        if (point.x < mLayout.getLineLeft(line) || point.x > mLayout.getLineRight(line)) {
        if (point.x < mLayout.getLineLeft(line) || point.x > mLayout.getLineRight(line)) {
+2 −1
Original line number Original line Diff line number Diff line
@@ -90,7 +90,8 @@ public class LayoutGetRangeForRectTest {


        mLineCenters = new float[mLayout.getLineCount()];
        mLineCenters = new float[mLayout.getLineCount()];
        for (int i = 0; i < mLayout.getLineCount(); ++i) {
        for (int i = 0; i < mLayout.getLineCount(); ++i) {
            mLineCenters[i] = (mLayout.getLineTop(i) + mLayout.getLineBottomWithoutSpacing(i)) / 2f;
            mLineCenters[i] = (mLayout.getLineTop(i)
                    + mLayout.getLineBottom(i, /* includeLineSpacing= */ false)) / 2f;
        }
        }


        mGraphemeClusterSegmentIterator =
        mGraphemeClusterSegmentIterator =