From 5670baef9b56cf5451a7b5d8f0853d5c9a781294 Mon Sep 17 00:00:00 2001 From: Sally Date: Thu, 24 Sep 2020 18:27:31 +0000 Subject: Add support for Force Bold Text This CL: 1) Saves the original unbolded typeface and returns this in getTypeface 2) Sets Paint's typeface to a bolded one if the Setting is enabled, or a config value is YES 3) Bolds by adding 300 to the current font weight (400 is normal, 700 is considered bold, weight is capped at 1000) 4) Resets the Paint typeface to the original value if the Setting is disabled, or the config value is not YES Bug: b/110991537 Test: manual; unit tests Change-Id: If21190fd5ad9d2e1721ffe464945f00ff20f62c6 --- core/java/android/widget/TextView.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'core/java/android/widget/TextView.java') diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 6f14dfb89e6b..058ccb7938a4 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -735,6 +735,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private boolean mLocalesChanged = false; private int mTextSizeUnit = -1; + // True if force bold text feature is enabled. This feature makes all text bolder. + private boolean mForceBoldTextEnabled; + private Typeface mOriginalTypeface; + // True if setKeyListener() has been explicitly called private boolean mListenerChanged = false; // True if internationalized input should be used for numbers and date and time. @@ -1645,6 +1649,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener attributes.mTypefaceIndex = MONOSPACE; } + mForceBoldTextEnabled = getContext().getResources().getConfiguration().forceBoldText + == Configuration.FORCE_BOLD_TEXT_YES; applyTextAppearance(attributes); if (isPassword) { @@ -4267,6 +4273,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener invalidate(); } } + if (newConfig.forceBoldText == Configuration.FORCE_BOLD_TEXT_YES) { + mForceBoldTextEnabled = true; + setTypeface(getTypeface()); + } else if (newConfig.forceBoldText == Configuration.FORCE_BOLD_TEXT_NO + || newConfig.forceBoldText == Configuration.FORCE_BOLD_TEXT_UNDEFINED) { + mForceBoldTextEnabled = false; + setTypeface(getTypeface()); + } } /** @@ -4418,6 +4432,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * @attr ref android.R.styleable#TextView_textStyle */ public void setTypeface(@Nullable Typeface tf) { + mOriginalTypeface = tf; + if (mForceBoldTextEnabled) { + int newWeight = tf != null ? tf.getWeight() + 300 : 400; + newWeight = Math.min(newWeight, 1000); + int typefaceStyle = tf != null ? tf.getStyle() : 0; + boolean italic = (typefaceStyle & Typeface.ITALIC) != 0; + tf = Typeface.create(tf, newWeight, italic); + } if (mTextPaint.getTypeface() != tf) { mTextPaint.setTypeface(tf); @@ -4441,7 +4463,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener */ @InspectableProperty public Typeface getTypeface() { - return mTextPaint.getTypeface(); + return mOriginalTypeface; } /** -- cgit v1.2.3