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

Commit e7cada6d authored by Seigo Nonaka's avatar Seigo Nonaka Committed by Android (Google) Code Review
Browse files

Merge "Update SystemFonts.getAvailableFonts returns updated font files" into sc-dev

parents 4858950d 5916647c
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -1414,6 +1414,16 @@ public class Typeface {
        return Arrays.binarySearch(mSupportedAxes, axis) >= 0;
    }

    /** @hide */
    public List<FontFamily> getFallback() {
        ArrayList<FontFamily> families = new ArrayList<>();
        int familySize = nativeGetFamilySize(native_instance);
        for (int i = 0; i < familySize; ++i) {
            families.add(new FontFamily(nativeGetFamily(native_instance, i)));
        }
        return families;
    }

    private static native long nativeCreateFromTypeface(long native_instance, int style);
    private static native long nativeCreateFromTypefaceWithExactStyle(
            long native_instance, int weight, boolean italic);
@@ -1439,6 +1449,13 @@ public class Typeface {
    @CriticalNative
    private static native long nativeGetReleaseFunc();

    @CriticalNative
    private static native int nativeGetFamilySize(long naitvePtr);

    @CriticalNative
    private static native long nativeGetFamily(long nativePtr, int index);


    private static native void nativeRegisterGenericFamily(String str, long nativePtr);

    private static native int nativeWriteTypefaces(
+3 −2
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ public final class FontFamily {
                nAddFont(builderPtr, mFonts.get(i).getNativePtr());
            }
            final long ptr = nBuild(builderPtr, langTags, variant, isCustomFallback);
            final FontFamily family = new FontFamily(mFonts, ptr);
            final FontFamily family = new FontFamily(ptr);
            sFamilyRegistory.registerNativeAllocation(family, ptr);
            return family;
        }
@@ -146,7 +146,8 @@ public final class FontFamily {
    private final long mNativePtr;

    // Use Builder instead.
    private FontFamily(@NonNull ArrayList<Font> fonts, long ptr) {
    /** @hide */
    public FontFamily(long ptr) {
        mNativePtr = ptr;
    }

+7 −37
Original line number Diff line number Diff line
@@ -68,44 +68,23 @@ public final class SystemFonts {
     */
    public static @NonNull Set<Font> getAvailableFonts() {
        synchronized (LOCK) {
            if (sAvailableFonts != null) {
                return sAvailableFonts;
            }

            if (Typeface.ENABLE_LAZY_TYPEFACE_INITIALIZATION) {
                sAvailableFonts = collectAllFonts();
            } else {
            if (sAvailableFonts == null) {
                Set<Font> set = new ArraySet<>();
                for (FontFamily[] items : sFamilyMap.values()) {
                    for (FontFamily family : items) {
                        for (int i = 0; i < family.getSize(); ++i) {
                            set.add(family.getFont(i));
                for (Typeface tf : Typeface.getSystemFontMap().values()) {
                    List<FontFamily> families = tf.getFallback();
                    for (int i = 0; i < families.size(); ++i) {
                        FontFamily family = families.get(i);
                        for (int j = 0; j < family.getSize(); ++j) {
                            set.add(family.getFont(j));
                        }
                    }
                }

                sAvailableFonts = Collections.unmodifiableSet(set);
            }
            return sAvailableFonts;
        }
    }

    private static @NonNull Set<Font> collectAllFonts() {
        // TODO: use updated fonts
        FontConfig fontConfig = getSystemPreinstalledFontConfig();
        Map<String, FontFamily[]> map = buildSystemFallback(fontConfig);

        Set<Font> res = new ArraySet<>();
        for (FontFamily[] families : map.values()) {
            for (FontFamily family : families) {
                for (int i = 0; i < family.getSize(); ++i) {
                    res.add(family.getFont(i));
                }
            }
        }
        return res;
    }

    private static @Nullable ByteBuffer mmap(@NonNull String fullPath) {
        try (FileInputStream file = new FileInputStream(fullPath)) {
            final FileChannel fileChannel = file.getChannel();
@@ -329,13 +308,4 @@ public final class SystemFonts {
        Typeface.initSystemDefaultTypefaces(fallbackMap, fontConfig.getAliases(), result);
        return result;
    }

    /**
     * @hide
     */
    public void resetFallbackMapping(Map<String, FontFamily[]> fallbackMap) {
        synchronized (LOCK) {
            sFamilyMap = fallbackMap;
        }
    }
}
+31 −19
Original line number Diff line number Diff line
@@ -355,6 +355,17 @@ static void Typeface_forceSetStaticFinalField(JNIEnv *env, jclass cls, jstring f
    env->SetStaticObjectField(cls, fid, typeface);
}

// Critical Native
static jint Typeface_getFamilySize(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle) {
    return toTypeface(faceHandle)->fFontCollection->getFamilies().size();
}

// Critical Native
static jlong Typeface_getFamily(CRITICAL_JNI_PARAMS_COMMA jlong faceHandle, jint index) {
    std::shared_ptr<minikin::FontFamily> family =
            toTypeface(faceHandle)->fFontCollection->getFamilies()[index];
    return reinterpret_cast<jlong>(new FontFamilyWrapper(std::move(family)));
}

///////////////////////////////////////////////////////////////////////////////

@@ -368,8 +379,7 @@ static const JNINativeMethod gTypefaceMethods[] = {
        {"nativeGetReleaseFunc", "()J", (void*)Typeface_getReleaseFunc},
        {"nativeGetStyle", "(J)I", (void*)Typeface_getStyle},
        {"nativeGetWeight", "(J)I", (void*)Typeface_getWeight},
    { "nativeCreateFromArray",    "([JJII)J",
                                           (void*)Typeface_createFromArray },
        {"nativeCreateFromArray", "([JJII)J", (void*)Typeface_createFromArray},
        {"nativeSetDefault", "(J)V", (void*)Typeface_setDefault},
        {"nativeGetSupportedAxes", "(J)[I", (void*)Typeface_getSupportedAxes},
        {"nativeRegisterGenericFamily", "(Ljava/lang/String;J)V",
@@ -378,6 +388,8 @@ static const JNINativeMethod gTypefaceMethods[] = {
        {"nativeReadTypefaces", "(Ljava/nio/ByteBuffer;)[J", (void*)Typeface_readTypefaces},
        {"nativeForceSetStaticFinalField", "(Ljava/lang/String;Landroid/graphics/Typeface;)V",
         (void*)Typeface_forceSetStaticFinalField},
        {"nativeGetFamilySize", "(J)I", (void*)Typeface_getFamilySize},
        {"nativeGetFamily", "(JI)J", (void*)Typeface_getFamily},
};

int register_android_graphics_Typeface(JNIEnv* env)