List of usage examples for org.apache.pdfbox.cos COSStream getInt
public int getInt(String key)
From source file:net.padaf.preflight.font.CompositeFontValidator.java
License:Apache License
/** * Standard information of a stream element will be checked by the * StreamHelper./*w ww .j a va 2 s . c o 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
/** * This method return false and updates the FontContainer if the Composite * Font TYPE 2 is damaged or missing.//from w w w.jav a 2 s .com * * @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.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. * // w ww . ja v a2 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 w w w.j ava 2s .co 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 ww w. ja v a 2 s . co m * * @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.helpers.StreamValidationHelper.java
License:Apache License
protected void checkStreamLength(DocumentHandler handler, COSObject cObj, List<ValidationError> result) throws ValidationException { COSStream streamObj = (COSStream) cObj.getObject(); int length = streamObj.getInt(COSName.getPDFName(STREAM_DICTIONARY_KEY_LENGHT)); InputStream ra = null;/*w ww. j av a 2 s . co m*/ try { ra = handler.getSource().getInputStream(); Integer offset = (Integer) handler.getDocument().getDocument().getXrefTable() .get(new COSObjectKey(cObj)); // ---- go to the beginning of the object long skipped = 0; while (skipped != offset) { long curSkip = ra.skip(offset - skipped); if (curSkip < 0) { throw new ValidationException("Unable to skip bytes in the PDFFile to check stream length"); } skipped += curSkip; } // ---- go to the stream key word if (readUntilStream(ra)) { int c = ra.read(); if (c == '\r') { ra.read(); } // else c is '\n' no more character to read // ---- Here is the true beginning of the Stream Content. // ---- Read the given length of bytes and check the 10 next bytes // ---- to see if there are endstream. byte[] buffer = new byte[1024]; int nbBytesToRead = length; do { int cr = 0; if (nbBytesToRead > 1024) { cr = ra.read(buffer, 0, 1024); } else { cr = ra.read(buffer, 0, nbBytesToRead); } if (cr == -1) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); return; } else { nbBytesToRead = nbBytesToRead - cr; } } while (nbBytesToRead > 0); int len = "endstream".length() + 2; byte[] buffer2 = new byte[len]; for (int i = 0; i < len; ++i) { buffer2[i] = (byte) ra.read(); } // ---- check the content of 10 last characters String endStream = new String(buffer2); if (buffer2[0] == '\r' && buffer2[1] == '\n') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else if (buffer2[0] == '\r' && buffer2[1] == 'e') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else if (buffer2[0] == '\n' && buffer2[1] == 'e') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } catch (IOException e) { throw new ValidationException("Unable to read a stream to validate it due to : " + e.getMessage(), e); } finally { if (ra != null) { IOUtils.closeQuietly(ra); } } }
From source file:org.apache.padaf.preflight.font.CompositeFontValidator.java
License:Apache License
/** * Standard information of a stream element will be checked by the * StreamHelper./*from ww w. ja v a2s . 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)); if (wmode == -1) { /* * According to the getInt javadoc, -1 is returned if there are no result. * In the PDF Reference v1.7 p449, we can read that Default value is 0. */ wmode = FONT_DICTIONARY_DEFAULT_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(); 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:org.apache.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. * /* 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; } ((TrueTypeFontContainer) this.fontContainer).setFontObjectAndInitializeInnerFields(ttf); ((TrueTypeFontContainer) this.fontContainer).setCMap(getCMapOfFontProgram(ttf)); return 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:org.apache.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 av a 2 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); return true; } catch (IOException e) { throw new ValidationException("Unable to check Type1 metrics due to : " + e.getMessage(), e); } }
From source file:org.apache.padaf.preflight.helpers.StreamValidationHelper.java
License:Apache License
protected void checkStreamLength(DocumentHandler handler, COSObject cObj, List<ValidationError> result) throws ValidationException { COSStream streamObj = (COSStream) cObj.getObject(); int length = streamObj.getInt(COSName.getPDFName(STREAM_DICTIONARY_KEY_LENGHT)); InputStream ra = null;/*from w ww .ja v a 2 s . co m*/ try { ra = handler.getSource().getInputStream(); Long offset = handler.getDocument().getDocument().getXrefTable().get(new COSObjectKey(cObj)); // ---- go to the beginning of the object long skipped = 0; if (offset != null) { while (skipped != offset) { long curSkip = ra.skip(offset - skipped); if (curSkip < 0) { throw new ValidationException("Unable to skip bytes in the PDFFile to check stream length"); } skipped += curSkip; } // ---- go to the stream key word if (readUntilStream(ra)) { int c = ra.read(); if (c == '\r') { ra.read(); } // else c is '\n' no more character to read // ---- Here is the true beginning of the Stream Content. // ---- Read the given length of bytes and check the 10 next bytes // ---- to see if there are endstream. byte[] buffer = new byte[1024]; int nbBytesToRead = length; do { int cr = 0; if (nbBytesToRead > 1024) { cr = ra.read(buffer, 0, 1024); } else { cr = ra.read(buffer, 0, nbBytesToRead); } if (cr == -1) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); return; } else { nbBytesToRead = nbBytesToRead - cr; } } while (nbBytesToRead > 0); int len = "endstream".length() + 2; byte[] buffer2 = new byte[len]; for (int i = 0; i < len; ++i) { buffer2[i] = (byte) ra.read(); } // ---- check the content of 10 last characters String endStream = new String(buffer2); if (buffer2[0] == '\r' && buffer2[1] == '\n') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else if (buffer2[0] == '\r' && buffer2[1] == 'e') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else if (buffer2[0] == '\n' && buffer2[1] == 'e') { if (!endStream.contains("endstream")) { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { result.add(new ValidationResult.ValidationError( ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide")); } } else { /* * * Offset is null. The stream isn't used, check is useless. * * TODO : Is it the truth? */ } } catch (IOException e) { throw new ValidationException("Unable to read a stream to validate it due to : " + e.getMessage(), e); } finally { if (ra != null) { IOUtils.closeQuietly(ra); } } }