Example usage for org.apache.pdfbox.cos COSName getPDFName

List of usage examples for org.apache.pdfbox.cos COSName getPDFName

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName getPDFName.

Prototype

public static COSName getPDFName(String aName) 

Source Link

Document

This will get a COSName object with that name.

Usage

From source file:net.padaf.preflight.font.SimpleFontValidator.java

License:Apache License

/**
 * Check if All required fields of a Font Dictionary are present. If there are
 * some missing fields, this method returns false and the FontContainer is
 * updated./* w  w  w  .j  a v a  2s  .c  om*/
 * 
 * @return
 */
protected boolean checkMandatoryFields() {
    String type = fDictionary.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String subtype = fDictionary.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    if (this.fDescriptor == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontDescriptor is missing, so the Font Program isn't embedded."));
        this.fontContainer.setFontProgramEmbedded(false);
        return false;
    }

    if ((type == null || "".equals(type)) || (subtype == null || "".equals(subtype))) {
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "Type and/or Subtype keys are missing"));
        return false;
    } else {
        extractElementsToCheck();
        // ---- according to the subtype, the validation process isn't the same.
        return checkSpecificMandatoryFields();
    }
}

From source file:net.padaf.preflight.font.TrueTypeFontValidator.java

License:Apache License

/**
 * This methods validates the Font Stream. If the font is damaged or missing
 * the FontContainer is updated and false is returned. Moreover, this method
 * checks the Encoding property of the FontDescriptor dictionary.
 * /*from  w  ww.  j  a  v  a  2 s . co m*/
 * @return
 */
protected boolean checkFontFileElement() throws ValidationException {
    PDStream ff1 = pFontDesc.getFontFile();
    PDStream ff2 = pFontDesc.getFontFile2();
    PDStream ff3 = pFontDesc.getFontFile3();
    boolean onlyOne = (ff1 != null && ff2 == null && ff3 == null) || (ff1 == null && ff2 != null && ff3 == null)
            || (ff1 == null && ff2 == null && ff3 != null);

    if (ff2 == null || !onlyOne) {
        this.fontContainer.addError(new ValidationResult.ValidationError(
                ValidationConstants.ERROR_FONTS_FONT_FILEX_INVALID, "The FontFile2 is invalid"));
        return false;
    }

    // ---- Stream validation should be done by the StreamValidateHelper.
    // ---- Process font specific check
    COSStream stream = ff2.getStream();
    if (stream == null) {
        this.fontContainer.addError(new ValidationResult.ValidationError(
                ValidationConstants.ERROR_FONTS_FONT_FILEX_INVALID, "The FontFile is missing"));
        this.fontContainer.setFontProgramEmbedded(false);
        return false;
    }

    boolean hasLength1 = stream.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH1)) > 0;
    if (!hasLength1) {
        this.fontContainer.addError(new ValidationResult.ValidationError(
                ValidationConstants.ERROR_FONTS_FONT_FILEX_INVALID, "The FontFile is invalid"));
        return false;
    }

    // ---- check the encoding part.
    if (pFontDesc.isNonSymbolic()) {
        // ---- only MacRomanEncoding or WinAnsiEncoding are allowed for a non
        // symbolic font
        Encoding encodingValue = this.pFont.getFontEncoding();
        if (encodingValue == null
                || !(encodingValue instanceof MacRomanEncoding || encodingValue instanceof WinAnsiEncoding)) {
            this.fontContainer
                    .addError(new ValidationResult.ValidationError(ValidationConstants.ERROR_FONTS_ENCODING,
                            "The Encoding is invalid for the NonSymbolic TTF"));
            return false;
        }
    } else if (pFontDesc.isSymbolic()) {
        // ---- For symbolic font, no encoding entry is allowed and only one
        // encoding entry is expected into the FontFile CMap
        if (((COSDictionary) this.fDictionary.getCOSObject())
                .getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_ENCODING)) != null) {
            this.fontContainer
                    .addError(new ValidationResult.ValidationError(ValidationConstants.ERROR_FONTS_ENCODING,
                            "The Encoding should be missing for the Symbolic TTF"));
            return false;
        } // else check the content of the Font CMap (see below)

    } else {
        // ----- should never happen
        return true;
    }

    /*
     * ---- try to load the font using the TTFParser object. If the font is
     * invalid, an exception will be thrown. Because of it is a Embedded Font
     * Program, some tables are required and other are optional see PDF
     * Reference (5.8)
     */
    ByteArrayInputStream bis = null;
    try {
        bis = new ByteArrayInputStream(ff2.getByteArray());
        TrueTypeFont ttf = new TTFParser(true).parseTTF(bis);
        if (pFontDesc.isSymbolic() && ttf.getCMAP().getCmaps().length != 1) {
            this.fontContainer
                    .addError(new ValidationResult.ValidationError(ValidationConstants.ERROR_FONTS_ENCODING,
                            "The Encoding should be missing for the Symbolic TTF"));
            return false;
        }

        return checkFontMetrics(ttf) && checkFontFileMetaData(pFontDesc, ff2);
    } catch (IOException e) {
        this.fontContainer.addError(new ValidationResult.ValidationError(
                ValidationConstants.ERROR_FONTS_TRUETYPE_DAMAGED, "The FontFile can't be read"));
        return false;
    } finally {
        if (bis != null) {
            IOUtils.closeQuietly(bis);
        }
    }
}

From source file:net.padaf.preflight.font.Type1FontValidator.java

License:Apache License

/**
 * This methods validates the Font Stream, if the font program is damaged or
 * missing the FontContainer is updated and false is returned.
 * //from   www  .j  a  v a2 s  . c  o  m
 * @throws ValidationException
 */
protected boolean checkFontFileElement() throws ValidationException {
    // ---- if the this font is a Subset, the CharSet entry must be present in
    // the FontDescriptor
    if (isSubSet(pFontDesc.getFontName())) {
        String charsetStr = pFontDesc.getCharSet();
        if (charsetStr == null || "".equals(charsetStr)) {
            this.fontContainer
                    .addError(new ValidationResult.ValidationError(ERROR_FONTS_CHARSET_MISSING_FOR_SUBSET,
                            "The Charset entry is missing for the Type1 Subset"));
            return false;
        }
    }

    // ---- FontFile Validation
    PDStream ff1 = pFontDesc.getFontFile();
    PDStream ff2 = pFontDesc.getFontFile2();
    PDStream ff3 = pFontDesc.getFontFile3();
    boolean onlyOne = (ff1 != null && ff2 == null && ff3 == null) || (ff1 == null && ff2 != null && ff3 == null)
            || (ff1 == null && ff2 == null && ff3 != null);

    if ((ff1 == null && (ff3 == null
            || !"Type1C".equals(((COSDictionary) ff3.getCOSObject()).getNameAsString(COSName.SUBTYPE))))
            || !onlyOne) {
        this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontFile is invalid"));
        return false;
    }

    if (ff1 != null) {
        COSStream stream = ff1.getStream();
        if (stream == null) {
            this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                    "The FontFile is missing"));
            this.fontContainer.setFontProgramEmbedded(false);
            return false;
        }

        boolean hasLength1 = stream.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH1)) > 0;
        boolean hasLength2 = stream.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH2)) > 0;
        boolean hasLength3 = stream.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH3)) > 0;
        if (!(hasLength1 && hasLength2 && hasLength3)) {
            this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                    "The FontFile is invalid"));
            return false;
        }

        // ---- Stream validation should be done by the StreamValidateHelper.
        // ---- Process font specific check
        // ---- try to load the font using the java.awt.font object.
        // ---- if the font is invalid, an exception will be thrown
        ByteArrayInputStream bis = null;
        try {
            bis = new ByteArrayInputStream(ff1.getByteArray());
            Font.createFont(Font.TYPE1_FONT, bis);
            return checkFontMetricsDataAndFeedFontContainer(ff1) && checkFontFileMetaData(pFontDesc, ff1);
        } catch (IOException e) {
            this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_TYPE1_DAMAGED,
                    "The FontFile can't be read"));
            return false;
        } catch (FontFormatException e) {
            this.fontContainer.addError(
                    new ValidationResult.ValidationError(ERROR_FONTS_TYPE1_DAMAGED, "The FontFile is damaged"));
            return false;
        } finally {
            if (bis != null) {
                IOUtils.closeQuietly(bis);
            }
        }
    } else {
        return checkCIDFontWidths(ff3) && checkFontFileMetaData(pFontDesc, ff3);
    }
}

From source file:net.padaf.preflight.font.Type1FontValidator.java

License:Apache License

/**
 * This method checks the metric consistency and adds the FontContainer in the
 * DocumentHandler./*from  w  w w.j  a v  a2  s .  c  om*/
 * 
 * @param fontStream
 * @return
 * @throws ValidationException
 */
protected boolean checkFontMetricsDataAndFeedFontContainer(PDStream fontStream) throws ValidationException {
    try {

        // ---- Parse the Type1 Font program
        ByteArrayInputStream bis = new ByteArrayInputStream(fontStream.getByteArray());
        COSStream streamObj = fontStream.getStream();
        int length1 = streamObj.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH1));
        int length2 = streamObj.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH2));

        Type1Parser parserForMetrics = Type1Parser.createParserWithEncodingObject(bis, length1, length2,
                pFont.getFontEncoding());
        Type1 parsedData = parserForMetrics.parse();

        ((Type1FontContainer) this.fontContainer).setFontObject(parsedData);

        List<?> pdfWidths = this.pFont.getWidths();
        int firstChar = pFont.getFirstChar();
        float defaultGlyphWidth = pFontDesc.getMissingWidth();

        COSArray widths = null;
        if (pdfWidths instanceof COSArrayList) {
            widths = ((COSArrayList) pdfWidths).toList();
        } else {
            widths = ((COSArray) pdfWidths);
        }

        ((Type1FontContainer) this.fontContainer).setWidthsArray(widths.toList());
        ((Type1FontContainer) this.fontContainer).setFirstCharInWidthsArray(firstChar);
        ((Type1FontContainer) this.fontContainer).setDefaultGlyphWidth(defaultGlyphWidth);

        return true;
    } catch (IOException e) {
        throw new ValidationException("Unable to check Type1 metrics due to : " + e.getMessage(), e);
    }
}

From source file:net.padaf.preflight.font.Type1MetricHelper.java

License:Apache License

/**
 * Returns an int value which represent the token.
 * /*from w  ww  .j a v a2s  .c  o  m*/
 * @param token
 * @return -1 if the token must be ignored
 * @throws IOException
 */
protected int tokenIdentifier(byte[] token) throws IOException {

    String tokenAsStr = new String(token, "US-ASCII");
    if ("/FamilyName".equals(tokenAsStr)) {
        return FAMILY_NAME_TOKEN;
    }

    if ("/FullName".equals(tokenAsStr)) {
        return FULL_NAME_TOKEN;
    }

    if ("/FontName".equals(tokenAsStr)) {
        return FONT_NAME_TOKEN;
    }

    if ("dup".equals(tokenAsStr)) {
        return DUP_TOKEN;
    }

    if ("/Encoding".equals(tokenAsStr)) {
        return ENCODING_TOKEN;
    }

    if ("readonly".equals(tokenAsStr)) {
        return READONLY_TOKEN;
    }

    if ("/lenIV".equals(tokenAsStr)) {
        return LEN_IV_TOKEN;
    }

    if ("/CharStrings".equals(tokenAsStr)) {
        return CHARSTRINGS_TOKEN;
    }

    if (labelToCid.containsKey(tokenAsStr)
            || this.encoding.getNameToCodeMap().containsKey(COSName.getPDFName(tokenAsStr.replace("/", "")))) {
        return CHAR_LABEL_TOKEN;
    }

    String regex = "/[^\\s\\(\\)\\[\\]\\{\\}/<>%]+";
    if (tokenAsStr.matches(regex)) {
        return OBJ_NAME_TOKEN;
    }

    return -1;
}

From source file:net.padaf.preflight.font.Type3FontValidator.java

License:Apache License

/**
 * This methods stores in attributes all required element. We extract these
 * elements because of the PDType3Font object returns sometime default value
 * if the field is missing, so to avoid mistake during required field
 * validation we store them.//from w  w  w . j  a v  a2 s. c o m
 */
private void extractFontDictionaryEntries() {
    // ---- required elements
    this.fontBBox = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_FONTBBOX));
    this.fontMatrix = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_FONTMATRIX));
    this.charProcs = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CHARPROCS));
    this.fontEncoding = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_ENCODING));
    this.firstChar = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_FIRSTCHAR));
    this.lastChar = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_LASTCHAR));
    this.widths = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_WIDTHS));

    // ---- Optional elements
    this.toUnicode = this.fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_TOUNICODE));
    this.resources = this.fDictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_RESOURCES));
}

From source file:net.padaf.preflight.font.Type3FontValidator.java

License:Apache License

/**
 * 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 Encoding) 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.//from   w  ww.jav a  2s . com
 * 
 * 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>
 * 
 * @return
 */
private boolean checkEncoding() {
    COSDocument cDoc = this.handler.getDocument().getDocument();

    EncodingManager emng = new EncodingManager();
    if (COSUtils.isString(this.fontEncoding, cDoc)) {
        // ---- Encoding is a Name, check if it is an Existing Encoding
        String enc = COSUtils.getAsString(this.fontEncoding, cDoc);
        try {
            type3Encoding = emng.getEncoding(COSName.getPDFName(enc));
        } catch (IOException e) {
            // ---- the encoding doesn't exist
            this.fontContainer.addError(new ValidationError(ERROR_FONTS_ENCODING));
            return false;
        }
    } else if (COSUtils.isDictionary(this.fontEncoding, cDoc)) {
        COSDictionary encodingDictionary = COSUtils.getAsDictionary(this.fontEncoding, cDoc);
        try {
            type3Encoding = new DictionaryEncoding(encodingDictionary);
        } catch (IOException e) {
            // ---- the encoding doesn't exist
            this.fontContainer.addError(new ValidationError(ERROR_FONTS_ENCODING));
            return false;
        }

        COSBase diff = encodingDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_DIFFERENCES));
        if (diff != null) {
            if (!COSUtils.isArray(diff, cDoc)) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                        "The differences element of the encoding dictionary isn't an array"));
                return false;
            }

            // ---- The DictionaryEncoding object doesn't throw exception if the
            // Differences isn't well formed.
            // So check if the array has the right format.
            COSArray differences = COSUtils.getAsArray(diff, cDoc);
            for (int i = 0; i < differences.size(); ++i) {
                COSBase item = differences.get(i);
                if (!(item instanceof COSInteger || item instanceof COSName)) {
                    // ---- Error, the Differences array is invalid
                    this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                            "Differences Array should contain COSInt or COSName, no other type"));
                    return false;
                }
            }
        }
    } else {
        // ---- the encoding entry is invalid
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                "The Encoding entry doesn't have the right type"));
        return false;
    }

    return true;
}

From source file:net.padaf.preflight.font.Type3FontValidator.java

License:Apache License

/**
 * 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.
 * /*  w  ww .j  a v  a  2  s .c om*/
 * This method checks that all character code 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.
 * 
 * @param errors
 * @return
 */
private boolean checkCharProcsAndMetrics() throws ValidationException {
    COSDocument cDoc = this.handler.getDocument().getDocument();

    // ---- the Widths value can be a reference to an object
    // ---- Access the object using the COSkey
    COSArray wArr = COSUtils.getAsArray(this.widths, cDoc);
    if (wArr == null) {
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Witdhs array is unreachable"));
        return false;
    }

    COSDictionary charProcsDictionary = COSUtils.getAsDictionary(this.charProcs, cDoc);
    if (charProcsDictionary == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                "The CharProcs element isn't a dictionary"));
        return false;
    }

    // ---- firstChar and lastChar must be integer.
    int fc = ((COSInteger) this.firstChar).intValue();
    int lc = ((COSInteger) this.lastChar).intValue();

    // ---- 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 <= 0.
    int expectedLength = (lc - fc) + 1;
    if (wArr.size() != expectedLength) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                "The length of Witdhs array is invalid. Expected : \"" + expectedLength + "\" Current : \""
                        + wArr.size() + "\""));
        return false;
    }

    // ---- Check width consistency
    for (int i = 0; i < expectedLength; i++) {
        int cid = fc + i;
        COSBase arrContent = wArr.get(i);
        if (COSUtils.isNumeric(arrContent, cDoc)) {
            float width = COSUtils.getAsFloat(arrContent, cDoc);

            String charName = null;
            try {
                charName = this.type3Encoding.getName(cid);
            } catch (IOException e) {
                // shouldn't occur
                throw new ValidationException("Unable to check Widths consistency", e);
            }

            COSBase item = charProcsDictionary.getItem(COSName.getPDFName(charName));
            COSStream charStream = COSUtils.getAsStream(item, cDoc);
            if (charStream == null && width != 0) {
                GlyphException glyphEx = new GlyphException(ERROR_FONTS_METRICS, cid,
                        "The CharProcs \"" + charName + "\" doesn't exist but the width is " + width);
                GlyphDetail glyphDetail = new GlyphDetail(cid, glyphEx);
                this.fontContainer.addKnownCidElement(glyphDetail);
            } else {
                try {
                    // --- Parse the Glyph description to obtain the Width
                    PDFAType3StreamParser parser = new PDFAType3StreamParser(this.handler);
                    PDResources pRes = null;
                    if (this.resources != null) {
                        COSDictionary resAsDict = COSUtils.getAsDictionary(this.resources, cDoc);
                        if (resAsDict != null) {
                            pRes = new PDResources(resAsDict);
                        }
                    }
                    parser.resetEngine();
                    parser.processSubStream(null, pRes, charStream);

                    if (width != parser.getWidth()) {
                        GlyphException glyphEx = new GlyphException(ERROR_FONTS_METRICS, cid,
                                "The CharProcs \"" + charName + "\" should have a width equals to " + width);
                        GlyphDetail glyphDetail = new GlyphDetail(cid, glyphEx);
                        this.fontContainer.addKnownCidElement(glyphDetail);
                    } else {
                        // Glyph is OK, we keep the CID.
                        GlyphDetail glyphDetail = new GlyphDetail(cid);
                        this.fontContainer.addKnownCidElement(glyphDetail);
                    }
                } catch (ContentStreamException e) {
                    this.fontContainer.addError(new ValidationError(e.getValidationError()));
                    return false;
                } catch (IOException e) {
                    this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                            "The CharProcs references an element which can't be read"));
                    return false;
                }
            }

        } else {
            this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                    "The Witdhs array is invalid. (some element aren't integer)"));
            return false;
        }
    }
    return true;
}

From source file:net.padaf.preflight.font.Type3FontValidator.java

License:Apache License

/**
 * If the Resources entry is present, this method check its content. Only
 * fonts and Images are checked because this resource describes glyphs. REMARK
 * : The font and the image aren't validated because they will be validated by
 * an other ValidationHelper.//ww w  . ja va 2s  .c  om
 * 
 * @return
 */
private boolean checkResources() throws ValidationException {
    if (this.resources == null) {
        // ---- No resources dictionary.
        return true;
    }

    COSDocument cDoc = this.handler.getDocument().getDocument();
    COSDictionary dictionary = COSUtils.getAsDictionary(this.resources, cDoc);
    if (dictionary == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                "The Resources element isn't a dictionary"));
        return false;
    }

    COSBase cbImg = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_XOBJECT));
    COSBase cbFont = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_FONT));

    if (cbImg == null && cbFont == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                "The Resources element doesn't have Glyph information"));
        return false;
    }

    if (cbImg != null) {
        // ---- the referenced objects must be present in the PDF file
        COSDictionary dicImgs = COSUtils.getAsDictionary(cbImg, cDoc);
        Set<COSName> keyList = dicImgs.keySet();
        for (Object key : keyList) {

            COSBase item = dictionary.getItem((COSName) key);
            COSDictionary xObjImg = COSUtils.getAsDictionary(item, cDoc);
            if (xObjImg == null) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            if (!XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG
                    .equals(xObjImg.getString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE)))) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }
        }
    }

    if (cbFont != null) {
        // ---- the referenced object must be present in the PDF file
        COSDictionary dicFonts = COSUtils.getAsDictionary(cbFont, cDoc);
        Set<COSName> keyList = dicFonts.keySet();
        for (Object key : keyList) {

            COSBase item = dictionary.getItem((COSName) key);
            COSDictionary xObjFont = COSUtils.getAsDictionary(item, cDoc);
            if (xObjFont == null) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            if (!FONT_DICTIONARY_VALUE_FONT
                    .equals(xObjFont.getString(COSName.getPDFName(DICTIONARY_KEY_TYPE)))) {
                this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID,
                        "The Resources dictionary of type 3 font is invalid"));
                return false;
            }

            try {
                PDFont aFont = PDFontFactory.createFont(xObjFont);
                // FontContainer aContainer = this.handler.retrieveFontContainer(aFont);
                AbstractFontContainer aContainer = this.handler.getFont(aFont.getCOSObject());
                // ---- another font is used in the Type3, check if the font is valid.
                if (aContainer.isValid() != State.VALID) {
                    this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                            "The Resources dictionary of type 3 font contains invalid font"));
                    return false;
                }
            } catch (IOException e) {
                throw new ValidationException("Unable to valid the Type3 : " + e.getMessage());
            }
        }
    }

    List<ValidationError> errors = new ArrayList<ValidationError>();
    ExtGStateContainer extGStates = new ExtGStateContainer(dictionary, cDoc);
    boolean res = extGStates.validateTransparencyRules(errors);
    for (ValidationError err : errors) {
        this.fontContainer.addError(err);
    }

    return res && validateShadingPattern(dictionary, errors);
}

From source file:net.padaf.preflight.graphics.AbstractXObjValidator.java

License:Apache License

/**
 * This method checks the SMask entry in the XObject dictionary. According to
 * the PDF Reference, a SMask in a XObject is a Stream. So if it is not null,
 * it should be an error but a SMask with the name None is authorized in the
 * PDF/A Specification 6.4. If the validation fails (SMask not null and
 * different from None), the error list is updated with the error code
 * ERROR_GRAPHIC_TRANSPARENCY_SMASK (2.2.2).
 * //w w  w.jav a 2s. co  m
 * @param error
 *          the list of error to update if the validation fails
 * @return true if the SMask is valid, false otherwise.
 */
protected boolean checkSMask(List<ValidationError> error) {
    COSBase smask = xobject.getItem(COSName.getPDFName(TRANSPARENCY_DICTIONARY_KEY_SOFT_MASK));
    // 
    if (smask != null && !(COSUtils.isString(smask, cosDocument)
            && TRANSPARENCY_DICTIONARY_VALUE_SOFT_MASK_NONE.equals(COSUtils.getAsString(smask, cosDocument)))) {
        error.add(new ValidationError(ERROR_GRAPHIC_TRANSPARENCY_SMASK, "Soft Mask must be null or None"));
        return false;
    }

    return true;
}