Java Examples for org.apache.pdfbox.pdmodel.font.PDFontFactory
The following java examples will help you to understand the usage of org.apache.pdfbox.pdmodel.font.PDFontFactory. These source code samples are taken from different open source projects.
Example 1
| Project: pdfbox-master File: PDFStreamEngine.java View source code |
/**
* Process text from the PDF Stream. You should override this method if you want to
* perform an action when encoded text is being processed.
*
* @param string the encoded text
* @throws IOException if there is an error processing the string
*/
protected void showText(byte[] string) throws IOException {
PDGraphicsState state = getGraphicsState();
PDTextState textState = state.getTextState();
// get the current font
PDFont font = textState.getFont();
if (font == null) {
LOG.warn("No current font, will use default");
font = PDFontFactory.createDefaultFont();
}
float fontSize = textState.getFontSize();
float horizontalScaling = textState.getHorizontalScaling() / 100f;
float charSpacing = textState.getCharacterSpacing();
// put the text state parameters into matrix form
Matrix parameters = new Matrix(// 0
fontSize * horizontalScaling, // 0
0, // 0
0, // 0
fontSize, 0, // 1
textState.getRise());
// read the stream until it is empty
InputStream in = new ByteArrayInputStream(string);
while (in.available() > 0) {
// decode a character
int before = in.available();
int code = font.readCode(in);
int codeLength = before - in.available();
String unicode = font.toUnicode(code);
// Word spacing shall be applied to every occurrence of the single-byte character code
// 32 in a string when using a simple font or a composite font that defines code 32 as
// a single-byte code.
float wordSpacing = 0;
if (codeLength == 1 && code == 32) {
wordSpacing += textState.getWordSpacing();
}
// text rendering matrix (text space -> device space)
Matrix ctm = state.getCurrentTransformationMatrix();
Matrix textRenderingMatrix = parameters.multiply(textMatrix).multiply(ctm);
// changes to vertical text should be tested with PDFBOX-2294 and PDFBOX-1422
if (font.isVertical()) {
// position vector, in text space
Vector v = font.getPositionVector(code);
// apply the position vector to the horizontal origin to get the vertical origin
textRenderingMatrix.translate(v);
}
// get glyph's horizontal and vertical displacements, in text space
Vector w = font.getDisplacement(code);
// process the decoded glyph
saveGraphicsState();
Matrix textMatrixOld = textMatrix;
Matrix textLineMatrixOld = textLineMatrix;
showGlyph(textRenderingMatrix, font, code, unicode, w);
textMatrix = textMatrixOld;
textLineMatrix = textLineMatrixOld;
restoreGraphicsState();
// calculate the combined displacements
float tx, ty;
if (font.isVertical()) {
tx = 0;
ty = w.getY() * fontSize + charSpacing + wordSpacing;
} else {
tx = (w.getX() * fontSize + charSpacing + wordSpacing) * horizontalScaling;
ty = 0;
}
// update the text matrix
textMatrix.concatenate(Matrix.getTranslateInstance(tx, ty));
}
}Example 2
| Project: PDF-to-unusual-HTML-master File: PDResources.java View source code |
/**
* This will get the map of fonts. This will never return null. The keys are string
* and the values are PDFont objects.
*
* @param fontCache A map of existing PDFont objects to reuse.
* @return The map of fonts.
*
* @throws IOException If there is an error getting the fonts.
*/
public Map getFonts(Map fontCache) throws IOException {
Map retval = null;
COSDictionary fonts = (COSDictionary) resources.getDictionaryObject(COSName.FONT);
if (fonts == null) {
fonts = new COSDictionary();
resources.setItem(COSName.FONT, fonts);
}
Map actuals = new HashMap();
retval = new COSDictionaryMap(actuals, fonts);
for (COSName fontName : fonts.keySet()) {
COSBase font = fonts.getDictionaryObject(fontName);
//PDF, we will just ignore entries that are not dictionaries.
if (font instanceof COSDictionary) {
COSDictionary fontDictionary = (COSDictionary) font;
actuals.put(fontName.getName(), PDFontFactory.createFont(fontDictionary, fontCache));
}
}
return retval;
}Example 3
| Project: with-aes-master File: Type3FontValidator.java View source code |
/**
* 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.
*
* @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);
}Example 4
| Project: padaf-master File: Type3FontValidator.java View source code |
/**
* 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.
*
* @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);
}