use of com.tom_roush.fontbox.ttf.TrueTypeFont 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.TrueTypeFont 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.TrueTypeFont in project PdfBox-Android by TomRoush.
the class PDCIDFontType2 method findFontOrSubstitute.
private TrueTypeFont findFontOrSubstitute() throws IOException {
TrueTypeFont ttfFont;
CIDFontMapping mapping = FontMappers.instance().getCIDFont(getBaseFont(), getFontDescriptor(), getCIDSystemInfo());
if (mapping.isCIDFont()) {
ttfFont = mapping.getFont();
} else {
ttfFont = (TrueTypeFont) mapping.getTrueTypeFont();
}
if (mapping.isFallback()) {
Log.w("PdfBox-Android", "Using fallback font " + ttfFont.getName() + " for CID-keyed TrueType font " + getBaseFont());
}
return ttfFont;
}
use of com.tom_roush.fontbox.ttf.TrueTypeFont 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.TrueTypeFont in project PdfBox-Android by TomRoush.
the class FontMapperImpl method findFontBoxFont.
/**
* Finds a font with the given PostScript name, or a suitable substitute, or null.
*
* @param postScriptName PostScript font name
*/
private FontBoxFont findFontBoxFont(String postScriptName) {
Type1Font t1 = (Type1Font) findFont(FontFormat.PFB, postScriptName);
if (t1 != null) {
return t1;
}
TrueTypeFont ttf = (TrueTypeFont) findFont(FontFormat.TTF, postScriptName);
if (ttf != null) {
return ttf;
}
OpenTypeFont otf = (OpenTypeFont) findFont(FontFormat.OTF, postScriptName);
if (otf != null) {
return otf;
}
return null;
}
Aggregations