Search in sources :

Example 1 with CMap

use of com.tom_roush.fontbox.cmap.CMap in project PdfBox-Android by TomRoush.

the class CMapManager method parseCMap.

/**
 * Parse the given CMap.
 *
 * @param cMapStream the CMap to be read
 * @return the parsed CMap
 */
public static CMap parseCMap(InputStream cMapStream) throws IOException {
    CMap targetCmap = null;
    if (cMapStream != null) {
        CMapParser parser = new CMapParser();
        targetCmap = parser.parse(cMapStream);
    }
    return targetCmap;
}
Also used : CMapParser(com.tom_roush.fontbox.cmap.CMapParser) CMap(com.tom_roush.fontbox.cmap.CMap)

Example 2 with CMap

use of com.tom_roush.fontbox.cmap.CMap in project PdfBox-Android by TomRoush.

the class PDType0Font method fetchCMapUCS2.

/**
 * Fetches the corresponding UCS2 CMap if the font's CMap is predefined.
 */
private void fetchCMapUCS2() throws IOException {
    // if the font is composite and uses a predefined cmap (excluding Identity-H/V)
    // or whose descendant CIDFont uses the Adobe-GB1, Adobe-CNS1, Adobe-Japan1, or
    // Adobe-Korea1 character collection:
    COSName name = dict.getCOSName(COSName.ENCODING);
    if (isCMapPredefined && !(name == COSName.IDENTITY_H || name == COSName.IDENTITY_V) || isDescendantCJK) {
        // a) Map the character code to a CID using the font's CMap
        // b) Obtain the ROS from the font's CIDSystemInfo
        // c) Construct a second CMap name by concatenating the ROS in the format "R-O-UCS2"
        // d) Obtain the CMap with the constructed name
        // e) Map the CID according to the CMap from step d), producing a Unicode value
        // todo: not sure how to interpret the PDF spec here, do we always override? or only when Identity-H/V?
        String strName = null;
        if (isDescendantCJK) {
            strName = descendantFont.getCIDSystemInfo().getRegistry() + "-" + descendantFont.getCIDSystemInfo().getOrdering() + "-" + descendantFont.getCIDSystemInfo().getSupplement();
        } else if (name != null) {
            strName = name.getName();
        }
        // try to find the corresponding Unicode (UC2) CMap
        if (strName != null) {
            CMap prdCMap = CMapManager.getPredefinedCMap(strName);
            String ucs2Name = prdCMap.getRegistry() + "-" + prdCMap.getOrdering() + "-UCS2";
            cMapUCS2 = CMapManager.getPredefinedCMap(ucs2Name);
        }
    }
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) CMap(com.tom_roush.fontbox.cmap.CMap)

Example 3 with CMap

use of com.tom_roush.fontbox.cmap.CMap in project PdfBox-Android by TomRoush.

the class CMapManager method getPredefinedCMap.

/**
 * Fetches the predefined CMap from disk (or cache).
 *
 * @param cMapName CMap name
 * @return The predefined CMap, never null.
 * @throws IOException
 */
public static CMap getPredefinedCMap(String cMapName) throws IOException {
    CMap cmap = cMapCache.get(cMapName);
    if (cmap != null) {
        return cmap;
    }
    CMapParser parser = new CMapParser();
    CMap targetCmap = parser.parsePredefined(cMapName);
    // limit the cache to predefined CMaps
    cMapCache.put(targetCmap.getName(), targetCmap);
    return targetCmap;
}
Also used : CMapParser(com.tom_roush.fontbox.cmap.CMapParser) CMap(com.tom_roush.fontbox.cmap.CMap)

Example 4 with CMap

use of com.tom_roush.fontbox.cmap.CMap in project PdfBox-Android by TomRoush.

the class PDFont method loadUnicodeCmap.

private CMap loadUnicodeCmap() {
    COSBase toUnicode = dict.getDictionaryObject(COSName.TO_UNICODE);
    if (toUnicode == null) {
        return null;
    }
    CMap cmap = null;
    try {
        cmap = readCMap(toUnicode);
        if (cmap != null && !cmap.hasUnicodeMappings()) {
            Log.w("PdfBox-Android", "Invalid ToUnicode CMap in font " + getName());
            String cmapName = cmap.getName() != null ? cmap.getName() : "";
            String ordering = cmap.getOrdering() != null ? cmap.getOrdering() : "";
            COSBase encoding = dict.getDictionaryObject(COSName.ENCODING);
            if (// 
            cmapName.contains("Identity") || // 
            ordering.contains("Identity") || // 
            COSName.IDENTITY_H.equals(encoding) || COSName.IDENTITY_V.equals(encoding)) {
                // assume that if encoding is identity, then the reverse is also true
                cmap = CMapManager.getPredefinedCMap(COSName.IDENTITY_H.getName());
            }
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", "Could not read ToUnicode CMap in font " + getName(), ex);
    }
    return cmap;
}
Also used : CMap(com.tom_roush.fontbox.cmap.CMap) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Aggregations

CMap (com.tom_roush.fontbox.cmap.CMap)4 CMapParser (com.tom_roush.fontbox.cmap.CMapParser)2 COSBase (com.tom_roush.pdfbox.cos.COSBase)1 COSName (com.tom_roush.pdfbox.cos.COSName)1 IOException (java.io.IOException)1