Search in sources :

Example 1 with FamilyResourceEntry

use of androidx.core.content.res.FontResourcesParserCompat.FamilyResourceEntry in project Carbon by ZieIony.

the class ResourcesCompat method loadFont.

/**
 * Load the given font. This method will always return null for asynchronous requests, which
 * provide a fontCallback, as there is no immediate result. When the callback is not provided,
 * the request is treated as synchronous and fails if async loading is required.
 */
private static Typeface loadFont(@NonNull Context context, Resources wrapper, TypedValue value, int id, int style, int weight, @Nullable androidx.core.content.res.ResourcesCompat.FontCallback fontCallback, @Nullable Handler handler, boolean isRequestFromLayoutInflator) {
    if (value.string == null) {
        throw new NotFoundException("Resource \"" + wrapper.getResourceName(id) + "\" (" + Integer.toHexString(id) + ") is not a Font: " + value);
    }
    final String file = value.string.toString();
    if (!file.startsWith("res/")) {
        // Early exit if the specified string is unlikely to be a resource path.
        if (fontCallback != null) {
            fontCallback.callbackFailAsync(FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
        }
        return null;
    }
    Typeface typeface = TypefaceCompat.findFromCache(wrapper, id, (style & Typeface.ITALIC) != 0, weight);
    if (typeface != null) {
        if (fontCallback != null) {
            fontCallback.callbackSuccessAsync(typeface, handler);
        }
        return typeface;
    }
    try {
        if (file.toLowerCase().endsWith(".xml")) {
            final XmlResourceParser rp = wrapper.getXml(id);
            final FamilyResourceEntry familyEntry = FontResourcesParserCompat.parse(rp, wrapper);
            if (familyEntry == null) {
                Log.e(TAG, "Failed to find font-family tag");
                if (fontCallback != null) {
                    fontCallback.callbackFailAsync(FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
                }
                return null;
            }
            return TypefaceCompat.createFromResourcesFamilyXml(context, familyEntry, wrapper, id, style, weight, fontCallback, handler, isRequestFromLayoutInflator);
        }
        typeface = TypefaceCompat.createFromResourcesFontFile(context, wrapper, id, file, (style & Typeface.ITALIC) != 0, weight);
        if (fontCallback != null) {
            if (typeface != null) {
                fontCallback.callbackSuccessAsync(typeface, handler);
            } else {
                fontCallback.callbackFailAsync(FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
            }
        }
        return typeface;
    } catch (XmlPullParserException e) {
        Log.e(TAG, "Failed to parse xml resource " + file, e);
    } catch (IOException e) {
        Log.e(TAG, "Failed to read xml resource " + file, e);
    }
    if (fontCallback != null) {
        fontCallback.callbackFailAsync(FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
    }
    return null;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Typeface(android.graphics.Typeface) NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FamilyResourceEntry(androidx.core.content.res.FontResourcesParserCompat.FamilyResourceEntry) IOException(java.io.IOException)

Aggregations

NotFoundException (android.content.res.Resources.NotFoundException)1 XmlResourceParser (android.content.res.XmlResourceParser)1 Typeface (android.graphics.Typeface)1 FamilyResourceEntry (androidx.core.content.res.FontResourcesParserCompat.FamilyResourceEntry)1 IOException (java.io.IOException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1