Search in sources :

Example 1 with CMap

use of org.apache.fontbox.cmap.CMap in project pdfbox by apache.

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(org.apache.pdfbox.cos.COSName) CMap(org.apache.fontbox.cmap.CMap)

Example 2 with CMap

use of org.apache.fontbox.cmap.CMap in project pdfbox by apache.

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(org.apache.fontbox.cmap.CMapParser) CMap(org.apache.fontbox.cmap.CMap)

Example 3 with CMap

use of org.apache.fontbox.cmap.CMap in project pdfbox by apache.

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(org.apache.fontbox.cmap.CMapParser) CMap(org.apache.fontbox.cmap.CMap)

Example 4 with CMap

use of org.apache.fontbox.cmap.CMap in project pdfbox by apache.

the class Type0FontValidator method processCMapAsStream.

/**
 * This method checks mandatory fields of the CMap stream. This method also checks if the CMap
 * stream is damaged using the CMapParser of the fontbox api. The standard information of a
 * stream element will be checked by the StreamValidationProcess.
 *
 * @param aCMap the cmap stream.
 */
private void processCMapAsStream(COSStream aCMap) {
    COSBase sysinfo = aCMap.getItem(COSName.CIDSYSTEMINFO);
    checkCIDSystemInfo(sysinfo);
    try (InputStream cmapStream = aCMap.createInputStream()) {
        // extract information from the CMap stream
        CMap fontboxCMap = new CMapParser().parse(cmapStream);
        int wmValue = fontboxCMap.getWMode();
        String cmnValue = fontboxCMap.getName();
        /*
             * According to the getInt javadoc, -1 is returned if there is no result. In the PDF Reference v1.7 p449,
             * we can read that the default value is 0.
             */
        int wmode = aCMap.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_WMODE), FONT_DICTIONARY_DEFAULT_CMAP_WMODE);
        String type = aCMap.getNameAsString(COSName.TYPE);
        String cmapName = aCMap.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_NAME));
        if (cmapName == null || "".equals(cmapName) || wmode > 1) {
            this.fontContainer.push(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, font.getName() + ": Some elements in the CMap dictionary are missing or invalid"));
        } else if (!(wmValue == wmode && cmapName.equals(cmnValue))) {
            this.fontContainer.push(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, font.getName() + ": CMapName or WMode is inconsistent"));
        } else if (!FONT_DICTIONARY_VALUE_TYPE_CMAP.equals(type)) {
            this.fontContainer.push(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, font.getName() + ": The CMap type is invalid"));
        }
    } catch (IOException e) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_CID_CMAP_DAMAGED, font.getName() + ": The CMap type is damaged", e));
    }
    COSDictionary cmapUsed = (COSDictionary) aCMap.getDictionaryObject(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_USECMAP));
    if (cmapUsed != null) {
        checkCMapEncoding(cmapUsed);
    }
    compareCIDSystemInfo(aCMap);
}
Also used : CMapParser(org.apache.fontbox.cmap.CMapParser) COSDictionary(org.apache.pdfbox.cos.COSDictionary) InputStream(java.io.InputStream) CMap(org.apache.fontbox.cmap.CMap) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Aggregations

CMap (org.apache.fontbox.cmap.CMap)4 CMapParser (org.apache.fontbox.cmap.CMapParser)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSName (org.apache.pdfbox.cos.COSName)1 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)1