Merge "Use weight & style data for fonts from xml source"

This commit is contained in:
Clara Bayarri
2017-02-17 10:39:49 +00:00
committed by Android (Google) Code Review
3 changed files with 37 additions and 18 deletions

View File

@@ -81,18 +81,21 @@ static void FontFamily_unref(jlong familyPtr) {
}
static void addSkTypeface(jlong builderPtr, sk_sp<SkTypeface> 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<NativeFamilyBuilder*>(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 },
};

View File

@@ -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<FontConfig.Axis> 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);
}

View File

@@ -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);