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.CompositeFontValidator.java

License:Apache License

/**
 * Check the content of the CIDSystemInfo dictionary. A CIDSystemInfo
 * dictionary must contain :/*from   w  w w.  java2s. c om*/
 * <UL>
 * <li>a Name - Registry
 * <li>a Name - Ordering
 * <li>a Integer - Supplement
 * </UL>
 * 
 * @param sysinfo
 * @param cDoc
 * @return
 */
private boolean checkCIDSystemInfo(COSBase sysinfo, COSDocument cDoc) {
    COSDictionary cidSysInfo = COSUtils.getAsDictionary(sysinfo, cDoc);
    if (cidSysInfo == null) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_SYSINFO));
        return false;
    }

    COSBase reg = cidSysInfo.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_REGISTRY));
    COSBase ord = cidSysInfo.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_ORDERING));
    COSBase sup = cidSysInfo.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_SUPPLEMENT));

    if (!(COSUtils.isString(reg, cDoc) && COSUtils.isString(ord, cDoc) && COSUtils.isInteger(sup, cDoc))) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_SYSINFO));
        return false;
    }

    return true;
}

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

License:Apache License

/**
 * Standard information of a stream element will be checked by the
 * StreamHelper.//from  w  ww  .j ava2s.co m
 * 
 * This method checks mandatory fields of the CMap stream. This method checks
 * too if the CMap stream is damaged using the CMapParser of the fontbox api.
 * 
 * @param aCMap
 * @return
 */
private boolean processCMapAsStream(COSStream aCMap) {
    COSDocument cDoc = handler.getDocument().getDocument();

    String type = aCMap.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String cmapName = aCMap.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_NAME));
    COSBase sysinfo = aCMap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_SYSINFO));
    int wmode = aCMap.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_WMODE));
    COSBase cmapUsed = aCMap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CMAP_USECMAP));

    if (!FONT_DICTIONARY_VALUE_TYPE_CMAP.equals(type)) {
        // ---- CMap type is invalid
        this.fontContainer.addError(
                new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, "The CMap type is invalid"));
        return false;
    }

    // ---- check the content of the CIDSystemInfo
    if (!checkCIDSystemInfo(sysinfo, cDoc)) {
        return false;
    }

    if (cmapName == null || "".equals(cmapName) || wmode > 1) {
        this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING,
                "Some elements in the CMap dictionary are missing or invalid"));
        return false;
    }

    try {

        CMap fontboxCMap = new CMapParser().parse(null, aCMap.getUnfilteredStream());
        int wmValue = fontboxCMap.getWMode();
        String cmnValue = fontboxCMap.getName(); //getCmapEntry("CMapName");

        if (wmValue != wmode) {

            this.fontContainer.addError(
                    new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING, "WMode is inconsistent"));
            return false;
        }

        if (!cmnValue.equals(cmapName)) {

            this.fontContainer.addError(new ValidationError(ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING,
                    "CMapName is inconsistent"));
            return false;
        }

    } catch (IOException e) {
        this.fontContainer
                .addError(new ValidationError(ERROR_FONTS_CID_CMAP_DAMAGED, "The CMap type is damaged"));
        return false;
    }

    if (cmapUsed != null) {
        return checkCMap(cmapUsed);
    }

    return true;
}

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

License:Apache License

/**
 * The CIDSystemInfo must have the same Registry and Ordering for CMap and
 * CIDFont. This control is useless if CMap is Identity-H or Identity-V so
 * this method is called by the checkCMap method.
 * //from  w w w.j  a  va 2 s.  c  o m
 * @param errors
 * @return
 */
private boolean compareCIDSystemInfo() {
    COSDocument cDoc = handler.getDocument().getDocument();
    if (!isIdentityCMap) {
        COSDictionary cmsi = COSUtils
                .getAsDictionary(this.cmap.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_SYSINFO)), cDoc);
        COSDictionary cfsi = COSUtils.getAsDictionary(
                this.cidFont.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CID_SYSINFO)), cDoc);

        String regCM = COSUtils
                .getAsString(cmsi.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_REGISTRY)), cDoc);
        String ordCM = COSUtils
                .getAsString(cmsi.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_ORDERING)), cDoc);

        String regCF = COSUtils
                .getAsString(cfsi.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_REGISTRY)), cDoc);
        String ordCF = COSUtils
                .getAsString(cfsi.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_SYSINFO_ORDERING)), cDoc);

        if (!regCF.equals(regCM) || !ordCF.equals(ordCM)) {
            this.fontContainer.addError(
                    new ValidationError(ERROR_FONTS_CIDKEYED_SYSINFO, "The CIDSystemInfo is inconsistent"));
            return false;
        }
    } // else cmap is null because it is a Identity-H/V
    return true;
}

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

License:Apache License

/**
 * This method return false and updates the FontContainer if the Composite
 * Font TYPE 0 or TYPE1C is damaged. This method checks the Widths
 * consistency./*from  w  w  w . j av a 2s .  c  om*/
 * 
 * @param pfDescriptor
 * @return
 */
boolean checkFontFileElement_CIDFontType0(PDFontDescriptorDictionary pfDescriptor) throws ValidationException {
    // ---- FontFile Validation
    PDStream ff1 = pfDescriptor.getFontFile();
    PDStream ff2 = pfDescriptor.getFontFile2();
    PDStream ff3 = pfDescriptor.getFontFile3();

    boolean onlyOne = (ff1 != null && ff2 == null && ff3 == null) || (ff1 == null && ff2 != null && ff3 == null)
            || (ff1 == null && ff2 == null && ff3 != null);

    if ((ff3 == null) || !onlyOne) {
        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
    COSStream stream = ff3.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;
    }

    // ---- Lengthx aren't mandatory for this type of font
    // ---- But the Subtype is a mandatory field with specific values
    String st = stream.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));
    if (!(FONT_DICTIONARY_VALUE_TYPE0C.equals(st) || FONT_DICTIONARY_VALUE_TYPE1C.equals(st))) {
        this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontFile3 stream doesn't have the right Subtype"));
        return false;
    }

    // ---- try to load the font using the java.awt.font object.
    // ---- if the font is invalid, an exception will be thrown
    try {
        CFFParser cffParser = new CFFParser();
        List<CFFFont> lCFonts = cffParser.parse(ff3.getByteArray());

        if (lCFonts == null || lCFonts.isEmpty()) {
            this.fontContainer.addError(new ValidationResult.ValidationError(ERROR_FONTS_CID_DAMAGED,
                    "The FontFile can't be read"));
            return false;
        }

        return checkCIDFontWidths(lCFonts) && checkFontFileMetaData(pfDescriptor, ff3);
    } catch (IOException e) {
        this.fontContainer.addError(
                new ValidationResult.ValidationError(ERROR_FONTS_CID_DAMAGED, "The FontFile can't be read"));
        return false;
    }
}

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

License:Apache License

/**
 * This method return false and updates the FontContainer if the Composite
 * Font TYPE 2 is damaged or missing./*from   www  .ja  va  2s .c o  m*/
 * 
 * @param pfDescriptor
 * @return
 */
boolean checkFontFileElement_CIDFontType2(PDFontDescriptorDictionary pfDescriptor) throws ValidationException {

    // ---- FontFile Validation
    PDStream ff1 = pfDescriptor.getFontFile();
    PDStream ff2 = pfDescriptor.getFontFile2();
    PDStream ff3 = pfDescriptor.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(ERROR_FONTS_FONT_FILEX_INVALID,
                "The FontFile 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(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;
    }

    // ---- try to load the font using the java.awt.font object.
    // ---- if the font is invalid, an exception will be thrown
    TrueTypeFont ttf = null;
    try {
        // ---- According to PDF Reference, CIDFontType2 is a TrueType font.
        // ---- Remark : Java.awt.Font throws exception when a CIDFontType2 is
        // parsed even if it is valid.
        ttf = new CIDFontType2Parser(true).parseTTF(new ByteArrayInputStream(ff2.getByteArray()));
    } catch (Exception e) {
        // ---- Exceptionally, Exception is catched Here because of damaged font
        // can throw NullPointer Exception...
        this.fontContainer.addError(
                new ValidationResult.ValidationError(ERROR_FONTS_CID_DAMAGED, "The FontFile can't be read"));
        return false;
    }

    return checkTTFontMetrics(ttf) && checkFontFileMetaData(pfDescriptor, ff2);
}

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

License:Apache License

/**
 * For a CIDFont the width array, there are two formats of width array :
 * <UL>/*from  w w w .  j  ava2 s  .c  om*/
 * <li>C [W1...Wn] : C is an integer specifying a starting CID value and the
 * array of n numbers specify widths for n consecutive CIDs.
 * <li>Cf Cl W : Defines the same width W for the range Cf to Cl
 * </UL>
 * This method gets a linked hash map of width where the key is a CID and the
 * value is the Width.
 * 
 * @return
 * @throws ValidationException
 */
protected LinkedHashMap<Integer, Integer> getWidthsArray() throws ValidationException {
    LinkedHashMap<Integer, Integer> widthsMap = new LinkedHashMap<Integer, Integer>();
    COSDocument cDoc = handler.getDocument().getDocument();
    COSBase cBase = this.cidFont.getItem(COSName.getPDFName("W"));
    COSArray wArr = COSUtils.getAsArray(cBase, cDoc);

    for (int i = 0; i < wArr.size();) {

        int firstCid = wArr.getInt(i);

        if (i + 1 >= wArr.size()) {
            throw new ValidationException("Invalid format of the W entry");
        }

        COSBase cb = wArr.getObject(i + 1);
        if (COSUtils.isArray(cb, cDoc)) {

            // ---- First Format
            COSArray seqWidths = COSUtils.getAsArray(cb, cDoc);
            widthsMap.put(firstCid, seqWidths.getInt(0));
            for (int jw = 1; jw < seqWidths.size(); jw++) {
                widthsMap.put((firstCid + jw), seqWidths.getInt(jw));
            }

            i = i + 2;

        } else {

            // ---- Second Format
            if (i + 2 >= wArr.size()) {
                throw new ValidationException("Invalid format of the W entry");
            }

            int lastCid = wArr.getInt(i + 1);
            int commonWidth = wArr.getInt(i + 2);
            for (int jw = firstCid; jw <= lastCid; ++jw) {
                widthsMap.put((firstCid + jw), commonWidth);
            }

            i = i + 3;

        }

    }

    return widthsMap;
}

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

License:Apache License

/**
 * If the embedded font is a subset, the CIDSet entry is mandatory and must be
 * a Stream. This method returns true if the CIDSet entry respects conditions,
 * otherwise the method returns false and the FontContainer is updated.
 * /*from w  w  w  . j  a v a2s.  com*/
 * @param pfDescriptor
 * @return
 */
protected boolean checkCIDSet(PDFontDescriptorDictionary pfDescriptor) {
    if (isSubSet(pfDescriptor.getFontName())) {
        COSBase cidset = pfDescriptor.getCOSDictionary()
                .getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_CIDSET));
        if (cidset == null || !COSUtils.isStream(cidset, this.handler.getDocument().getDocument())) {
            this.fontContainer
                    .addError(new ValidationResult.ValidationError(ERROR_FONTS_CIDSET_MISSING_FOR_SUBSET,
                            "The CIDSet entry is missing for the Composite Subset"));
            return false;
        }
    }
    return true;
}

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

License:Apache License

public FontValidator getFontValidator(COSObject cObj, DocumentHandler handler) throws ValidationException {
    COSDictionary dic = (COSDictionary) cObj.getObject();
    String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
    String subtype = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    if ((type == null || "".equals(type)) || (subtype == null || "".equals(subtype))) {
        throw new ValidationException(
                "Type and/or Subtype keys are missing : " + ERROR_FONTS_DICTIONARY_INVALID);
    } else {/*from w  ww.ja va2s  . com*/
        if (FONT_DICTIONARY_VALUE_TRUETYPE.equals(subtype)) {
            return new TrueTypeFontValidator(handler, cObj);
        } else if (FONT_DICTIONARY_VALUE_MMTYPE.equals(subtype)
                || FONT_DICTIONARY_VALUE_TYPE1.equals(subtype)) {
            return new Type1FontValidator(handler, cObj);
        } else if (FONT_DICTIONARY_VALUE_TYPE3.equals(subtype)) {
            return new Type3FontValidator(handler, cObj);
        } else if (FONT_DICTIONARY_VALUE_COMPOSITE.equals(subtype)) {
            return new CompositeFontValidator(handler, cObj);
        } else if (FONT_DICTIONARY_VALUE_TYPE2.equals(subtype) || FONT_DICTIONARY_VALUE_TYPE1C.equals(subtype)
                || FONT_DICTIONARY_VALUE_TYPE0C.equals(subtype)
                || FONT_DICTIONARY_VALUE_TYPE0.equals(subtype)) {
            // ---- Font managed by a Composite font.
            // this dictionary will be checked by a CompositeFontValidator
            return null;
        } else {
            throw new ValidationException("Unknown font type : " + subtype);
        }
    }
}

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

License:Apache License

public SimpleFontValidator(DocumentHandler handler, COSObject obj) throws ValidationException {
    super(handler, obj);
    COSBase tmpFontDesc = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_FONT_DESC));
    this.fDescriptor = COSUtils.getAsDictionary(tmpFontDesc, handler.getDocument().getDocument());
    if (this.fDescriptor != null) {
        this.pFontDesc = new PDFontDescriptorDictionary(this.fDescriptor);
    }//from  w  w  w.  j  a  v a2  s. co  m
}

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

License:Apache License

/**
 * Extract element from the COSObject to avoid useless access to this object.
 *///from  w  w  w .  j  a  v a  2s.  c o m
private void extractElementsToCheck() {
    // ---- Here is required elements
    this.basefont = fDictionary.getNameAsString(COSName.getPDFName(FONT_DICTIONARY_KEY_BASEFONT));
    this.firstChar = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_FIRSTCHAR));
    this.lastChar = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_LASTCHAR));
    this.widths = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_WIDTHS));
    // ---- Here is optional elements
    this.encoding = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_ENCODING));
    this.toUnicode = fDictionary.getItem(COSName.getPDFName(FONT_DICTIONARY_KEY_TOUNICODE));
}