b24dd102200fbc980374b04f3cebdb185948292b
[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
46         super.paintShape(canvas, state);
47
48         final String image = getImageForStyle(canvas, state);
49         if (image != null && image.endsWith(".svg")) {
50             // Remove the "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint"
51             // message and tweak Batik rendering options to increase performance.
52             canvas.getGraphics().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
53             canvas.getGraphics().setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
54             canvas.getGraphics().setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING, RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);
55
56             final Rectangle rect = getImageBounds(canvas, state);
57             canvas.getGraphics().translate(rect.x, rect.y);
58
59             ((ScilabCanvas) canvas).paintSvgForegroundImage(rect.width, rect.height, image);
60
61             if (mxUtils.isTrue(state.getStyle(), mxConstants.STYLE_GLASS, false)) {
62                 drawGlassEffect(canvas, state);
63             }
64         } else {
65             super.paintShape(canvas, state);
66         }
67     }
68
69     /**
70      * Get the image bounds for an SVG image
71      *
72      * @param canvas the current canvas
73      * @param state the current state
74      * @return the bounds of the image
75      * @see com.mxgraph.shape.mxImageShape#getImageBounds(com.mxgraph.canvas.mxGraphics2DCanvas, com.mxgraph.view.mxCellState)
76      */
77     @Override
78     public Rectangle getImageBounds(mxGraphics2DCanvas canvas, mxCellState state) {
79         final double rotation = mxUtils.getDouble(state.getStyle(),
80                                 mxConstants.STYLE_ROTATION, 0);
81         final AffineTransform transform = new AffineTransform();
82         transform.rotate(Math.toRadians(rotation), state.getCenterX(),
83                          state.getCenterY());
84         final Shape shape = transform.createTransformedShape(state
85                             .getRectangle());
86         return shape.getBounds();
87     }
88 }