use of com.tom_roush.fontbox.ttf.TTFParser in project PdfBox-Android by TomRoush.
the class TestTTFParser method testPostTable.
/**
* Test the post table parser.
*
* @throws IOException if an error occurs.
*/
@Test
public void testPostTable() throws IOException {
InputStream input = PDFont.class.getResourceAsStream("/com/tom_roush/pdfbox/resources/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 com.tom_roush.fontbox.ttf.TTFParser in project PdfBox-Android by TomRoush.
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 {
File fontFile = new File(testContext.getCacheDir(), "F001u_3_7j.pdf");
OutputStream os = new FileOutputStream(fontFile);
IOUtils.copy(testContext.getAssets().open("com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf"), os);
os.close();
TrueTypeFont ttf1 = new TTFParser().parse(fontFile);
testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf1), fontFile);
ttf1.close();
TrueTypeFont ttf2 = new TTFParser().parse(new FileInputStream(fontFile));
testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf2), fontFile);
ttf2.close();
}
use of com.tom_roush.fontbox.ttf.TTFParser in project PdfBox-Android by TomRoush.
the class TrueTypeEmbedder method buildFontFile2.
public void buildFontFile2(InputStream ttfStream) throws IOException {
PDStream stream = new PDStream(document, ttfStream, COSName.FLATE_DECODE);
// as the stream was closed within the PDStream constructor, we have to recreate it
InputStream input = null;
try {
input = stream.createInputStream();
ttf = new TTFParser().parseEmbedded(input);
if (!isEmbeddingPermitted(ttf)) {
throw new IOException("This font does not permit embedding");
}
if (fontDescriptor == null) {
fontDescriptor = createFontDescriptor(ttf);
}
} finally {
IOUtils.closeQuietly(input);
}
stream.getCOSObject().setLong(COSName.LENGTH1, ttf.getOriginalDataSize());
fontDescriptor.setFontFile2(stream);
}
use of com.tom_roush.fontbox.ttf.TTFParser in project PdfBox-Android by TomRoush.
the class FileSystemFontProvider method addTrueTypeFont.
/**
* Adds an OTF or TTF font to the file cache. To reduce memory, the parsed font is not cached.
*/
private void addTrueTypeFont(File ttfFile) throws IOException {
try {
if (ttfFile.getPath().endsWith(".otf")) {
OTFParser parser = new OTFParser(false, true);
OpenTypeFont otf = parser.parse(ttfFile);
addTrueTypeFontImpl(otf, ttfFile);
} else {
TTFParser parser = new TTFParser(false, true);
TrueTypeFont ttf = parser.parse(ttfFile);
addTrueTypeFontImpl(ttf, ttfFile);
}
} catch (// TTF parser is buggy
NullPointerException e) {
Log.e("PdfBox-Android", "Could not load font file: " + ttfFile, e);
} catch (IOException e) {
Log.e("PdfBox-Android", "Could not load font file: " + ttfFile, e);
}
}
use of com.tom_roush.fontbox.ttf.TTFParser in project PdfBox-Android by TomRoush.
the class TestCMap method testPDFBox3997.
/**
* PDFBOX-3997: test unicode that is above the basic multilingual plane, here: helicopter
* symbol, or D83D DE81 in the Noto Emoji font.
*
* @throws IOException
*/
@Test
public // TODO: PdfBox-Android - provide test file
void testPDFBox3997() throws IOException {
String fontPath = "target/pdfs/NotoEmoji-Regular.ttf";
assumeTrue(new File(fontPath).exists());
TrueTypeFont ttf = new TTFParser().parse(fontPath);
CmapLookup cmap = ttf.getUnicodeCmapLookup(false);
Assert.assertEquals(886, cmap.getGlyphId(0x1F681));
ttf.close();
}
Aggregations