source: josm/trunk/src/com/kitfox/svg/Gradient.java@ 10739

Last change on this file since 10739 was 8084, checked in by bastiK, 9 years ago

add svn:eol-style=native for svgsalamander

  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
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, 3:25 AM
35 */
36package com.kitfox.svg;
37
38import com.kitfox.svg.xml.StyleAttribute;
39import java.awt.Color;
40import java.awt.geom.AffineTransform;
41import java.net.URI;
42import java.util.ArrayList;
43import java.util.Iterator;
44import java.util.logging.Level;
45import java.util.logging.Logger;
46
47/**
48 * @author Mark McKay
49 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
50 */
51abstract public class Gradient extends FillElement
52{
53 public static final String TAG_NAME = "gradient";
54
55 public static final int SM_PAD = 0;
56 public static final int SM_REPEAT = 1;
57 public static final int SM_REFLECT = 2;
58 int spreadMethod = SM_PAD;
59 public static final int GU_OBJECT_BOUNDING_BOX = 0;
60 public static final int GU_USER_SPACE_ON_USE = 1;
61 protected int gradientUnits = GU_OBJECT_BOUNDING_BOX;
62 //Either this gradient contains a list of stops, or it will take it's
63 // stops from the referenced gradient
64 ArrayList stops = new ArrayList();
65 URI stopRef = null;
66 protected AffineTransform gradientTransform = null;
67
68 //Cache arrays of stop values here
69 float[] stopFractions;
70 Color[] stopColors;
71
72 /**
73 * Creates a new instance of Gradient
74 */
75 public Gradient()
76 {
77 }
78
79 public String getTagName()
80 {
81 return TAG_NAME;
82 }
83
84 /**
85 * Called after the start element but before the end element to indicate
86 * each child tag that has been processed
87 */
88 public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
89 {
90 super.loaderAddChild(helper, child);
91
92 if (!(child instanceof Stop))
93 {
94 return;
95 }
96 appendStop((Stop) child);
97 }
98
99 protected void build() throws SVGException
100 {
101 super.build();
102
103 StyleAttribute sty = new StyleAttribute();
104 String strn;
105
106 if (getPres(sty.setName("spreadMethod")))
107 {
108 strn = sty.getStringValue().toLowerCase();
109 if (strn.equals("repeat"))
110 {
111 spreadMethod = SM_REPEAT;
112 } else if (strn.equals("reflect"))
113 {
114 spreadMethod = SM_REFLECT;
115 } else
116 {
117 spreadMethod = SM_PAD;
118 }
119 }
120
121 if (getPres(sty.setName("gradientUnits")))
122 {
123 strn = sty.getStringValue().toLowerCase();
124 if (strn.equals("userspaceonuse"))
125 {
126 gradientUnits = GU_USER_SPACE_ON_USE;
127 } else
128 {
129 gradientUnits = GU_OBJECT_BOUNDING_BOX;
130 }
131 }
132
133 if (getPres(sty.setName("gradientTransform")))
134 {
135 gradientTransform = parseTransform(sty.getStringValue());
136 }
137 //If we still don't have one, set it to identity
138 if (gradientTransform == null)
139 {
140 gradientTransform = new AffineTransform();
141 }
142
143
144 //Check to see if we're using our own stops or referencing someone else's
145 if (getPres(sty.setName("xlink:href")))
146 {
147 try
148 {
149 stopRef = sty.getURIValue(getXMLBase());
150//System.err.println("Gradient: " + sty.getStringValue() + ", " + getXMLBase() + ", " + src);
151// URI src = getXMLBase().resolve(href);
152// stopRef = (Gradient)diagram.getUniverse().getElement(src);
153 } catch (Exception e)
154 {
155 throw new SVGException("Could not resolve relative URL in Gradient: " + sty.getStringValue() + ", " + getXMLBase(), e);
156 }
157 }
158 }
159
160 public float[] getStopFractions()
161 {
162 if (stopRef != null)
163 {
164 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
165 return grad.getStopFractions();
166 }
167
168 if (stopFractions != null)
169 {
170 return stopFractions;
171 }
172
173 stopFractions = new float[stops.size()];
174 int idx = 0;
175 for (Iterator it = stops.iterator(); it.hasNext();)
176 {
177 Stop stop = (Stop) it.next();
178 float val = stop.offset;
179 if (idx != 0 && val < stopFractions[idx - 1])
180 {
181 val = stopFractions[idx - 1];
182 }
183 stopFractions[idx++] = val;
184 }
185
186 return stopFractions;
187 }
188
189 public Color[] getStopColors()
190 {
191 if (stopRef != null)
192 {
193 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
194 return grad.getStopColors();
195 }
196
197 if (stopColors != null)
198 {
199 return stopColors;
200 }
201
202 stopColors = new Color[stops.size()];
203 int idx = 0;
204 for (Iterator it = stops.iterator(); it.hasNext();)
205 {
206 Stop stop = (Stop) it.next();
207 int stopColorVal = stop.color.getRGB();
208 Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255));
209 stopColors[idx++] = stopColor;
210 }
211
212 return stopColors;
213 }
214
215 public void setStops(Color[] colors, float[] fractions)
216 {
217 if (colors.length != fractions.length)
218 {
219 throw new IllegalArgumentException();
220 }
221
222 this.stopColors = colors;
223 this.stopFractions = fractions;
224 stopRef = null;
225 }
226
227 private int clamp(int val, int min, int max)
228 {
229 if (val < min)
230 {
231 return min;
232 }
233 if (val > max)
234 {
235 return max;
236 }
237 return val;
238 }
239
240 public void setStopRef(URI grad)
241 {
242 stopRef = grad;
243 }
244
245 public void appendStop(Stop stop)
246 {
247 stops.add(stop);
248 }
249
250 /**
251 * Updates all attributes in this diagram associated with a time event. Ie,
252 * all attributes with track information.
253 *
254 * @return - true if this node has changed state as a result of the time
255 * update
256 */
257 public boolean updateTime(double curTime) throws SVGException
258 {
259// if (trackManager.getNumTracks() == 0) return false;
260 boolean stateChange = false;
261
262 //Get current values for parameters
263 StyleAttribute sty = new StyleAttribute();
264 boolean shapeChange = false;
265 String strn;
266
267
268 if (getPres(sty.setName("spreadMethod")))
269 {
270 int newVal;
271 strn = sty.getStringValue().toLowerCase();
272 if (strn.equals("repeat"))
273 {
274 newVal = SM_REPEAT;
275 } else if (strn.equals("reflect"))
276 {
277 newVal = SM_REFLECT;
278 } else
279 {
280 newVal = SM_PAD;
281 }
282 if (spreadMethod != newVal)
283 {
284 spreadMethod = newVal;
285 stateChange = true;
286 }
287 }
288
289 if (getPres(sty.setName("gradientUnits")))
290 {
291 int newVal;
292 strn = sty.getStringValue().toLowerCase();
293 if (strn.equals("userspaceonuse"))
294 {
295 newVal = GU_USER_SPACE_ON_USE;
296 } else
297 {
298 newVal = GU_OBJECT_BOUNDING_BOX;
299 }
300 if (newVal != gradientUnits)
301 {
302 gradientUnits = newVal;
303 stateChange = true;
304 }
305 }
306
307 if (getPres(sty.setName("gradientTransform")))
308 {
309 AffineTransform newVal = parseTransform(sty.getStringValue());
310 if (newVal != null && newVal.equals(gradientTransform))
311 {
312 gradientTransform = newVal;
313 stateChange = true;
314 }
315 }
316
317
318 //Check to see if we're using our own stops or referencing someone else's
319 if (getPres(sty.setName("xlink:href")))
320 {
321 try
322 {
323 URI newVal = sty.getURIValue(getXMLBase());
324 if ((newVal == null && stopRef != null) || !newVal.equals(stopRef))
325 {
326 stopRef = newVal;
327 stateChange = true;
328 }
329 } catch (Exception e)
330 {
331 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
332 "Could not parse xlink:href", e);
333 }
334 }
335
336 //Check stops, if any
337 for (Iterator it = stops.iterator(); it.hasNext();)
338 {
339 Stop stop = (Stop) it.next();
340 if (stop.updateTime(curTime))
341 {
342 stateChange = true;
343 stopFractions = null;
344 stopColors = null;
345 }
346 }
347
348 return stateChange;
349 }
350}
Note: See TracBrowser for help on using the repository browser.