diff options
| author | Seigo Nonaka <nona@google.com> | 2021-12-10 10:49:43 -0800 |
|---|---|---|
| committer | Seigo Nonaka <nona@google.com> | 2022-01-06 23:03:28 -0800 |
| commit | a1b7ced11c774d89b0b18b1076f5f0ce8598f0e3 (patch) | |
| tree | c445fc34bfc5eea6ce9ec9d194ed14d51551ac3f /core/java/android/text/TextLine.java | |
| parent | bb5068359996253178aba8fe0aad9a832988ae96 (diff) | |
Implement fallback line spacing for BoringLayout
The fallback line spacing is a feature of extending the line height
when the fallback font has taller glyph. This was implemented to
StaticLayout in Android P but not yet implemented in BoringLayout.
This CL enables this feature to the BoringLayout as well.
Not to break existing apps, change this behavior only if the
targetSdk version is T or later.
Bug: 210923482
Test: atest FallbackLineSpacingTest BoringLayoutFallbackLineSpacingTest
Test: atest CtsGraphicsTestCases
Test: atest CtsTextTestCases
Change-Id: I6214d52cde25a044bc6e246d2118e35d3a243c9d
Diffstat (limited to 'core/java/android/text/TextLine.java')
| -rw-r--r-- | core/java/android/text/TextLine.java | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/core/java/android/text/TextLine.java b/core/java/android/text/TextLine.java index 1a7ec7f99c95..2b396612cf3c 100644 --- a/core/java/android/text/TextLine.java +++ b/core/java/android/text/TextLine.java @@ -71,6 +71,8 @@ public class TextLine { private Spanned mSpanned; private PrecomputedText mComputed; + private boolean mUseFallbackExtent = false; + // The start and end of a potentially existing ellipsis on this text line. // We use them to filter out replacement and metric affecting spans on ellipsized away chars. private int mEllipsisStart; @@ -141,6 +143,7 @@ public class TextLine { tl.mTabs = null; tl.mChars = null; tl.mComputed = null; + tl.mUseFallbackExtent = false; tl.mMetricAffectingSpanSpanSet.recycle(); tl.mCharacterStyleSpanSet.recycle(); @@ -171,17 +174,20 @@ public class TextLine { * @param ellipsisStart the start of the ellipsis relative to the line * @param ellipsisEnd the end of the ellipsis relative to the line. When there * is no ellipsis, this should be equal to ellipsisStart. + * @param useFallbackLineSpacing true for enabling fallback line spacing. false for disabling + * fallback line spacing. */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public void set(TextPaint paint, CharSequence text, int start, int limit, int dir, Directions directions, boolean hasTabs, TabStops tabStops, - int ellipsisStart, int ellipsisEnd) { + int ellipsisStart, int ellipsisEnd, boolean useFallbackLineSpacing) { mPaint = paint; mText = text; mStart = start; mLen = limit - start; mDir = dir; mDirections = directions; + mUseFallbackExtent = useFallbackLineSpacing; if (mDirections == null) { throw new IllegalArgumentException("Directions cannot be null"); } @@ -845,6 +851,31 @@ public class TextLine { previousLeading); } + private void expandMetricsFromPaint(TextPaint wp, int start, int end, + int contextStart, int contextEnd, boolean runIsRtl, FontMetricsInt fmi) { + + final int previousTop = fmi.top; + final int previousAscent = fmi.ascent; + final int previousDescent = fmi.descent; + final int previousBottom = fmi.bottom; + final int previousLeading = fmi.leading; + + if (mCharsValid) { + int count = end - start; + int contextCount = contextEnd - contextStart; + wp.getFontMetricsInt(mChars, start, count, contextStart, contextCount, runIsRtl, + fmi); + } else { + int delta = mStart; + wp.getFontMetricsInt(mText, delta + start, delta + end, + delta + contextStart, delta + contextEnd, runIsRtl, fmi); + } + + updateMetrics(fmi, previousTop, previousAscent, previousDescent, previousBottom, + previousLeading); + } + + static void updateMetrics(FontMetricsInt fmi, int previousTop, int previousAscent, int previousDescent, int previousBottom, int previousLeading) { fmi.top = Math.min(fmi.top, previousTop); @@ -949,6 +980,10 @@ public class TextLine { shapeTextRun(consumer, wp, start, end, contextStart, contextEnd, runIsRtl, leftX); } + if (mUseFallbackExtent && fmi != null) { + expandMetricsFromPaint(wp, start, end, contextStart, contextEnd, runIsRtl, fmi); + } + if (c != null) { if (wp.bgColor != 0) { int previousColor = wp.getColor(); |
