diff options
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/content/res/Resources.java | 31 | ||||
| -rw-r--r-- | core/java/android/content/res/ResourcesImpl.java | 32 |
2 files changed, 62 insertions, 1 deletions
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index ad113075c96b..c3185a7cad05 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -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; @@ -333,7 +334,35 @@ public class Resources { return res; } throw new NotFoundException("String resource ID #0x" - + Integer.toHexString(id)); + + 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)); } /** diff --git a/core/java/android/content/res/ResourcesImpl.java b/core/java/android/content/res/ResourcesImpl.java index eb010e46ea91..05892e0ad660 100644 --- a/core/java/android/content/res/ResourcesImpl.java +++ b/core/java/android/content/res/ResourcesImpl.java @@ -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; @@ -740,6 +742,36 @@ public class ResourcesImpl { } /** + * 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. * Last, parse the XML and generate the CSL. |
