diff options
| author | 2022-09-23 15:06:28 -0700 | |
|---|---|---|
| committer | 2022-09-26 09:25:26 -0700 | |
| commit | 5bfd33e89cb51611f2c9d033944c7996e932743a (patch) | |
| tree | be36797b2c75527151bff00875460bea1e3509ef | |
| parent | 789753961d6e54fe4a45dad91bb7ef15590916fb (diff) | |
Add Layout#getLineBottom with includeLineSpacing parameter
Bug: 247609599
Test: atest android.text.cts.StaticLayoutTest
Test: atest android.text.LayoutGetRangeForRectTest
Change-Id: I7363fe3974d712f992bca5aaefa5ca76499656d2
| -rw-r--r-- | core/api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/text/Layout.java | 26 | ||||
| -rw-r--r-- | core/java/android/widget/Editor.java | 32 | ||||
| -rw-r--r-- | core/java/android/widget/TextView.java | 6 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/text/LayoutGetRangeForRectTest.java | 3 |
5 files changed, 39 insertions, 29 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index fdc106c4078a..954a63b2a8ba 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -45192,6 +45192,7 @@ package android.text { method public final int getLineAscent(int); method public final int getLineBaseline(int); method public final int getLineBottom(int); + method public int getLineBottom(int, boolean); method public int getLineBounds(int, android.graphics.Rect); method public abstract boolean getLineContainsTab(int); method public abstract int getLineCount(); diff --git a/core/java/android/text/Layout.java b/core/java/android/text/Layout.java index b5f7c545aa07..dbb41f47a495 100644 --- a/core/java/android/text/Layout.java +++ b/core/java/android/text/Layout.java @@ -1841,7 +1841,7 @@ public abstract class Layout { // Find the first line whose vertical center is below the top of the area. int startLine = getLineForVertical((int) area.top); int startLineTop = getLineTop(startLine); - int startLineBottom = getLineBottomWithoutSpacing(startLine); + int startLineBottom = getLineBottom(startLine, /* includeLineSpacing= */ false); if (area.top > (startLineTop + startLineBottom) / 2f) { startLine++; 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. int endLine = getLineForVertical((int) area.bottom); int endLineTop = getLineTop(endLine); - int endLineBottom = getLineBottomWithoutSpacing(endLine); + int endLineBottom = getLineBottom(endLine, /* includeLineSpacing= */ false); if (area.bottom < (endLineTop + endLineBottom) / 2f) { endLine--; } @@ -2229,17 +2229,21 @@ public abstract class Layout { * Return the vertical position of the bottom of the specified 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 - * added. + * Return the vertical position of the bottom of the specified line. * - * @hide + * @param line index of the line + * @param includeLineSpacing whether to include the line spacing */ - public final int getLineBottomWithoutSpacing(int line) { - return getLineTop(line + 1) - getLineExtra(line); + public int getLineBottom(int line, boolean includeLineSpacing) { + if (includeLineSpacing) { + return getLineTop(line + 1); + } else { + return getLineTop(line + 1) - getLineExtra(line); + } } /** @@ -2394,7 +2398,7 @@ public abstract class Layout { int line = getLineForOffset(point); int top = getLineTop(line); - int bottom = getLineBottomWithoutSpacing(line); + int bottom = getLineBottom(line, /* includeLineSpacing= */ false); boolean clamped = shouldClampCursor(line); float h1 = getPrimaryHorizontal(point, clamped) - 0.5f; @@ -2530,7 +2534,7 @@ public abstract class Layout { final int endline = getLineForOffset(end); int top = getLineTop(startline); - int bottom = getLineBottomWithoutSpacing(endline); + int bottom = getLineBottom(endline, /* includeLineSpacing= */ false); if (startline == endline) { addSelection(startline, start, end, top, bottom, consumer); @@ -2559,7 +2563,7 @@ public abstract class Layout { } top = getLineTop(endline); - bottom = getLineBottomWithoutSpacing(endline); + bottom = getLineBottom(endline, /* includeLineSpacing= */ false); addSelection(endline, getLineStart(endline), end, top, bottom, consumer); diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 424b8aef7dae..8f590f89fa64 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -572,8 +572,8 @@ public class Editor { final Layout layout = mTextView.getLayout(); final int line = layout.getLineForOffset(mTextView.getSelectionStart()); - final int sourceHeight = - layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line); + final int sourceHeight = layout.getLineBottom(line, /* includeLineSpacing= */ false) + - layout.getLineTop(line); final int height = (int)(sourceHeight * zoom); final int width = (int)(aspectRatio * Math.max(sourceHeight, mMinLineHeightForMagnifier)); @@ -2340,7 +2340,7 @@ public class Editor { final int offset = mTextView.getSelectionStart(); final int line = layout.getLineForOffset(offset); 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); updateCursorPosition(top, bottom, layout.getPrimaryHorizontal(offset, clamped)); @@ -3443,7 +3443,7 @@ public class Editor { @Override protected int getVerticalLocalPosition(int line) { final Layout layout = mTextView.getLayout(); - return layout.getLineBottomWithoutSpacing(line); + return layout.getLineBottom(line, /* includeLineSpacing= */ false); } @Override @@ -4109,7 +4109,8 @@ public class Editor { @Override protected int getVerticalLocalPosition(int line) { final Layout layout = mTextView.getLayout(); - return layout.getLineBottomWithoutSpacing(line) - mContainerMarginTop; + return layout.getLineBottom(line, /* includeLineSpacing= */ false) + - mContainerMarginTop; } @Override @@ -4706,8 +4707,9 @@ public class Editor { + viewportToContentVerticalOffset; final float insertionMarkerBaseline = layout.getLineBaseline(line) + viewportToContentVerticalOffset; - final float insertionMarkerBottom = layout.getLineBottomWithoutSpacing(line) - + viewportToContentVerticalOffset; + final float insertionMarkerBottom = + layout.getLineBottom(line, /* includeLineSpacing= */ false) + + viewportToContentVerticalOffset; final boolean isTopVisible = mTextView .isPositionVisible(insertionMarkerX, insertionMarkerTop); final boolean isBottomVisible = mTextView @@ -5137,7 +5139,7 @@ public class Editor { mPositionX = getCursorHorizontalPosition(layout, offset) - mHotspotX - getHorizontalOffset() + getCursorOffset(); - mPositionY = layout.getLineBottomWithoutSpacing(line); + mPositionY = layout.getLineBottom(line, /* includeLineSpacing= */ false); // Take TextView's padding and scroll into account. mPositionX += mTextView.viewportToContentHorizontalOffset(); @@ -5233,8 +5235,8 @@ public class Editor { if (mNewMagnifierEnabled) { Layout layout = mTextView.getLayout(); final int line = layout.getLineForOffset(getCurrentCursorOffset()); - return layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line) - >= mMaxLineHeightForMagnifier; + return layout.getLineBottom(line, /* includeLineSpacing= */ false) + - layout.getLineTop(line) >= mMaxLineHeightForMagnifier; } final float magnifierContentHeight = Math.round( mMagnifierAnimator.mMagnifier.getHeight() @@ -5389,7 +5391,8 @@ public class Editor { // Vertically snap to middle of current line. 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; return true; } @@ -5473,7 +5476,8 @@ public class Editor { updateCursorPosition(); } final int lineHeight = - layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line); + layout.getLineBottom(line, /* includeLineSpacing= */ false) + - layout.getLineTop(line); float zoom = mInitialZoom; if (lineHeight < mMinLineHeightForMagnifier) { zoom = zoom * mMinLineHeightForMagnifier / lineHeight; @@ -5823,8 +5827,8 @@ public class Editor { private MotionEvent transformEventForTouchThrough(MotionEvent ev) { final Layout layout = mTextView.getLayout(); final int line = layout.getLineForOffset(getCurrentCursorOffset()); - final int textHeight = - layout.getLineBottomWithoutSpacing(line) - layout.getLineTop(line); + final int textHeight = layout.getLineBottom(line, /* includeLineSpacing= */ false) + - layout.getLineTop(line); // Transforms the touch events to screen coordinates. // And also shift up to make the hit point is on the text. // Note: diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 41d00a267c59..ee2cd536a6a7 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -9341,7 +9341,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener PointF point = convertFromScreenToContentCoordinates(gesture.getInsertionPoint()); int line = mLayout.getLineForVertical((int) point.y); if (point.y < mLayout.getLineTop(line) - || point.y > mLayout.getLineBottomWithoutSpacing(line)) { + || point.y > mLayout.getLineBottom(line, /* includeLineSpacing= */ false)) { return handleGestureFailure(gesture); } 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. 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)) { // The points are below the last line, or they are between two lines. return handleGestureFailure(gesture); @@ -9423,7 +9423,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener int line = mLayout.getLineForVertical((int) point.y); if (point.y < mLayout.getLineTop(line) - || point.y > mLayout.getLineBottomWithoutSpacing(line)) { + || point.y > mLayout.getLineBottom(line, /* includeLineSpacing= */ false)) { return handleGestureFailure(gesture); } if (point.x < mLayout.getLineLeft(line) || point.x > mLayout.getLineRight(line)) { diff --git a/core/tests/coretests/src/android/text/LayoutGetRangeForRectTest.java b/core/tests/coretests/src/android/text/LayoutGetRangeForRectTest.java index 32fdb5e850bd..787a4055b49d 100644 --- a/core/tests/coretests/src/android/text/LayoutGetRangeForRectTest.java +++ b/core/tests/coretests/src/android/text/LayoutGetRangeForRectTest.java @@ -90,7 +90,8 @@ public class LayoutGetRangeForRectTest { mLineCenters = new float[mLayout.getLineCount()]; 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 = |