Search in sources :

Example 1 with TrueTypeCollection

use of org.apache.fontbox.ttf.TrueTypeCollection in project pdfbox by apache.

the class FileSystemFontProvider method readTrueTypeFont.

private TrueTypeFont readTrueTypeFont(String postScriptName, File file) throws IOException {
    if (file.getName().toLowerCase().endsWith(".ttc")) {
        TrueTypeCollection ttc = new TrueTypeCollection(file);
        TrueTypeFont ttf = ttc.getFontByName(postScriptName);
        if (ttf == null) {
            ttc.close();
            throw new IOException("Font " + postScriptName + " not found in " + file);
        }
        return ttf;
    } else {
        TTFParser ttfParser = new TTFParser(false, true);
        return ttfParser.parse(file);
    }
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) TrueTypeCollection(org.apache.fontbox.ttf.TrueTypeCollection) IOException(java.io.IOException) TTFParser(org.apache.fontbox.ttf.TTFParser)

Example 2 with TrueTypeCollection

use of org.apache.fontbox.ttf.TrueTypeCollection in project pdfbox-graphics2d by rototor.

the class PdfBoxGraphics2DFontTextDrawer method mapFont.

/**
 * Try to map the java.awt.Font to a PDFont.
 *
 * @param font
 *            the java.awt.Font for which a mapping should be found
 * @param env
 *            environment of the font mapper
 * @return the PDFont or null if none can be found.
 */
@SuppressWarnings("WeakerAccess")
protected PDFont mapFont(final Font font, final IFontTextDrawerEnv env) throws IOException, FontFormatException {
    /*
		 * If we have any font registering's, we must perform them now
		 */
    for (final FontEntry fontEntry : fontFiles) {
        if (fontEntry.overrideName == null) {
            Font javaFont = Font.createFont(Font.TRUETYPE_FONT, fontEntry.file);
            fontEntry.overrideName = javaFont.getFontName();
        }
        if (fontEntry.file.getName().toLowerCase(Locale.US).endsWith(".ttc")) {
            TrueTypeCollection collection = new TrueTypeCollection(fontEntry.file);
            collection.processAllFonts(new TrueTypeCollection.TrueTypeFontProcessor() {

                @Override
                public void process(TrueTypeFont ttf) throws IOException {
                    PDFont pdFont = PDType0Font.load(env.getDocument(), ttf, true);
                    fontMap.put(fontEntry.overrideName, pdFont);
                    fontMap.put(pdFont.getName(), pdFont);
                }
            });
        } else {
            /*
				 * We load the font using the file.
				 */
            PDFont pdFont = PDType0Font.load(env.getDocument(), fontEntry.file);
            fontMap.put(fontEntry.overrideName, pdFont);
        }
    }
    fontFiles.clear();
    return fontMap.get(font.getFontName());
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) TrueTypeCollection(org.apache.fontbox.ttf.TrueTypeCollection) PDType1Font(org.apache.pdfbox.pdmodel.font.PDType1Font) PDFont(org.apache.pdfbox.pdmodel.font.PDFont) TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) PDType0Font(org.apache.pdfbox.pdmodel.font.PDType0Font)

Aggregations

TrueTypeCollection (org.apache.fontbox.ttf.TrueTypeCollection)2 TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)2 IOException (java.io.IOException)1 TTFParser (org.apache.fontbox.ttf.TTFParser)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 PDType0Font (org.apache.pdfbox.pdmodel.font.PDType0Font)1 PDType1Font (org.apache.pdfbox.pdmodel.font.PDType1Font)1