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

Commit 18e9f9f3 authored by Clara Bayarri's avatar Clara Bayarri
Browse files

Expose Resources.getFont

Based on the work already done in aapt2, load a Font from a
resource id.

Test: WIP
Change-Id: Idc06bfbfd16452a328bfcc6ea9dcfb723b633f0c
parent bcd68cf5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10681,6 +10681,7 @@ package android.content.res {
    method public android.graphics.drawable.Drawable getDrawable(int, android.content.res.Resources.Theme) throws android.content.res.Resources.NotFoundException;
    method public deprecated android.graphics.drawable.Drawable getDrawableForDensity(int, int) throws android.content.res.Resources.NotFoundException;
    method public android.graphics.drawable.Drawable getDrawableForDensity(int, int, android.content.res.Resources.Theme);
    method public android.graphics.Typeface getFont(int) throws android.content.res.Resources.NotFoundException;
    method public float getFraction(int, int, int);
    method public int getIdentifier(java.lang.String, java.lang.String, java.lang.String);
    method public int[] getIntArray(int) throws android.content.res.Resources.NotFoundException;
+1 −0
Original line number Diff line number Diff line
@@ -11199,6 +11199,7 @@ package android.content.res {
    method public android.graphics.drawable.Drawable getDrawable(int, android.content.res.Resources.Theme) throws android.content.res.Resources.NotFoundException;
    method public deprecated android.graphics.drawable.Drawable getDrawableForDensity(int, int) throws android.content.res.Resources.NotFoundException;
    method public android.graphics.drawable.Drawable getDrawableForDensity(int, int, android.content.res.Resources.Theme);
    method public android.graphics.Typeface getFont(int) throws android.content.res.Resources.NotFoundException;
    method public float getFraction(int, int, int);
    method public int getIdentifier(java.lang.String, java.lang.String, java.lang.String);
    method public int[] getIntArray(int) throws android.content.res.Resources.NotFoundException;
+1 −0
Original line number Diff line number Diff line
@@ -10713,6 +10713,7 @@ package android.content.res {
    method public android.graphics.drawable.Drawable getDrawable(int, android.content.res.Resources.Theme) throws android.content.res.Resources.NotFoundException;
    method public deprecated android.graphics.drawable.Drawable getDrawableForDensity(int, int) throws android.content.res.Resources.NotFoundException;
    method public android.graphics.drawable.Drawable getDrawableForDensity(int, int, android.content.res.Resources.Theme);
    method public android.graphics.Typeface getFont(int) throws android.content.res.Resources.NotFoundException;
    method public float getFraction(int, int, int);
    method public int getIdentifier(java.lang.String, java.lang.String, java.lang.String);
    method public int[] getIntArray(int) throws android.content.res.Resources.NotFoundException;
+30 −1
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.annotation.StyleableRes;
import android.annotation.XmlRes;
import android.content.pm.ActivityInfo;
import android.graphics.Movie;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable.ConstantState;
import android.graphics.drawable.DrawableInflater;
@@ -336,6 +337,34 @@ public class Resources {
                + Integer.toHexString(id));
    }

    /**
     * Return the Typeface value associated with a particular resource ID.
     * {@more}
     *
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     *
     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
     *
     * @return Typeface The Typeface data associated with the resource.
     */
    @NonNull public Typeface getFont(@StringRes int id) throws NotFoundException {
        final TypedValue value = obtainTempTypedValue();
        try {
            final ResourcesImpl impl = mResourcesImpl;
            impl.getValue(id, value, true);
            Typeface typeface = impl.loadFont(value, id);
            if (typeface != null) {
                return typeface;
            }
        } finally {
            releaseTempTypedValue(value);
        }
        throw new NotFoundException("Font resource ID #0x"
                + Integer.toHexString(id));
    }

    /**
     * Returns the character sequence necessary for grammatically correct pluralization
     * of the given resource ID for the given quantity.
+32 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.annotation.StyleableRes;
import android.content.pm.ActivityInfo;
import android.content.pm.ActivityInfo.Config;
import android.content.res.Resources.NotFoundException;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.icu.text.PluralRules;
@@ -47,6 +48,7 @@ import android.util.Xml;
import android.view.Display;
import android.view.DisplayAdjustments;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Locale;
@@ -739,6 +741,36 @@ public class ResourcesImpl {
        return dr;
    }

    /**
     * Loads a font from XML or resources stream.
     */
    @Nullable
    public Typeface loadFont(TypedValue value, int id) {
        if (value.string == null) {
            throw new NotFoundException("Resource \"" + getResourceName(id) + "\" ("
                    + Integer.toHexString(id) + ") is not a Font: " + value);
        }

        final String file = value.string.toString();

        if (DEBUG_LOAD) {
            Log.v(TAG, "Loading font for cookie " + value.assetCookie + ": " + file);
        }

        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
        try {
            if (file.endsWith(".xml")) {
                // TODO handle xml type font definitions
            } else {
                return Typeface.createFromResources(
                        mAssets, value.string.toString(), value.assetCookie);
            }
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        }
        return null;
    }

    /**
     * Given the value and id, we can get the XML filename as in value.data, based on that, we
     * first try to load CSL from the cache. If not found, try to get from the constant state.
Loading