@Override public AffineTransform clone() throws CloneNotSupportedException { return new AffineTransform(this); }
/** * Multiply matrix of two AffineTransform objects * * @param t1 - the AffineTransform object is a multiplicand * @param t2 - the AffineTransform object is a multiplier * @return an AffineTransform object that is a result of t1 multiplied by matrix t2. */ AffineTransform multiply(AffineTransform t1, AffineTransform t2) { return new AffineTransform( t1.m00 * t2.m00 + t1.m10 * t2.m01, // m00 t1.m00 * t2.m10 + t1.m10 * t2.m11, // m01 t1.m01 * t2.m00 + t1.m11 * t2.m01, // m10 t1.m01 * t2.m10 + t1.m11 * t2.m11, // m11 t1.m02 * t2.m00 + t1.m12 * t2.m01 + t2.m02, // m02 t1.m02 * t2.m10 + t1.m12 * t2.m11 + t2.m12);// m12 }
public static AffineTransform getScaleInstance(double scx, double scY) { AffineTransform t = new AffineTransform(); t.setToScale(scx, scY); return t; }
public static AffineTransform getTranslateInstance(double mx, double my) { AffineTransform t = new AffineTransform(); t.setToTranslation(mx, my); return t; }
public static AffineTransform getShearInstance(double shx, double shy) { AffineTransform m = new AffineTransform(); m.setToShear(shx, shy); return m; }
/** * Get an affine transformation representing a counter-clockwise rotation over the passed angle * * @param angle angle in radians to rotate over * @return {@link AffineTransform} representing the rotation */ public static AffineTransform getRotateInstance(double angle) { AffineTransform t = new AffineTransform(); t.setToRotation(angle); return t; }
/** * Get an affine transformation representing a counter-clockwise rotation over the passed angle, * using the passed point as the center of rotation * * @param angle angle in radians to rotate over * @param x x-coordinate of center of rotation * @param y y-coordinate of center of rotation * @return {@link AffineTransform} representing the rotation */ public static AffineTransform getRotateInstance(double angle, double x, double y) { AffineTransform t = new AffineTransform(); t.setToRotation(angle, x, y); return t; }
/** * Creates a skewX transformation. * * @param values values of the transformation * @return AffineTransform for the skew operation */ private static AffineTransform createSkewXTransformation(List<String> values) { if (values.size() != 1) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES); } double tan = Math.tan(Math.toRadians((float) CssUtils.parseFloat(values.get(0)))); //Differs from the notation in the PDF-spec for skews return new AffineTransform(1, 0, tan, 1, 0, 0); }
/** * Creates a skewY transformation. * * @param values values of the transformation * @return AffineTransform for the skew operation */ private static AffineTransform createSkewYTransformation(List<String> values) { if (values.size() != 1) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES); } double tan = Math.tan(Math.toRadians((float) CssUtils.parseFloat(values.get(0)))); //Differs from the notation in the PDF-spec for skews return new AffineTransform(1, tan, 0, 1, 0, 0); }
public AffineTransform createInverse() throws NoninvertibleTransformException { double det = getDeterminant(); if (Math.abs(det) < ZERO) { // awt.204=Determinant is zero throw new NoninvertibleTransformException("Determinant is zero. Cannot invert transformation"); //$NON-NLS-1$ } return new AffineTransform( m11 / det, // m00 -m10 / det, // m10 -m01 / det, // m01 m00 / det, // m11 (m01 * m12 - m11 * m02) / det, // m02 (m10 * m02 - m00 * m12) / det // m12 ); }
/** * Creates a matrix transformation. * * @param values values of the transformation * @return AffineTransform for the matrix keyword */ private static AffineTransform createMatrixTransformation(List<String> values) { if (values.size() != 6) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES); } float a = CssUtils.parseAbsoluteLength(values.get(0)); float b = CssUtils.parseAbsoluteLength(values.get(1)); float c = CssUtils.parseAbsoluteLength(values.get(2)); float d = CssUtils.parseAbsoluteLength(values.get(3)); float e = CssUtils.parseAbsoluteLength(values.get(4)); float f = CssUtils.parseAbsoluteLength(values.get(5)); return new AffineTransform(a, b, c, d, e, f); }
/** * Converts the {@link Transform} instance, i.e. the list of {@link SingleTransform} instances, * to the equivalent {@link AffineTransform} instance relatively to the available area, * including resolving of percent values to point values. * * @param t a {@link Transform} instance to convert * @param width the width of available area, the point value of which is equivalent to 100% for percentage resolving * @param height the height of available area, the point value of which is equivalent to 100% for percentage resolving */ public static AffineTransform getAffineTransform(Transform t, float width, float height) { List<SingleTransform> multipleTransform = t.getMultipleTransform(); AffineTransform affineTransform = new AffineTransform(); for (int k = multipleTransform.size() - 1; k >= 0; k--) { SingleTransform transform = multipleTransform.get(k); float[] floats = new float[6]; for (int i = 0; i < 4; i++) floats[i] = transform.getFloats()[i]; for (int i = 4; i < 6; i++) floats[i] = transform.getUnitValues()[i - 4].getUnitType() == UnitValue.POINT ? transform.getUnitValues()[i - 4].getValue() : transform.getUnitValues()[i - 4].getValue() / 100 * (i == 4 ? width : height); affineTransform.preConcatenate(new AffineTransform(floats)); } return affineTransform; }
/** * Converts the {@link Transform} instance, i.e. the list of {@link SingleTransform} instances, * to the equivalent {@link AffineTransform} instance relatively to the available area, * including resolving of percent values to point values. * * @param t a {@link Transform} instance to convert * @param width the width of available area, the point value of which is equivalent to 100% for percentage resolving * @param height the height of available area, the point value of which is equivalent to 100% for percentage resolving */ public static AffineTransform getAffineTransform(Transform t, float width, float height) { List<SingleTransform> multipleTransform = t.getMultipleTransform(); AffineTransform affineTransform = new AffineTransform(); for (int k = multipleTransform.size() - 1; k >= 0; k--) { SingleTransform transform = multipleTransform.get(k); float[] floats = new float[6]; for (int i = 0; i < 4; i++) floats[i] = transform.getFloats()[i]; for (int i = 4; i < 6; i++) floats[i] = transform.getUnitValues()[i - 4].getUnitType() == UnitValue.POINT ? transform.getUnitValues()[i - 4].getValue() : transform.getUnitValues()[i - 4].getValue() / 100 * (i == 4 ? width : height); affineTransform.preConcatenate(new AffineTransform(floats)); } return affineTransform; }
/** * Converts a string containing a transform declaration into an AffineTransform object. * This class only supports the transformations as described in the SVG specification: * - matrix * - translate * - skewx * - skewy * - rotate * - scale * * @param transform value to be parsed * @return the AffineTransform object */ public static AffineTransform parseTransform(String transform) { if (transform == null) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_NULL); } if (transform.isEmpty()) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_EMPTY); } AffineTransform matrix = new AffineTransform(); List<String> listWithTransformations = splitString(transform); for (String transformation : listWithTransformations) { AffineTransform newMatrix = transformationStringToMatrix(transformation); if (newMatrix != null) { matrix.concatenate(newMatrix); } } return matrix; }
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); } } }
AffineTransform calculateTransformation(SvgDrawContext context){ Rectangle viewPort = context.getCurrentViewPort(); float horizontal = viewPort.getX(); float vertical = viewPort.getY() + viewPort.getHeight(); // flip coordinate space vertically and translate on the y axis with the viewport height AffineTransform transform = AffineTransform.getTranslateInstance(0,0); //Identity-transform transform.concatenate(AffineTransform.getTranslateInstance(horizontal,vertical)); transform.concatenate(new AffineTransform(1,0,0,-1,0,0)); return transform; }
@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))); } }
@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))); } }
}; Point[] transformedAppBoxPoints = new Point[xObjRectPoints.length]; new AffineTransform(xObjMatrix.toDoubleArray()).transform(xObjRectPoints, 0, transformedAppBoxPoints, 0, xObjRectPoints.length);
}; Point[] transformedAppBoxPoints = new Point[xObjRectPoints.length]; new AffineTransform(xObjMatrix.toDoubleArray()).transform(xObjRectPoints, 0, transformedAppBoxPoints, 0, xObjRectPoints.length);