Bug #12502 fixed: Rotate, flip did not work on ports
[scilab.git] / scilab / modules / graph / src / java / org / scilab / modules / graph / shape / SvgShape.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010-2010 - DIGITEO - Clement DAVID <clement.david@scilab.org>
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 package org.scilab.modules.graph.shape;
17
18 import java.awt.Rectangle;
19 import java.awt.RenderingHints;
20 import java.awt.Shape;
21 import java.awt.geom.AffineTransform;
22
23 import org.apache.batik.ext.awt.RenderingHintsKeyExt;
24 import org.scilab.modules.graph.ScilabCanvas;
25
26 import com.mxgraph.canvas.mxGraphics2DCanvas;
27 import com.mxgraph.shape.mxLabelShape;
28 import com.mxgraph.util.mxConstants;
29 import com.mxgraph.util.mxUtils;
30 import com.mxgraph.view.mxCellState;
31
32 /**
33  * Add the SVG capability to the standard Image shape
34  */
35 public class SvgShape extends mxLabelShape {
36     /**
37      * Paint the shape
38      *
39      * @param canvas the current canvas
40      * @param state the current state
41      * @see com.mxgraph.shape.mxImageShape#paintShape(com.mxgraph.canvas.mxGraphics2DCanvas, com.mxgraph.view.mxCellState)
42      */
43     @Override
44     public void paintShape(mxGraphics2DCanvas canvas, mxCellState state) {
45         // paint previously set background without the rotation applied
46         double angle = mxUtils.getDouble(state.getStyle(), mxConstants.STYLE_ROTATION, 0);
47         if (angle % 180. != 0.) {
48             mxCellState nonRotatedState = (mxCellState) state.clone();
49
50             double tmp = nonRotatedState.getCenterY() - nonRotatedState.getWidth() / 2;
51             nonRotatedState.setX(nonRotatedState.getCenterX() - nonRotatedState.getHeight() / 2);
52             nonRotatedState.setY(tmp);
53
54
55             tmp = nonRotatedState.getWidth();
56             nonRotatedState.setWidth(nonRotatedState.getHeight());
57             nonRotatedState.setHeight(tmp);
58
59             Rectangle rect = nonRotatedState.getRectangle();
60             canvas.getGraphics().clipRect(rect.x, rect.y, rect.width, rect.height);
61             super.paintShape(canvas, nonRotatedState);
62         } else {
63             super.paintShape(canvas, state);
64         }
65
66         final String image = getImageForStyle(canvas, state);
67         if (image != null && image.endsWith(".svg")) {
68             // Remove the "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint"
69             // message and tweak Batik rendering options to increase performance.
70             canvas.getGraphics().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
71             canvas.getGraphics().setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
72             canvas.getGraphics().setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING, RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);
73
74             final Rectangle rect = getImageBounds(canvas, state);
75             canvas.getGraphics().translate(rect.x, rect.y);
76
77             ((ScilabCanvas) canvas).paintSvgForegroundImage(rect.width, rect.height, image);
78
79             if (mxUtils.isTrue(state.getStyle(), mxConstants.STYLE_GLASS, false)) {
80                 drawGlassEffect(canvas, state);
81             }
82         } else {
83             super.paintShape(canvas, state);
84         }
85     }
86
87     /**
88      * Get the image bounds for an SVG image
89      *
90      * @param canvas the current canvas
91      * @param state the current state
92      * @return the bounds of the image
93      * @see com.mxgraph.shape.mxImageShape#getImageBounds(com.mxgraph.canvas.mxGraphics2DCanvas, com.mxgraph.view.mxCellState)
94      */
95     @Override
96     public Rectangle getImageBounds(mxGraphics2DCanvas canvas, mxCellState state) {
97         final int rotation = (int) mxUtils.getDouble(state.getStyle(),
98                              mxConstants.STYLE_ROTATION, 0);
99         final AffineTransform transform = new AffineTransform();
100         transform.rotate(Math.toRadians(rotation), state.getCenterX(),
101                          state.getCenterY());
102         final Shape shape = transform.createTransformedShape(state
103                             .getRectangle());
104         return shape.getBounds();
105     }
106 }