From 762dc8bdaec8a3ac32874f6aaa67e2063439445f Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Mon, 31 Jul 2017 15:09:37 -0700 Subject: [PATCH] Introduce new public API for specifying exact weight. Adding Typeface.create(Tyepface, int weight, boolean italic). This API is a most primitive API for specifying exact weight value for drawing text. Test: CtsGraphicsTestCases:android.graphics.cts.TypefaceTest Bug: 63135308 Change-Id: I5766870f68026fd1b13815e794f5df243cd8c912 --- api/current.txt | 1 + api/system-current.txt | 1 + api/test-current.txt | 1 + graphics/java/android/graphics/Typeface.java | 74 +++++++++++++------- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/api/current.txt b/api/current.txt index 708b981911c41..a8e3e07ce89df 100644 --- a/api/current.txt +++ b/api/current.txt @@ -13826,6 +13826,7 @@ package android.graphics { public class Typeface { method public static android.graphics.Typeface create(java.lang.String, int); method public static android.graphics.Typeface create(android.graphics.Typeface, int); + method public static android.graphics.Typeface create(android.graphics.Typeface, int, boolean); method public static android.graphics.Typeface createFromAsset(android.content.res.AssetManager, java.lang.String); method public static android.graphics.Typeface createFromFile(java.io.File); method public static android.graphics.Typeface createFromFile(java.lang.String); diff --git a/api/system-current.txt b/api/system-current.txt index d07f498d76221..722feb57b9e55 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -14624,6 +14624,7 @@ package android.graphics { public class Typeface { method public static android.graphics.Typeface create(java.lang.String, int); method public static android.graphics.Typeface create(android.graphics.Typeface, int); + method public static android.graphics.Typeface create(android.graphics.Typeface, int, boolean); method public static android.graphics.Typeface createFromAsset(android.content.res.AssetManager, java.lang.String); method public static android.graphics.Typeface createFromFile(java.io.File); method public static android.graphics.Typeface createFromFile(java.lang.String); diff --git a/api/test-current.txt b/api/test-current.txt index 2bca3aca3a338..c4939578a5932 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -13895,6 +13895,7 @@ package android.graphics { public class Typeface { method public static android.graphics.Typeface create(java.lang.String, int); method public static android.graphics.Typeface create(android.graphics.Typeface, int); + method public static android.graphics.Typeface create(android.graphics.Typeface, int, boolean); method public static android.graphics.Typeface createFromAsset(android.content.res.AssetManager, java.lang.String); method public static android.graphics.Typeface createFromFile(java.io.File); method public static android.graphics.Typeface createFromFile(java.lang.String); diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java index 1d8b5830aa926..3a8dfb014d5d7 100644 --- a/graphics/java/android/graphics/Typeface.java +++ b/graphics/java/android/graphics/Typeface.java @@ -548,29 +548,7 @@ public class Typeface { final int weight = (mWeight == RESOLVE_BY_FONT_TABLE) ? base.mWeight : mWeight; final boolean italic = (mItalic == RESOLVE_BY_FONT_TABLE) ? (base.mStyle & ITALIC) != 0 : mItalic == 1; - final int key = weight << 1 | (italic ? 1 : 0); - - Typeface typeface; - synchronized(sLock) { - SparseArray innerCache = sTypefaceCache.get(base.native_instance); - if (innerCache != null) { - typeface = innerCache.get(key); - if (typeface != null) { - return typeface; - } - } - - typeface = new Typeface( - nativeCreateFromTypefaceWithExactStyle( - base.native_instance, weight, italic)); - - if (innerCache == null) { - innerCache = new SparseArray<>(4); // [regular, bold] x [upright, italic] - sTypefaceCache.put(base.native_instance, innerCache); - } - innerCache.put(key, typeface); - } - return typeface; + return createWeightStyle(base, weight, italic); } /** @@ -688,7 +666,8 @@ public class Typeface { * style from the same family of an existing typeface object. If family is * null, this selects from the default font's family. * - * @param family May be null. The name of the existing type face. + * @param family An existing {@link Typeface} object. In case of {@code null}, the default + * typeface is used instead. * @param style The style (normal, bold, italic) of the typeface. * e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC * @return The best matching typeface. @@ -727,6 +706,53 @@ public class Typeface { return typeface; } + /** + * Creates a typeface object that best matches the specified existing typeface and the specified + * weight and italic style + * + * @param family An existing {@link Typeface} object. In case of {@code null}, the default + * typeface is used instead. + * @param weight The desired weight to be drawn. + * @param italic {@code true} if italic style is desired to be drawn. Otherwise, {@code false} + * @return A {@link Typeface} object for drawing specified weight and italic style. Never + * returns {@code null} + */ + public static @NonNull Typeface create(@Nullable Typeface family, + @IntRange(from = 1, to = 1000) int weight, boolean italic) { + Preconditions.checkArgumentInRange(weight, 0, 1000, "weight"); + if (family == null) { + family = sDefaultTypeface; + } + return createWeightStyle(family, weight, italic); + } + + private static @NonNull Typeface createWeightStyle(@NonNull Typeface base, + @IntRange(from = 1, to = 1000) int weight, boolean italic) { + final int key = weight << 1 | (italic ? 1 : 0); + + Typeface typeface; + synchronized(sLock) { + SparseArray innerCache = sTypefaceCache.get(base.native_instance); + if (innerCache != null) { + typeface = innerCache.get(key); + if (typeface != null) { + return typeface; + } + } + + typeface = new Typeface( + nativeCreateFromTypefaceWithExactStyle( + base.native_instance, weight, italic)); + + if (innerCache == null) { + innerCache = new SparseArray<>(4); // [regular, bold] x [upright, italic] + sTypefaceCache.put(base.native_instance, innerCache); + } + innerCache.put(key, typeface); + } + return typeface; + } + /** @hide */ public static Typeface createFromTypefaceWithVariation(@Nullable Typeface family, @NonNull List axes) {