Search in sources :

Example 1 with GlyphData

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

the class PDTrueTypeFont method getPath.

@Override
public GeneralPath getPath(String name) throws IOException {
    // handle glyph names and uniXXXX names
    int gid = ttf.nameToGID(name);
    if (gid == 0) {
        try {
            // handle GID pseudo-names
            gid = Integer.parseInt(name);
            if (gid > ttf.getNumberOfGlyphs()) {
                gid = 0;
            }
        } catch (NumberFormatException e) {
            gid = 0;
        }
    }
    // I'm assuming .notdef paths are not drawn, as it PDFBOX-2421
    if (gid == 0) {
        return new GeneralPath();
    }
    GlyphData glyph = ttf.getGlyph().getGlyph(gid);
    if (glyph != null) {
        return glyph.getPath();
    } else {
        return new GeneralPath();
    }
}
Also used : GlyphData(org.apache.fontbox.ttf.GlyphData) GeneralPath(java.awt.geom.GeneralPath) UniUtil.getUniNameOfCodePoint(org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint)

Example 2 with GlyphData

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

the class PDTrueTypeFont method getPath.

@Override
public GeneralPath getPath(int code) throws IOException {
    int gid = codeToGID(code);
    GlyphData glyph = ttf.getGlyph().getGlyph(gid);
    // some glyphs have no outlines (e.g. space, table, newline)
    if (glyph == null) {
        return new GeneralPath();
    } else {
        return glyph.getPath();
    }
}
Also used : GlyphData(org.apache.fontbox.ttf.GlyphData) GeneralPath(java.awt.geom.GeneralPath) UniUtil.getUniNameOfCodePoint(org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint)

Example 3 with GlyphData

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

the class PDCIDFontType2 method getPath.

@Override
public GeneralPath getPath(int code) throws IOException {
    if (ttf instanceof OpenTypeFont && ((OpenTypeFont) ttf).isPostScript()) {
        // we're not supposed to have CFF fonts inside PDCIDFontType2, but if we do,
        // then we treat their CIDs as GIDs, see PDFBOX-3344
        int cid = codeToGID(code);
        Type2CharString charstring = ((OpenTypeFont) ttf).getCFF().getFont().getType2CharString(cid);
        return charstring.getPath();
    } else {
        int gid = codeToGID(code);
        GlyphData glyph = ttf.getGlyph().getGlyph(gid);
        if (glyph != null) {
            return glyph.getPath();
        }
        return new GeneralPath();
    }
}
Also used : Type2CharString(org.apache.fontbox.cff.Type2CharString) GlyphData(org.apache.fontbox.ttf.GlyphData) GeneralPath(java.awt.geom.GeneralPath) OpenTypeFont(org.apache.fontbox.ttf.OpenTypeFont)

Example 4 with GlyphData

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

the class PDCIDFontType2Embedder method buildVerticalMetrics.

/**
 * Builds vertical metrics with a custom CIDToGIDMap (for embedding font subset).
 */
private void buildVerticalMetrics(Map<Integer, Integer> cidToGid) throws IOException {
    if (!buildVerticalHeader(cidFont)) {
        return;
    }
    float scaling = 1000f / ttf.getHeader().getUnitsPerEm();
    VerticalHeaderTable vhea = ttf.getVerticalHeader();
    VerticalMetricsTable vmtx = ttf.getVerticalMetrics();
    GlyphTable glyf = ttf.getGlyph();
    HorizontalMetricsTable hmtx = ttf.getHorizontalMetrics();
    long v_y = Math.round(vhea.getAscender() * scaling);
    long w1 = Math.round(-vhea.getAdvanceHeightMax() * scaling);
    COSArray heights = new COSArray();
    COSArray w2 = new COSArray();
    int prev = Integer.MIN_VALUE;
    // Use a sorted list to get an optimal width array
    Set<Integer> keys = new TreeSet<>(cidToGid.keySet());
    for (int cid : keys) {
        // Unlike buildWidths, we look up with cid (not gid) here because this is
        // the original TTF, not the rebuilt one.
        GlyphData glyph = glyf.getGlyph(cid);
        if (glyph == null) {
            continue;
        }
        long height = Math.round((glyph.getYMaximum() + vmtx.getTopSideBearing(cid)) * scaling);
        long advance = Math.round(-vmtx.getAdvanceHeight(cid) * scaling);
        if (height == v_y && advance == w1) {
            // skip default metrics
            continue;
        }
        // c [w1_1y v_1x v_1y w1_2y v_2x v_2y ... w1_ny v_nx v_ny]
        if (prev != cid - 1) {
            w2 = new COSArray();
            // c
            heights.add(COSInteger.get(cid));
            heights.add(w2);
        }
        // w1_iy
        w2.add(COSInteger.get(advance));
        long width = Math.round(hmtx.getAdvanceWidth(cid) * scaling);
        // v_ix
        w2.add(COSInteger.get(width / 2));
        // v_iy
        w2.add(COSInteger.get(height));
        prev = cid;
    }
    cidFont.setItem(COSName.W2, heights);
}
Also used : COSInteger(org.apache.pdfbox.cos.COSInteger) GlyphData(org.apache.fontbox.ttf.GlyphData) COSArray(org.apache.pdfbox.cos.COSArray) VerticalHeaderTable(org.apache.fontbox.ttf.VerticalHeaderTable) TreeSet(java.util.TreeSet) GlyphTable(org.apache.fontbox.ttf.GlyphTable) VerticalMetricsTable(org.apache.fontbox.ttf.VerticalMetricsTable) HorizontalMetricsTable(org.apache.fontbox.ttf.HorizontalMetricsTable)

Example 5 with GlyphData

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

the class PDTrueTypeFont method getHeight.

@Override
public float getHeight(int code) throws IOException {
    int gid = codeToGID(code);
    GlyphData glyph = ttf.getGlyph().getGlyph(gid);
    if (glyph != null) {
        return glyph.getBoundingBox().getHeight();
    }
    return 0;
}
Also used : GlyphData(org.apache.fontbox.ttf.GlyphData) UniUtil.getUniNameOfCodePoint(org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint)

Aggregations

GlyphData (org.apache.fontbox.ttf.GlyphData)6 GeneralPath (java.awt.geom.GeneralPath)3 UniUtil.getUniNameOfCodePoint (org.apache.pdfbox.pdmodel.font.UniUtil.getUniNameOfCodePoint)3 TreeSet (java.util.TreeSet)1 Type2CharString (org.apache.fontbox.cff.Type2CharString)1 GlyphTable (org.apache.fontbox.ttf.GlyphTable)1 HorizontalMetricsTable (org.apache.fontbox.ttf.HorizontalMetricsTable)1 OpenTypeFont (org.apache.fontbox.ttf.OpenTypeFont)1 VerticalHeaderTable (org.apache.fontbox.ttf.VerticalHeaderTable)1 VerticalMetricsTable (org.apache.fontbox.ttf.VerticalMetricsTable)1 COSArray (org.apache.pdfbox.cos.COSArray)1 COSInteger (org.apache.pdfbox.cos.COSInteger)1