Search in sources :

Example 11 with TrueTypeFont

use of com.tom_roush.fontbox.ttf.TrueTypeFont in project PdfBox-Android by TomRoush.

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(com.tom_roush.fontbox.ttf.TrueTypeFont) OpenTypeFont(com.tom_roush.fontbox.ttf.OpenTypeFont) FontBoxFont(com.tom_roush.fontbox.FontBoxFont)

Aggregations

TrueTypeFont (com.tom_roush.fontbox.ttf.TrueTypeFont)11 TTFParser (com.tom_roush.fontbox.ttf.TTFParser)4 OpenTypeFont (com.tom_roush.fontbox.ttf.OpenTypeFont)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 File (java.io.File)2 FontBoxFont (com.tom_roush.fontbox.FontBoxFont)1 CmapLookup (com.tom_roush.fontbox.ttf.CmapLookup)1 CmapSubtable (com.tom_roush.fontbox.ttf.CmapSubtable)1 CmapTable (com.tom_roush.fontbox.ttf.CmapTable)1 OTFParser (com.tom_roush.fontbox.ttf.OTFParser)1 PostScriptTable (com.tom_roush.fontbox.ttf.PostScriptTable)1 TrueTypeCollection (com.tom_roush.fontbox.ttf.TrueTypeCollection)1 TrueTypeFontProcessor (com.tom_roush.fontbox.ttf.TrueTypeCollection.TrueTypeFontProcessor)1 Type1Font (com.tom_roush.fontbox.type1.Type1Font)1 BoundingBox (com.tom_roush.fontbox.util.BoundingBox)1 SetMatrix (com.tom_roush.pdfbox.contentstream.operator.state.SetMatrix)1 PDCIDFont (com.tom_roush.pdfbox.pdmodel.font.PDCIDFont)1 PDCIDFontType2 (com.tom_roush.pdfbox.pdmodel.font.PDCIDFontType2)1 PDFontDescriptor (com.tom_roush.pdfbox.pdmodel.font.PDFontDescriptor)1