/** * Computes the cross product of this vector and the specified matrix * * @param by the matrix to cross this vector with * @return the result of the cross product */ public Vector cross(Matrix by) { float x = vals[I1] * by.get(Matrix.I11) + vals[I2] * by.get(Matrix.I21) + vals[I3] * by.get(Matrix.I31); float y = vals[I1] * by.get(Matrix.I12) + vals[I2] * by.get(Matrix.I22) + vals[I3] * by.get(Matrix.I32); float z = vals[I1] * by.get(Matrix.I13) + vals[I2] * by.get(Matrix.I23) + vals[I3] * by.get(Matrix.I33); return new Vector(x, y, z); }
/** * Used for creating sub-TextRenderInfos for each individual character * * @param parent the parent TextRenderInfo * @param str the content of a TextRenderInfo * @param horizontalOffset the unscaled horizontal offset of the character that this TextRenderInfo represents */ private TextRenderInfo(TextRenderInfo parent, PdfString str, float horizontalOffset) { super(parent.gs); this.string = str; Matrix offsetMatrix = new Matrix(horizontalOffset, 0); this.textToUserSpaceTransformMatrix = offsetMatrix.multiply(parent.textToUserSpaceTransformMatrix); this.textMatrix = offsetMatrix.multiply(parent.textMatrix); this.canvasTagHierarchy = parent.canvasTagHierarchy; this.fontMatrix = parent.gs.getFont().getFontMatrix(); }
/** * @return the size of the image, in User space units */ public float getArea() { // the image space area is 1, so we multiply that by the determinant of the CTM to get the transformed area return ctm.getDeterminant(); }
Matrix currentMatrix = new Matrix(matrix.getAsNumber(0).floatValue(), matrix.getAsNumber(1).floatValue(), matrix.getAsNumber(2).floatValue(), matrix.getAsNumber(3).floatValue(), matrix.getAsNumber(4).floatValue(), matrix.getAsNumber(5).floatValue()); Matrix toConcatenate = new Matrix((float) Math.cos(angle), (float) (-Math.sin(angle)), (float) (Math.sin(angle)), (float) (Math.cos(angle)), (float) translationWidth, (float) translationHeight); currentMatrix = currentMatrix.multiply(toConcatenate); matrix = new PdfArray(new float[]{currentMatrix.get(0), currentMatrix.get(1), currentMatrix.get(3), currentMatrix.get(4), currentMatrix.get(6), currentMatrix.get(7)});
/** * {@inheritDoc} */ public void invoke(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands) { processor.textMatrix = new Matrix(); processor.textLineMatrix = processor.textMatrix; processor.beginText(); } }
/** * Updates current transformation matrix. * * @param newCtm new current transformation matrix. */ public void updateCtm(Matrix newCtm) { ctm = newCtm.multiply(ctm); }
Matrix currentMatrix = new Matrix(matrix.getAsNumber(0).floatValue(), matrix.getAsNumber(1).floatValue(), matrix.getAsNumber(2).floatValue(), matrix.getAsNumber(3).floatValue(), matrix.getAsNumber(4).floatValue(), matrix.getAsNumber(5).floatValue()); Matrix toConcatenate = new Matrix((float) Math.cos(angle), (float) (-Math.sin(angle)), (float) (Math.sin(angle)), (float) (Math.cos(angle)), (float) translationWidth, (float) translationHeight); currentMatrix = currentMatrix.multiply(toConcatenate); matrix = new PdfArray(new float[]{currentMatrix.get(0), currentMatrix.get(1), currentMatrix.get(3), currentMatrix.get(4), currentMatrix.get(6), currentMatrix.get(7)});
/** * Subtracts a matrix from this matrix and returns the results * @param arg the matrix to subtract from this matrix * @return a Matrix object */ public Matrix subtract(Matrix arg){ Matrix rslt = new Matrix(); float[] a = vals; float[] b = arg.vals; float[] c = rslt.vals; c[I11] = a[I11]-b[I11]; c[I12] = a[I12]-b[I12]; c[I13] = a[I13]-b[I13]; c[I21] = a[I21]-b[I21]; c[I22] = a[I22]-b[I22]; c[I23] = a[I23]-b[I23]; c[I31] = a[I31]-b[I31]; c[I32] = a[I32]-b[I32]; c[I33] = a[I33]-b[I33]; return rslt; }
/** * Creates a new TextRenderInfo object * * @param str the PDF string that should be displayed * @param gs the graphics state (note: at this time, this is not immutable, so don't cache it) * @param textMatrix the text matrix at the time of the render operation * @param canvasTagHierarchy the marked content tags sequence, if available */ public TextRenderInfo(PdfString str, CanvasGraphicsState gs, Matrix textMatrix, Stack<CanvasTag> canvasTagHierarchy) { super(gs); this.string = str; this.textToUserSpaceTransformMatrix = textMatrix.multiply(gs.getCtm()); this.textMatrix = textMatrix; this.canvasTagHierarchy = Collections.<CanvasTag>unmodifiableList(new ArrayList<>(canvasTagHierarchy)); this.fontMatrix = gs.getFont().getFontMatrix(); }
/** * {@inheritDoc} */ public void invoke(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands) { float tx = ((PdfNumber) operands.get(0)).floatValue(); float ty = ((PdfNumber) operands.get(1)).floatValue(); Matrix translationMatrix = new Matrix(tx, ty); processor.textMatrix = translationMatrix.multiply(processor.textLineMatrix); processor.textLineMatrix = processor.textMatrix; } }
private Point[] transformPoints(Matrix transformationMatrix, Point... points) { try { AffineTransform t = new AffineTransform( transformationMatrix.get(Matrix.I11), transformationMatrix.get(Matrix.I12), transformationMatrix.get(Matrix.I21), transformationMatrix.get(Matrix.I22), transformationMatrix.get(Matrix.I31), transformationMatrix.get(Matrix.I32) ); t = t.createInverse(); Point[] transformed = new Point[points.length]; t.transform(points, 0, transformed, 0, points.length); return transformed; } catch (NoninvertibleTransformException e) { throw new RuntimeException(e.getMessage(), e); } } }
/** * adds a matrix from this matrix and returns the results * @param arg the matrix to subtract from this matrix * @return a Matrix object */ public Matrix add(Matrix arg){ Matrix rslt = new Matrix(); float[] a = vals; float[] b = arg.vals; float[] c = rslt.vals; c[I11] = a[I11]+b[I11]; c[I12] = a[I12]+b[I12]; c[I13] = a[I13]+b[I13]; c[I21] = a[I21]+b[I21]; c[I22] = a[I22]+b[I22]; c[I23] = a[I23]+b[I23]; c[I31] = a[I31]+b[I31]; c[I32] = a[I32]+b[I32]; c[I33] = a[I33]+b[I33]; return rslt; }
/** * Adjusts the text matrix for the specified adjustment value (see TJ operator in the PDF spec for information) * * @param tj the text adjustment */ private void applyTextAdjust(float tj) { float adjustBy = -tj / 1000f * getGraphicsState().getFontSize() * (getGraphicsState().getHorizontalScaling() / 100f); textMatrix = new Matrix(adjustBy, 0).multiply(textMatrix); }
@Override public void drawBackground(DrawContext drawContext) { PdfCanvas canvas = drawContext.getCanvas(); Matrix ctm = canvas.getGraphicsState().getCtm(); // Avoid rotation Float angle = this.getPropertyAsFloat(Property.ROTATION_ANGLE); boolean avoidRotation = null != angle && hasProperty(Property.BACKGROUND); boolean restoreRotation = hasOwnProperty(Property.ROTATION_ANGLE); if (avoidRotation) { AffineTransform transform = new AffineTransform(ctm.get(0), ctm.get(1), ctm.get(3), ctm.get(4), ctm.get(6), ctm.get(7)); try { transform = transform.createInverse(); } catch (NoninvertibleTransformException e) { throw new RuntimeException(e.getMessage(), e); } transform.concatenate(new AffineTransform()); canvas.concatMatrix(transform); setProperty(Property.ROTATION_ANGLE, null); } super.drawBackground(drawContext); // restore concat matrix and rotation angle if (avoidRotation) { if (restoreRotation) { setProperty(Property.ROTATION_ANGLE, angle); } else { deleteOwnProperty(Property.ROTATION_ANGLE); } canvas.concatMatrix(new AffineTransform(ctm.get(0), ctm.get(1), ctm.get(3), ctm.get(4), ctm.get(6), ctm.get(7))); } }
/** * multiplies this matrix by 'b' and returns the result * See http://en.wikipedia.org/wiki/Matrix_multiplication * @param by The matrix to multiply by * @return the resulting matrix */ public Matrix multiply(Matrix by){ Matrix rslt = new Matrix(); float[] a = vals; float[] b = by.vals; float[] c = rslt.vals; c[I11] = a[I11]*b[I11] + a[I12]*b[I21] + a[I13]*b[I31]; c[I12] = a[I11]*b[I12] + a[I12]*b[I22] + a[I13]*b[I32]; c[I13] = a[I11]*b[I13] + a[I12]*b[I23] + a[I13]*b[I33]; c[I21] = a[I21]*b[I11] + a[I22]*b[I21] + a[I23]*b[I31]; c[I22] = a[I21]*b[I12] + a[I22]*b[I22] + a[I23]*b[I32]; c[I23] = a[I21]*b[I13] + a[I22]*b[I23] + a[I23]*b[I33]; c[I31] = a[I31]*b[I11] + a[I32]*b[I21] + a[I33]*b[I31]; c[I32] = a[I31]*b[I12] + a[I32]*b[I22] + a[I33]*b[I32]; c[I33] = a[I31]*b[I13] + a[I32]*b[I23] + a[I33]*b[I33]; return rslt; }
/** * Displays text. * * @param string the text to display */ private void displayPdfString(PdfString string) { TextRenderInfo renderInfo = new TextRenderInfo(string, getGraphicsState(), textMatrix, markedContentStack); textMatrix = new Matrix(renderInfo.getUnscaledWidth(), 0).multiply(textMatrix); eventOccurred(renderInfo, EventType.RENDER_TEXT); }
@Override public void drawBackground(DrawContext drawContext) { PdfCanvas canvas = drawContext.getCanvas(); Matrix ctm = canvas.getGraphicsState().getCtm(); // Avoid rotation Float angle = this.getPropertyAsFloat(Property.ROTATION_ANGLE); boolean avoidRotation = null != angle && hasProperty(Property.BACKGROUND); boolean restoreRotation = hasOwnProperty(Property.ROTATION_ANGLE); if (avoidRotation) { AffineTransform transform = new AffineTransform(ctm.get(0), ctm.get(1), ctm.get(3), ctm.get(4), ctm.get(6), ctm.get(7)); try { transform = transform.createInverse(); } catch (NoninvertibleTransformException e) { throw new RuntimeException(e.getMessage(), e); } transform.concatenate(new AffineTransform()); canvas.concatMatrix(transform); setProperty(Property.ROTATION_ANGLE, null); } super.drawBackground(drawContext); // restore concat matrix and rotation angle if (avoidRotation) { if (restoreRotation) { setProperty(Property.ROTATION_ANGLE, angle); } else { deleteOwnProperty(Property.ROTATION_ANGLE); } canvas.concatMatrix(new AffineTransform(ctm.get(0), ctm.get(1), ctm.get(3), ctm.get(4), ctm.get(6), ctm.get(7))); } }
/** * Updates current transformation matrix. */ public void updateCtm(float a, float b, float c, float d, float e, float f) { updateCtm(new Matrix(a, b, c, d, e, f)); }
/** * {@inheritDoc} */ public void invoke(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands) { float a = ((PdfNumber) operands.get(0)).floatValue(); float b = ((PdfNumber) operands.get(1)).floatValue(); float c = ((PdfNumber) operands.get(2)).floatValue(); float d = ((PdfNumber) operands.get(3)).floatValue(); float e = ((PdfNumber) operands.get(4)).floatValue(); float f = ((PdfNumber) operands.get(5)).floatValue(); processor.textLineMatrix = new Matrix(a, b, c, d, e, f); processor.textMatrix = processor.textLineMatrix; } }
/** * {@inheritDoc} */ public void invoke(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands) { float a = ((PdfNumber) operands.get(0)).floatValue(); float b = ((PdfNumber) operands.get(1)).floatValue(); float c = ((PdfNumber) operands.get(2)).floatValue(); float d = ((PdfNumber) operands.get(3)).floatValue(); float e = ((PdfNumber) operands.get(4)).floatValue(); float f = ((PdfNumber) operands.get(5)).floatValue(); Matrix matrix = new Matrix(a, b, c, d, e, f); processor.getGraphicsState().updateCtm(matrix); } }