Search in sources :

Example 1 with PostScriptTable

use of org.apache.fontbox.ttf.PostScriptTable in project pdfbox by apache.

the class PDTrueTypeFont method readEncodingFromFont.

@Override
protected Encoding readEncodingFromFont() throws IOException {
    if (!isEmbedded() && getStandard14AFM() != null) {
        // read from AFM
        return new Type1Encoding(getStandard14AFM());
    } else {
        // which will override this anyway
        if (getSymbolicFlag() != null && !getSymbolicFlag()) {
            return StandardEncoding.INSTANCE;
        }
        // normalise the standard 14 name, e.g "Symbol,Italic" -> "Symbol"
        FontName standard14Name = Standard14Fonts.getMappedFontName(getName());
        // likewise, if the font is standard 14 then we know it's Standard Encoding
        if (isStandard14() && standard14Name != FontName.SYMBOL && standard14Name != FontName.ZAPF_DINGBATS) {
            return StandardEncoding.INSTANCE;
        }
        // synthesize an encoding, so that getEncoding() is always usable
        PostScriptTable post = ttf.getPostScript();
        Map<Integer, String> codeToName = new HashMap<>();
        for (int code = 0; code <= 256; code++) {
            int gid = codeToGID(code);
            if (gid > 0) {
                String name = null;
                if (post != null) {
                    name = post.getName(gid);
                }
                if (name == null) {
                    // GID pseudo-name
                    name = Integer.toString(gid);
                }
                codeToName.put(code, name);
            }
        }
        return new BuiltInEncoding(codeToName);
    }
}
Also used : PostScriptTable(org.apache.fontbox.ttf.PostScriptTable) Type1Encoding(org.apache.pdfbox.pdmodel.font.encoding.Type1Encoding) HashMap(java.util.HashMap) BuiltInEncoding(org.apache.pdfbox.pdmodel.font.encoding.BuiltInEncoding) FontName(org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName) UniUtil.getUniNameOfCodePoint(org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint)

Example 2 with PostScriptTable

use of org.apache.fontbox.ttf.PostScriptTable in project pdfbox by apache.

the class TrueTypeEmbedder method createFontDescriptor.

/**
 * Creates a new font descriptor dictionary for the given TTF.
 */
private PDFontDescriptor createFontDescriptor(TrueTypeFont ttf) throws IOException {
    PDFontDescriptor fd = new PDFontDescriptor();
    fd.setFontName(ttf.getName());
    OS2WindowsMetricsTable os2 = ttf.getOS2Windows();
    if (os2 == null) {
        throw new IOException("os2 table is missing in font " + ttf.getName());
    }
    PostScriptTable post = ttf.getPostScript();
    if (post == null) {
        throw new IOException("post table is missing in font " + ttf.getName());
    }
    // Flags
    fd.setFixedPitch(post.getIsFixedPitch() > 0 || ttf.getHorizontalHeader().getNumberOfHMetrics() == 1);
    int fsSelection = os2.getFsSelection();
    fd.setItalic(((fsSelection & (ITALIC | OBLIQUE)) != 0));
    switch(os2.getFamilyClass()) {
        case OS2WindowsMetricsTable.FAMILY_CLASS_CLAREDON_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_FREEFORM_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_MODERN_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_OLDSTYLE_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_SLAB_SERIFS:
            fd.setSerif(true);
            break;
        case OS2WindowsMetricsTable.FAMILY_CLASS_SCRIPTS:
            fd.setScript(true);
            break;
        default:
            break;
    }
    fd.setFontWeight(os2.getWeightClass());
    fd.setSymbolic(true);
    fd.setNonSymbolic(false);
    // ItalicAngle
    fd.setItalicAngle(post.getItalicAngle());
    // FontBBox
    HeaderTable header = ttf.getHeader();
    PDRectangle rect = new PDRectangle();
    float scaling = 1000f / header.getUnitsPerEm();
    rect.setLowerLeftX(header.getXMin() * scaling);
    rect.setLowerLeftY(header.getYMin() * scaling);
    rect.setUpperRightX(header.getXMax() * scaling);
    rect.setUpperRightY(header.getYMax() * scaling);
    fd.setFontBoundingBox(rect);
    // Ascent, Descent
    HorizontalHeaderTable hHeader = ttf.getHorizontalHeader();
    fd.setAscent(hHeader.getAscender() * scaling);
    fd.setDescent(hHeader.getDescender() * scaling);
    // CapHeight, XHeight
    if (os2.getVersion() >= 1.2) {
        fd.setCapHeight(os2.getCapHeight() * scaling);
        fd.setXHeight(os2.getHeight() * scaling);
    } else {
        GeneralPath capHPath = ttf.getPath("H");
        if (capHPath != null) {
            fd.setCapHeight(Math.round(capHPath.getBounds2D().getMaxY()) * scaling);
        } else {
            // estimate by summing the typographical +ve ascender and -ve descender
            fd.setCapHeight((os2.getTypoAscender() + os2.getTypoDescender()) * scaling);
        }
        GeneralPath xPath = ttf.getPath("x");
        if (xPath != null) {
            fd.setXHeight(Math.round(xPath.getBounds2D().getMaxY()) * scaling);
        } else {
            // estimate by halving the typographical ascender
            fd.setXHeight(os2.getTypoAscender() / 2.0f * scaling);
        }
    }
    // StemV - there's no true TTF equivalent of this, so we estimate it
    fd.setStemV(fd.getFontBoundingBox().getWidth() * .13f);
    return fd;
}
Also used : PostScriptTable(org.apache.fontbox.ttf.PostScriptTable) GeneralPath(java.awt.geom.GeneralPath) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) HorizontalHeaderTable(org.apache.fontbox.ttf.HorizontalHeaderTable) HeaderTable(org.apache.fontbox.ttf.HeaderTable) OS2WindowsMetricsTable(org.apache.fontbox.ttf.OS2WindowsMetricsTable) HorizontalHeaderTable(org.apache.fontbox.ttf.HorizontalHeaderTable)

Example 3 with PostScriptTable

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

Aggregations

PostScriptTable (org.apache.fontbox.ttf.PostScriptTable)3 GeneralPath (java.awt.geom.GeneralPath)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 CmapSubtable (org.apache.fontbox.ttf.CmapSubtable)1 CmapTable (org.apache.fontbox.ttf.CmapTable)1 HeaderTable (org.apache.fontbox.ttf.HeaderTable)1 HorizontalHeaderTable (org.apache.fontbox.ttf.HorizontalHeaderTable)1 OS2WindowsMetricsTable (org.apache.fontbox.ttf.OS2WindowsMetricsTable)1 TTFParser (org.apache.fontbox.ttf.TTFParser)1 TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 FontName (org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName)1 UniUtil.getUniNameOfCodePoint (org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint)1 BuiltInEncoding (org.apache.pdfbox.pdmodel.font.encoding.BuiltInEncoding)1 Type1Encoding (org.apache.pdfbox.pdmodel.font.encoding.Type1Encoding)1 Test (org.junit.Test)1