From 9a43e6cb40c8ac8977bb37c9af28f928d2591d10 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Wed, 12 Jul 2023 16:34:31 +0900 Subject: [PATCH] Stop ellipsize in system server System server don't have UI but doing ellipsize with real font with hard coded font size and hard coded width. This is the only font usage in the system server, so let's remove this for preparing skiping font initialization in the system server. In the system server, this method is used for loading label. During loading labels, the hard coded font size 42 px and 1000px width has been used. This is equivalent to about 41 characters in Roboto. No behavior changes to the this public API users except for system server. Bug: 291239366 Test: Manually done by booting device (Pixel 5) Change-Id: I71c940de4b76bf32c14ead66a7912dfb8f25a384 --- core/java/android/text/TextUtils.java | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java index f256210365ba1..447c3bcdc186b 100644 --- a/core/java/android/text/TextUtils.java +++ b/core/java/android/text/TextUtils.java @@ -27,6 +27,7 @@ import android.annotation.PluralsRes; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.content.res.Resources; +import android.graphics.Typeface; import android.icu.lang.UCharacter; import android.icu.text.CaseMap; import android.icu.text.Edits; @@ -2482,12 +2483,28 @@ public class TextUtils { if (ellipsizeDip == 0) { return gettingCleaned.toString(); } else { - // Truncate - final TextPaint paint = new TextPaint(); - paint.setTextSize(42); + final float assumedFontSizePx = 42; + if (Typeface.getSystemFontMap().isEmpty()) { + // In the system server, the font files may not be loaded, so unable to perform + // ellipsize, so use the estimated char count for the ellipsize. - return TextUtils.ellipsize(gettingCleaned.toString(), paint, ellipsizeDip, - TextUtils.TruncateAt.END); + // The median of glyph widths of the Roboto is 0.57em, so use it as a reference + // of the glyph width. + final float assumedCharWidthInEm = 0.57f; + final float assumedCharWidthInPx = assumedFontSizePx * assumedCharWidthInEm; + + // Even if the argument name is `ellipsizeDip`, the unit of this argument is pixels. + final int charCount = (int) ((ellipsizeDip + 0.5f) / assumedCharWidthInPx); + return TextUtils.trimToSize(gettingCleaned.toString(), charCount) + + getEllipsisString(TruncateAt.END); + } else { + // Truncate + final TextPaint paint = new TextPaint(); + paint.setTextSize(assumedFontSizePx); + + return TextUtils.ellipsize(gettingCleaned.toString(), paint, ellipsizeDip, + TextUtils.TruncateAt.END); + } } }