1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | */
|
---|
34 | package com.kitfox.svg;
|
---|
35 |
|
---|
36 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
37 | import java.awt.Graphics2D;
|
---|
38 | import java.awt.Rectangle;
|
---|
39 | import java.awt.Shape;
|
---|
40 | import java.awt.geom.AffineTransform;
|
---|
41 | import java.awt.geom.PathIterator;
|
---|
42 | import java.awt.geom.Rectangle2D;
|
---|
43 | import java.util.ArrayList;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | *
|
---|
47 | * @author kitfox
|
---|
48 | */
|
---|
49 | public class Marker extends Group
|
---|
50 | {
|
---|
51 | public static final String TAG_NAME = "marker";
|
---|
52 |
|
---|
53 | AffineTransform viewXform;
|
---|
54 | AffineTransform markerXform;
|
---|
55 | Rectangle2D viewBox;
|
---|
56 | float refX;
|
---|
57 | float refY;
|
---|
58 | float markerWidth = 1;
|
---|
59 | float markerHeight = 1;
|
---|
60 | float orient = Float.NaN;
|
---|
61 | boolean markerUnitsStrokeWidth = true; //if set to false 'userSpaceOnUse' is assumed
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public String getTagName()
|
---|
65 | {
|
---|
66 | return TAG_NAME;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | protected void build() throws SVGException
|
---|
71 | {
|
---|
72 | super.build();
|
---|
73 |
|
---|
74 | StyleAttribute sty = new StyleAttribute();
|
---|
75 |
|
---|
76 | if (getPres(sty.setName("refX")))
|
---|
77 | {
|
---|
78 | refX = sty.getFloatValueWithUnits();
|
---|
79 | }
|
---|
80 | if (getPres(sty.setName("refY")))
|
---|
81 | {
|
---|
82 | refY = sty.getFloatValueWithUnits();
|
---|
83 | }
|
---|
84 | if (getPres(sty.setName("markerWidth")))
|
---|
85 | {
|
---|
86 | markerWidth = sty.getFloatValueWithUnits();
|
---|
87 | }
|
---|
88 | if (getPres(sty.setName("markerHeight")))
|
---|
89 | {
|
---|
90 | markerHeight = sty.getFloatValueWithUnits();
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (getPres(sty.setName("orient")))
|
---|
94 | {
|
---|
95 | if ("auto".equals(sty.getStringValue()))
|
---|
96 | {
|
---|
97 | orient = Float.NaN;
|
---|
98 | } else
|
---|
99 | {
|
---|
100 | orient = sty.getFloatValue();
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (getPres(sty.setName("viewBox")))
|
---|
105 | {
|
---|
106 | float[] dim = sty.getFloatList();
|
---|
107 | viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]);
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (viewBox == null)
|
---|
111 | {
|
---|
112 | viewBox = new Rectangle(0, 0, 1, 1);
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (getPres(sty.setName("markerUnits")))
|
---|
116 | {
|
---|
117 | String markerUnits = sty.getStringValue();
|
---|
118 | if (markerUnits != null && markerUnits.equals("userSpaceOnUse"))
|
---|
119 | {
|
---|
120 | markerUnitsStrokeWidth = false;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | //Transform pattern onto unit square
|
---|
125 | viewXform = new AffineTransform();
|
---|
126 | viewXform.scale(1.0 / viewBox.getWidth(), 1.0 / viewBox.getHeight());
|
---|
127 | viewXform.translate(-viewBox.getX(), -viewBox.getY());
|
---|
128 |
|
---|
129 | markerXform = new AffineTransform();
|
---|
130 | markerXform.scale(markerWidth, markerHeight);
|
---|
131 | markerXform.concatenate(viewXform);
|
---|
132 | markerXform.translate(-refX, -refY);
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | protected boolean outsideClip(Graphics2D g) throws SVGException
|
---|
137 | {
|
---|
138 | Shape clip = g.getClip();
|
---|
139 | Rectangle2D rect = super.getBoundingBox();
|
---|
140 | if (clip == null || clip.intersects(rect))
|
---|
141 | {
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return true;
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void render(Graphics2D g) throws SVGException
|
---|
151 | {
|
---|
152 | AffineTransform oldXform = g.getTransform();
|
---|
153 | g.transform(markerXform);
|
---|
154 |
|
---|
155 | super.render(g);
|
---|
156 |
|
---|
157 | g.setTransform(oldXform);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void render(Graphics2D g, MarkerPos pos, float strokeWidth) throws SVGException
|
---|
161 | {
|
---|
162 | AffineTransform cacheXform = g.getTransform();
|
---|
163 |
|
---|
164 | g.translate(pos.x, pos.y);
|
---|
165 | if (markerUnitsStrokeWidth)
|
---|
166 | {
|
---|
167 | g.scale(strokeWidth, strokeWidth);
|
---|
168 | }
|
---|
169 |
|
---|
170 | g.rotate(Math.atan2(pos.dy, pos.dx));
|
---|
171 |
|
---|
172 | g.transform(markerXform);
|
---|
173 |
|
---|
174 | super.render(g);
|
---|
175 |
|
---|
176 | g.setTransform(cacheXform);
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Override
|
---|
180 | public Shape getShape()
|
---|
181 | {
|
---|
182 | Shape shape = super.getShape();
|
---|
183 | return markerXform.createTransformedShape(shape);
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public Rectangle2D getBoundingBox() throws SVGException
|
---|
188 | {
|
---|
189 | Rectangle2D rect = super.getBoundingBox();
|
---|
190 | return markerXform.createTransformedShape(rect).getBounds2D();
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Updates all attributes in this diagram associated with a time event. Ie,
|
---|
195 | * all attributes with track information.
|
---|
196 | *
|
---|
197 | * @return - true if this node has changed state as a result of the time
|
---|
198 | * update
|
---|
199 | */
|
---|
200 | @Override
|
---|
201 | public boolean updateTime(double curTime) throws SVGException
|
---|
202 | {
|
---|
203 | boolean changeState = super.updateTime(curTime);
|
---|
204 |
|
---|
205 | build();
|
---|
206 |
|
---|
207 | //Marker properties do not change
|
---|
208 | return changeState;
|
---|
209 | }
|
---|
210 |
|
---|
211 | //--------------------------------
|
---|
212 | public static final int MARKER_START = 0;
|
---|
213 | public static final int MARKER_MID = 1;
|
---|
214 | public static final int MARKER_END = 2;
|
---|
215 |
|
---|
216 | public static class MarkerPos
|
---|
217 | {
|
---|
218 |
|
---|
219 | int type;
|
---|
220 | double x;
|
---|
221 | double y;
|
---|
222 | double dx;
|
---|
223 | double dy;
|
---|
224 |
|
---|
225 | public MarkerPos(int type, double x, double y, double dx, double dy)
|
---|
226 | {
|
---|
227 | this.type = type;
|
---|
228 | this.x = x;
|
---|
229 | this.y = y;
|
---|
230 | this.dx = dx;
|
---|
231 | this.dy = dy;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | public static class MarkerLayout
|
---|
236 | {
|
---|
237 |
|
---|
238 | private ArrayList<MarkerPos> markerList = new ArrayList<MarkerPos>();
|
---|
239 | boolean started = false;
|
---|
240 |
|
---|
241 | public void layout(Shape shape)
|
---|
242 | {
|
---|
243 | double px = 0;
|
---|
244 | double py = 0;
|
---|
245 | double[] coords = new double[6];
|
---|
246 | for (PathIterator it = shape.getPathIterator(null);
|
---|
247 | !it.isDone(); it.next())
|
---|
248 | {
|
---|
249 | switch (it.currentSegment(coords))
|
---|
250 | {
|
---|
251 | case PathIterator.SEG_MOVETO:
|
---|
252 | px = coords[0];
|
---|
253 | py = coords[1];
|
---|
254 | started = false;
|
---|
255 | break;
|
---|
256 | case PathIterator.SEG_CLOSE:
|
---|
257 | started = false;
|
---|
258 | break;
|
---|
259 | case PathIterator.SEG_LINETO:
|
---|
260 | {
|
---|
261 | double x = coords[0];
|
---|
262 | double y = coords[1];
|
---|
263 | markerIn(px, py, x - px, y - py);
|
---|
264 | markerOut(x, y, x - px, y - py);
|
---|
265 | px = x;
|
---|
266 | py = y;
|
---|
267 | break;
|
---|
268 | }
|
---|
269 | case PathIterator.SEG_QUADTO:
|
---|
270 | {
|
---|
271 | double k0x = coords[0];
|
---|
272 | double k0y = coords[1];
|
---|
273 | double x = coords[2];
|
---|
274 | double y = coords[3];
|
---|
275 |
|
---|
276 |
|
---|
277 | //Best in tangent
|
---|
278 | if (px != k0x || py != k0y)
|
---|
279 | {
|
---|
280 | markerIn(px, py, k0x - px, k0y - py);
|
---|
281 | } else
|
---|
282 | {
|
---|
283 | markerIn(px, py, x - px, y - py);
|
---|
284 | }
|
---|
285 |
|
---|
286 | //Best out tangent
|
---|
287 | if (x != k0x || y != k0y)
|
---|
288 | {
|
---|
289 | markerOut(x, y, x - k0x, y - k0y);
|
---|
290 | } else
|
---|
291 | {
|
---|
292 | markerOut(x, y, x - px, y - py);
|
---|
293 | }
|
---|
294 |
|
---|
295 | markerIn(px, py, k0x - px, k0y - py);
|
---|
296 | markerOut(x, y, x - k0x, y - k0y);
|
---|
297 | px = x;
|
---|
298 | py = y;
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | case PathIterator.SEG_CUBICTO:
|
---|
302 | {
|
---|
303 | double k0x = coords[0];
|
---|
304 | double k0y = coords[1];
|
---|
305 | double k1x = coords[2];
|
---|
306 | double k1y = coords[3];
|
---|
307 | double x = coords[4];
|
---|
308 | double y = coords[5];
|
---|
309 |
|
---|
310 | //Best in tangent
|
---|
311 | if (px != k0x || py != k0y)
|
---|
312 | {
|
---|
313 | markerIn(px, py, k0x - px, k0y - py);
|
---|
314 | } else if (px != k1x || py != k1y)
|
---|
315 | {
|
---|
316 | markerIn(px, py, k1x - px, k1y - py);
|
---|
317 | } else
|
---|
318 | {
|
---|
319 | markerIn(px, py, x - px, y - py);
|
---|
320 | }
|
---|
321 |
|
---|
322 | //Best out tangent
|
---|
323 | if (x != k1x || y != k1y)
|
---|
324 | {
|
---|
325 | markerOut(x, y, x - k1x, y - k1y);
|
---|
326 | } else if (x != k0x || y != k0y)
|
---|
327 | {
|
---|
328 | markerOut(x, y, x - k0x, y - k0y);
|
---|
329 | } else
|
---|
330 | {
|
---|
331 | markerOut(x, y, x - px, y - py);
|
---|
332 | }
|
---|
333 | px = x;
|
---|
334 | py = y;
|
---|
335 | break;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | for (int i = 1; i < markerList.size(); ++i)
|
---|
341 | {
|
---|
342 | MarkerPos prev = (MarkerPos) markerList.get(i - 1);
|
---|
343 | MarkerPos cur = (MarkerPos) markerList.get(i);
|
---|
344 |
|
---|
345 | if (cur.type == MARKER_START)
|
---|
346 | {
|
---|
347 | prev.type = MARKER_END;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | MarkerPos last = (MarkerPos) markerList.get(markerList.size() - 1);
|
---|
351 | last.type = MARKER_END;
|
---|
352 | }
|
---|
353 |
|
---|
354 | private void markerIn(double x, double y, double dx, double dy)
|
---|
355 | {
|
---|
356 | if (started == false)
|
---|
357 | {
|
---|
358 | started = true;
|
---|
359 | markerList.add(new MarkerPos(MARKER_START, x, y, dx, dy));
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | private void markerOut(double x, double y, double dx, double dy)
|
---|
364 | {
|
---|
365 | markerList.add(new MarkerPos(MARKER_MID, x, y, dx, dy));
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * @return the markerList
|
---|
370 | */
|
---|
371 | public ArrayList<MarkerPos> getMarkerList()
|
---|
372 | {
|
---|
373 | return markerList;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|