org.apache.fontbox.ttf.OpenTypeFont Java Examples

The following examples show how to use org.apache.fontbox.ttf.OpenTypeFont. 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: FontMapperImpl.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
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: FileSystemFontProvider.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private OpenTypeFont getOTFFont(String postScriptName, File file)
{
    try
    {
        // todo JH: we don't yet support loading CFF fonts from OTC collections

        OTFParser parser = new OTFParser(false, true);
        OpenTypeFont otf = parser.parse(file);

        if (LOG.isDebugEnabled())
        {
            LOG.debug("Loaded " + postScriptName + " from " + file);
        }
        return otf;
    }
    catch (IOException e)
    {
        LOG.error("Could not load font file: " + file, e);
    }
    return null;
}
 
Example #4
Source File: FontMapperImpl.java    From sambox with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
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 #6
Source File: FileSystemFontProvider.java    From sambox with Apache License 2.0 6 votes vote down vote up
private static OpenTypeFont getOTFFont(String postScriptName, File file)
{
    try
    {
        // todo JH: we don't yet support loading CFF fonts from OTC collections
        OTFParser parser = new OTFParser(false, true);
        OpenTypeFont otf = parser.parse(file);
        LOG.debug("Loaded {} from {}", postScriptName, file);
        return otf;
    }
    catch (IOException e)
    {
        LOG.error("Could not load font file: " + file, e);
    }
    return null;
}
 
Example #7
Source File: FontState.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private boolean useLayoutTables(FontKey key, OpenTypeFont otf) throws IOException
{
    if (otf == null)
        return false;
    else if (useLayoutTables)
        return true;
    else if (otf.hasLayoutTables() && !useLayoutTablesFailed) {
        gdef = otf.getGDEF();
        gsub = otf.getGSUB();
        gpos = otf.getGPOS();
        if ((gsub == null) && (gpos == null))
            useLayoutTablesFailed = true;
        return !useLayoutTablesFailed;
    } else
        return false;
}
 
Example #8
Source File: FontMapperImpl.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Finds a CFF CID-Keyed font with the given PostScript name, or a suitable substitute, or null.
 * This method can also map CJK fonts via their CIDSystemInfo (ROS).
 * 
 * @param fontDescriptor FontDescriptor
 * @param cidSystemInfo the CID system info, e.g. "Adobe-Japan1", if any.
 */
@Override
public CIDFontMapping getCIDFont(String baseFont, PDFontDescriptor fontDescriptor,
                                        PDCIDSystemInfo cidSystemInfo)
{
    // try name match or substitute with OTF
    OpenTypeFont otf1 = (OpenTypeFont)findFont(FontFormat.OTF, baseFont);
    if (otf1 != null)
    {
        return new CIDFontMapping(otf1, null, false);
    }

    // try name match or substitute with TTF
    TrueTypeFont ttf = (TrueTypeFont)findFont(FontFormat.TTF, baseFont);
    if (ttf != null)
    {
        return new CIDFontMapping(null, ttf, false);
    }

    if (cidSystemInfo != null)
    {
        // "In Acrobat 3.0.1 and later, Type 0 fonts that use a CMap whose CIDSystemInfo
        // dictionary defines the Adobe-GB1, Adobe-CNS1 Adobe-Japan1, or Adobe-Korea1 character
        // collection can also be substituted." - Adobe Supplement to the ISO 32000

        String collection = cidSystemInfo.getRegistry() + "-" + cidSystemInfo.getOrdering();
        
        if (collection.equals("Adobe-GB1") || collection.equals("Adobe-CNS1") ||
            collection.equals("Adobe-Japan1") || collection.equals("Adobe-Korea1"))
        {
            // try automatic substitutes via character collection
            PriorityQueue<FontMatch> queue = getFontMatches(fontDescriptor, cidSystemInfo);
            FontMatch bestMatch = queue.poll();
            if (bestMatch != null)
            {
                FontBoxFont font = bestMatch.info.getFont();
                if (font instanceof OpenTypeFont)
                {
                    return new CIDFontMapping((OpenTypeFont)font, null, true);
                }
                else if (font != null)
                {
                    return new CIDFontMapping(null, font, true);
                }  
            }
        }
    }

    // last-resort fallback
    return new CIDFontMapping(null, lastResortFont, true);
}
 
Example #9
Source File: CIDFontMapping.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public CIDFontMapping(OpenTypeFont font, FontBoxFont fontBoxFont, boolean isFallback)
{
    super(font, isFallback);
    this.ttf = fontBoxFont;
}
 
Example #10
Source File: FontMapperImpl.java    From sambox with Apache License 2.0 4 votes vote down vote up
/**
 * Finds a CFF CID-Keyed font with the given PostScript name, or a suitable substitute, or null. This method can
 * also map CJK fonts via their CIDSystemInfo (ROS).
 * 
 * @param fontDescriptor FontDescriptor
 * @param cidSystemInfo the CID system info, e.g. "Adobe-Japan1", if any.
 */
@Override
public CIDFontMapping getCIDFont(String baseFont, PDFontDescriptor fontDescriptor,
        PDCIDSystemInfo cidSystemInfo)
{
    // try name match or substitute with OTF
    OpenTypeFont otf1 = (OpenTypeFont) findFont(FontFormat.OTF, baseFont);
    if (otf1 != null)
    {
        return new CIDFontMapping(otf1, null, false);
    }

    // try name match or substitute with TTF
    TrueTypeFont ttf = (TrueTypeFont) findFont(FontFormat.TTF, baseFont);
    if (ttf != null)
    {
        return new CIDFontMapping(null, ttf, false);
    }

    if (cidSystemInfo != null)
    {
        // "In Acrobat 3.0.1 and later, Type 0 fonts that use a CMap whose CIDSystemInfo
        // dictionary defines the Adobe-GB1, Adobe-CNS1 Adobe-Japan1, or Adobe-Korea1 character
        // collection can also be substituted." - Adobe Supplement to the ISO 32000

        String collection = cidSystemInfo.getRegistry() + "-" + cidSystemInfo.getOrdering();

        if (collection.equals("Adobe-GB1") || collection.equals("Adobe-CNS1")
                || collection.equals("Adobe-Japan1") || collection.equals("Adobe-Korea1"))
        {
            // try automatic substitutes via character collection
            PriorityQueue<FontMatch> queue = getFontMatches(fontDescriptor, cidSystemInfo);
            FontMatch bestMatch = queue.poll();
            if (bestMatch != null)
            {
                FontBoxFont font = bestMatch.info.getFont();
                if (font instanceof OpenTypeFont)
                {
                    return new CIDFontMapping((OpenTypeFont) font, null, true);
                }
                else if (font != null)
                {
                    return new CIDFontMapping(null, font, true);
                }
            }
        }
    }

    // last-resort fallback
    return new CIDFontMapping(null, lastResortFont, true);
}
 
Example #11
Source File: PDCIDFontType2.java    From sambox with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param fontDictionary The font dictionary according to the PDF specification.
 * @param parent The parent font.
 * @param trueTypeFont The true type font used to create the parent font
 * @throws IOException
 */
public PDCIDFontType2(COSDictionary fontDictionary, PDType0Font parent,
        TrueTypeFont trueTypeFont) throws IOException
{
    super(fontDictionary, parent);

    PDFontDescriptor fd = getFontDescriptor();
    if (trueTypeFont != null)
    {
        ttf = trueTypeFont;
        isEmbedded = true;
        isDamaged = false;
    }
    else
    {
        boolean fontIsDamaged = false;
        TrueTypeFont ttfFont = null;

        PDStream stream = null;
        if (fd != null)
        {
            stream = fd.getFontFile2();
            if (stream == null)
            {
                stream = fd.getFontFile3();
            }
            if (stream == null)
            {
                // Acrobat looks in FontFile too, even though it is not in the spec, see PDFBOX-2599
                stream = fd.getFontFile();
            }
        }

        if (stream != null)
        {
            try
            {
                // embedded OTF or TTF
                OTFParser otfParser = new OTFParser(true);
                OpenTypeFont otf = otfParser.parse(stream.createInputStream());
                ttfFont = otf;

                if (otf.isPostScript())
                {
                    // PDFBOX-3344 contains PostScript outlines instead of TrueType
                    fontIsDamaged = true;
                    LOG.warn(
                            "Found CFF/OTF but expected embedded TTF font " + fd.getFontName());
                }

            }
            catch (NullPointerException | IOException e) // TTF parser is buggy
            {
                fontIsDamaged = true;
                LOG.warn("Could not read embedded OTF for font " + getBaseFont(), e);
            }
        }
        isEmbedded = ttfFont != null;
        isDamaged = fontIsDamaged;

        if (ttfFont == null)
        {
            ttfFont = findFontOrSubstitute();
        }
        ttf = ttfFont;
    }
    cmap = ttf.getUnicodeCmapLookup(false);
    cid2gid = readCIDToGIDMap();
    if (cid2gid != null)
    {
        for (int cid = 0; cid < cid2gid.length; cid++)
        {
            int gid = cid2gid[cid];
            if (gid != 0)
            {
                gid2cid.put(gid, cid);
            }
        }
    }
}
 
Example #12
Source File: CIDFontMapping.java    From sambox with Apache License 2.0 4 votes vote down vote up
public CIDFontMapping(OpenTypeFont font, FontBoxFont fontBoxFont, boolean isFallback)
{
    super(font, isFallback);
    this.ttf = fontBoxFont;
}