Search in sources :

Example 1 with TrueTypeFont

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]);
}
Also used : TrueTypeFont(com.tom_roush.fontbox.ttf.TrueTypeFont) PostScriptTable(com.tom_roush.fontbox.ttf.PostScriptTable) CmapTable(com.tom_roush.fontbox.ttf.CmapTable) InputStream(java.io.InputStream) TTFParser(com.tom_roush.fontbox.ttf.TTFParser) CmapSubtable(com.tom_roush.fontbox.ttf.CmapSubtable) Test(org.junit.Test)

Example 2 with TrueTypeFont

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();
}
Also used : TrueTypeFont(com.tom_roush.fontbox.ttf.TrueTypeFont) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) TTFParser(com.tom_roush.fontbox.ttf.TTFParser) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with TrueTypeFont

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;
}
Also used : TrueTypeFont(com.tom_roush.fontbox.ttf.TrueTypeFont)

Example 4 with TrueTypeFont

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);
    }
}
Also used : TrueTypeFont(com.tom_roush.fontbox.ttf.TrueTypeFont) OTFParser(com.tom_roush.fontbox.ttf.OTFParser) OpenTypeFont(com.tom_roush.fontbox.ttf.OpenTypeFont) IOException(java.io.IOException) TTFParser(com.tom_roush.fontbox.ttf.TTFParser)

Example 5 with TrueTypeFont

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;
}
Also used : TrueTypeFont(com.tom_roush.fontbox.ttf.TrueTypeFont) OpenTypeFont(com.tom_roush.fontbox.ttf.OpenTypeFont) Type1Font(com.tom_roush.fontbox.type1.Type1Font)

Aggregations

TrueTypeFont (com.tom_roush.fontbox.ttf.TrueTypeFont)11 TTFParser (com.tom_roush.fontbox.ttf.TTFParser)4 OpenTypeFont (com.tom_roush.fontbox.ttf.OpenTypeFont)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 File (java.io.File)2 FontBoxFont (com.tom_roush.fontbox.FontBoxFont)1 CmapLookup (com.tom_roush.fontbox.ttf.CmapLookup)1 CmapSubtable (com.tom_roush.fontbox.ttf.CmapSubtable)1 CmapTable (com.tom_roush.fontbox.ttf.CmapTable)1 OTFParser (com.tom_roush.fontbox.ttf.OTFParser)1 PostScriptTable (com.tom_roush.fontbox.ttf.PostScriptTable)1 TrueTypeCollection (com.tom_roush.fontbox.ttf.TrueTypeCollection)1 TrueTypeFontProcessor (com.tom_roush.fontbox.ttf.TrueTypeCollection.TrueTypeFontProcessor)1 Type1Font (com.tom_roush.fontbox.type1.Type1Font)1 BoundingBox (com.tom_roush.fontbox.util.BoundingBox)1 SetMatrix (com.tom_roush.pdfbox.contentstream.operator.state.SetMatrix)1 PDCIDFont (com.tom_roush.pdfbox.pdmodel.font.PDCIDFont)1 PDCIDFontType2 (com.tom_roush.pdfbox.pdmodel.font.PDCIDFontType2)1 PDFontDescriptor (com.tom_roush.pdfbox.pdmodel.font.PDFontDescriptor)1