Search in sources :

Example 6 with FontFormatException

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

the class Type1Font method initNames.

/* Need to parse the ascii contents of the Type1 font file,
     * looking for FullName, FamilyName and FontName.
     * If explicit names are not found then extract them from first text line.
     * Operating on bytes so can't use Java String utilities, which
     * is a large part of why this is a hack.
     *
     * Also check for mandatory FontType and verify if it is supported.
     */
private void initNames(ByteBuffer bb) throws FontFormatException {
    boolean eof = false;
    String fontType = null;
    try {
        //  (according to Type1 spec they are optional)
        while ((fullName == null || familyName == null || psName == null || fontType == null) && !eof) {
            int tokenType = nextTokenType(bb);
            if (tokenType == PSNAMETOKEN) {
                int pos = bb.position();
                if (bb.get(pos) == 'F') {
                    String s = getSimpleToken(bb);
                    if ("FullName".equals(s)) {
                        if (nextTokenType(bb) == PSSTRINGTOKEN) {
                            fullName = getString(bb);
                        }
                    } else if ("FamilyName".equals(s)) {
                        if (nextTokenType(bb) == PSSTRINGTOKEN) {
                            familyName = getString(bb);
                        }
                    } else if ("FontName".equals(s)) {
                        if (nextTokenType(bb) == PSNAMETOKEN) {
                            psName = getSimpleToken(bb);
                        }
                    } else if ("FontType".equals(s)) {
                        /* look for
                                 /FontType id def
                            */
                        String token = getSimpleToken(bb);
                        if ("def".equals(getSimpleToken(bb))) {
                            fontType = token;
                        }
                    }
                } else {
                    // skip token
                    while (bb.get() > ' ') ;
                }
            } else if (tokenType == PSEOFTOKEN) {
                eof = true;
            }
        }
    } catch (Exception e) {
        throw new FontFormatException(e.toString());
    }
    /* Ignore all fonts besides Type1 (e.g. Type3 fonts) */
    if (!"1".equals(fontType)) {
        throw new FontFormatException("Unsupported font type");
    }
    if (psName == null) {
        //no explicit FontName
        // Try to extract font name from the first text line.
        // According to Type1 spec first line consist of
        //  "%!FontType1-SpecVersion: FontName FontVersion"
        // or
        //  "%!PS-AdobeFont-1.0: FontName version"
        bb.position(0);
        if (bb.getShort() != 0x2521) {
            //if pfb (do not start with "%!")
            //skip segment header and "%!"
            bb.position(8);
        //NB: assume that first segment is ASCII one
        //  (is it possible to have valid Type1 font with first binary segment?)
        }
        String formatType = getSimpleToken(bb);
        if (!formatType.startsWith("FontType1-") && !formatType.startsWith("PS-AdobeFont-")) {
            throw new FontFormatException("Unsupported font format [" + formatType + "]");
        }
        psName = getSimpleToken(bb);
    }
    //NB: At least psName must be already initialized by this moment
    if (eof) {
        //if we find fullName or familyName then use it as another name too
        if (fullName != null) {
            familyName = fullName2FamilyName(fullName);
        } else if (familyName != null) {
            fullName = familyName;
        } else {
            //fallback - use postscript font name to deduce full and family names
            fullName = psName2FullName(psName);
            familyName = psName2FamilyName(psName);
        }
    }
}
Also used : FontFormatException(java.awt.FontFormatException) 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 7 with FontFormatException

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

the class Type1Font method verify.

private void verify() throws FontFormatException {
    /* Normal usage should not call getBuffer(), as its state
         * ie endianness, position etc, are shared. verify() can do
         * this as its called only from within the constructor before
         * there are other users of this object.
         */
    ByteBuffer bb = getBuffer();
    if (bb.capacity() < 6) {
        throw new FontFormatException("short file");
    }
    int val = bb.get(0) & 0xff;
    if ((bb.get(0) & 0xff) == 0x80) {
        verifyPFB(bb);
        bb.position(6);
    } else {
        verifyPFA(bb);
        bb.position(0);
    }
    initNames(bb);
    if (familyName == null || fullName == null) {
        throw new FontFormatException("Font name not found");
    }
    setStyle();
}
Also used : ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) FontFormatException(java.awt.FontFormatException)

Example 8 with FontFormatException

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

the class TrueTypeFont method open.

/* This is intended to be called, and the returned value used,
     * from within a block synchronized on this font object.
     * ie the channel returned may be nulled out at any time by "close()"
     * unless the caller holds a lock.
     * Deadlock warning: FontManager.addToPool(..) acquires a global lock,
     * which means nested locks may be in effect.
     */
private synchronized FileChannel open(boolean usePool) throws FontFormatException {
    if (disposerRecord.channel == null) {
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger().info("open TTF: " + 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;
                }
            });
            disposerRecord.channel = raf.getChannel();
            fileSize = (int) disposerRecord.channel.size();
            if (usePool) {
                FontManager fm = FontManagerFactory.getInstance();
                if (fm instanceof SunFontManager) {
                    ((SunFontManager) fm).addToPool(this);
                }
            }
        } catch (NullPointerException e) {
            close();
            throw new FontFormatException(e.toString());
        } catch (ClosedChannelException e) {
            /* NIO I/O is interruptible, recurse to retry operation.
                 * The call to channel.size() above can throw this exception.
                 * Clear interrupts before recursing in case NIO didn't.
                 * Note that close() sets disposerRecord.channel to null.
                 */
            Thread.interrupted();
            close();
            open();
        } catch (IOException e) {
            close();
            throw new FontFormatException(e.toString());
        }
    }
    return disposerRecord.channel;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) RandomAccessFile(java.io.RandomAccessFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException)

Example 9 with FontFormatException

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

the class TrueTypeFont method getTableBuffer.

ByteBuffer getTableBuffer(int tag) {
    DirectoryEntry entry = null;
    for (int i = 0; i < numTables; i++) {
        if (tableDirectory[i].tag == tag) {
            entry = tableDirectory[i];
            break;
        }
    }
    if (entry == null || entry.length == 0 || entry.offset + entry.length > fileSize) {
        return null;
    }
    int bread = 0;
    ByteBuffer buffer = ByteBuffer.allocate(entry.length);
    synchronized (this) {
        try {
            if (disposerRecord.channel == null) {
                open();
            }
            disposerRecord.channel.position(entry.offset);
            bread = disposerRecord.channel.read(buffer);
            buffer.flip();
        } catch (ClosedChannelException e) {
            /* NIO I/O is interruptible, recurse to retry operation.
                 * Clear interrupts before recursing in case NIO didn't.
                 */
            Thread.interrupted();
            close();
            return getTableBuffer(tag);
        } catch (IOException e) {
            return null;
        } catch (FontFormatException e) {
            return null;
        }
        if (bread < entry.length) {
            return null;
        } else {
            return buffer;
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FontFormatException(java.awt.FontFormatException)

Example 10 with FontFormatException

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

the class Type1Font method readBlock.

public synchronized ByteBuffer readBlock(int offset, int length) {
    ByteBuffer mappedBuf = null;
    try {
        mappedBuf = getBuffer();
        if (offset > fileSize) {
            offset = fileSize;
        }
        mappedBuf.position(offset);
        return mappedBuf.slice();
    } catch (FontFormatException e) {
        return null;
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) FontFormatException(java.awt.FontFormatException)

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