Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 596f0b1a authored by Raph Levien's avatar Raph Levien Committed by Android (Google) Code Review
Browse files

Merge "Support fallbacks for custom typefaces (Minikin)" into lmp-preview-dev

parents cdd5c8f3 d573794d
Loading
Loading
Loading
Loading
+49 −3
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@
#include "GraphicsJNI.h"
#include <ScopedPrimitiveArray.h>
#include <ScopedUtfChars.h>
#include <android_runtime/android_util_AssetManager.h>
#include <androidfw/AssetManager.h>
#include "Utils.h"

#ifdef USE_MINIKIN
#include <minikin/FontFamily.h>
@@ -51,19 +54,60 @@ static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) {
#endif
}

#ifdef USE_MINIKIN
static jboolean addSkTypeface(FontFamily* family, SkTypeface* face) {
    MinikinFont* minikinFont = new MinikinFontSkia(face);
    bool result = family->addFont(minikinFont);
    minikinFont->Unref();
    return result;
}
#endif

static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) {
#ifdef USE_MINIKIN
    NPE_CHECK_RETURN_ZERO(env, path);
    ScopedUtfChars str(env, path);
    ALOGD("addFont %s", str.c_str());
    SkTypeface* face = SkTypeface::CreateFromFile(str.c_str());
    if (face == NULL) {
        ALOGE("addFont failed to create font %s", str.c_str());
        return false;
    }
    MinikinFont* minikinFont = new MinikinFontSkia(face);
    FontFamily* fontFamily = (FontFamily*)familyPtr;
    return fontFamily->addFont(minikinFont);
    return addSkTypeface(fontFamily, face);
#else
    return false;
#endif
}

static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr,
        jobject jassetMgr, jstring jpath) {
#ifdef USE_MINIKIN
    NPE_CHECK_RETURN_ZERO(env, jassetMgr);
    NPE_CHECK_RETURN_ZERO(env, jpath);

    AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr);
    if (NULL == mgr) {
        return false;
    }

    ScopedUtfChars str(env, jpath);
    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
    if (NULL == asset) {
        return false;
    }

    SkStream* stream = new AssetStreamAdaptor(asset,
                                              AssetStreamAdaptor::kYes_OwnAsset,
                                              AssetStreamAdaptor::kYes_HasMemoryBase);
    SkTypeface* face = SkTypeface::CreateFromStream(stream);
    // Note: SkTypeface::CreateFromStream holds its own reference to the stream
    stream->unref();
    if (face == NULL) {
        ALOGE("addFontFromAsset failed to create font %s", str.c_str());
        return false;
    }
    FontFamily* fontFamily = (FontFamily*)familyPtr;
    return addSkTypeface(fontFamily, face);
#else
    return false;
#endif
@@ -75,6 +119,8 @@ static JNINativeMethod gFontFamilyMethods[] = {
    { "nCreateFamily",            "(Ljava/lang/String;I)J", (void*)FontFamily_create },
    { "nUnrefFamily",             "(J)V", (void*)FontFamily_unref },
    { "nAddFont",                 "(JLjava/lang/String;)Z", (void*)FontFamily_addFont },
    { "nAddFontFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;)Z",
                                           (void*)FontFamily_addFontFromAsset },
};

int register_android_graphics_FontFamily(JNIEnv* env)
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ namespace android {

class MinikinFontSkia : public MinikinFont {
public:
    // Note: this takes ownership of the reference (will unref on dtor)
    explicit MinikinFontSkia(SkTypeface *typeface);

    ~MinikinFontSkia();
+13 −5
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.graphics;

import android.content.res.AssetManager;

import java.io.File;

/**
@@ -58,11 +60,17 @@ public class FontFamily {
        }
    }

    public boolean addFont(File path) {
        return nAddFont(mNativePtr, path.getAbsolutePath());
    public boolean addFont(String path) {
        return nAddFont(mNativePtr, path);
    }

    public boolean addFontFromAsset(AssetManager mgr, String path) {
        return nAddFontFromAsset(mNativePtr, mgr, path);
    }

    static native long nCreateFamily(String lang, int variant);
    static native void nUnrefFamily(long nativePtr);
    static native boolean nAddFont(long nativeFamily, String path);
    private static native long nCreateFamily(String lang, int variant);
    private static native void nUnrefFamily(long nativePtr);
    private static native boolean nAddFont(long nativeFamily, String path);
    private static native boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr,
            String path);
}
+20 −2
Original line number Diff line number Diff line
@@ -178,6 +178,15 @@ public class Typeface {
     * @return The new typeface.
     */
    public static Typeface createFromAsset(AssetManager mgr, String path) {
        if (sFallbackFonts != null) {
            FontFamily fontFamily = new FontFamily();
            if (fontFamily.addFontFromAsset(mgr, path)) {
                FontFamily[] families = { fontFamily };
                return createFromFamiliesWithDefault(families);
            } else {
                return null;
            }
        }
        return new Typeface(nativeCreateFromAsset(mgr, path));
    }

@@ -188,7 +197,7 @@ public class Typeface {
     * @return The new typeface.
     */
    public static Typeface createFromFile(File path) {
        return new Typeface(nativeCreateFromFile(path.getAbsolutePath()));
        return createFromFile(path.getAbsolutePath());
    }

    /**
@@ -198,6 +207,15 @@ public class Typeface {
     * @return The new typeface.
     */
    public static Typeface createFromFile(String path) {
        if (sFallbackFonts != null) {
            FontFamily fontFamily = new FontFamily();
            if (fontFamily.addFont(path)) {
                FontFamily[] families = { fontFamily };
                return createFromFamiliesWithDefault(families);
            } else {
                return null;
            }
        }
        return new Typeface(nativeCreateFromFile(path));
    }

@@ -247,7 +265,7 @@ public class Typeface {
        // TODO: expand to handle attributes like lang and variant
        FontFamily fontFamily = new FontFamily(family.lang, family.variant);
        for (String fontFile : family.fontFiles) {
            fontFamily.addFont(new File(fontFile));
            fontFamily.addFont(fontFile);
        }
        return fontFamily;
    }