Search in sources :

Example 1 with NamingTable

use of org.apache.fontbox.ttf.NamingTable in project tika by apache.

the class TrueTypeParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    TikaInputStream tis = TikaInputStream.cast(stream);
    // Ask FontBox to parse the file for us
    TrueTypeFont font = null;
    try {
        TTFParser parser = new TTFParser();
        if (tis != null && tis.hasFile()) {
            font = parser.parse(tis.getFile());
        } else {
            font = parser.parse(stream);
        }
        // Report the details of the font
        metadata.set(Metadata.CONTENT_TYPE, TYPE.toString());
        metadata.set(TikaCoreProperties.CREATED, font.getHeader().getCreated());
        metadata.set(TikaCoreProperties.MODIFIED, font.getHeader().getModified());
        metadata.set(AdobeFontMetricParser.MET_DOC_VERSION, Float.toString(font.getHeader().getVersion()));
        // Pull out the naming info
        NamingTable fontNaming = font.getNaming();
        for (NameRecord nr : fontNaming.getNameRecords()) {
            if (nr.getNameId() == NameRecord.NAME_FONT_FAMILY_NAME) {
                metadata.set(AdobeFontMetricParser.MET_FONT_FAMILY_NAME, nr.getString());
            }
            if (nr.getNameId() == NameRecord.NAME_FONT_SUB_FAMILY_NAME) {
                metadata.set(AdobeFontMetricParser.MET_FONT_SUB_FAMILY_NAME, nr.getString());
            }
            if (nr.getNameId() == NameRecord.NAME_FULL_FONT_NAME) {
                metadata.set(AdobeFontMetricParser.MET_FONT_NAME, nr.getString());
                metadata.set(TikaCoreProperties.TITLE, nr.getString());
            }
            if (nr.getNameId() == NameRecord.NAME_POSTSCRIPT_NAME) {
                metadata.set(AdobeFontMetricParser.MET_PS_NAME, nr.getString());
            }
            if (nr.getNameId() == NameRecord.NAME_COPYRIGHT) {
                metadata.set("Copyright", nr.getString());
            }
            if (nr.getNameId() == NameRecord.NAME_TRADEMARK) {
                metadata.set("Trademark", nr.getString());
            }
        }
    } finally {
        if (font != null) {
            font.close();
        }
    }
    // For now, we only output metadata, no textual contents
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    xhtml.endDocument();
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) NameRecord(org.apache.fontbox.ttf.NameRecord) NamingTable(org.apache.fontbox.ttf.NamingTable) TikaInputStream(org.apache.tika.io.TikaInputStream) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) TTFParser(org.apache.fontbox.ttf.TTFParser)

Aggregations

NameRecord (org.apache.fontbox.ttf.NameRecord)1 NamingTable (org.apache.fontbox.ttf.NamingTable)1 TTFParser (org.apache.fontbox.ttf.TTFParser)1 TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1