/** * Returns the displacement vector (w0, w1) in text space, for the given character. * For horizontal text only the x component is used, for vertical text only the y component. * * @param code character code * @return displacement vector * @throws IOException */ public Vector getDisplacement(int code) throws IOException { return new Vector(getWidth(code) / 1000, 0); }
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
private boolean checkWidths(int glyphCode) throws IOException { org.apache.pdfbox.pdmodel.font.PDFont font = getFontFromResources(); float expectedWidth = font.getWidth(glyphCode); float foundWidth = font.getWidthFromFont(glyphCode); // consistent is defined to be a difference of no more than 1/1000 unit. return Math.abs(foundWidth - expectedWidth) <= 1; }
/** * 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); // bbox in EM -> user units Shape bbox = new Rectangle2D.Float(0, 0, font.getWidth(code) / 1000, 1); AffineTransform at = textRenderingMatrix.createAffineTransform(); bbox = at.createTransformedShape(bbox); // 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.RED); graphics.setStroke(new BasicStroke(.5f)); graphics.draw(bbox); // restore graphics.setStroke(stroke); graphics.setColor(color); graphics.setClip(clip); }
float xadvance = font.getWidth(text.getCharacterCodes()[0]); // todo: should iterate all chars Rectangle2D rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), xadvance, bbox.getHeight());
float xadvance = font.getWidth(text.getCharacterCodes()[0]); // todo: should iterate all chars rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), xadvance, bbox.getHeight());
/** * Returns the displacement vector (w0, w1) in text space, for the given character. * For horizontal text only the x component is used, for vertical text only the y component. * * @param code character code * @return displacement vector * @throws IOException */ public Vector getDisplacement(int code) throws IOException { return new Vector(getWidth(code) / 1000, 0); }
/** * Returns the displacement vector (w0, w1) in text space, for the given character. * For horizontal text only the x component is used, for vertical text only the y component. * * @param code character code * @return displacement vector */ public Vector getDisplacement(int code) throws IOException { return new Vector(getWidth(code) / 1000, 0); }
displacementX = font.getWidth(code) / 1000;
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
/** * 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); // bbox in EM -> user units Shape bbox = new Rectangle2D.Float(0, 0, font.getWidth(code) / 1000, 1); AffineTransform at = textRenderingMatrix.createAffineTransform(); bbox = at.createTransformedShape(bbox); // 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.RED); graphics.setStroke(new BasicStroke(.5f)); graphics.draw(bbox); // restore graphics.setStroke(stroke); graphics.setColor(color); graphics.setClip(clip); }
Shape calculateFontBounds(TextPosition text) throws IOException { // glyph space -> user space // note: text.getTextMatrix() is *not* the Text Matrix, it's the Text Rendering Matrix AffineTransform at = text.getTextMatrix().createAffineTransform(); // show rectangle with the real vertical bounds, based on the font bounding box y values // usually, the height is identical to what you see when marking text in Adobe Reader PDFont font = text.getFont(); BoundingBox bbox = font.getBoundingBox(); // advance width, bbox height (glyph space) float xadvance = font.getWidth(text.getCharacterCodes()[0]); // todo: should iterate all chars Rectangle2D.Float rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), xadvance, bbox.getHeight()); if (font instanceof PDType3Font) { // bbox and font matrix are unscaled at.concatenate(font.getFontMatrix().createAffineTransform()); } else { // bbox and font matrix are already scaled to 1000 at.scale(1/1000f, 1/1000f); } Shape s = at.createTransformedShape(rect); s = flipAT.createTransformedShape(s); s = rotateAT.createTransformedShape(s); return s; }
float xadvance = font.getWidth(text.getCharacterCodes()[0]); // todo: should iterate all chars rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), xadvance, bbox.getHeight());
displacementX = font.getWidth(code) / 1000.0F; TrueTypeFont ttf = null; if (font instanceof PDTrueTypeFont) {
displacementX = font.getWidth(code) / 1000;
displacementX = font.getWidth(code) / 1000;