Search in sources :

Example 66 with ValidationError

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

the class Type3FontValidator method checkFontMatrix.

/**
 * Check that the FontMatrix element has the right format as declared in the PDF reference document.
 */
private void checkFontMatrix() {
    COSBase fontMatrix = fontDictionary.getItem(COSName.FONT_MATRIX);
    if (!COSUtils.isArray(fontMatrix, cosDocument)) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": The FontMatrix element isn't an array"));
        return;
    }
    /*
         * Check the content of the FontMatrix. Should be an array with 6 numbers
         */
    COSArray matrix = COSUtils.getAsArray(fontMatrix, cosDocument);
    if (matrix.size() != 6) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": The FontMatrix element is invalid"));
        return;
    }
    for (int i = 0; i < 6; i++) {
        COSBase elt = matrix.get(i);
        if (!(COSUtils.isFloat(elt, cosDocument) || COSUtils.isInteger(elt, cosDocument))) {
            this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": An element of FontMatrix isn't a number"));
            return;
        }
    }
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 67 with ValidationError

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

the class Type3FontValidator method checkCharProcsAndMetrics.

/**
 * CharProcs is a dictionary where the key is a character name and the value is a Stream which contains the glyph
 * representation of the key.
 *
 * This method checks that all characters codes defined in the Widths Array exist in the CharProcs dictionary. If
 * the CharProcs doesn't know the Character, it is mapped with the .notdef one.
 *
 * For each character, the Glyph width must be the same as the Width value declared in the Widths array.
 */
private void checkCharProcsAndMetrics() throws ValidationException {
    List<Float> widths = getWidths(font);
    if (widths == null || widths.isEmpty()) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": The Witdhs array is unreachable"));
        return;
    }
    COSDictionary charProcs = COSUtils.getAsDictionary(fontDictionary.getItem(COSName.CHAR_PROCS), cosDocument);
    if (charProcs == null) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": The CharProcs element isn't a dictionary"));
        return;
    }
    int fc = font.getCOSObject().getInt(COSName.FIRST_CHAR, -1);
    int lc = font.getCOSObject().getInt(COSName.LAST_CHAR, -1);
    /*
         * wArr length = (lc - fc) + 1 and it is an array of int. 
         * If FirstChar is greater than LastChar, the validation
         * will fail because of the array will have an expected size &lt;= 0.
         */
    int expectedLength = (lc - fc) + 1;
    if (widths.size() != expectedLength) {
        this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": The length of Witdhs array is invalid. Expected : \"" + expectedLength + "\" Current : \"" + widths.size() + "\""));
        return;
    }
    // Check width consistency
    for (int i = 0; i < expectedLength; i++) {
        int code = fc + i;
        float width = widths.get(i);
        PDType3CharProc charProc = getCharProc(code);
        if (charProc != null) {
            try {
                float fontProgramWidth = getWidthFromCharProc(charProc);
                if (Math.abs(width - fontProgramWidth) < 0.001f) {
                    // Glyph is OK, we keep the CID.
                    // PDF/A-1b only states that the width "shall be consistent".
                    // For PDF/A-2,3 the description has been enhanced and is now requesting
                    // "consistent is defined to be a difference of no more than 1/1000 unit"
                    // We interpret this as clarification of the PDF/A-1b requirement.
                    this.fontContainer.markAsValid(code);
                } else {
                    GlyphException glyphEx = new GlyphException(ERROR_FONTS_METRICS, code, font.getName() + ": The character with CID " + code + " should have a width equals to " + width + ", but has " + fontProgramWidth);
                    this.fontContainer.markAsInvalid(code, glyphEx);
                }
            } catch (ContentStreamException e) {
                // TODO spaces/isartor-6-2-3-3-t02-fail-h.pdf --> si ajout de l'erreur dans le container le test
                // echoue... pourquoi si la font est utilisée ca devrait planter???
                this.context.addValidationError(new ValidationError(e.getErrorCode(), e.getMessage(), e));
                return;
            } catch (IOException e) {
                this.fontContainer.push(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, font.getName() + ": The CharProcs references an element which can't be read", e));
                return;
            }
        }
    }
}
Also used : PDType3CharProc(org.apache.pdfbox.pdmodel.font.PDType3CharProc) ContentStreamException(org.apache.pdfbox.preflight.content.ContentStreamException) COSDictionary(org.apache.pdfbox.cos.COSDictionary) GlyphException(org.apache.pdfbox.preflight.font.util.GlyphException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Example 68 with ValidationError

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

the class Type3FontValidator method checkEncoding.

/**
 * For a Type3 font, the mapping between the Character Code and the
 * Character name is entirely defined in the /Encoding entry. The /Encoding
 * Entry can be a Name (For the 5 predefined encodings) or a dictionary. If
 * it is a dictionary, the /Differences array contains the correspondence
 * between a character code and a set of character name which are different
 * from the encoding entry of the dictionary.
 *
 * This method checks that the encoding is :
 * <UL>
 * <li>An existing encoding name.
 * <li>A dictionary with an existing encoding name (the name is optional)
 * and a well formed "Differences" array (the array is optional)
 * </UL>
 */
@Override
protected void checkEncoding() {
    COSBase fontEncoding = fontDictionary.getItem(COSName.ENCODING);
    if (COSUtils.isString(fontEncoding, cosDocument)) {
        String enc = COSUtils.getAsString(fontEncoding, cosDocument);
        checkEncodingAsString(enc);
    } else if (COSUtils.isDictionary(fontEncoding, cosDocument)) {
        COSDictionary encodingDictionary = COSUtils.getAsDictionary(fontEncoding, cosDocument);
        checkEncodingAsDictionary(encodingDictionary);
    } else {
        // the encoding entry is invalid
        this.fontContainer.push(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, font.getName() + ": The Encoding entry doesn't have the right type"));
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 69 with ValidationError

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

the class Type3FontValidator method checkResources.

/**
 * If the Resources entry is present, this method checks its content. Only fonts and images are checked because this
 * resource describes glyphs.
 */
private void checkResources() throws ValidationException {
    COSBase resources = this.fontDictionary.getItem(COSName.RESOURCES);
    if (resources != null) {
        COSDictionary dictionary = COSUtils.getAsDictionary(resources, cosDocument);
        if (dictionary == null) {
            this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": The Resources element isn't a dictionary"));
            return;
        }
        // process Fonts and XObjects
        ContextHelper.validateElement(context, new PDResources(dictionary), RESOURCES_PROCESS);
        COSBase cbFont = dictionary.getItem(COSName.FONT);
        if (cbFont != null) {
            /*
                 * Check that all referenced object are present in the PDF file
                 */
            COSDictionary dicFonts = COSUtils.getAsDictionary(cbFont, cosDocument);
            Set<COSName> keyList = dicFonts.keySet();
            for (Object key : keyList) {
                COSBase item = dicFonts.getItem((COSName) key);
                COSDictionary xObjFont = COSUtils.getAsDictionary(item, cosDocument);
                try {
                    PDFont aFont = PDFontFactory.createFont(xObjFont);
                    FontContainer<?> aContainer = this.context.getFontContainer(aFont.getCOSObject());
                    // another font is used in the Type3, check if the font is valid.
                    if (!aContainer.isValid()) {
                        this.fontContainer.push(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, font.getName() + ": The Resources dictionary of type 3 font contains invalid font"));
                    }
                } catch (IOException e) {
                    context.addValidationError(new ValidationError(PreflightConstants.ERROR_FONTS_DAMAGED, font.getName() + ": Unable to valid the Type3 : " + e.getMessage(), e));
                }
            }
        }
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSName(org.apache.pdfbox.cos.COSName) COSBase(org.apache.pdfbox.cos.COSBase) PDResources(org.apache.pdfbox.pdmodel.PDResources) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Example 70 with ValidationError

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

the class PreflightStreamEngine method registerError.

public void registerError(String msg, String errorCode, boolean warning, Throwable cause) {
    ValidationError error = new ValidationError(errorCode, msg, cause);
    error.setWarning(warning);
    this.context.addValidationError(error);
}
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