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

Commit dbeee4e4 authored by Siyamed Sinir's avatar Siyamed Sinir
Browse files

Update Typeface.createFromFile/Asset

This CL updates Typeface.createFromFile and Typeface.createFromAsset so
that they would use the Typeface.Builder. To keep the behavior similar
to previous behavior, the existence of the given font file is checked,
and an exception is thrown if the file does not exist.

Test: atest FrameworksCoreTests:TypefaceTest
Test: atest android.graphics.cts.TypefaceTest
Test: atest CtsGraphicsTestCases:PaintTest

Bug: 72834540
Change-Id: Ibe9907440130ca91369b16c6cbac503f4a34e301
parent 27a6db48
Loading
Loading
Loading
Loading
+25 −44
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
@@ -801,37 +802,19 @@ public class Typeface {
     * @return The new typeface.
     */
    public static Typeface createFromAsset(AssetManager mgr, String path) {
        if (path == null) {
            throw new NullPointerException();  // for backward compatibility
        }
        synchronized (sDynamicCacheLock) {
            Typeface typeface = new Builder(mgr, path).build();
            if (typeface != null) return typeface;
        Preconditions.checkNotNull(path); // for backward compatibility
        Preconditions.checkNotNull(mgr);

            final String key = Builder.createAssetUid(mgr, path, 0 /* ttcIndex */,
                    null /* axes */, RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE,
                    DEFAULT_FAMILY);
            typeface = sDynamicTypefaceCache.get(key);
        Typeface typeface = new Builder(mgr, path).build();
        if (typeface != null) return typeface;
        // check if the file exists, and throw an exception for backward compatibility
        try (InputStream inputStream = mgr.open(path)) {
        } catch (IOException e) {
            throw new RuntimeException("Font asset not found " + path);
        }

            final FontFamily fontFamily = new FontFamily();
            if (fontFamily.addFontFromAssetManager(mgr, path, 0, true /* isAsset */,
                    0 /* ttc index */, RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE,
                    null /* axes */)) {
                if (!fontFamily.freeze()) {
        return Typeface.DEFAULT;
    }
                final FontFamily[] families = { fontFamily };
                typeface = createFromFamiliesWithDefault(families, DEFAULT_FAMILY,
                        RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE);
                sDynamicTypefaceCache.put(key, typeface);
                return typeface;
            } else {
                fontFamily.abortCreation();
            }
        }
        throw new RuntimeException("Font asset not found " + path);
    }

    /**
     * Creates a unique id for a given font provider and query.
@@ -848,13 +831,22 @@ public class Typeface {
    /**
     * Create a new typeface from the specified font file.
     *
     * @param path The path to the font data.
     * @param file The path to the font data.
     * @return The new typeface.
     */
    public static Typeface createFromFile(@Nullable File path) {
    public static Typeface createFromFile(@Nullable File file) {
        // For the compatibility reasons, leaving possible NPE here.
        // See android.graphics.cts.TypefaceTest#testCreateFromFileByFileReferenceNull
        return createFromFile(path.getAbsolutePath());

        Typeface typeface = new Builder(file).build();
        if (typeface != null) return typeface;

        // check if the file exists, and throw an exception for backward compatibility
        if (!file.exists()) {
            throw new RuntimeException("Font asset not found " + file.getAbsolutePath());
        }

        return Typeface.DEFAULT;
    }

    /**
@@ -864,19 +856,8 @@ public class Typeface {
     * @return The new typeface.
     */
    public static Typeface createFromFile(@Nullable String path) {
        final FontFamily fontFamily = new FontFamily();
        if (fontFamily.addFont(path, 0 /* ttcIndex */, null /* axes */,
                  RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE)) {
            if (!fontFamily.freeze()) {
                return Typeface.DEFAULT;
            }
            FontFamily[] families = { fontFamily };
            return createFromFamiliesWithDefault(families, DEFAULT_FAMILY,
                    RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE);
        } else {
            fontFamily.abortCreation();
        }
        throw new RuntimeException("Font not found " + path);
        Preconditions.checkNotNull(path); // for backward compatibility
        return createFromFile(new File(path));
    }

    /**