diff --git a/core/jni/android/graphics/FontFamily.cpp b/core/jni/android/graphics/FontFamily.cpp index 6ec658b9b916e..ce359dcfc1ce6 100644 --- a/core/jni/android/graphics/FontFamily.cpp +++ b/core/jni/android/graphics/FontFamily.cpp @@ -81,18 +81,21 @@ static void FontFamily_unref(jlong familyPtr) { } static void addSkTypeface(jlong builderPtr, sk_sp face, const void* fontData, - size_t fontSize, int ttcIndex) { + size_t fontSize, int ttcIndex, jint givenWeight, jboolean givenItalic) { minikin::MinikinFont* minikinFont = new MinikinFontSkia(std::move(face), fontData, fontSize, ttcIndex); NativeFamilyBuilder* builder = reinterpret_cast(builderPtr); - int weight; - bool italic; - if (!minikin::FontFamily::analyzeStyle(minikinFont, &weight, &italic)) { - ALOGE("analyzeStyle failed. Using default style"); - weight = 400; - italic = false; + int weight = givenWeight / 100; + bool italic = givenItalic; + if (weight == 0) { + if (!minikin::FontFamily::analyzeStyle(minikinFont, &weight, &italic)) { + ALOGE("analyzeStyle failed. Using default style"); + weight = 4; + italic = false; + } } - builder->fonts.push_back(minikin::Font(minikinFont, minikin::FontStyle(weight / 100, italic))); + + builder->fonts.push_back(minikin::Font(minikinFont, minikin::FontStyle(weight, italic))); minikinFont->Unref(); } @@ -146,7 +149,7 @@ static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong builderPtr, ALOGE("addFont failed to create font"); return false; } - addSkTypeface(builderPtr, std::move(face), fontPtr, (size_t)fontSize, ttcIndex); + addSkTypeface(builderPtr, std::move(face), fontPtr, (size_t)fontSize, ttcIndex, 0, false); return true; } @@ -218,7 +221,8 @@ static void releaseAsset(const void* ptr, void* context) { } static jboolean FontFamily_addFontFromAssetManager(JNIEnv* env, jobject, jlong builderPtr, - jobject jassetMgr, jstring jpath, jint cookie, jboolean isAsset) { + jobject jassetMgr, jstring jpath, jint cookie, jboolean isAsset, jint weight, + jboolean isItalic) { NPE_CHECK_RETURN_ZERO(env, jassetMgr); NPE_CHECK_RETURN_ZERO(env, jpath); @@ -261,7 +265,7 @@ static jboolean FontFamily_addFontFromAssetManager(JNIEnv* env, jobject, jlong b return false; } - addSkTypeface(builderPtr, std::move(face), buf, bufSize, 0 /* ttc index */); + addSkTypeface(builderPtr, std::move(face), buf, bufSize, 0 /* ttc index */, weight, isItalic); return true; } @@ -275,7 +279,7 @@ static const JNINativeMethod gFontFamilyMethods[] = { { "nAddFont", "(JLjava/nio/ByteBuffer;I)Z", (void*)FontFamily_addFont }, { "nAddFontWeightStyle", "(JLjava/nio/ByteBuffer;ILjava/util/List;IZ)Z", (void*)FontFamily_addFontWeightStyle }, - { "nAddFontFromAssetManager", "(JLandroid/content/res/AssetManager;Ljava/lang/String;IZ)Z", + { "nAddFontFromAssetManager", "(JLandroid/content/res/AssetManager;Ljava/lang/String;IZIZ)Z", (void*)FontFamily_addFontFromAssetManager }, }; diff --git a/graphics/java/android/graphics/FontFamily.java b/graphics/java/android/graphics/FontFamily.java index 2c93c325f5fc8..317f232ef7ecf 100644 --- a/graphics/java/android/graphics/FontFamily.java +++ b/graphics/java/android/graphics/FontFamily.java @@ -111,12 +111,24 @@ public class FontFamily { return nAddFontWeightStyle(mBuilderPtr, font, ttcIndex, axes, weight, style); } + /** + * @param mgr The AssetManager to use for this context. + * @param path The path to the font file to load. + * @param cookie If available, the resource cookie given by Resources. + * @param isAsset {@code true} if this is from the assets/ folder, {@code false} if from + * resources + * @param weight The weight of the font. If 0 is given, the weight and italic will be resolved + * using the OS/2 table in the font. + * @param isItalic Whether this font is italic. If the weight is set to 0, this will be resolved + * using the OS/2 table in the font. + * @return + */ public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie, - boolean isAsset) { + boolean isAsset, int weight, boolean isItalic) { if (mBuilderPtr == 0) { throw new IllegalStateException("Unable to call addFontFromAsset after freezing."); } - return nAddFontFromAssetManager(mBuilderPtr, mgr, path, cookie, isAsset); + return nAddFontFromAssetManager(mBuilderPtr, mgr, path, cookie, isAsset, weight, isItalic); } private static native long nInitBuilder(String lang, int variant); @@ -134,5 +146,5 @@ public class FontFamily { int ttcIndex, List listOfAxis, int weight, boolean isItalic); private static native boolean nAddFontFromAssetManager(long builderPtr, AssetManager mgr, - String path, int cookie, boolean isAsset); + String path, int cookie, boolean isAsset, int weight, boolean isItalic); } diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java index 7eb8099975185..52e18a9095cf4 100644 --- a/graphics/java/android/graphics/Typeface.java +++ b/graphics/java/android/graphics/Typeface.java @@ -144,7 +144,8 @@ public class Typeface { if (typeface != null) return typeface; FontFamily fontFamily = new FontFamily(); - if (fontFamily.addFontFromAssetManager(mgr, path, cookie, false /* isAsset */)) { + if (fontFamily.addFontFromAssetManager(mgr, path, cookie, false /* isAsset */, + 0 /* use OS/2 table to determine weight and italic */, false)) { fontFamily.freeze(); FontFamily[] families = {fontFamily}; typeface = createFromFamiliesWithDefault(families); @@ -199,7 +200,8 @@ public class Typeface { FontConfig.Font font = fonts.get(i); // TODO: Use style and weight info if (!fontFamily.addFontFromAssetManager(mgr, font.getFontName(), - 0 /* resourceCookie */, false /* isAsset */)) { + 0 /* resourceCookie */, false /* isAsset */, font.getWeight(), + font.isItalic())) { return null; } } @@ -498,7 +500,8 @@ public class Typeface { if (typeface != null) return typeface; FontFamily fontFamily = new FontFamily(); - if (fontFamily.addFontFromAssetManager(mgr, path, 0, true /* isAsset */)) { + if (fontFamily.addFontFromAssetManager(mgr, path, 0, true /* isAsset */, + 0 /* use OS/2 table to determine weight and italic */, false)) { fontFamily.freeze(); FontFamily[] families = { fontFamily }; typeface = createFromFamiliesWithDefault(families);