Search in sources :

Example 1 with FontMetrics

use of org.apache.fontbox.afm.FontMetrics in project tika by apache.

the class AdobeFontMetricParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    FontMetrics fontMetrics;
    AFMParser parser = new AFMParser(stream);
    // Have FontBox process the file
    fontMetrics = parser.parse();
    // Get the comments in the file to display in xhtml
    List<String> unModifiableComments = fontMetrics.getComments();
    //have to copy because we modify list in extractCreationDate
    List<String> comments = new ArrayList<>();
    for (String comment : unModifiableComments) {
        comments.add(comment);
    }
    // Get the creation date
    extractCreationDate(metadata, comments);
    metadata.set(Metadata.CONTENT_TYPE, AFM_TYPE.toString());
    metadata.set(TikaCoreProperties.TITLE, fontMetrics.getFullName());
    // Add metadata associated with the font type
    addMetadataByString(metadata, MET_AVG_CHAR_WIDTH, Float.toString(fontMetrics.getAverageCharacterWidth()));
    addMetadataByString(metadata, MET_DOC_VERSION, Float.toString(fontMetrics.getAFMVersion()));
    addMetadataByString(metadata, MET_FONT_NAME, fontMetrics.getFontName());
    addMetadataByString(metadata, MET_FONT_FULL_NAME, fontMetrics.getFullName());
    addMetadataByString(metadata, MET_FONT_FAMILY_NAME, fontMetrics.getFamilyName());
    addMetadataByString(metadata, MET_FONT_VERSION, fontMetrics.getFontVersion());
    addMetadataByString(metadata, MET_FONT_WEIGHT, fontMetrics.getWeight());
    addMetadataByString(metadata, MET_FONT_NOTICE, fontMetrics.getNotice());
    addMetadataByString(metadata, MET_FONT_UNDERLINE_THICKNESS, Float.toString(fontMetrics.getUnderlineThickness()));
    // Output the remaining comments as text
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    // Display the comments
    if (comments.size() > 0) {
        xhtml.element("h1", "Comments");
        xhtml.startElement("div", "class", "comments");
        for (String comment : comments) {
            xhtml.element("p", comment);
        }
        xhtml.endElement("div");
    }
    xhtml.endDocument();
}
Also used : AFMParser(org.apache.fontbox.afm.AFMParser) FontMetrics(org.apache.fontbox.afm.FontMetrics) ArrayList(java.util.ArrayList) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler)

Aggregations

ArrayList (java.util.ArrayList)1 AFMParser (org.apache.fontbox.afm.AFMParser)1 FontMetrics (org.apache.fontbox.afm.FontMetrics)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1