org.apache.fontbox.ttf.GlyphData Java Examples

The following examples show how to use org.apache.fontbox.ttf.GlyphData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDTrueTypeFont.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #2
Source File: PDCIDFontType2.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #3
Source File: PDCIDFontType2.java    From sambox with Apache License 2.0 6 votes vote down vote up
@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();
    }
    int gid = codeToGID(code);
    GlyphData glyph = ttf.getGlyph().getGlyph(gid);
    if (glyph != null)
    {
        return glyph.getPath();
    }
    return new GeneralPath();
}
 
Example #4
Source File: PDTrueTypeFont.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@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;
}
 
Example #5
Source File: PDTrueTypeFont.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example #6
Source File: PDCIDFontType2Embedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Build vertical metrics with Identity CIDToGIDMap (for embedding full font).
 */
private void buildVerticalMetrics(COSDictionary cidFont) throws IOException
{
    if (!buildVerticalHeader(cidFont))
    {
        return;
    }

    int cidMax = ttf.getNumberOfGlyphs();
    int[] gidMetrics = new int[cidMax * 4];
    for (int cid = 0; cid < cidMax; cid++)
    {
        GlyphData glyph = ttf.getGlyph().getGlyph(cid);
        if (glyph == null)
        {
            gidMetrics[cid * 4] = Integer.MIN_VALUE;
        }
        else
        {
            gidMetrics[cid * 4] = cid;
            gidMetrics[cid * 4 + 1] = ttf.getVerticalMetrics().getAdvanceHeight(cid);
            gidMetrics[cid * 4 + 2] = ttf.getHorizontalMetrics().getAdvanceWidth(cid);
            gidMetrics[cid * 4 + 3] = glyph.getYMaximum() + ttf.getVerticalMetrics().getTopSideBearing(cid);
        }
    }

    cidFont.setItem(COSName.W2, getVerticalMetrics(gidMetrics));
}
 
Example #7
Source File: PDTrueTypeFont.java    From sambox with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #8
Source File: PDTrueTypeFont.java    From sambox with Apache License 2.0 5 votes vote down vote up
@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();
    }
    return glyph.getPath();
}
 
Example #9
Source File: PDTrueTypeFont.java    From sambox with Apache License 2.0 5 votes vote down vote up
@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();
    }
    return new GeneralPath();
}
 
Example #10
Source File: PDCIDFontType2Embedder.java    From sambox with Apache License 2.0 5 votes vote down vote up
/**
 * Build vertical metrics with Identity CIDToGIDMap (for embedding full font).
 */
private void buildVerticalMetrics(COSDictionary cidFont) throws IOException
{
    if (!buildVerticalHeader(cidFont))
    {
        return;
    }

    int cidMax = ttf.getNumberOfGlyphs();
    int[] gidMetrics = new int[cidMax * 4];
    for (int cid = 0; cid < cidMax; cid++)
    {
        GlyphData glyph = ttf.getGlyph().getGlyph(cid);
        if (glyph == null)
        {
            gidMetrics[cid * 4] = Integer.MIN_VALUE;
        }
        else
        {
            gidMetrics[cid * 4] = cid;
            gidMetrics[cid * 4 + 1] = ttf.getVerticalMetrics().getAdvanceHeight(cid);
            gidMetrics[cid * 4 + 2] = ttf.getHorizontalMetrics().getAdvanceWidth(cid);
            gidMetrics[cid * 4 + 3] = glyph.getYMaximum()
                    + ttf.getVerticalMetrics().getTopSideBearing(cid);
        }
    }

    cidFont.setItem(COSName.W2, getVerticalMetrics(gidMetrics));
}
 
Example #11
Source File: FontState.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String getGlyphPath(PathCacheKey pck, GlyphTable glyphs) {
    try {
        GlyphData gd = glyphs.getGlyph(pck.getGlyph());
        if (gd != null) {
            GeneralPath p = gd.getPath();
            if (p != null)
                return getGlyphPath(pck.getFontKey(), p, pck.getAdvance());
        }
    } catch (IOException e) {
    }
    return null;
}
 
Example #12
Source File: PDCIDFontType2Embedder.java    From sambox with Apache License 2.0 4 votes vote down vote up
/**
 * Builds vertical metrics with a custom CIDToGIDMap (for embedding font subset).
 */
private void buildVerticalMetrics(Map<Integer, Integer> cidToGid) throws IOException
{
    // The "vhea" and "vmtx" tables that specify vertical metrics shall never be used by a conforming
    // reader. The only way to specify vertical metrics in PDF shall be by means of the DW2 and W2
    // entries in a CIDFont dictionary.

    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<Integer>(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();
            heights.add(COSInteger.get(cid)); // c
            heights.add(w2);
        }
        w2.add(COSInteger.get(advance)); // w1_iy
        long width = Math.round(hmtx.getAdvanceWidth(cid) * scaling);
        w2.add(COSInteger.get(width / 2)); // v_ix
        w2.add(COSInteger.get(height)); // v_iy
        prev = cid;
    }
    cidFont.setItem(COSName.W2, heights);
}