Search in sources :

Example 1 with ContentStreamException

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

Aggregations

IOException (java.io.IOException)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 PDType3CharProc (org.apache.pdfbox.pdmodel.font.PDType3CharProc)1 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)1 ContentStreamException (org.apache.pdfbox.preflight.content.ContentStreamException)1 GlyphException (org.apache.pdfbox.preflight.font.util.GlyphException)1