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; }
private void translateImage(float xDistance, float yDistance, AffineTransform t) { t.translate(xDistance, yDistance); t.getMatrix(matrix); if (fixedXPosition != null) { fixedXPosition += (float) t.getTranslateX(); } if (fixedYPosition != null) { fixedYPosition += (float) t.getTranslateY(); } }
/** * Add a counter-clockwise rotation to this transformation * * @param angle angle in radians to rotate over */ public void rotate(double angle) { concatenate(AffineTransform.getRotateInstance(angle)); }
public static AffineTransform getTranslateInstance(double mx, double my) { AffineTransform t = new AffineTransform(); t.setToTranslation(mx, my); return t; }
}; Point[] transformedAppBoxPoints = new Point[xObjRectPoints.length]; new AffineTransform(xObjMatrix.toDoubleArray()).transform(xObjRectPoints, 0, transformedAppBoxPoints, 0, xObjRectPoints.length); AffineTransform at = AffineTransform.getTranslateInstance(-transformedRect.getX(), -transformedRect.getY()); float scaleX = transformedRect.getWidth() == 0 ? 1 : annotBBox.getWidth() / transformedRect.getWidth(); float scaleY = transformedRect.getHeight() == 0 ? 1 : annotBBox.getHeight() / transformedRect.getHeight(); at.preConcatenate(AffineTransform.getScaleInstance(scaleX, scaleY)); at.preConcatenate(AffineTransform.getTranslateInstance(annotBBox.getX(), annotBBox.getY()));
@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))); } }
setProperty(Property.ROTATION_INITIAL_HEIGHT, height); AffineTransform rotationTransform = new AffineTransform(); rotationTransform.translate((float) rotationPointX, (float) rotationPointY); // move point back at place rotationTransform.rotate(angle); // rotate rotationTransform.translate((float) -rotationPointX, (float) -rotationPointY); // move rotation point to origin move(occupiedAreaShiftX, occupiedAreaShiftY); } else { rotationTransform = AffineTransform.getRotateInstance(angle); List<Point> rotatedPoints = transformPoints(rectangleToPointsList(occupiedArea.getBBox()), rotationTransform); float[] shift = calculateShiftToPositionBBoxOfPointsAt(x, y + height, rotatedPoints);
/** * This method creates {@link AffineTransform} instance that could be used * to rotate content inside the occupied area. Be aware that it should be used only after * layout rendering is finished and correct occupied area for the rotated element is calculated. * * @return {@link AffineTransform} that rotates the content and places it inside occupied area. */ protected AffineTransform createRotationTransformInsideOccupiedArea() { Float angle = this.<Float>getProperty(Property.ROTATION_ANGLE); AffineTransform rotationTransform = AffineTransform.getRotateInstance((float) angle); Rectangle contentBox = this.getOccupiedAreaBBox(); List<Point> rotatedContentBoxPoints = transformPoints(rectangleToPointsList(contentBox), rotationTransform); // Occupied area for rotated elements is already calculated on layout in such way to enclose rotated content; // therefore we can simply rotate content as is and then shift it to the occupied area. float[] shift = calculateShiftToPositionBBoxOfPointsAt(occupiedArea.getBBox().getLeft(), occupiedArea.getBBox().getTop(), rotatedContentBoxPoints); rotationTransform.preConcatenate(AffineTransform.getTranslateInstance(shift[0], shift[1])); return rotationTransform; }
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); } } }
public void translate(double mx, double my) { concatenate(AffineTransform.getTranslateInstance(mx, my)); }
@Override public AffineTransform clone() throws CloneNotSupportedException { return new AffineTransform(this); }
private void applyConcatMatrix(DrawContext drawContext, Float angle) { AffineTransform rotationTransform = AffineTransform.getRotateInstance((float) angle); Rectangle rect = getBorderAreaBBox(); List<Point> rotatedPoints = transformPoints(rectangleToPointsList(rect), rotationTransform); float[] shift = calculateShiftToPositionBBoxOfPointsAt(rect.getX(), rect.getY() + rect.getHeight(), rotatedPoints); double[] matrix = new double[6]; rotationTransform.getMatrix(matrix); drawContext.getCanvas().concatMatrix(matrix[0], matrix[1], matrix[2], matrix[3], shift[0], shift[1]); }
/** * This method creates {@link AffineTransform} instance that could be used * to transform content inside the occupied area, * considering the centre of the occupiedArea as the origin of a coordinate system for transformation. * * @return {@link AffineTransform} that transforms the content and places it inside occupied area. */ private AffineTransform createTransformationInsideOccupiedArea() { Rectangle backgroundArea = applyMargins(occupiedArea.clone().getBBox(), false); float x = backgroundArea.getX(); float y = backgroundArea.getY(); float height = backgroundArea.getHeight(); float width = backgroundArea.getWidth(); AffineTransform transform = AffineTransform.getTranslateInstance(-1 * (x + width / 2), -1 * (y + height / 2)); transform.preConcatenate(Transform.getAffineTransform(this.<Transform>getProperty(Property.TRANSFORM), width, height)); transform.preConcatenate(AffineTransform.getTranslateInstance(x + width / 2, y + height / 2)); return transform; }
/** * 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; }
private float adjustPositionAfterRotation(float angle, float maxWidth, float maxHeight) { if (angle != 0) { AffineTransform t = AffineTransform.getRotateInstance(angle); Point p00 = t.transform(new Point(0, 0), new Point()); Point p01 = t.transform(new Point(0, (float) height), new Point()); Point p10 = t.transform(new Point((float) width, 0), new Point()); Point p11 = t.transform(new Point((float) width, (float) height), new Point());
Rectangle layoutBox = area.getBBox().clone(); AffineTransform t = new AffineTransform(); Image modelElement = (Image) (getModelElement()); PdfXObject xObject = modelElement.getXObject(); angle = 0f; t.rotate((float) angle); initialOccupiedAreaBBox = getOccupiedAreaBBox().clone(); float scaleCoef = adjustPositionAfterRotation((float) angle, layoutBox.getWidth(), layoutBox.getHeight()); initialOccupiedAreaBBox.setWidth(imageItselfScaledWidth); if (xObject instanceof PdfFormXObject) { t.scale(scaleCoef, scaleCoef);
public void scale(double scx, double scy) { concatenate(AffineTransform.getScaleInstance(scx, scy)); }
/** * 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; }
AffineTransform translation = AffineTransform.getTranslateInstance(x, y); currentCanvas.concatMatrix(translation); if (partOfClipPath) { try { inverseMatrix = translation.createInverse(); } catch (NoninvertibleTransformException ex) { LoggerFactory.getLogger(UseSvgNodeRenderer.class)
/** * Replaces the text matrix. Contrast with {@link PdfCanvas#concatMatrix} * @param transform new textmatrix as transformation * @return current canvas * */ public PdfCanvas setTextMatrix(AffineTransform transform) { float[] matrix = new float[6]; transform.getMatrix(matrix); return setTextMatrix(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]); }