Search in sources :

Example 6 with TrueTypeFont

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

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 (NullPointerException | IOException e) {
        // NPE due to TTF parser being buggy
        LOG.error("Could not load font file: " + ttfFile, e);
    }
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) OTFParser(org.apache.fontbox.ttf.OTFParser) OpenTypeFont(org.apache.fontbox.ttf.OpenTypeFont) IOException(java.io.IOException) TTFParser(org.apache.fontbox.ttf.TTFParser)

Example 7 with TrueTypeFont

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

the class FileSystemFontProvider method readTrueTypeFont.

private TrueTypeFont readTrueTypeFont(String postScriptName, File file) throws IOException {
    if (file.getName().toLowerCase().endsWith(".ttc")) {
        TrueTypeCollection ttc = new TrueTypeCollection(file);
        TrueTypeFont ttf = ttc.getFontByName(postScriptName);
        if (ttf == null) {
            ttc.close();
            throw new IOException("Font " + postScriptName + " not found in " + file);
        }
        return ttf;
    } else {
        TTFParser ttfParser = new TTFParser(false, true);
        return ttfParser.parse(file);
    }
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) TrueTypeCollection(org.apache.fontbox.ttf.TrueTypeCollection) IOException(java.io.IOException) TTFParser(org.apache.fontbox.ttf.TTFParser)

Example 8 with TrueTypeFont

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

the class FontMapperImpl method getCIDFont.

/**
 * 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);
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) OpenTypeFont(org.apache.fontbox.ttf.OpenTypeFont) FontBoxFont(org.apache.fontbox.FontBoxFont)

Example 9 with TrueTypeFont

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

the class FontMapperImpl method getTrueTypeFont.

/**
 * Finds a TrueType font with the given PostScript name, or a suitable substitute, or null.
 *
 * @param fontDescriptor FontDescriptor
 */
@Override
public FontMapping<TrueTypeFont> getTrueTypeFont(String baseFont, PDFontDescriptor fontDescriptor) {
    TrueTypeFont ttf = (TrueTypeFont) findFont(FontFormat.TTF, baseFont);
    if (ttf != null) {
        return new FontMapping<>(ttf, false);
    } else {
        // fallback - todo: i.e. fuzzy match
        String fontName = getFallbackFontName(fontDescriptor);
        ttf = (TrueTypeFont) findFont(FontFormat.TTF, fontName);
        if (ttf == null) {
            // we have to return something here as TTFs aren't strictly required on the system
            ttf = lastResortFont;
        }
        return new FontMapping<>(ttf, true);
    }
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont)

Example 10 with TrueTypeFont

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

the class FontMapperImpl method findFontBoxFont.

/**
 * 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;
}
Also used : TrueTypeFont(org.apache.fontbox.ttf.TrueTypeFont) OpenTypeFont(org.apache.fontbox.ttf.OpenTypeFont) Type1Font(org.apache.fontbox.type1.Type1Font)

Aggregations

TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)13 TTFParser (org.apache.fontbox.ttf.TTFParser)7 IOException (java.io.IOException)3 OpenTypeFont (org.apache.fontbox.ttf.OpenTypeFont)3 PDType0Font (org.apache.pdfbox.pdmodel.font.PDType0Font)3 File (java.io.File)2 TrueTypeCollection (org.apache.fontbox.ttf.TrueTypeCollection)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 FontBoxFont (org.apache.fontbox.FontBoxFont)1 CmapLookup (org.apache.fontbox.ttf.CmapLookup)1 CmapSubtable (org.apache.fontbox.ttf.CmapSubtable)1 CmapTable (org.apache.fontbox.ttf.CmapTable)1 NameRecord (org.apache.fontbox.ttf.NameRecord)1 NamingTable (org.apache.fontbox.ttf.NamingTable)1 OTFParser (org.apache.fontbox.ttf.OTFParser)1 PostScriptTable (org.apache.fontbox.ttf.PostScriptTable)1 Type1Font (org.apache.fontbox.type1.Type1Font)1