Search in sources :

Example 16 with FontFormatException

use of java.awt.FontFormatException in project jdk8u_jdk by JetBrains.

the class TrueTypeFont method init.

protected void init(int fIndex) throws FontFormatException {
    int headerOffset = 0;
    ByteBuffer buffer = readBlock(0, TTCHEADERSIZE);
    try {
        switch(buffer.getInt()) {
            case ttcfTag:
                // skip TTC version ID
                buffer.getInt();
                directoryCount = buffer.getInt();
                if (fIndex >= directoryCount) {
                    throw new FontFormatException("Bad collection index");
                }
                fontIndex = fIndex;
                buffer = readBlock(TTCHEADERSIZE + 4 * fIndex, 4);
                headerOffset = buffer.getInt();
                break;
            case v1ttTag:
            case trueTag:
            case ottoTag:
                break;
            default:
                throw new FontFormatException("Unsupported sfnt " + getPublicFileName());
        }
        /* Now have the offset of this TT font (possibly within a TTC)
             * After the TT version/scaler type field, is the short
             * representing the number of tables in the table directory.
             * The table directory begins at 12 bytes after the header.
             * Each table entry is 16 bytes long (4 32-bit ints)
             */
        buffer = readBlock(headerOffset + 4, 2);
        numTables = buffer.getShort();
        directoryOffset = headerOffset + DIRECTORYHEADERSIZE;
        ByteBuffer bbuffer = readBlock(directoryOffset, numTables * DIRECTORYENTRYSIZE);
        IntBuffer ibuffer = bbuffer.asIntBuffer();
        DirectoryEntry table;
        tableDirectory = new DirectoryEntry[numTables];
        for (int i = 0; i < numTables; i++) {
            tableDirectory[i] = table = new DirectoryEntry();
            table.tag = ibuffer.get();
            /* checksum */
            ibuffer.get();
            table.offset = ibuffer.get();
            table.length = ibuffer.get();
            if (table.offset + table.length > fileSize) {
                throw new FontFormatException("bad table, tag=" + table.tag);
            }
        }
        if (getDirectoryEntry(headTag) == null) {
            throw new FontFormatException("missing head table");
        }
        if (getDirectoryEntry(maxpTag) == null) {
            throw new FontFormatException("missing maxp table");
        }
        if (getDirectoryEntry(hmtxTag) != null && getDirectoryEntry(hheaTag) == null) {
            throw new FontFormatException("missing hhea table");
        }
        initNames();
    } catch (Exception e) {
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger().severe(e.toString());
        }
        if (e instanceof FontFormatException) {
            throw (FontFormatException) e;
        } else {
            throw new FontFormatException(e.toString());
        }
    }
    if (familyName == null || fullName == null) {
        throw new FontFormatException("Font name not found");
    }
    /* The os2_Table is needed to gather some info, but we don't
         * want to keep it around (as a field) so obtain it once and
         * pass it to the code that needs it.
         */
    ByteBuffer os2_Table = getTableBuffer(os_2Tag);
    setStyle(os2_Table);
    setCJKSupport(os2_Table);
}
Also used : IntBuffer(java.nio.IntBuffer) ByteBuffer(java.nio.ByteBuffer) FontFormatException(java.awt.FontFormatException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FontFormatException(java.awt.FontFormatException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 17 with FontFormatException

use of java.awt.FontFormatException in project jdk8u_jdk by JetBrains.

the class TestLayoutVsICU method getFont.

private Font getFont(String fontName, Map<String, String> fontAttrs) {
    Font f;
    if (false)
        try {
            f = Font.getFont(fontName);
            if (f != null) {
                if (OPT_VERBOSE) {
                    System.out.println("Loaded default path to " + fontName);
                }
                return f;
            }
        } catch (Throwable t) {
            if (OPT_VERBOSE) {
                t.printStackTrace();
                System.out.println("problem loading font " + fontName + " - " + t.toString());
            }
        }
    File homeDir = new File(System.getProperty("user.home"));
    File fontDir = new File(homeDir, "fonts");
    File fontFile = new File(fontDir, fontName);
    //System.out.println("## trying " + fontFile.getAbsolutePath());
    if (fontFile.canRead()) {
        try {
            if (!verifyFont(fontFile, fontAttrs)) {
                System.out.println("Warning: failed to verify " + fontName);
            }
            f = Font.createFont(Font.TRUETYPE_FONT, fontFile);
            if (f != null & OPT_VERBOSE) {
                System.out.println("> loaded from " + fontFile.getAbsolutePath() + " - " + f.toString());
            }
            return f;
        } catch (FontFormatException e) {
            if (OPT_VERBOSE) {
                e.printStackTrace();
                System.out.println("problem loading font " + fontName + " - " + e.toString());
            }
        } catch (IOException e) {
            if (OPT_VERBOSE) {
                e.printStackTrace();
                System.out.println("problem loading font " + fontName + " - " + e.toString());
            }
        }
    }
    return null;
}
Also used : IOException(java.io.IOException) File(java.io.File) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font)

Example 18 with FontFormatException

use of java.awt.FontFormatException in project pcgen by PCGen.

the class Main method initPrintPreviewFonts.

private static void initPrintPreviewFonts() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String fontDir = ConfigurationSettings.getOutputSheetsDir() + File.separator + "fonts" + File.separator + "NotoSans" + File.separator;
    try {
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-Regular.ttf")));
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-Bold.ttf")));
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-Italic.ttf")));
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontDir + "NotoSans-BoldItalic.ttf")));
    } catch (IOException | FontFormatException ex) {
        Logging.errorPrint("Unexpected exception loading fonts fo print p", ex);
    }
}
Also used : IOException(java.io.IOException) GraphicsEnvironment(java.awt.GraphicsEnvironment) File(java.io.File) FontFormatException(java.awt.FontFormatException)

Example 19 with FontFormatException

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

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 20 with FontFormatException

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

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