Java Code Examples for org.apache.pdfbox.pdmodel.font.PDFont#isVertical()
The following examples show how to use
org.apache.pdfbox.pdmodel.font.PDFont#isVertical() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageDrawer.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Render the font using the Glyph2D interface. * * @param glyph2D the Glyph2D implementation provided a GeneralPath for each glyph * @param font the font * @param code character code * @param displacement the glyph's displacement (advance) * @param at the transformation * @throws IOException if something went wrong */ private void drawGlyph2D(Glyph2D glyph2D, PDFont font, int code, Vector displacement, AffineTransform at) throws IOException { PDGraphicsState state = getGraphicsState(); RenderingMode renderingMode = state.getTextState().getRenderingMode(); GeneralPath path = glyph2D.getPathForCharacterCode(code); if (path != null) { // Stretch non-embedded glyph if it does not match the height/width contained in the PDF. // Vertical fonts have zero X displacement, so the following code scales to 0 if we don't skip it. // TODO: How should vertical fonts be handled? if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code)) { float fontWidth = font.getWidthFromFont(code); if (fontWidth > 0 && // ignore spaces Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001) { float pdfWidth = displacement.getX() * 1000; at.scale(pdfWidth / fontWidth, 1); } } // render glyph Shape glyph = at.createTransformedShape(path); if (renderingMode.isFill()) { graphics.setComposite(state.getNonStrokingJavaComposite()); graphics.setPaint(getNonStrokingPaint()); setClip(); if (isContentRendered()) { graphics.fill(glyph); } } if (renderingMode.isStroke()) { graphics.setComposite(state.getStrokingJavaComposite()); graphics.setPaint(getStrokingPaint()); graphics.setStroke(getStroke()); setClip(); if (isContentRendered()) { graphics.draw(glyph); } } if (renderingMode.isClip()) { textClippings.add(glyph); } } }
Example 2
Source File: PDFStreamEngine.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Called when a string of text with spacing adjustments is to be shown. * * @param array array of encoded text strings and adjustments * @throws IOException if there was an error showing the text */ public void showTextStrings(COSArray array) throws IOException { PDTextState textState = getGraphicsState().getTextState(); float fontSize = textState.getFontSize(); float horizontalScaling = textState.getHorizontalScaling() / 100f; PDFont font = textState.getFont(); boolean isVertical = false; if (font != null) { isVertical = font.isVertical(); } for (COSBase obj : array) { if (obj instanceof COSNumber) { float tj = ((COSNumber)obj).floatValue(); // calculate the combined displacements float tx; float ty; if (isVertical) { tx = 0; ty = -tj / 1000 * fontSize; } else { tx = -tj / 1000 * fontSize * horizontalScaling; ty = 0; } applyTextAdjustment(tx, ty); } else if(obj instanceof COSString) { byte[] string = ((COSString)obj).getBytes(); showText(string); } else { throw new IOException("Unknown type in array for TJ operation:" + obj); } } }
Example 3
Source File: PDFStreamEngine.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * 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( fontSize * horizontalScaling, 0, // 0 0, fontSize, // 0 0, textState.getRise()); // 1 // 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); // get glyph's position vector if this is vertical text // 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; float 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)); } }