Search in sources :

Example 11 with FontFormatException

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

the class TrueTypeFont method readBlock.

ByteBuffer readBlock(int offset, int length) {
    ByteBuffer buffer = ByteBuffer.allocate(length);
    try {
        synchronized (this) {
            if (disposerRecord.channel == null) {
                open();
            }
            if (offset + length > fileSize) {
                if (offset > fileSize) {
                    // assert?
                    return null;
                } else {
                    buffer = ByteBuffer.allocate(fileSize - offset);
                }
            }
            disposerRecord.channel.position(offset);
            disposerRecord.channel.read(buffer);
            buffer.flip();
        }
    } catch (FontFormatException e) {
        return null;
    } catch (ClosedChannelException e) {
        /* NIO I/O is interruptible, recurse to retry operation.
             * Clear interrupts before recursing in case NIO didn't.
             */
        Thread.interrupted();
        close();
        readBlock(buffer, offset, length);
    } catch (IOException e) {
        return null;
    }
    return buffer;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FontFormatException(java.awt.FontFormatException)

Example 12 with FontFormatException

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

the class Type1Font method getBuffer.

private synchronized ByteBuffer getBuffer() throws FontFormatException {
    MappedByteBuffer mapBuf = (MappedByteBuffer) bufferRef.get();
    if (mapBuf == null) {
        //System.out.println("open T1 " + platName);
        try {
            RandomAccessFile raf = (RandomAccessFile) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

                public Object run() {
                    try {
                        return new RandomAccessFile(platName, "r");
                    } catch (FileNotFoundException ffne) {
                    }
                    return null;
                }
            });
            FileChannel fc = raf.getChannel();
            fileSize = (int) fc.size();
            mapBuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
            mapBuf.position(0);
            bufferRef = new WeakReference(mapBuf);
            fc.close();
        } catch (NullPointerException e) {
            throw new FontFormatException(e.toString());
        } catch (ClosedChannelException e) {
            /* NIO I/O is interruptible, recurse to retry operation.
                 * Clear interrupts before recursing in case NIO didn't.
                 */
            Thread.interrupted();
            return getBuffer();
        } catch (IOException e) {
            throw new FontFormatException(e.toString());
        }
    }
    return mapBuf;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MappedByteBuffer(java.nio.MappedByteBuffer) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) WeakReference(java.lang.ref.WeakReference) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException)

Example 13 with FontFormatException

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

the class Type1Font method verifyPFB.

private void verifyPFB(ByteBuffer bb) throws FontFormatException {
    int pos = 0;
    while (true) {
        try {
            int segType = bb.getShort(pos) & 0xffff;
            if (segType == 0x8001 || segType == 0x8002) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
                int segLen = bb.getInt(pos + 2);
                bb.order(ByteOrder.BIG_ENDIAN);
                if (segLen <= 0) {
                    throw new FontFormatException("bad segment length");
                }
                pos += segLen + 6;
            } else if (segType == 0x8003) {
                return;
            } else {
                throw new FontFormatException("bad pfb file");
            }
        } catch (BufferUnderflowException bue) {
            throw new FontFormatException(bue.toString());
        } catch (Exception e) {
            throw new FontFormatException(e.toString());
        }
    }
}
Also used : FontFormatException(java.awt.FontFormatException) BufferUnderflowException(java.nio.BufferUnderflowException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferUnderflowException(java.nio.BufferUnderflowException) FontFormatException(java.awt.FontFormatException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 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 15 with FontFormatException

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

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)21 IOException (java.io.IOException)19 FileNotFoundException (java.io.FileNotFoundException)9 File (java.io.File)8 ClosedChannelException (java.nio.channels.ClosedChannelException)7 Font (java.awt.Font)6 InputStream (java.io.InputStream)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 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MappedByteBuffer (java.nio.MappedByteBuffer)3 RandomAccessFile (java.io.RandomAccessFile)2 BufferUnderflowException (java.nio.BufferUnderflowException)2 HongsExpedient (app.hongs.HongsExpedient)1 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 GraphicsEnvironment (java.awt.GraphicsEnvironment)1 BufferedImage (java.awt.image.BufferedImage)1