Search in sources :

Example 1 with FontFormatException

use of java.awt.FontFormatException in project platform_frameworks_base by android.

the class FontFamily_Delegate method loadFont.

private static Font loadFont(String path) {
    if (path.startsWith(SYSTEM_FONTS)) {
        String relativePath = path.substring(SYSTEM_FONTS.length());
        File f = new File(sFontLocation, relativePath);
        try {
            return Font.createFont(Font.TRUETYPE_FONT, f);
        } catch (Exception e) {
            if (path.endsWith(EXTENSION_OTF) && e instanceof FontFormatException) {
                // warning.
                return null;
            }
            Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, String.format("Unable to load font %1$s", relativePath), e, null);
        }
    } else {
        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.", null, null);
    }
    return null;
}
Also used : File(java.io.File) FontFormatException(java.awt.FontFormatException) FontFormatException(java.awt.FontFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with FontFormatException

use of java.awt.FontFormatException in project platform_frameworks_base by android.

the class FontFamily_Delegate method nAddFontFromAsset.

@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
    if (ffd == null) {
        return false;
    }
    ffd.mValid = true;
    if (mgr == null) {
        return false;
    }
    if (mgr instanceof BridgeAssetManager) {
        InputStream fontStream = null;
        try {
            AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
            if (assetRepository == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
                return false;
            }
            if (!assetRepository.isSupported()) {
                // Don't log any warnings on unsupported IDEs.
                return false;
            }
            // Check cache
            FontInfo fontInfo = sCache.get(path);
            if (fontInfo != null) {
                // renew the font's lease.
                sCache.put(path, fontInfo);
                ffd.addFont(fontInfo);
                return true;
            }
            fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
            if (fontStream == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
                return false;
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            fontInfo = new FontInfo();
            fontInfo.mFont = font;
            fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
            fontInfo.mIsItalic = font.isItalic();
            ffd.addFont(fontInfo);
            return true;
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
        } catch (FontFormatException e) {
            if (path.endsWith(EXTENSION_OTF)) {
                // otf fonts are not supported on the user's config (JRE version + OS)
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
            } else {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
            }
        } finally {
            if (fontStream != null) {
                try {
                    fontStream.close();
                } catch (IOException ignored) {
                }
            }
        }
        return false;
    }
    // This should never happen. AssetManager is a final class (from user's perspective), and
    // we've replaced every creation of AssetManager with our implementation. We create an
    // exception and log it, but continue with rest of the rendering, without loading this font.
    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
    return false;
}
Also used : BridgeAssetManager(android.content.res.BridgeAssetManager) InputStream(java.io.InputStream) AssetRepository(com.android.ide.common.rendering.api.AssetRepository) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 3 with FontFormatException

use of java.awt.FontFormatException in project android_frameworks_base by DirtyUnicorns.

the class FontFamily_Delegate method loadFont.

private static Font loadFont(String path) {
    if (path.startsWith(SYSTEM_FONTS)) {
        String relativePath = path.substring(SYSTEM_FONTS.length());
        File f = new File(sFontLocation, relativePath);
        try {
            return Font.createFont(Font.TRUETYPE_FONT, f);
        } catch (Exception e) {
            if (path.endsWith(EXTENSION_OTF) && e instanceof FontFormatException) {
                // warning.
                return null;
            }
            Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, String.format("Unable to load font %1$s", relativePath), e, null);
        }
    } else {
        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.", null, null);
    }
    return null;
}
Also used : File(java.io.File) FontFormatException(java.awt.FontFormatException) FontFormatException(java.awt.FontFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with FontFormatException

use of java.awt.FontFormatException in project android_frameworks_base by ResurrectionRemix.

the class FontFamily_Delegate method loadFont.

private static Font loadFont(String path) {
    if (path.startsWith(SYSTEM_FONTS)) {
        String relativePath = path.substring(SYSTEM_FONTS.length());
        File f = new File(sFontLocation, relativePath);
        try {
            return Font.createFont(Font.TRUETYPE_FONT, f);
        } catch (Exception e) {
            if (path.endsWith(EXTENSION_OTF) && e instanceof FontFormatException) {
                // warning.
                return null;
            }
            Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, String.format("Unable to load font %1$s", relativePath), e, null);
        }
    } else {
        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.", null, null);
    }
    return null;
}
Also used : File(java.io.File) FontFormatException(java.awt.FontFormatException) FontFormatException(java.awt.FontFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with FontFormatException

use of java.awt.FontFormatException in project android_frameworks_base by ResurrectionRemix.

the class FontFamily_Delegate method nAddFontFromAsset.

@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
    if (ffd == null) {
        return false;
    }
    ffd.mValid = true;
    if (mgr == null) {
        return false;
    }
    if (mgr instanceof BridgeAssetManager) {
        InputStream fontStream = null;
        try {
            AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
            if (assetRepository == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
                return false;
            }
            if (!assetRepository.isSupported()) {
                // Don't log any warnings on unsupported IDEs.
                return false;
            }
            // Check cache
            FontInfo fontInfo = sCache.get(path);
            if (fontInfo != null) {
                // renew the font's lease.
                sCache.put(path, fontInfo);
                ffd.addFont(fontInfo);
                return true;
            }
            fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
            if (fontStream == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
                return false;
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            fontInfo = new FontInfo();
            fontInfo.mFont = font;
            fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
            fontInfo.mIsItalic = font.isItalic();
            ffd.addFont(fontInfo);
            return true;
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
        } catch (FontFormatException e) {
            if (path.endsWith(EXTENSION_OTF)) {
                // otf fonts are not supported on the user's config (JRE version + OS)
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
            } else {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
            }
        } finally {
            if (fontStream != null) {
                try {
                    fontStream.close();
                } catch (IOException ignored) {
                }
            }
        }
        return false;
    }
    // This should never happen. AssetManager is a final class (from user's perspective), and
    // we've replaced every creation of AssetManager with our implementation. We create an
    // exception and log it, but continue with rest of the rendering, without loading this font.
    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
    return false;
}
Also used : BridgeAssetManager(android.content.res.BridgeAssetManager) InputStream(java.io.InputStream) AssetRepository(com.android.ide.common.rendering.api.AssetRepository) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

FontFormatException (java.awt.FontFormatException)20 IOException (java.io.IOException)18 FileNotFoundException (java.io.FileNotFoundException)9 File (java.io.File)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 Font (java.awt.Font)5 ByteBuffer (java.nio.ByteBuffer)5 BridgeAssetManager (android.content.res.BridgeAssetManager)4 AssetRepository (com.android.ide.common.rendering.api.AssetRepository)4 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)4 InputStream (java.io.InputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MappedByteBuffer (java.nio.MappedByteBuffer)3 RandomAccessFile (java.io.RandomAccessFile)2 BufferUnderflowException (java.nio.BufferUnderflowException)2 GraphicsEnvironment (java.awt.GraphicsEnvironment)1 WeakReference (java.lang.ref.WeakReference)1 IntBuffer (java.nio.IntBuffer)1 FileChannel (java.nio.channels.FileChannel)1 PrivilegedAction (java.security.PrivilegedAction)1