2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2010 - DIGITEO - Clement DAVID <clement.david@scilab.org>
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
16 package org.scilab.modules.graph.shape;
18 import java.awt.Rectangle;
19 import java.awt.RenderingHints;
20 import java.awt.Shape;
21 import java.awt.geom.AffineTransform;
23 import org.apache.batik.ext.awt.RenderingHintsKeyExt;
24 import org.scilab.modules.graph.ScilabCanvas;
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;
33 * Add the SVG capability to the standard Image shape
35 public class SvgShape extends mxLabelShape {
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)
44 public void paintShape(mxGraphics2DCanvas canvas, mxCellState state) {
45 // paint previously set background without the rotation applied
46 if (mxUtils.getDouble(state.getStyle(), mxConstants.STYLE_ROTATION, 0) != 0) {
47 mxCellState nonRotatedState = (mxCellState) state.clone();
49 double tmp = nonRotatedState.getCenterY() - nonRotatedState.getWidth() / 2;
50 nonRotatedState.setX(nonRotatedState.getCenterX() - nonRotatedState.getHeight() / 2);
51 nonRotatedState.setY(tmp);
54 tmp = nonRotatedState.getWidth();
55 nonRotatedState.setWidth(nonRotatedState.getHeight());
56 nonRotatedState.setHeight(tmp);
58 Rectangle rect = nonRotatedState.getRectangle();
59 canvas.getGraphics().clipRect(rect.x, rect.y, rect.width, rect.height);
60 super.paintShape(canvas, nonRotatedState);
62 super.paintShape(canvas, state);
65 final String image = getImageForStyle(canvas, state);
66 if (image != null && image.endsWith(".svg")) {
67 // Remove the "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint"
68 // message and tweak Batik rendering options to increase performance.
69 canvas.getGraphics().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
70 canvas.getGraphics().setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
71 canvas.getGraphics().setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING, RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);
73 final Rectangle rect = getImageBounds(canvas, state);
74 canvas.getGraphics().translate(rect.x, rect.y);
76 ((ScilabCanvas) canvas).paintSvgForegroundImage(rect.width, rect.height, image);
78 if (mxUtils.isTrue(state.getStyle(), mxConstants.STYLE_GLASS, false)) {
79 drawGlassEffect(canvas, state);
82 super.paintShape(canvas, state);
87 * Get the image bounds for an SVG image
89 * @param canvas the current canvas
90 * @param state the current state
91 * @return the bounds of the image
92 * @see com.mxgraph.shape.mxImageShape#getImageBounds(com.mxgraph.canvas.mxGraphics2DCanvas, com.mxgraph.view.mxCellState)
95 public Rectangle getImageBounds(mxGraphics2DCanvas canvas, mxCellState state) {
96 final int rotation = (int) mxUtils.getDouble(state.getStyle(),
97 mxConstants.STYLE_ROTATION, 0);
98 final AffineTransform transform = new AffineTransform();
99 transform.rotate(Math.toRadians(rotation), state.getCenterX(),
101 final Shape shape = transform.createTransformedShape(state
103 return shape.getBounds();