List of usage examples for org.apache.pdfbox.pdmodel.font PDFont getCOSObject
@Override
public COSDictionary getCOSObject()
From source file:net.padaf.preflight.contentstream.ContentStreamWrapper.java
License:Apache License
/** * Process the validation of a Text operand contains in a ContentStream This * validation checks that ://from w w w .j av a2 s. co m * <UL> * <li>The font isn't missing if the Rendering Mode isn't 3 * <li>The font metrics are consistent * <li>All character used in the text are defined in the font program. * </UL> * * @param string * @throws IOException */ public void validText(byte[] string) throws IOException { // --- TextSize accessible through the TextState PDTextState textState = getGraphicsState().getTextState(); final int renderingMode = textState.getRenderingMode(); final PDFont font = textState.getFont(); if (font == null) { // ---- Unable to decode the Text without Font throwContentStreamException("Text operator can't be process without Font", ERROR_FONTS_UNKNOWN_FONT_REF); } // FontContainer fontContainer = documentHandler.retrieveFontContainer(font); AbstractFontContainer fontContainer = documentHandler.getFont(font.getCOSObject()); if (fontContainer != null && fontContainer.isValid() == State.INVALID) { return; } if (renderingMode == 3 && (fontContainer == null || !fontContainer.isFontProgramEmbedded())) { // font not embedded and rendering mode is 3. Valid case and nothing to check return; } if (fontContainer == null) { // ---- Font Must be embedded if the RenderingMode isn't 3 throwContentStreamException(font.getBaseFont() + " is unknown wasn't found by the FontHelperValdiator", ERROR_FONTS_UNKNOWN_FONT_REF); } if (!fontContainer.isFontProgramEmbedded()) { throwContentStreamException(font.getBaseFont() + " isn't embedded and the rendering mode isn't 3", ERROR_FONTS_FONT_FILEX_INVALID); } int codeLength = 1; for (int i = 0; i < string.length; i += codeLength) { // Decode the value to a Unicode character codeLength = 1; String c = null; try { c = font.encode(string, i, codeLength); if (c == null && i + 1 < string.length) { // maybe a multibyte encoding codeLength++; c = font.encode(string, i, codeLength); } } catch (IOException e) { throwContentStreamException("Encoding can't interpret the character code", ERROR_FONTS_ENCODING_ERROR); } // ---- According to the length of the character encoding, // convert the character to CID int cid = 0; for (int j = 0; j < codeLength; j++) { cid <<= 8; cid += ((string[i + j] + 256) % 256); } try { fontContainer.checkCID(cid); } catch (GlyphException e) { throwContentStreamException(e.getMessage(), e.getErrorCode()); } } }
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 . j a va 2 s . 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:org.apache.padaf.preflight.contentstream.ContentStreamWrapper.java
License:Apache License
/** * Process the validation of a Text operand contains in a ContentStream This * validation checks that :/* w w w . j a va2 s. c o m*/ * <UL> * <li>The font isn't missing if the Rendering Mode isn't 3 * <li>The font metrics are consistent * <li>All character used in the text are defined in the font program. * </UL> * * @param string * @throws IOException */ public void validText(byte[] string) throws IOException { // --- TextSize accessible through the TextState PDTextState textState = getGraphicsState().getTextState(); final int renderingMode = textState.getRenderingMode(); final PDFont font = textState.getFont(); if (font == null) { // ---- Unable to decode the Text without Font throwContentStreamException("Text operator can't be process without Font", ERROR_FONTS_UNKNOWN_FONT_REF); } // FontContainer fontContainer = documentHandler.retrieveFontContainer(font); AbstractFontContainer fontContainer = documentHandler.getFont(font.getCOSObject()); if (fontContainer != null && fontContainer.isValid() == State.INVALID) { return; } if (renderingMode == 3 && (fontContainer == null || !fontContainer.isFontProgramEmbedded())) { // font not embedded and rendering mode is 3. Valid case and nothing to check return; } if (fontContainer == null) { // ---- Font Must be embedded if the RenderingMode isn't 3 throwContentStreamException(font.getBaseFont() + " is unknown wasn't found by the FontHelperValdiator", ERROR_FONTS_UNKNOWN_FONT_REF); } if (!fontContainer.isFontProgramEmbedded()) { throwContentStreamException(font.getBaseFont() + " isn't embedded and the rendering mode isn't 3", ERROR_FONTS_FONT_FILEX_INVALID); } int codeLength = 1; for (int i = 0; i < string.length; i += codeLength) { int cid = -1; codeLength = 1; try { cid = font.encodeToCID(string, i, codeLength); if (cid == -1 && i + 1 < string.length) { // maybe a multibyte encoding codeLength++; cid = font.encodeToCID(string, i, codeLength); } } catch (IOException e) { throwContentStreamException("Encoding can't interpret the character code", ERROR_FONTS_ENCODING_ERROR); } try { fontContainer.checkCID(cid); } catch (GlyphException e) { if (renderingMode != 3) { throwContentStreamException(e.getMessage(), e.getErrorCode()); } } } }
From source file:org.apache.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./* w w w . j a v a2s. c o m*/ * * @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) { // ---- 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:org.xmlcml.font.NonStandardFontManager.java
License:Apache License
private String getFontName(PDFont pdFont) { String fontName;//from ww w .jav a2s .c o m AMIFont amiFont; PDFontDescriptor fd = AMIFont.getFontDescriptorOrDescendantFontDescriptor(pdFont); if (fd == null) { if (nullFontDescriptorReport) { LOG.error("****************** Null Font Descriptor : " + pdFont + "\n FURTHER ERRORS HIDDEN"); nullFontDescriptorReport = false; } } if (fd == null) { amiFont = this.lookupOrCreateFont(0, (COSDictionary) pdFont.getCOSObject()); fontName = amiFont.getFontName(); if (fontName == null) { throw new RuntimeException("No currentFontName"); } } else { fontName = fd.getFontName(); } return fontName; }
From source file:org.xmlcml.pdf2svg.AMIFont.java
License:Apache License
public static PDFont getFirstDescendantFont(PDFont pdFont) { COSDictionary dict = (COSDictionary) pdFont.getCOSObject(); COSArray array = dict == null ? null : (COSArray) dict.getDictionaryObject(COSName.DESCENDANT_FONTS); PDFont descendantFont = null;/*from w w w .j a v a 2s .c o m*/ try { descendantFont = array == null ? null : PDFontFactory.createFont((COSDictionary) array.getObject(0)); } catch (IOException e) { LOG.error("****************** Can't create descendant font! for " + pdFont); } return descendantFont; }