From 78c774defb238c05c42b34a12b6b3b0c64844ed7 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Fri, 10 Dec 2021 10:49:43 -0800 Subject: 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. This is a 2nd attempt of Ia6d6f9f44e73ddaf5e8fe9a8aead7a53efbddd44 The root cause of SystemUI crash was wrong API usage. (start, end) was passed instead of (start, count). Bug: 210923482 Test: atest FallbackLineSpacingTest BoringLayoutFallbackLineSpacingTest Test: atest CtsGraphicsTestCases Test: atest CtsTextTestCases Test: atest SystemUITests Change-Id: I9137607b0120934f7ad2a12c0f0b8aaa52915831 --- graphics/java/android/graphics/Paint.java | 136 +++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 1 deletion(-) (limited to 'graphics/java/android') diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 42e470b7f660..eefad8d0e4de 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -46,6 +46,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Locale; +import java.util.Objects; /** * The Paint class holds the style and color information about how to draw @@ -2130,6 +2131,116 @@ public class Paint { return fm; } + /** + * Returns the font metrics value for the given text. + * + * If the text is rendered with multiple font files, this function returns the large ascent and + * descent that are enough for drawing all font files. + * + * The context range is used for shaping context. Some script, e.g. Arabic or Devanagari, + * changes letter shape based on its location or surrounding characters. + * + * @param text a text to be measured. + * @param start a starting offset in the text. + * @param count a length of the text to be measured. + * @param contextStart a context starting offset in the text. + * @param contextCount a length of the context to be used. + * @param isRtl true if measuring on RTL context, otherwise false. + * @param outMetrics the output font metrics. + */ + public void getFontMetricsInt( + @NonNull CharSequence text, + @IntRange(from = 0) int start, @IntRange(from = 0) int count, + @IntRange(from = 0) int contextStart, @IntRange(from = 0) int contextCount, + boolean isRtl, + @NonNull FontMetricsInt outMetrics) { + + if (text == null) { + throw new IllegalArgumentException("text must not be null"); + } + if (start < 0 || start >= text.length()) { + throw new IllegalArgumentException("start argument is out of bounds."); + } + if (count < 0 || start + count > text.length()) { + throw new IllegalArgumentException("count argument is out of bounds."); + } + if (contextStart < 0 || contextStart >= text.length()) { + throw new IllegalArgumentException("ctxStart argument is out of bounds."); + } + if (contextCount < 0 || contextStart + contextCount > text.length()) { + throw new IllegalArgumentException("ctxCount argument is out of bounds."); + } + if (outMetrics == null) { + throw new IllegalArgumentException("outMetrics must not be null."); + } + + if (count == 0) { + getFontMetricsInt(outMetrics); + return; + } + + if (text instanceof String) { + nGetFontMetricsIntForText(mNativePaint, (String) text, start, count, contextStart, + contextCount, isRtl, outMetrics); + } else { + char[] buf = TemporaryBuffer.obtain(contextCount); + TextUtils.getChars(text, contextStart, contextStart + contextCount, buf, 0); + nGetFontMetricsIntForText(mNativePaint, buf, start - contextStart, count, 0, + contextCount, isRtl, outMetrics); + } + + } + + /** + * Returns the font metrics value for the given text. + * + * If the text is rendered with multiple font files, this function returns the large ascent and + * descent that are enough for drawing all font files. + * + * The context range is used for shaping context. Some script, e.g. Arabic or Devanagari, + * changes letter shape based on its location or surrounding characters. + * + * @param text a text to be measured. + * @param start a starting offset in the text. + * @param count a length of the text to be measured. + * @param contextStart a context starting offset in the text. + * @param contextCount a length of the context to be used. + * @param isRtl true if measuring on RTL context, otherwise false. + * @param outMetrics the output font metrics. + */ + public void getFontMetricsInt(@NonNull char[] text, + @IntRange(from = 0) int start, @IntRange(from = 0) int count, + @IntRange(from = 0) int contextStart, @IntRange(from = 0) int contextCount, + boolean isRtl, + @NonNull FontMetricsInt outMetrics) { + if (text == null) { + throw new IllegalArgumentException("text must not be null"); + } + if (start < 0 || start >= text.length) { + throw new IllegalArgumentException("start argument is out of bounds."); + } + if (count < 0 || start + count > text.length) { + throw new IllegalArgumentException("count argument is out of bounds."); + } + if (contextStart < 0 || contextStart >= text.length) { + throw new IllegalArgumentException("ctxStart argument is out of bounds."); + } + if (contextCount < 0 || contextStart + contextCount > text.length) { + throw new IllegalArgumentException("ctxCount argument is out of bounds."); + } + if (outMetrics == null) { + throw new IllegalArgumentException("outMetrics must not be null."); + } + + if (count == 0) { + getFontMetricsInt(outMetrics); + return; + } + + nGetFontMetricsIntForText(mNativePaint, text, start, count, contextStart, contextCount, + isRtl, outMetrics); + } + /** * Convenience method for callers that want to have FontMetrics values as * integers. @@ -2163,6 +2274,23 @@ public class Paint { " descent=" + descent + " bottom=" + bottom + " leading=" + leading; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof FontMetricsInt)) return false; + FontMetricsInt that = (FontMetricsInt) o; + return top == that.top + && ascent == that.ascent + && descent == that.descent + && bottom == that.bottom + && leading == that.leading; + } + + @Override + public int hashCode() { + return Objects.hash(top, ascent, descent, bottom, leading); + } } /** @@ -3117,6 +3245,13 @@ public class Paint { int contextStart, int contextEnd, boolean isRtl, int offset); private static native int nGetOffsetForAdvance(long paintPtr, char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl, float advance); + private static native void nGetFontMetricsIntForText(long paintPtr, char[] text, + int start, int count, int ctxStart, int ctxCount, boolean isRtl, + FontMetricsInt outMetrics); + private static native void nGetFontMetricsIntForText(long paintPtr, String text, + int start, int count, int ctxStart, int ctxCount, boolean isRtl, + FontMetricsInt outMetrics); + // ---------------- @FastNative ------------------------ @@ -3130,7 +3265,6 @@ public class Paint { @FastNative private static native int nGetFontMetricsInt(long paintPtr, FontMetricsInt fmi); - // ---------------- @CriticalNative ------------------------ @CriticalNative -- cgit v1.2.3-59-g8ed1b