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);
}
}
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]);
}
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());
}
Aggregations