summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Seigo Nonaka <nona@google.com> 2018-10-22 19:29:45 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2018-10-22 19:29:45 +0000
commit04aad28f16ed8cb7ea9a825a20d9965b9265cee3 (patch)
tree5f22192453f6c2ffa31a9ae88fd7a83c1a14e021
parentcded3f16a0f49a9a6fc962e065079f4f7cf8c12f (diff)
parent9958c4ab21b9df323a29915181ab1f278f06ab11 (diff)
Merge "Introduce FontStyle for future extensibility"
-rwxr-xr-xapi/current.txt35
-rw-r--r--core/java/android/text/style/TextAppearanceSpan.java4
-rw-r--r--core/java/android/widget/TextView.java8
-rw-r--r--graphics/java/android/graphics/Typeface.java3
-rw-r--r--graphics/java/android/graphics/fonts/Font.java143
-rw-r--r--graphics/java/android/graphics/fonts/FontFamily.java2
-rw-r--r--graphics/java/android/graphics/fonts/FontStyle.java256
-rw-r--r--graphics/java/android/graphics/fonts/SystemFonts.java3
8 files changed, 333 insertions, 121 deletions
diff --git a/api/current.txt b/api/current.txt
index c697ffe4838e..21bab7cf5d1e 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -15317,20 +15317,9 @@ package android.graphics.fonts {
method public java.nio.ByteBuffer getBuffer();
method public java.io.File getFile();
method public android.os.LocaleList getLocaleList();
+ method public int getSlant();
method public int getTtcIndex();
method public int getWeight();
- method public boolean isItalic();
- field public static final int FONT_WEIGHT_BLACK = 900; // 0x384
- field public static final int FONT_WEIGHT_BOLD = 700; // 0x2bc
- field public static final int FONT_WEIGHT_EXTRA_BOLD = 800; // 0x320
- field public static final int FONT_WEIGHT_EXTRA_LIGHT = 200; // 0xc8
- field public static final int FONT_WEIGHT_LIGHT = 300; // 0x12c
- field public static final int FONT_WEIGHT_MAX = 1000; // 0x3e8
- field public static final int FONT_WEIGHT_MEDIUM = 500; // 0x1f4
- field public static final int FONT_WEIGHT_MIN = 1; // 0x1
- 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
}
public static class Font.Builder {
@@ -15343,7 +15332,7 @@ package android.graphics.fonts {
method public android.graphics.fonts.Font build() throws java.io.IOException;
method public android.graphics.fonts.Font.Builder setFontVariationSettings(java.lang.String);
method public android.graphics.fonts.Font.Builder setFontVariationSettings(android.graphics.fonts.FontVariationAxis[]);
- method public android.graphics.fonts.Font.Builder setItalic(boolean);
+ method public android.graphics.fonts.Font.Builder setSlant(int);
method public android.graphics.fonts.Font.Builder setTtcIndex(int);
method public android.graphics.fonts.Font.Builder setWeight(int);
}
@@ -15359,6 +15348,26 @@ package android.graphics.fonts {
method public android.graphics.fonts.FontFamily build();
}
+ public final class FontStyle {
+ ctor public FontStyle();
+ ctor public FontStyle(int, int);
+ method public int getSlant();
+ method public int getWeight();
+ field public static final int FONT_SLANT_ITALIC = 1; // 0x1
+ field public static final int FONT_SLANT_UPRIGHT = 0; // 0x0
+ field public static final int FONT_WEIGHT_BLACK = 900; // 0x384
+ field public static final int FONT_WEIGHT_BOLD = 700; // 0x2bc
+ field public static final int FONT_WEIGHT_EXTRA_BOLD = 800; // 0x320
+ field public static final int FONT_WEIGHT_EXTRA_LIGHT = 200; // 0xc8
+ field public static final int FONT_WEIGHT_LIGHT = 300; // 0x12c
+ field public static final int FONT_WEIGHT_MAX = 1000; // 0x3e8
+ field public static final int FONT_WEIGHT_MEDIUM = 500; // 0x1f4
+ field public static final int FONT_WEIGHT_MIN = 1; // 0x1
+ 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
+ }
+
public final class FontVariationAxis {
ctor public FontVariationAxis(java.lang.String, float);
method public static android.graphics.fonts.FontVariationAxis[] fromFontVariationSettings(java.lang.String);
diff --git a/core/java/android/text/style/TextAppearanceSpan.java b/core/java/android/text/style/TextAppearanceSpan.java
index f846a356d8fa..23557694a48d 100644
--- a/core/java/android/text/style/TextAppearanceSpan.java
+++ b/core/java/android/text/style/TextAppearanceSpan.java
@@ -22,7 +22,7 @@ import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.LeakyTypefaceStorage;
import android.graphics.Typeface;
-import android.graphics.fonts.Font;
+import android.graphics.fonts.FontStyle;
import android.os.LocaleList;
import android.os.Parcel;
import android.text.ParcelableSpan;
@@ -490,7 +490,7 @@ public class TextAppearanceSpan extends MetricAffectingSpan implements Parcelabl
if (styledTypeface != null) {
final Typeface readyTypeface;
if (mTextFontWeight >= 0) {
- final int weight = Math.min(Font.FONT_WEIGHT_MAX, mTextFontWeight);
+ final int weight = Math.min(FontStyle.FONT_WEIGHT_MAX, mTextFontWeight);
final boolean italic = (style & Typeface.ITALIC) != 0;
readyTypeface = ds.setTypeface(Typeface.create(styledTypeface, weight, italic));
} else {
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 3dd6fd1410bd..66809dbc4e45 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -63,7 +63,7 @@ import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
-import android.graphics.fonts.Font;
+import android.graphics.fonts.FontStyle;
import android.graphics.fonts.FontVariationAxis;
import android.icu.text.DecimalFormatSymbols;
import android.os.AsyncTask;
@@ -2073,7 +2073,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
*/
private void setTypefaceFromAttrs(@Nullable Typeface typeface, @Nullable String familyName,
@XMLTypefaceAttr int typefaceIndex, @Typeface.Style int style,
- @IntRange(from = -1, to = Font.FONT_WEIGHT_MAX) int weight) {
+ @IntRange(from = -1, 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);
@@ -2100,9 +2100,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
private void resolveStyleAndSetTypeface(@NonNull Typeface typeface, @Typeface.Style int style,
- @IntRange(from = -1, to = Font.FONT_WEIGHT_MAX) int weight) {
+ @IntRange(from = -1, to = FontStyle.FONT_WEIGHT_MAX) int weight) {
if (weight >= 0) {
- weight = Math.min(Font.FONT_WEIGHT_MAX, weight);
+ weight = Math.min(FontStyle.FONT_WEIGHT_MAX, weight);
final boolean italic = (style & Typeface.ITALIC) != 0;
setTypeface(Typeface.create(typeface, weight, italic));
} else {
diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java
index 7ad207f339d1..ba47300210cd 100644
--- a/graphics/java/android/graphics/Typeface.java
+++ b/graphics/java/android/graphics/Typeface.java
@@ -27,6 +27,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.content.res.AssetManager;
+import android.graphics.fonts.FontStyle;
import android.graphics.fonts.FontVariationAxis;
import android.graphics.fonts.SystemFonts;
import android.net.Uri;
@@ -148,7 +149,7 @@ public class Typeface {
@UnsupportedAppUsage
private @Style int mStyle = 0;
- private @IntRange(from = 0, to = android.graphics.fonts.Font.FONT_WEIGHT_MAX) int mWeight = 0;
+ private @IntRange(from = 0, to = FontStyle.FONT_WEIGHT_MAX) int mWeight = 0;
// Value for weight and italic. Indicates the value is resolved by font metadata.
// Must be the same as the C++ constant in core/jni/android/graphics/FontFamily.cpp
diff --git a/graphics/java/android/graphics/fonts/Font.java b/graphics/java/android/graphics/fonts/Font.java
index bd1ac25bf8df..f426b2d3465b 100644
--- a/graphics/java/android/graphics/fonts/Font.java
+++ b/graphics/java/android/graphics/fonts/Font.java
@@ -51,61 +51,6 @@ public final class Font {
private static final int STYLE_NORMAL = 0;
/**
- * A minimum weight value for the font
- */
- public static final int FONT_WEIGHT_MIN = 1;
-
- /**
- * A font weight value for the thin weight
- */
- public static final int FONT_WEIGHT_THIN = 100;
-
- /**
- * A font weight value for the extra-light weight
- */
- public static final int FONT_WEIGHT_EXTRA_LIGHT = 200;
-
- /**
- * A font weight value for the light weight
- */
- public static final int FONT_WEIGHT_LIGHT = 300;
-
- /**
- * A font weight value for the normal weight
- */
- public static final int FONT_WEIGHT_NORMAL = 400;
-
- /**
- * A font weight value for the medium weight
- */
- public static final int FONT_WEIGHT_MEDIUM = 500;
-
- /**
- * A font weight value for the semi-bold weight
- */
- public static final int FONT_WEIGHT_SEMI_BOLD = 600;
-
- /**
- * A font weight value for the bold weight.
- */
- public static final int FONT_WEIGHT_BOLD = 700;
-
- /**
- * A font weight value for the extra-bold weight
- */
- public static final int FONT_WEIGHT_EXTRA_BOLD = 800;
-
- /**
- * A font weight value for the black weight
- */
- public static final int FONT_WEIGHT_BLACK = 900;
-
- /**
- * A maximum weight value for the font
- */
- public static final int FONT_WEIGHT_MAX = 1000;
-
- /**
* A builder class for creating new Font.
*/
public static class Builder {
@@ -275,66 +220,68 @@ public final class Font {
* <tr>
* <td align="center">100</td>
* <td align="center">Thin</td>
- * <td align="center">{@link Font#FONT_WEIGHT_THIN}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_THIN}</td>
* </tr>
* <tr>
* <td align="center">200</td>
* <td align="center">Extra Light (Ultra Light)</td>
- * <td align="center">{@link Font#FONT_WEIGHT_EXTRA_LIGHT}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_EXTRA_LIGHT}</td>
* </tr>
* <tr>
* <td align="center">300</td>
* <td align="center">Light</td>
- * <td align="center">{@link Font#FONT_WEIGHT_LIGHT}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_LIGHT}</td>
* </tr>
* <tr>
* <td align="center">400</td>
* <td align="center">Normal (Regular)</td>
- * <td align="center">{@link Font#FONT_WEIGHT_NORMAL}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_NORMAL}</td>
* </tr>
* <tr>
* <td align="center">500</td>
* <td align="center">Medium</td>
- * <td align="center">{@link Font#FONT_WEIGHT_MEDIUM}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_MEDIUM}</td>
* </tr>
* <tr>
* <td align="center">600</td>
* <td align="center">Semi Bold (Demi Bold)</td>
- * <td align="center">{@link Font#FONT_WEIGHT_SEMI_BOLD}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_SEMI_BOLD}</td>
* </tr>
* <tr>
* <td align="center">700</td>
* <td align="center">Bold</td>
- * <td align="center">{@link Font#FONT_WEIGHT_BOLD}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_BOLD}</td>
* </tr>
* <tr>
* <td align="center">800</td>
* <td align="center">Extra Bold (Ultra Bold)</td>
- * <td align="center">{@link Font#FONT_WEIGHT_EXTRA_BOLD}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_EXTRA_BOLD}</td>
* </tr>
* <tr>
* <td align="center">900</td>
* <td align="center">Black (Heavy)</td>
- * <td align="center">{@link Font#FONT_WEIGHT_BLACK}</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_BLACK}</td>
* </tr>
* </tbody>
* </p>
*
- * @see Font#FONT_WEIGHT_THIN
- * @see Font#FONT_WEIGHT_EXTRA_LIGHT
- * @see Font#FONT_WEIGHT_LIGHT
- * @see Font#FONT_WEIGHT_NORMAL
- * @see Font#FONT_WEIGHT_MEDIUM
- * @see Font#FONT_WEIGHT_SEMI_BOLD
- * @see Font#FONT_WEIGHT_BOLD
- * @see Font#FONT_WEIGHT_EXTRA_BOLD
- * @see Font#FONT_WEIGHT_BLACK
+ * @see FontStyle#FONT_WEIGHT_THIN
+ * @see FontStyle#FONT_WEIGHT_EXTRA_LIGHT
+ * @see FontStyle#FONT_WEIGHT_LIGHT
+ * @see FontStyle#FONT_WEIGHT_NORMAL
+ * @see FontStyle#FONT_WEIGHT_MEDIUM
+ * @see FontStyle#FONT_WEIGHT_SEMI_BOLD
+ * @see FontStyle#FONT_WEIGHT_BOLD
+ * @see FontStyle#FONT_WEIGHT_EXTRA_BOLD
+ * @see FontStyle#FONT_WEIGHT_BLACK
* @param weight a weight value
* @return this builder
*/
public @NonNull Builder setWeight(
- @IntRange(from = FONT_WEIGHT_MIN, to = FONT_WEIGHT_MAX) int weight) {
- Preconditions.checkArgument(FONT_WEIGHT_MIN <= weight && weight <= FONT_WEIGHT_MAX);
+ @IntRange(from = FontStyle.FONT_WEIGHT_MIN, to = FontStyle.FONT_WEIGHT_MAX)
+ int weight) {
+ Preconditions.checkArgument(
+ FontStyle.FONT_WEIGHT_MIN <= weight && weight <= FontStyle.FONT_WEIGHT_MAX);
mWeight = weight;
return this;
}
@@ -346,13 +293,12 @@ public final class Font {
* will resolve the style by reading font tables.
*
* For example, if you want to use italic font as upright font, call {@code
- * setItalic(false)} explicitly.
+ * setSlant(false)} explicitly.
*
- * @param italic {@code true} if the font is italic. Otherwise {@code false}.
* @return this builder
*/
- public @NonNull Builder setItalic(boolean italic) {
- mItalic = italic ? STYLE_ITALIC : STYLE_NORMAL;
+ public @NonNull Builder setSlant(@FontStyle.FontSlant int slant) {
+ mItalic = slant == FontStyle.FONT_SLANT_UPRIGHT ? STYLE_NORMAL : STYLE_ITALIC;
return this;
}
@@ -414,8 +360,11 @@ public final class Font {
mItalic = STYLE_NORMAL;
}
}
- mWeight = Math.max(FONT_WEIGHT_MIN, Math.min(FONT_WEIGHT_MAX, mWeight));
+ mWeight = Math.max(FontStyle.FONT_WEIGHT_MIN,
+ Math.min(FontStyle.FONT_WEIGHT_MAX, mWeight));
final boolean italic = (mItalic == STYLE_ITALIC);
+ final int slant = (mItalic == STYLE_ITALIC)
+ ? FontStyle.FONT_SLANT_ITALIC : FontStyle.FONT_SLANT_UPRIGHT;
final long builderPtr = nInitBuilder();
if (mAxes != null) {
for (FontVariationAxis axis : mAxes) {
@@ -424,8 +373,8 @@ public final class Font {
}
final ByteBuffer readonlyBuffer = mBuffer.asReadOnlyBuffer();
final long ptr = nBuild(builderPtr, readonlyBuffer, mWeight, italic, mTtcIndex);
- final Font font = new Font(ptr, readonlyBuffer, mFile, mWeight, italic, mTtcIndex,
- mAxes, mLocaleList);
+ final Font font = new Font(ptr, readonlyBuffer, mFile,
+ new FontStyle(mWeight, slant), mTtcIndex, mAxes, mLocaleList);
sFontRegistory.registerNativeAllocation(font, ptr);
return font;
}
@@ -454,8 +403,7 @@ public final class Font {
private final long mNativePtr; // address of the shared ptr of minikin::Font
private final @NonNull ByteBuffer mBuffer;
private final @Nullable File mFile;
- private final @IntRange(from = 0, to = 1000) int mWeight;
- private final boolean mItalic;
+ private final FontStyle mFontStyle;
private final @IntRange(from = 0) int mTtcIndex;
private final @Nullable FontVariationAxis[] mAxes;
private final @NonNull String mLocaleList;
@@ -464,13 +412,11 @@ public final class Font {
* Use Builder instead
*/
private Font(long nativePtr, @NonNull ByteBuffer buffer, @Nullable File file,
- @IntRange(from = 0, to = 1000) int weight, boolean italic,
- @IntRange(from = 0) int ttcIndex, @Nullable FontVariationAxis[] axes,
- @NonNull String localeList) {
+ @NonNull FontStyle fontStyle, @IntRange(from = 0) int ttcIndex,
+ @Nullable FontVariationAxis[] axes, @NonNull String localeList) {
mBuffer = buffer;
mFile = file;
- mWeight = weight;
- mItalic = italic;
+ mFontStyle = fontStyle;
mNativePtr = nativePtr;
mTtcIndex = ttcIndex;
mAxes = axes;
@@ -504,17 +450,17 @@ public final class Font {
* @return a weight value
*/
public @IntRange(from = 0, to = 1000)int getWeight() {
- return mWeight;
+ return mFontStyle.getWeight();
}
/**
- * Returns true if this font is marked as italic, otherwise returns false.
+ * Get a slant value associated with this font.
*
- * @see Builder#setItalic(boolean)
- * @return true if italic, otherwise false
+ * @see Builder#setSlant(boolean)
+ * @return a slant value
*/
- public boolean isItalic() {
- return mItalic;
+ public @FontStyle.FontSlant int getSlant() {
+ return mFontStyle.getSlant();
}
/**
@@ -564,21 +510,20 @@ public final class Font {
return false;
}
Font f = (Font) o;
- return f.mWeight == mWeight && f.mItalic == mItalic && f.mTtcIndex == mTtcIndex
+ return mFontStyle.equals(f.mFontStyle) && f.mTtcIndex == mTtcIndex
&& Arrays.equals(f.mAxes, mAxes) && f.mBuffer.equals(mBuffer);
}
@Override
public int hashCode() {
- return Objects.hash(mWeight, mItalic, mTtcIndex, Arrays.hashCode(mAxes), mBuffer);
+ return Objects.hash(mFontStyle, mTtcIndex, Arrays.hashCode(mAxes), mBuffer);
}
@Override
public String toString() {
return "Font {"
+ "path=" + mFile
- + ", weight=" + mWeight
- + ", italic=" + mItalic
+ + ", style=" + mFontStyle
+ ", ttcIndex=" + mTtcIndex
+ ", axes=" + FontVariationAxis.toFontVariationSettings(mAxes)
+ ", localeList=" + mLocaleList
diff --git a/graphics/java/android/graphics/fonts/FontFamily.java b/graphics/java/android/graphics/fonts/FontFamily.java
index 3bcdc31a3160..52a37da47cff 100644
--- a/graphics/java/android/graphics/fonts/FontFamily.java
+++ b/graphics/java/android/graphics/fonts/FontFamily.java
@@ -124,7 +124,7 @@ public final class FontFamily {
}
private static int makeStyleIdentifier(@NonNull Font font) {
- return font.getWeight() | (font.isItalic() ? (1 << 16) : 0);
+ return font.getWeight() | (font.getSlant() << 16);
}
private static native long nInitBuilder();
diff --git a/graphics/java/android/graphics/fonts/FontStyle.java b/graphics/java/android/graphics/fonts/FontStyle.java
new file mode 100644
index 000000000000..82fc7ac01772
--- /dev/null
+++ b/graphics/java/android/graphics/fonts/FontStyle.java
@@ -0,0 +1,256 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.graphics.fonts;
+
+import android.annotation.IntDef;
+import android.annotation.IntRange;
+import android.annotation.Nullable;
+
+import com.android.internal.util.Preconditions;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.Objects;
+
+/**
+ * A font style object.
+ *
+ * This class represents a single font style which is a pair of weight value and slant value.
+ * Here are common font styles examples:
+ * <p>
+ * <pre>
+ * <code>
+ * final FontStyle NORMAL = new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT);
+ * final FontStyle BOLD = new FontStyle(FONT_WEIGHT_BOLD, FONT_SLANT_UPRIGHT);
+ * final FontStyle ITALIC = new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_ITALIC);
+ * final FontStyle BOLD_ITALIC = new FontStyle(FONT_WEIGHT_BOLD, FONT_SLANT_ITALIC);
+ * </code>
+ * </pre>
+ * </p>
+ *
+ */
+public final class FontStyle {
+ private static final String TAG = "FontStyle";
+
+ /**
+ * A minimum weight value for the font
+ */
+ public static final int FONT_WEIGHT_MIN = 1;
+
+ /**
+ * A font weight value for the thin weight
+ */
+ public static final int FONT_WEIGHT_THIN = 100;
+
+ /**
+ * A font weight value for the extra-light weight
+ */
+ public static final int FONT_WEIGHT_EXTRA_LIGHT = 200;
+
+ /**
+ * A font weight value for the light weight
+ */
+ public static final int FONT_WEIGHT_LIGHT = 300;
+
+ /**
+ * A font weight value for the normal weight
+ */
+ public static final int FONT_WEIGHT_NORMAL = 400;
+
+ /**
+ * A font weight value for the medium weight
+ */
+ public static final int FONT_WEIGHT_MEDIUM = 500;
+
+ /**
+ * A font weight value for the semi-bold weight
+ */
+ public static final int FONT_WEIGHT_SEMI_BOLD = 600;
+
+ /**
+ * A font weight value for the bold weight.
+ */
+ public static final int FONT_WEIGHT_BOLD = 700;
+
+ /**
+ * A font weight value for the extra-bold weight
+ */
+ public static final int FONT_WEIGHT_EXTRA_BOLD = 800;
+
+ /**
+ * A font weight value for the black weight
+ */
+ public static final int FONT_WEIGHT_BLACK = 900;
+
+ /**
+ * A maximum weight value for the font
+ */
+ public static final int FONT_WEIGHT_MAX = 1000;
+
+ /**
+ * A font slant value for upright
+ */
+ public static final int FONT_SLANT_UPRIGHT = 0;
+
+ /**
+ * A font slant value for italic
+ */
+ public static final int FONT_SLANT_ITALIC = 1;
+
+ // TODO: Support FONT_SLANT_OBLIQUE
+
+ /** @hide */
+ @IntDef(prefix = { "FONT_SLANT_" }, value = {
+ FONT_SLANT_UPRIGHT,
+ FONT_SLANT_ITALIC
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface FontSlant {}
+
+ private final @IntRange(from = 0, to = 1000) int mWeight;
+ private final @FontSlant int mSlant;
+ // TODO: Support width
+
+ public FontStyle() {
+ mWeight = FONT_WEIGHT_NORMAL;
+ mSlant = FONT_SLANT_UPRIGHT;
+ }
+
+ /**
+ * Create FontStyle with specific weight and italic
+ *
+ * <p>
+ * <table>
+ * <thead>
+ * <tr>
+ * <th align="center">Value</th>
+ * <th align="center">Name</th>
+ * <th align="center">Android Definition</th>
+ * </tr>
+ * </thead>
+ * <tbody>
+ * <tr>
+ * <td align="center">100</td>
+ * <td align="center">Thin</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_THIN}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">200</td>
+ * <td align="center">Extra Light (Ultra Light)</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_EXTRA_LIGHT}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">300</td>
+ * <td align="center">Light</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_LIGHT}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">400</td>
+ * <td align="center">Normal (Regular)</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_NORMAL}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">500</td>
+ * <td align="center">Medium</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_MEDIUM}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">600</td>
+ * <td align="center">Semi Bold (Demi Bold)</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_SEMI_BOLD}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">700</td>
+ * <td align="center">Bold</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_BOLD}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">800</td>
+ * <td align="center">Extra Bold (Ultra Bold)</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_EXTRA_BOLD}</td>
+ * </tr>
+ * <tr>
+ * <td align="center">900</td>
+ * <td align="center">Black (Heavy)</td>
+ * <td align="center">{@link FontStyle#FONT_WEIGHT_BLACK}</td>
+ * </tr>
+ * </tbody>
+ * </p>
+ *
+ * @see FontStyle#FONT_WEIGHT_THIN
+ * @see FontStyle#FONT_WEIGHT_EXTRA_LIGHT
+ * @see FontStyle#FONT_WEIGHT_LIGHT
+ * @see FontStyle#FONT_WEIGHT_NORMAL
+ * @see FontStyle#FONT_WEIGHT_MEDIUM
+ * @see FontStyle#FONT_WEIGHT_SEMI_BOLD
+ * @see FontStyle#FONT_WEIGHT_BOLD
+ * @see FontStyle#FONT_WEIGHT_EXTRA_BOLD
+ * @see FontStyle#FONT_WEIGHT_BLACK
+ * @param weight a weight value
+ * @param slant a slant value
+ */
+ public FontStyle(int weight, @FontSlant int slant) {
+ Preconditions.checkArgument(FONT_WEIGHT_MIN <= weight && weight <= FONT_WEIGHT_MAX,
+ "weight value must be [" + FONT_WEIGHT_MIN + ", " + FONT_WEIGHT_MAX + "]");
+ Preconditions.checkArgument(slant == FONT_SLANT_UPRIGHT || slant == FONT_SLANT_ITALIC,
+ "slant value must be FONT_SLANT_UPRIGHT or FONT_SLANT_UPRIGHT");
+ mWeight = weight;
+ mSlant = slant;
+ }
+
+
+ /**
+ * Gets the weight value
+ *
+ * @see FontStyle#setWeight(int)
+ * @return a weight value
+ */
+ public @IntRange(from = 0, to = 1000) int getWeight() {
+ return mWeight;
+ }
+
+ /**
+ * Gets the slant value
+ *
+ * @return a slant value
+ */
+ public @FontSlant int getSlant() {
+ return mSlant;
+ }
+
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (o == null || !(o instanceof FontStyle)) {
+ return false;
+ }
+ FontStyle fontStyle = (FontStyle) o;
+ return fontStyle.mWeight == mWeight && fontStyle.mSlant == mSlant;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(mWeight, mSlant);
+ }
+
+ @Override
+ public String toString() {
+ return "FontStyle { weight=" + mWeight + ", slant=" + mSlant + "}";
+ }
+}
diff --git a/graphics/java/android/graphics/fonts/SystemFonts.java b/graphics/java/android/graphics/fonts/SystemFonts.java
index 2d21bbbd4e43..750adb2757c8 100644
--- a/graphics/java/android/graphics/fonts/SystemFonts.java
+++ b/graphics/java/android/graphics/fonts/SystemFonts.java
@@ -192,7 +192,8 @@ public final class SystemFonts {
try {
font = new Font.Builder(buffer, new File(fullPath), languageTags)
.setWeight(fontConfig.getWeight())
- .setItalic(fontConfig.isItalic())
+ .setSlant(fontConfig.isItalic() ? FontStyle.FONT_SLANT_ITALIC
+ : FontStyle.FONT_SLANT_UPRIGHT)
.setTtcIndex(fontConfig.getTtcIndex())
.setFontVariationSettings(fontConfig.getAxes())
.build();