Search in sources :

Example 61 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class SimpleFontValidator method checkMandatoryField.

protected void checkMandatoryField() {
    String missingFields = "";
    boolean areFieldsPresent = fontDictionary.containsKey(COSName.TYPE);
    if (!areFieldsPresent) {
        missingFields = "type, ";
    }
    boolean subType = fontDictionary.containsKey(COSName.SUBTYPE);
    areFieldsPresent &= subType;
    if (!subType) {
        missingFields += "subType, ";
    }
    boolean baseFont = fontDictionary.containsKey(COSName.BASE_FONT);
    areFieldsPresent &= baseFont;
    if (!baseFont) {
        missingFields += "baseFont, ";
    }
    boolean firstChar = fontDictionary.containsKey(COSName.FIRST_CHAR);
    areFieldsPresent &= firstChar;
    if (!firstChar) {
        missingFields += "firstChar, ";
    }
    boolean lastChar = fontDictionary.containsKey(COSName.LAST_CHAR);
    areFieldsPresent &= lastChar;
    if (!lastChar) {
        missingFields += "lastChar, ";
    }
    boolean widths = fontDictionary.containsKey(COSName.WIDTHS);
    areFieldsPresent &= widths;
    if (!widths) {
        missingFields += "widths, ";
    }
    if (!areFieldsPresent) {
        if (missingFields.endsWith(", ")) {
            missingFields = missingFields.substring(0, missingFields.length() - 2);
        }
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, this.font.getName() + ": some required fields are missing from the Font dictionary: " + missingFields + "."));
    }
}
Also used : ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 62 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class TrueTypeFontValidator method checkEncoding.

@Override
protected void checkEncoding() {
    PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
    PDFontDescriptor fd = ttFont.getFontDescriptor();
    if (fd != null) {
        /*
             * only MacRomanEncoding or WinAnsiEncoding are allowed for a non symbolic font.
             */
        if (fd.isNonSymbolic()) {
            Encoding encodingValue = ttFont.getEncoding();
            if (!(encodingValue instanceof MacRomanEncoding || encodingValue instanceof WinAnsiEncoding)) {
                this.fontContainer.push(new ValidationError(ERROR_FONTS_ENCODING, fd.getFontName() + ": The Encoding is invalid for the NonSymbolic TTF"));
            }
        }
        /*
             * For symbolic font, no encoding entry is allowed and only one encoding entry is expected into the FontFile
             * CMap (Check latter when the FontFile stream will be checked)
             */
        if (fd.isSymbolic() && fontDictionary.getItem(COSName.ENCODING) != null) {
            this.fontContainer.push(new ValidationError(ERROR_FONTS_ENCODING, fd.getFontName() + ": The Encoding should be missing for the Symbolic TTF"));
        }
    }
}
Also used : PDTrueTypeFont(org.apache.pdfbox.pdmodel.font.PDTrueTypeFont) Encoding(org.apache.pdfbox.pdmodel.font.encoding.Encoding) MacRomanEncoding(org.apache.pdfbox.pdmodel.font.encoding.MacRomanEncoding) WinAnsiEncoding(org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding) MacRomanEncoding(org.apache.pdfbox.pdmodel.font.encoding.MacRomanEncoding) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) WinAnsiEncoding(org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding) PDFontDescriptor(org.apache.pdfbox.pdmodel.font.PDFontDescriptor)

Example 63 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class Type0FontValidator method checkMandatoryFields.

/**
 * This methods extracts from the Font dictionary all mandatory fields. If a mandatory field is missing, the list of
 * ValidationError in the FontContainer is updated.
 */
protected void checkMandatoryFields() {
    COSDictionary fontDictionary = font.getCOSObject();
    boolean areFieldsPResent = fontDictionary.containsKey(COSName.TYPE);
    areFieldsPResent &= fontDictionary.containsKey(COSName.SUBTYPE);
    areFieldsPResent &= fontDictionary.containsKey(COSName.BASE_FONT);
    areFieldsPResent &= fontDictionary.containsKey(COSName.DESCENDANT_FONTS);
    areFieldsPResent &= fontDictionary.containsKey(COSName.ENCODING);
    if (!areFieldsPResent) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": Some keys are missing from composite font dictionary"));
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 64 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError 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)

Example 65 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class Type3FontValidator method checkMandatoryField.

protected void checkMandatoryField() {
    boolean areFieldsPResent = fontDictionary.containsKey(COSName.FONT_BBOX);
    areFieldsPResent &= fontDictionary.containsKey(COSName.FONT_MATRIX);
    areFieldsPResent &= fontDictionary.containsKey(COSName.CHAR_PROCS);
    areFieldsPResent &= fontDictionary.containsKey(COSName.ENCODING);
    areFieldsPResent &= fontDictionary.containsKey(COSName.FIRST_CHAR);
    areFieldsPResent &= fontDictionary.containsKey(COSName.LAST_CHAR);
    areFieldsPResent &= fontDictionary.containsKey(COSName.WIDTHS);
    if (!areFieldsPResent) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": Some required fields are missing from the Font dictionary."));
    }
}
Also used : ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Aggregations

ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)108 COSBase (org.apache.pdfbox.cos.COSBase)36 COSDictionary (org.apache.pdfbox.cos.COSDictionary)28 IOException (java.io.IOException)27 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSObject (org.apache.pdfbox.cos.COSObject)13 COSArray (org.apache.pdfbox.cos.COSArray)10 COSStream (org.apache.pdfbox.cos.COSStream)10 COSString (org.apache.pdfbox.cos.COSString)8 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)8 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)8 COSName (org.apache.pdfbox.cos.COSName)7 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)7 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)5 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)5 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)4