diff --git a/core/api/current.txt b/core/api/current.txt index 7eee5f4a7bf6f..5a30aaf8f9a3d 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -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 { diff --git a/core/java/android/text/style/TextAppearanceSpan.java b/core/java/android/text/style/TextAppearanceSpan.java index 85b7ae901b388..d61228b295af9 100644 --- a/core/java/android/text/style/TextAppearanceSpan.java +++ b/core/java/android/text/style/TextAppearanceSpan.java @@ -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 -1 - * if it does not specify one. + * Returns the text font weight specified by this span, or + * FontStyle.FONT_WEIGHT_UNSPECIFIED if it does not specify one. */ public int getTextFontWeight() { return mTextFontWeight; diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 1144b598e47ad..7a2aa48b76fb8 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -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; } diff --git a/graphics/java/android/graphics/fonts/FontStyle.java b/graphics/java/android/graphics/fonts/FontStyle.java index 09799fdf5a138..48969aa710594 100644 --- a/graphics/java/android/graphics/fonts/FontStyle.java +++ b/graphics/java/android/graphics/fonts/FontStyle.java @@ -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 */