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
This commit is contained in:
Sally
2020-09-24 18:27:31 +00:00
parent e636cf3121
commit 5670baef9b

View File

@@ -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;
}
/**