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 | * Created on January 26, 2004, 9:00 AM
|
---|
35 | */
|
---|
36 | package com.kitfox.svg;
|
---|
37 |
|
---|
38 | import com.kitfox.svg.xml.StyleAttribute;
|
---|
39 | import java.awt.Graphics2D;
|
---|
40 | import java.awt.Shape;
|
---|
41 | import java.awt.geom.AffineTransform;
|
---|
42 | import java.awt.geom.Area;
|
---|
43 | import java.awt.geom.Point2D;
|
---|
44 | import java.awt.geom.Rectangle2D;
|
---|
45 | import java.net.URI;
|
---|
46 | import java.util.List;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Maintains bounding box for this element
|
---|
50 | *
|
---|
51 | * @author Mark McKay
|
---|
52 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
53 | */
|
---|
54 | abstract public class RenderableElement extends TransformableElement
|
---|
55 | {
|
---|
56 | AffineTransform cachedXform = null;
|
---|
57 |
|
---|
58 | Shape cachedClip = null;
|
---|
59 | public static final int VECTOR_EFFECT_NONE = 0;
|
---|
60 | public static final int VECTOR_EFFECT_NON_SCALING_STROKE = 1;
|
---|
61 | int vectorEffect;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Creates a new instance of BoundedElement
|
---|
65 | */
|
---|
66 | public RenderableElement()
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | public RenderableElement(String id, SVGElement parent)
|
---|
71 | {
|
---|
72 | super(id, parent);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | protected void build() throws SVGException
|
---|
77 | {
|
---|
78 | super.build();
|
---|
79 |
|
---|
80 | StyleAttribute sty = new StyleAttribute();
|
---|
81 |
|
---|
82 | if (getPres(sty.setName("vector-effect")))
|
---|
83 | {
|
---|
84 | if ("non-scaling-stroke".equals(sty.getStringValue()))
|
---|
85 | {
|
---|
86 | vectorEffect = VECTOR_EFFECT_NON_SCALING_STROKE;
|
---|
87 | } else
|
---|
88 | {
|
---|
89 | vectorEffect = VECTOR_EFFECT_NONE;
|
---|
90 | }
|
---|
91 | } else
|
---|
92 | {
|
---|
93 | vectorEffect = VECTOR_EFFECT_NONE;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | abstract public void render(Graphics2D g) throws SVGException;
|
---|
98 |
|
---|
99 | abstract void pick(Point2D point, boolean boundingBox, List<List<SVGElement>> retVec) throws SVGException;
|
---|
100 |
|
---|
101 | abstract void pick(Rectangle2D pickArea, AffineTransform ltw, boolean boundingBox, List<List<SVGElement>> retVec) throws SVGException;
|
---|
102 |
|
---|
103 | abstract public Rectangle2D getBoundingBox() throws SVGException;
|
---|
104 | /*
|
---|
105 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
---|
106 | {
|
---|
107 | super.loaderStartElement(helper, attrs, parent);
|
---|
108 | }
|
---|
109 | */
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Pushes transform stack, transforms to local coordinates and sets up
|
---|
113 | * clipping mask.
|
---|
114 | */
|
---|
115 | protected void beginLayer(Graphics2D g) throws SVGException
|
---|
116 | {
|
---|
117 | if (xform != null)
|
---|
118 | {
|
---|
119 | cachedXform = g.getTransform();
|
---|
120 | g.transform(xform);
|
---|
121 | }
|
---|
122 |
|
---|
123 | StyleAttribute styleAttrib = new StyleAttribute();
|
---|
124 |
|
---|
125 | //Get clipping path
|
---|
126 | // StyleAttribute styleAttrib = getStyle("clip-path", false);
|
---|
127 | Shape clipPath = null;
|
---|
128 | int clipPathUnits = ClipPath.CP_USER_SPACE_ON_USE;
|
---|
129 | if (getStyle(styleAttrib.setName("clip-path"), false)
|
---|
130 | && !"none".equals(styleAttrib.getStringValue()))
|
---|
131 | {
|
---|
132 | URI uri = styleAttrib.getURIValue(getXMLBase());
|
---|
133 | if (uri != null)
|
---|
134 | {
|
---|
135 | ClipPath ele = (ClipPath) diagram.getUniverse().getElement(uri);
|
---|
136 | clipPath = ele.getClipPathShape();
|
---|
137 | clipPathUnits = ele.getClipPathUnits();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | //Return if we're out of clipping range
|
---|
142 | if (clipPath != null)
|
---|
143 | {
|
---|
144 | if (clipPathUnits == ClipPath.CP_OBJECT_BOUNDING_BOX && (this instanceof ShapeElement))
|
---|
145 | {
|
---|
146 | Rectangle2D rect = ((ShapeElement) this).getBoundingBox();
|
---|
147 | AffineTransform at = new AffineTransform();
|
---|
148 | at.scale(rect.getWidth(), rect.getHeight());
|
---|
149 | clipPath = at.createTransformedShape(clipPath);
|
---|
150 | }
|
---|
151 |
|
---|
152 | cachedClip = g.getClip();
|
---|
153 | if (cachedClip == null)
|
---|
154 | {
|
---|
155 | g.setClip(clipPath);
|
---|
156 | } else
|
---|
157 | {
|
---|
158 | Area newClip = new Area(cachedClip);
|
---|
159 | newClip.intersect(new Area(clipPath));
|
---|
160 | g.setClip(newClip);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Restores transform and clipping values to the way they were before this
|
---|
167 | * layer was drawn.
|
---|
168 | */
|
---|
169 | protected void finishLayer(Graphics2D g)
|
---|
170 | {
|
---|
171 | if (cachedClip != null)
|
---|
172 | {
|
---|
173 | g.setClip(cachedClip);
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (cachedXform != null)
|
---|
177 | {
|
---|
178 | g.setTransform(cachedXform);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|