Package org.apache.fontbox.ttf

Examples of org.apache.fontbox.ttf.TrueTypeFont


                throw new TikaException("Bad TrueType font.");
            }
        }
       
        // Ask FontBox to parse the file for us
        TrueTypeFont font;
        TTFParser parser = new TTFParser();
        if (tis != null && tis.hasFile()) {
            font = parser.parseTTF(tis.getFile());
        } else {
            font = parser.parseTTF(stream);
        }

        // Report the details of the font
        metadata.set(Metadata.CONTENT_TYPE, TYPE.toString());
        metadata.set(TikaCoreProperties.CREATED, font.getHeader().getCreated().getTime());
        metadata.set(
                TikaCoreProperties.MODIFIED,
                font.getHeader().getModified().getTime());

        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
        xhtml.startDocument();
        xhtml.endDocument();
    }
View Full Code Here


                    "The FontFile can't be read for " + this.font.getName()));
        }
        else
        {
            // there must be exactly one encoding in the "cmap" table if the font is symbolic
            TrueTypeFont ttf = pdTrueTypeFont.getTrueTypeFont();
            try
            {
                if (pdTrueTypeFont.isSymbolic() && ttf.getCmap().getCmaps().length != 1)
                {
                    this.fContainer.push(new ValidationError(ERROR_FONTS_ENCODING,
                            "Symbolic TrueType font has more than one 'cmap' entry for " +
                            this.font.getName()));
                }
View Full Code Here

                "org/apache/pdfbox/ttf/ArialMT.ttf");
        Assert.assertNotNull(arialIs);

        TTFParser parser = new TTFParser();

        TrueTypeFont arial = parser.parse(arialIs);

        CmapTable cmapTable = arial.getCmap();
        Assert.assertNotNull(cmapTable);

        CmapSubtable[] cmaps = cmapTable.getCmaps();
        Assert.assertNotNull(cmaps);

        CmapSubtable cmap = null;

        for (CmapSubtable e : cmaps)
        {
            if (e.getPlatformId() == NameRecord.PLATFORM_WINDOWS
                    && e.getPlatformEncodingId() == NameRecord.ENCODING_WINDOWS_UNICODE_BMP)
            {
                cmap = e;
                break;
            }
        }

        Assert.assertNotNull(cmap);

        PostScriptTable post = arial.getPostScript();
        Assert.assertNotNull(post);

        String[] glyphNames = arial.getPostScript().getGlyphNames();
        Assert.assertNotNull(glyphNames);

        // test a WGL4 (Macintosh standard) name
        int gid = cmap.getGlyphId(0x2122); // TRADE MARK SIGN
        Assert.assertEquals("trademark", glyphNames[gid]);
View Full Code Here

     * we attempt to find a good fallback based on the font descriptor.
     */
    public static TrueTypeFont getTrueTypeFallbackFont(PDFontDescriptor fontDescriptor)
    {
        String fontName = getFallbackFontName(fontDescriptor);
        TrueTypeFont ttf = getTrueTypeFont(fontName);
        if (ttf == null)
        {
            // we have to return something here as TTFs aren't strictly required on the system
            log.error("No TTF fallback font for '" + fontName + "'");
            return ttfFallbackFont;
View Full Code Here

     * @param postScriptName PostScript font name
     */
    public static TrueTypeFont getTrueTypeFont(String postScriptName)
    {
        // first ask the font provider for the font
        TrueTypeFont ttf = getProvider().getTrueTypeFont(postScriptName);
        if (ttf == null)
        {
            // then try substitutes
            for (String substituteName : getSubstitutes(postScriptName))
            {
View Full Code Here

        if (cff != null)
        {
            return cff;
        }

        TrueTypeFont ttf = getTrueTypeFont(postScriptName);
        if (ttf != null)
        {
            return ttf;
        }
View Full Code Here

     * Adds an OTF or TTF font to the file cache. To reduce memory, the parsed font is not cached.
     */
    private void addOpenTypeFont(File otfFile) throws IOException
    {
        TTFParser ttfParser = new TTFParser(false, true);
        TrueTypeFont ttf = null;
        try
        {
            ttf = ttfParser.parse(otfFile);
        }
        catch (NullPointerException e) // TTF parser is buggy
        {
            LOG.error("Could not load font file: " + otfFile, e);
        }
        catch (IOException e)
        {
            LOG.error("Could not load font file: " + otfFile, e);
        }

        try
        {
            // check for 'name' table
            NamingTable nameTable = ttf.getNaming();
            if (nameTable == null)
            {
                LOG.warn("Missing 'name' table in font " + otfFile);
            }
            else
            {
                // read PostScript name, if any
                if (nameTable.getPostScriptName() != null)
                {
                    String psName = nameTable.getPostScriptName();

                    String format;
                    if (ttf.getTableMap().get("CFF ") != null)
                    {
                        format = "OTF";
                        cffFontFiles.put(psName, otfFile);
                    }
                    else
                    {
                        format = "TTF";
                        ttfFontFiles.put(psName, otfFile);
                    }

                    if (LOG.isTraceEnabled())
                    {
                        LOG.trace(format +": '" + psName + "' / '" + nameTable.getFontFamily() +
                                "' / '" + nameTable.getFontSubFamily() + "'");
                    }
                }
                else
                {
                    LOG.warn("Missing 'name' entry for PostScript name in font " + otfFile);
                }
            }
        }
        finally
        {
            if (ttf != null)
            {
                ttf.close();
            }
        }
    }
View Full Code Here

    }

    @Override
    public synchronized TrueTypeFont getTrueTypeFont(String postScriptName)
    {
        TrueTypeFont ttf = ttfFonts.get(postScriptName);
        if (ttf != null)
        {
            return ttf;
        }
View Full Code Here

        PDFontDescriptor fd = getFontDescriptor();
        PDStream ff2Stream = fd.getFontFile2();
        PDStream ff3Stream = fd.getFontFile3();

        TrueTypeFont ttfFont = null;
        boolean fontIsDamaged = false;
        if (ff2Stream != null)
        {
            try
            {
                // embedded
                TTFParser ttfParser = new TTFParser(true);
                ttfFont = ttfParser.parse(ff2Stream.createInputStream());
            }
            catch (NullPointerException e) // TTF parser is buggy
            {
                LOG.warn("Could not read embedded TTF for font " + getBaseFont(), e);
                fontIsDamaged = true;
            }
            catch (IOException e)
            {
                LOG.warn("Could not read embedded TTF for font " + getBaseFont(), e);
                fontIsDamaged = true;
            }
        }
        else if (ff3Stream != null)
        {
            try
            {
                // embedded
                OTFParser otfParser = new OTFParser(true);
                OpenTypeFont otf = otfParser.parse(ff3Stream.createInputStream());
                ttfFont = otf;

                if (otf.isPostScript())
                {
                    // todo: we need more abstraction to support CFF fonts here
                    throw new IOException("Not implemented: OpenType font with CFF table " +
                                          getBaseFont());
                }

                if (otf.hasLayoutTables())
                {
                    LOG.error("OpenType Layout tables used in font " + getBaseFont() +
                              " are not implemented in PDFBox and will be ignored");
                }
            }
            catch (NullPointerException e) // TTF parser is buggy
            {
                fontIsDamaged = true;
                LOG.warn("Could not read embedded OTF for font " + getBaseFont(), e);
            }
            catch (IOException e)
            {
                fontIsDamaged = true;
                LOG.warn("Could not read embedded OTF for font " + getBaseFont(), e);
            }
        }
        isEmbedded = ttfFont != null;
        isDamaged = fontIsDamaged;

        if (ttfFont == null)
        {
            // substitute
            TrueTypeFont ttfSubstitute = ExternalFonts.getTrueTypeFont(getBaseFont());
            if (ttfSubstitute != null)
            {
                ttfFont = ttfSubstitute;
            }
            else
View Full Code Here

     */
    public PDTrueTypeFont(COSDictionary fontDictionary) throws IOException
    {
        super(fontDictionary);

        TrueTypeFont ttfFont = null;
        boolean fontIsDamaged = false;
        if (getFontDescriptor() != null)
        {
            PDFontDescriptor fd = super.getFontDescriptor();
            PDStream ff2Stream = fd.getFontFile2();
View Full Code Here

TOP

Related Classes of org.apache.fontbox.ttf.TrueTypeFont

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.