Search in sources :

Example 11 with TrueTypeFont

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

the class PDFontTest method testPDFBox3826.

/**
 * PDFBOX-3826: Test ability to reuse a TrueTypeFont created from a file or a stream for several
 * PDFs to avoid parsing it over and over again. Also check that full or partial embedding is
 * done, and do render and text extraction.
 *
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testPDFBox3826() throws IOException, URISyntaxException {
    URL url = PDFontTest.class.getClassLoader().getResource("org/apache/pdfbox/ttf/LiberationSans-Regular.ttf");
    File fontFile = new File(url.toURI());
    try (TrueTypeFont ttf1 = new TTFParser().parse(fontFile)) {
        testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf1), fontFile);
    }
    try (TrueTypeFont ttf2 = new TTFParser().parse(new FileInputStream(fontFile))) {
        testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf2), fontFile);
    }
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) File(java.io.File) URL(java.net.URL) TTFParser(org.apache.fontbox.ttf.TTFParser) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 12 with TrueTypeFont

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

the class TestTTFParser method testPostTable.

/**
 * Test the post table parser.
 *
 * @throws IOException if an error occurs.
 */
@Test
public void testPostTable() throws IOException {
    InputStream input = TestTTFParser.class.getClassLoader().getResourceAsStream("org/apache/pdfbox/ttf/LiberationSans-Regular.ttf");
    Assert.assertNotNull(input);
    TTFParser parser = new TTFParser();
    TrueTypeFont font = parser.parse(input);
    CmapTable cmapTable = font.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 = font.getPostScript();
    Assert.assertNotNull(post);
    String[] glyphNames = font.getPostScript().getGlyphNames();
    Assert.assertNotNull(glyphNames);
    // test a WGL4 (Macintosh standard) name
    // TRADE MARK SIGN
    int gid = cmap.getGlyphId(0x2122);
    Assert.assertEquals("trademark", glyphNames[gid]);
    // test an additional name
    // EURO SIGN
    gid = cmap.getGlyphId(0x20AC);
    Assert.assertEquals("Euro", glyphNames[gid]);
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) PostScriptTable(org.apache.fontbox.ttf.PostScriptTable) CmapTable(org.apache.fontbox.ttf.CmapTable) InputStream(java.io.InputStream) TTFParser(org.apache.fontbox.ttf.TTFParser) CmapSubtable(org.apache.fontbox.ttf.CmapSubtable) Test(org.junit.Test)

Example 13 with TrueTypeFont

use of org.apache.fontbox.ttf.TrueTypeFont 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

TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)13 TTFParser (org.apache.fontbox.ttf.TTFParser)7 IOException (java.io.IOException)3 OpenTypeFont (org.apache.fontbox.ttf.OpenTypeFont)3 PDType0Font (org.apache.pdfbox.pdmodel.font.PDType0Font)3 File (java.io.File)2 TrueTypeCollection (org.apache.fontbox.ttf.TrueTypeCollection)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 FontBoxFont (org.apache.fontbox.FontBoxFont)1 CmapLookup (org.apache.fontbox.ttf.CmapLookup)1 CmapSubtable (org.apache.fontbox.ttf.CmapSubtable)1 CmapTable (org.apache.fontbox.ttf.CmapTable)1 NameRecord (org.apache.fontbox.ttf.NameRecord)1 NamingTable (org.apache.fontbox.ttf.NamingTable)1 OTFParser (org.apache.fontbox.ttf.OTFParser)1 PostScriptTable (org.apache.fontbox.ttf.PostScriptTable)1 Type1Font (org.apache.fontbox.type1.Type1Font)1