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

Commit d8bca766 authored by Siyamed Sinir's avatar Siyamed Sinir Committed by Android (Google) Code Review
Browse files

Merge "Update Typeface.createFromFile/Asset"

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


            final String key = Builder.createAssetUid(mgr, path, 0 /* ttcIndex */,
        Typeface typeface = new Builder(mgr, path).build();
                    null /* axes */, RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE,
                    DEFAULT_FAMILY);
            typeface = sDynamicTypefaceCache.get(key);
        if (typeface != null) return typeface;
        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;
        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.
     * 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.
     * 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.
     * @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.
        // For the compatibility reasons, leaving possible NPE here.
        // See android.graphics.cts.TypefaceTest#testCreateFromFileByFileReferenceNull
        // 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.
     * @return The new typeface.
     */
     */
    public static Typeface createFromFile(@Nullable String path) {
    public static Typeface createFromFile(@Nullable String path) {
        final FontFamily fontFamily = new FontFamily();
        Preconditions.checkNotNull(path); // for backward compatibility
        if (fontFamily.addFont(path, 0 /* ttcIndex */, null /* axes */,
        return createFromFile(new File(path));
                  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);
    }
    }


    /**
    /**