1 | /*
|
---|
2 | * ShapeElement.java
|
---|
3 | *
|
---|
4 | *
|
---|
5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
---|
6 | * Copyright (C) 2004 Mark McKay
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Lesser General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2.1 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Lesser General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public
|
---|
19 | * License along with this library; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
21 | *
|
---|
22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
23 | * projects can be found at http://www.kitfox.com
|
---|
24 | *
|
---|
25 | * Created on January 26, 2004, 5:21 PM
|
---|
26 | */
|
---|
27 |
|
---|
28 | package com.kitfox.svg;
|
---|
29 |
|
---|
30 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
31 | import java.awt.AlphaComposite;
|
---|
32 | import java.awt.BasicStroke;
|
---|
33 | import java.awt.Color;
|
---|
34 | import java.awt.Composite;
|
---|
35 | import java.awt.Graphics2D;
|
---|
36 | import java.awt.Paint;
|
---|
37 | import java.awt.Shape;
|
---|
38 | import java.awt.geom.AffineTransform;
|
---|
39 | import java.awt.geom.Point2D;
|
---|
40 | import java.awt.geom.Rectangle2D;
|
---|
41 | import java.net.URI;
|
---|
42 | import java.util.List;
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Parent of shape objects
|
---|
48 | *
|
---|
49 | * @author Mark McKay
|
---|
50 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
51 | */
|
---|
52 | abstract public class ShapeElement extends RenderableElement
|
---|
53 | {
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * This is necessary to get text elements to render the stroke the correct
|
---|
57 | * width. It is an alternative to producing new font glyph sets at different
|
---|
58 | * sizes.
|
---|
59 | */
|
---|
60 | protected float strokeWidthScalar = 1f;
|
---|
61 |
|
---|
62 | /** Creates a new instance of ShapeElement */
|
---|
63 | public ShapeElement() {
|
---|
64 | }
|
---|
65 |
|
---|
66 | abstract public void render(java.awt.Graphics2D g) throws SVGException;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | protected void setStrokeWidthScalar(float strokeWidthScalar)
|
---|
70 | {
|
---|
71 | this.strokeWidthScalar = strokeWidthScalar;
|
---|
72 | }
|
---|
73 | */
|
---|
74 |
|
---|
75 | void pick(Point2D point, boolean boundingBox, List retVec) throws SVGException
|
---|
76 | {
|
---|
77 | StyleAttribute styleAttrib = new StyleAttribute();
|
---|
78 | // if (getStyle(styleAttrib.setName("fill")) && getShape().contains(point))
|
---|
79 | if ((boundingBox ? getBoundingBox() : getShape()).contains(point))
|
---|
80 | {
|
---|
81 | retVec.add(getPath(null));
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | void pick(Rectangle2D pickArea, AffineTransform ltw, boolean boundingBox, List retVec) throws SVGException
|
---|
86 | {
|
---|
87 | StyleAttribute styleAttrib = new StyleAttribute();
|
---|
88 | // if (getStyle(styleAttrib.setName("fill")) && getShape().contains(point))
|
---|
89 | if (ltw.createTransformedShape((boundingBox ? getBoundingBox() : getShape())).intersects(pickArea))
|
---|
90 | {
|
---|
91 | retVec.add(getPath(null));
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected void renderShape(Graphics2D g, Shape shape) throws SVGException
|
---|
96 | {
|
---|
97 | //g.setColor(Color.green);
|
---|
98 |
|
---|
99 | StyleAttribute styleAttrib = new StyleAttribute();
|
---|
100 |
|
---|
101 | //Don't process if not visible
|
---|
102 | if (getStyle(styleAttrib.setName("visibility")))
|
---|
103 | {
|
---|
104 | if (!styleAttrib.getStringValue().equals("visible")) return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (getStyle(styleAttrib.setName("display")))
|
---|
108 | {
|
---|
109 | if (styleAttrib.getStringValue().equals("none")) return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | //None, solid color, gradient, pattern
|
---|
113 | Paint fillPaint = Color.black; //Default to black. Must be explicitly set to none for no fill.
|
---|
114 | if (getStyle(styleAttrib.setName("fill")))
|
---|
115 | {
|
---|
116 | if (styleAttrib.getStringValue().equals("none")) fillPaint = null;
|
---|
117 | else
|
---|
118 | {
|
---|
119 | fillPaint = styleAttrib.getColorValue();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | //Default opacity
|
---|
124 | float opacity = 1f;
|
---|
125 | if (getStyle(styleAttrib.setName("opacity")))
|
---|
126 | {
|
---|
127 | opacity = styleAttrib.getRatioValue();
|
---|
128 | }
|
---|
129 |
|
---|
130 | float fillOpacity = opacity;
|
---|
131 | if (getStyle(styleAttrib.setName("fill-opacity")))
|
---|
132 | {
|
---|
133 | fillOpacity *= styleAttrib.getRatioValue();
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | Paint strokePaint = null; //Default is to stroke with none
|
---|
138 | if (getStyle(styleAttrib.setName("stroke")))
|
---|
139 | {
|
---|
140 | if (styleAttrib.getStringValue().equals("none")) strokePaint = null;
|
---|
141 | else
|
---|
142 | {
|
---|
143 | strokePaint = styleAttrib.getColorValue();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | float[] strokeDashArray = null;
|
---|
148 | if (getStyle(styleAttrib.setName("stroke-dasharray")))
|
---|
149 | {
|
---|
150 | strokeDashArray = styleAttrib.getFloatList();
|
---|
151 | if (strokeDashArray.length == 0) strokeDashArray = null;
|
---|
152 | }
|
---|
153 |
|
---|
154 | float strokeDashOffset = 0f;
|
---|
155 | if (getStyle(styleAttrib.setName("stroke-dashoffset")))
|
---|
156 | {
|
---|
157 | strokeDashOffset = styleAttrib.getFloatValueWithUnits();
|
---|
158 | }
|
---|
159 |
|
---|
160 | int strokeLinecap = BasicStroke.CAP_BUTT;
|
---|
161 | if (getStyle(styleAttrib.setName("stroke-linecap")))
|
---|
162 | {
|
---|
163 | String val = styleAttrib.getStringValue();
|
---|
164 | if (val.equals("round"))
|
---|
165 | {
|
---|
166 | strokeLinecap = BasicStroke.CAP_ROUND;
|
---|
167 | }
|
---|
168 | else if (val.equals("square"))
|
---|
169 | {
|
---|
170 | strokeLinecap = BasicStroke.CAP_SQUARE;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | int strokeLinejoin = BasicStroke.JOIN_MITER;
|
---|
175 | if (getStyle(styleAttrib.setName("stroke-linejoin")))
|
---|
176 | {
|
---|
177 | String val = styleAttrib.getStringValue();
|
---|
178 | if (val.equals("round"))
|
---|
179 | {
|
---|
180 | strokeLinejoin = BasicStroke.JOIN_ROUND;
|
---|
181 | }
|
---|
182 | else if (val.equals("bevel"))
|
---|
183 | {
|
---|
184 | strokeLinejoin = BasicStroke.JOIN_BEVEL;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | float strokeMiterLimit = 4f;
|
---|
189 | if (getStyle(styleAttrib.setName("stroke-miterlimit")))
|
---|
190 | {
|
---|
191 | strokeMiterLimit = Math.max(styleAttrib.getFloatValueWithUnits(), 1);
|
---|
192 | }
|
---|
193 |
|
---|
194 | float strokeOpacity = opacity;
|
---|
195 | if (getStyle(styleAttrib.setName("stroke-opacity")))
|
---|
196 | {
|
---|
197 | strokeOpacity *= styleAttrib.getRatioValue();
|
---|
198 | }
|
---|
199 |
|
---|
200 | float strokeWidth = 1f;
|
---|
201 | if (getStyle(styleAttrib.setName("stroke-width")))
|
---|
202 | {
|
---|
203 | strokeWidth = styleAttrib.getFloatValueWithUnits();
|
---|
204 | }
|
---|
205 | // if (strokeWidthScalar != 1f)
|
---|
206 | // {
|
---|
207 | strokeWidth *= strokeWidthScalar;
|
---|
208 | // }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 | //Draw the shape
|
---|
213 | if (fillPaint != null && fillOpacity != 0f)
|
---|
214 | {
|
---|
215 | if (fillOpacity <= 0)
|
---|
216 | {
|
---|
217 | //Do nothing
|
---|
218 | }
|
---|
219 | else if (fillOpacity < 1f)
|
---|
220 | {
|
---|
221 | Composite cachedComposite = g.getComposite();
|
---|
222 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillOpacity));
|
---|
223 |
|
---|
224 | g.setPaint(fillPaint);
|
---|
225 | g.fill(shape);
|
---|
226 |
|
---|
227 | g.setComposite(cachedComposite);
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | g.setPaint(fillPaint);
|
---|
232 | g.fill(shape);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | if (strokePaint != null && strokeOpacity != 0f)
|
---|
238 | {
|
---|
239 | BasicStroke stroke;
|
---|
240 | if (strokeDashArray == null)
|
---|
241 | {
|
---|
242 | stroke = new BasicStroke(strokeWidth, strokeLinecap, strokeLinejoin, strokeMiterLimit);
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | stroke = new BasicStroke(strokeWidth, strokeLinecap, strokeLinejoin, strokeMiterLimit, strokeDashArray, strokeDashOffset);
|
---|
247 | }
|
---|
248 |
|
---|
249 | Shape strokeShape = stroke.createStrokedShape(shape);
|
---|
250 |
|
---|
251 | if (strokeOpacity <= 0)
|
---|
252 | {
|
---|
253 | //Do nothing
|
---|
254 | }
|
---|
255 | else if (strokeOpacity < 1f)
|
---|
256 | {
|
---|
257 | Composite cachedComposite = g.getComposite();
|
---|
258 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, strokeOpacity));
|
---|
259 |
|
---|
260 | g.setPaint(strokePaint);
|
---|
261 | g.fill(strokeShape);
|
---|
262 |
|
---|
263 | g.setComposite(cachedComposite);
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | g.setPaint(strokePaint);
|
---|
268 | g.fill(strokeShape);
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 | abstract public Shape getShape();
|
---|
276 |
|
---|
277 | protected Rectangle2D includeStrokeInBounds(Rectangle2D rect) throws SVGException
|
---|
278 | {
|
---|
279 | StyleAttribute styleAttrib = new StyleAttribute();
|
---|
280 | if (!getStyle(styleAttrib.setName("stroke"))) return rect;
|
---|
281 |
|
---|
282 | double strokeWidth = 1;
|
---|
283 | if (getStyle(styleAttrib.setName("stroke-width"))) strokeWidth = styleAttrib.getDoubleValue();
|
---|
284 |
|
---|
285 | rect.setRect(
|
---|
286 | rect.getX() - strokeWidth / 2,
|
---|
287 | rect.getY() - strokeWidth / 2,
|
---|
288 | rect.getWidth() + strokeWidth,
|
---|
289 | rect.getHeight() + strokeWidth);
|
---|
290 |
|
---|
291 | return rect;
|
---|
292 | }
|
---|
293 |
|
---|
294 | }
|
---|