Search in sources :

Example 1 with TTFParser

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

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

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream) IOException(java.io.IOException) TTFParser(com.tom_roush.fontbox.ttf.TTFParser)

Example 4 with TTFParser

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);
    }
}
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 TTFParser

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

Aggregations

TTFParser (com.tom_roush.fontbox.ttf.TTFParser)5 TrueTypeFont (com.tom_roush.fontbox.ttf.TrueTypeFont)4 Test (org.junit.Test)3 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 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 OpenTypeFont (com.tom_roush.fontbox.ttf.OpenTypeFont)1 PostScriptTable (com.tom_roush.fontbox.ttf.PostScriptTable)1 PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1