From 5019e2765a1fb885d7c216639d65e4ddfda05fde Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Tue, 17 Oct 2023 17:52:22 +0900 Subject: Adjust the line height to the locale based font metrics The new API setMinimumFontMetrics API is useful for reserving a minimum amount of line spacing. Bug: 303326708 Test: CtsGraphicsTestCases CtsTextTestCases Change-Id: Ic25162b3a19d2b002b690560fde9512ea72bb492 --- graphics/java/android/graphics/Paint.java | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'graphics/java/android') diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 9fde0fd6e6ab..4eaa01309ab1 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -2113,6 +2113,31 @@ public class Paint { * The recommended additional space to add between lines of text. */ public float leading; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || !(o instanceof FontMetrics)) return false; + FontMetrics that = (FontMetrics) o; + return that.top == top && that.ascent == ascent && that.descent == descent + && that.bottom == bottom && that.leading == leading; + } + + @Override + public int hashCode() { + return Objects.hash(top, ascent, descent, bottom, leading); + } + + @Override + public String toString() { + return "FontMetrics{" + + "top=" + top + + ", ascent=" + ascent + + ", descent=" + descent + + ", bottom=" + bottom + + ", leading=" + leading + + '}'; + } } /** @@ -2309,6 +2334,33 @@ public class Paint { */ public int leading; + /** + * Set values from {@link FontMetricsInt}. + * @param fontMetricsInt a font metrics. + */ + @FlaggedApi(FLAG_FIX_LINE_HEIGHT_FOR_LOCALE) + public void set(@NonNull FontMetricsInt fontMetricsInt) { + top = fontMetricsInt.top; + ascent = fontMetricsInt.ascent; + descent = fontMetricsInt.descent; + bottom = fontMetricsInt.bottom; + leading = fontMetricsInt.leading; + } + + /** + * Set values from {@link FontMetrics} with rounding accordingly. + * @param fontMetrics a font metrics. + */ + @FlaggedApi(FLAG_FIX_LINE_HEIGHT_FOR_LOCALE) + public void set(@NonNull FontMetrics fontMetrics) { + // See GraphicsJNI::set_metrics_int method for consistency. + top = (int) Math.floor(fontMetrics.top); + ascent = Math.round(fontMetrics.ascent); + descent = Math.round(fontMetrics.descent); + bottom = (int) Math.ceil(fontMetrics.bottom); + leading = Math.round(fontMetrics.leading); + } + @Override public String toString() { return "FontMetricsInt: top=" + top + " ascent=" + ascent + " descent=" + descent + " bottom=" + bottom + -- cgit v1.2.3-59-g8ed1b