use of org.apache.pdfbox.pdmodel.font.PDVectorFont in project pdfbox by apache.
the class DebugPageDrawer method showGlyph.
/**
* Glyph bounding boxes.
*/
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
// draw glyph
super.showGlyph(textRenderingMatrix, font, code, unicode, displacement);
if (showGlyphBounds) {
Shape bbox;
// compute visual bounds
if (font instanceof PDType3Font) {
// todo: implement me
return;
} else {
AffineTransform at = textRenderingMatrix.createAffineTransform();
at.concatenate(font.getFontMatrix().createAffineTransform());
// get the path
PDVectorFont vectorFont = (PDVectorFont) font;
GeneralPath path = vectorFont.getNormalizedPath(code);
if (path == null) {
return;
}
// stretch non-embedded glyph if it does not match the width contained in the PDF
if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code)) {
float fontWidth = font.getWidthFromFont(code);
if (// ignore spaces
fontWidth > 0 && Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001) {
float pdfWidth = displacement.getX() * 1000;
at.scale(pdfWidth / fontWidth, 1);
}
}
Shape glyph = at.createTransformedShape(path);
bbox = glyph.getBounds2D();
}
// save
Graphics2D graphics = getGraphics();
Color color = graphics.getColor();
Stroke stroke = graphics.getStroke();
Shape clip = graphics.getClip();
// draw
graphics.setClip(graphics.getDeviceConfiguration().getBounds());
graphics.setColor(Color.cyan);
graphics.setStroke(new BasicStroke(.5f));
graphics.draw(bbox);
// restore
graphics.setStroke(stroke);
graphics.setColor(color);
graphics.setClip(clip);
}
}
use of org.apache.pdfbox.pdmodel.font.PDVectorFont in project pdfbox by apache.
the class PageDrawer method showFontGlyph.
@Override
protected void showFontGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
AffineTransform at = textRenderingMatrix.createAffineTransform();
at.concatenate(font.getFontMatrix().createAffineTransform());
// create cache if it does not exist
PDVectorFont vectorFont = ((PDVectorFont) font);
GlyphCache cache = glyphCaches.get(font);
if (cache == null) {
cache = new GlyphCache(vectorFont);
glyphCaches.put(font, cache);
}
GeneralPath path = cache.getPathForCharacterCode(code);
drawGlyph(path, font, code, displacement, at);
}
use of org.apache.pdfbox.pdmodel.font.PDVectorFont in project pdfbox by apache.
the class DrawPrintTextLocations method calculateGlyphBounds.
// this calculates the real (except for type 3 fonts) individual glyph bounds
private Shape calculateGlyphBounds(Matrix textRenderingMatrix, PDFont font, int code) throws IOException {
GeneralPath path = null;
AffineTransform at = textRenderingMatrix.createAffineTransform();
at.concatenate(font.getFontMatrix().createAffineTransform());
if (font instanceof PDType3Font) {
// It is difficult to calculate the real individual glyph bounds for type 3 fonts
// because these are not vector fonts, the content stream could contain almost anything
// that is found in page content streams.
PDType3Font t3Font = (PDType3Font) font;
PDType3CharProc charProc = t3Font.getCharProc(code);
if (charProc != null) {
BoundingBox fontBBox = t3Font.getBoundingBox();
PDRectangle glyphBBox = charProc.getGlyphBBox();
if (glyphBBox != null) {
// PDFBOX-3850: glyph bbox could be larger than the font bbox
glyphBBox.setLowerLeftX(Math.max(fontBBox.getLowerLeftX(), glyphBBox.getLowerLeftX()));
glyphBBox.setLowerLeftY(Math.max(fontBBox.getLowerLeftY(), glyphBBox.getLowerLeftY()));
glyphBBox.setUpperRightX(Math.min(fontBBox.getUpperRightX(), glyphBBox.getUpperRightX()));
glyphBBox.setUpperRightY(Math.min(fontBBox.getUpperRightY(), glyphBBox.getUpperRightY()));
path = glyphBBox.toGeneralPath();
}
}
} else if (font instanceof PDVectorFont) {
PDVectorFont vectorFont = (PDVectorFont) font;
path = vectorFont.getPath(code);
if (font instanceof PDTrueTypeFont) {
PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
int unitsPerEm = ttFont.getTrueTypeFont().getHeader().getUnitsPerEm();
at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
}
if (font instanceof PDType0Font) {
PDType0Font t0font = (PDType0Font) font;
if (t0font.getDescendantFont() instanceof PDCIDFontType2) {
int unitsPerEm = ((PDCIDFontType2) t0font.getDescendantFont()).getTrueTypeFont().getHeader().getUnitsPerEm();
at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
}
}
} else if (font instanceof PDSimpleFont) {
PDSimpleFont simpleFont = (PDSimpleFont) font;
// these two lines do not always work, e.g. for the TT fonts in file 032431.pdf
// which is why PDVectorFont is tried first.
String name = simpleFont.getEncoding().getName(code);
path = simpleFont.getPath(name);
} else {
// shouldn't happen, please open issue in JIRA
System.out.println("Unknown font class: " + font.getClass());
}
if (path == null) {
return null;
}
return at.createTransformedShape(path.getBounds2D());
}
Aggregations