Introduce FontStyle.FONT_WEIGHT_UNSPECIFIED constant

Introduce a new constant FontStyle.FONT_WEIGHT_UNSPECIFIED = -1, which is used when text weight is not specified. Also replace -1 with it in TextView, TextAppearanceSpan.

Bug: 256995042

Test: manually test
Change-Id: Ia8334f5513cc3e5b7383b67b24b27bf2866d05ad
This commit is contained in:
czq
2022-11-04 18:22:15 +08:00
parent 32ce057dd1
commit 2111954aaa
4 changed files with 19 additions and 11 deletions

View File

@@ -16583,6 +16583,7 @@ package android.graphics.fonts {
field public static final int FONT_WEIGHT_NORMAL = 400; // 0x190
field public static final int FONT_WEIGHT_SEMI_BOLD = 600; // 0x258
field public static final int FONT_WEIGHT_THIN = 100; // 0x64
field public static final int FONT_WEIGHT_UNSPECIFIED = -1; // 0xffffffff
}
public final class FontVariationAxis {

View File

@@ -149,7 +149,7 @@ public class TextAppearanceSpan extends MetricAffectingSpan implements Parcelabl
}
mTextFontWeight = a.getInt(com.android.internal.R.styleable
.TextAppearance_textFontWeight, -1);
.TextAppearance_textFontWeight, /*defValue*/ FontStyle.FONT_WEIGHT_UNSPECIFIED);
final String localeString = a.getString(com.android.internal.R.styleable
.TextAppearance_textLocale);
@@ -215,7 +215,7 @@ public class TextAppearanceSpan extends MetricAffectingSpan implements Parcelabl
mTextColorLink = linkColor;
mTypeface = null;
mTextFontWeight = -1;
mTextFontWeight = FontStyle.FONT_WEIGHT_UNSPECIFIED;
mTextLocales = null;
mShadowRadius = 0.0f;
@@ -359,8 +359,8 @@ public class TextAppearanceSpan extends MetricAffectingSpan implements Parcelabl
}
/**
* Returns the text font weight specified by this span, or <code>-1</code>
* if it does not specify one.
* Returns the text font weight specified by this span, or
* <code>FontStyle.FONT_WEIGHT_UNSPECIFIED</code> if it does not specify one.
*/
public int getTextFontWeight() {
return mTextFontWeight;

View File

@@ -2286,11 +2286,13 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
* @param familyName family name string, e.g. "serif"
* @param typefaceIndex an index of the typeface enum, e.g. SANS, SERIF.
* @param style a typeface style
* @param weight a weight value for the Typeface or -1 if not specified.
* @param weight a weight value for the Typeface or {@code FontStyle.FONT_WEIGHT_UNSPECIFIED}
* if not specified.
*/
private void setTypefaceFromAttrs(@Nullable Typeface typeface, @Nullable String familyName,
@XMLTypefaceAttr int typefaceIndex, @Typeface.Style int style,
@IntRange(from = -1, to = FontStyle.FONT_WEIGHT_MAX) int weight) {
@IntRange(from = FontStyle.FONT_WEIGHT_UNSPECIFIED, to = FontStyle.FONT_WEIGHT_MAX)
int weight) {
if (typeface == null && familyName != null) {
// Lookup normal Typeface from system font map.
final Typeface normalTypeface = Typeface.create(familyName, Typeface.NORMAL);
@@ -2317,7 +2319,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
private void resolveStyleAndSetTypeface(@NonNull Typeface typeface, @Typeface.Style int style,
@IntRange(from = -1, to = FontStyle.FONT_WEIGHT_MAX) int weight) {
@IntRange(from = FontStyle.FONT_WEIGHT_UNSPECIFIED, to = FontStyle.FONT_WEIGHT_MAX)
int weight) {
if (weight >= 0) {
weight = Math.min(FontStyle.FONT_WEIGHT_MAX, weight);
final boolean italic = (style & Typeface.ITALIC) != 0;
@@ -4018,7 +4021,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
boolean mFontFamilyExplicit = false;
int mTypefaceIndex = -1;
int mTextStyle = 0;
int mFontWeight = -1;
int mFontWeight = FontStyle.FONT_WEIGHT_UNSPECIFIED;
boolean mAllCaps = false;
int mShadowColor = 0;
float mShadowDx = 0, mShadowDy = 0, mShadowRadius = 0;
@@ -6943,18 +6946,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
if (isPassword) {
setTransformationMethod(PasswordTransformationMethod.getInstance());
setTypefaceFromAttrs(null/* fontTypeface */, null /* fontFamily */, MONOSPACE,
Typeface.NORMAL, -1 /* weight, not specifeid */);
Typeface.NORMAL, FontStyle.FONT_WEIGHT_UNSPECIFIED);
} else if (isVisiblePassword) {
if (mTransformation == PasswordTransformationMethod.getInstance()) {
forceUpdate = true;
}
setTypefaceFromAttrs(null/* fontTypeface */, null /* fontFamily */, MONOSPACE,
Typeface.NORMAL, -1 /* weight, not specified */);
Typeface.NORMAL, FontStyle.FONT_WEIGHT_UNSPECIFIED);
} else if (wasPassword || wasVisiblePassword) {
// not in password mode, clean up typeface and transformation
setTypefaceFromAttrs(null/* fontTypeface */, null /* fontFamily */,
DEFAULT_TYPEFACE /* typeface index */, Typeface.NORMAL,
-1 /* weight, not specified */);
FontStyle.FONT_WEIGHT_UNSPECIFIED);
if (mTransformation == PasswordTransformationMethod.getInstance()) {
forceUpdate = true;
}

View File

@@ -47,6 +47,10 @@ import java.util.Objects;
public final class FontStyle {
private static final String TAG = "FontStyle";
/**
* A default value when font weight is unspecified
*/
public static final int FONT_WEIGHT_UNSPECIFIED = -1;
/**
* A minimum weight value for the font
*/