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

Last change on this file since 8083 was 6002, checked in by Don-vip, 11 years ago

fix #8742 - update svgsalamander to release 0.1.18+patch (fix bug SVGSALAMANDER-26) -> allow to open more SVG files

File size: 10.4 KB
RevLine 
[4256]1/*
[6002]2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
[4256]5 *
[6002]6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
[4256]9 *
[6002]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.
[4256]17 *
[6002]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
[4256]33 *
34 * Created on January 26, 2004, 3:25 AM
35 */
36package com.kitfox.svg;
37
[6002]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;
[4256]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{
[6002]53 public static final String TAG_NAME = "gradient";
54
[4256]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;
[6002]67
[4256]68 //Cache arrays of stop values here
69 float[] stopFractions;
70 Color[] stopColors;
71
[6002]72 /**
73 * Creates a new instance of Gradient
74 */
75 public Gradient()
76 {
[4256]77 }
[6002]78
79 public String getTagName()
80 {
81 return TAG_NAME;
82 }
83
[4256]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
[6002]92 if (!(child instanceof Stop))
93 {
94 return;
95 }
96 appendStop((Stop) child);
[4256]97 }
98
99 protected void build() throws SVGException
100 {
101 super.build();
[6002]102
[4256]103 StyleAttribute sty = new StyleAttribute();
104 String strn;
[6002]105
[4256]106 if (getPres(sty.setName("spreadMethod")))
107 {
108 strn = sty.getStringValue().toLowerCase();
[6002]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 }
[4256]119 }
120
121 if (getPres(sty.setName("gradientUnits")))
122 {
123 strn = sty.getStringValue().toLowerCase();
[6002]124 if (strn.equals("userspaceonuse"))
125 {
126 gradientUnits = GU_USER_SPACE_ON_USE;
127 } else
128 {
129 gradientUnits = GU_OBJECT_BOUNDING_BOX;
130 }
[4256]131 }
132
[6002]133 if (getPres(sty.setName("gradientTransform")))
134 {
135 gradientTransform = parseTransform(sty.getStringValue());
136 }
[4256]137 //If we still don't have one, set it to identity
[6002]138 if (gradientTransform == null)
139 {
140 gradientTransform = new AffineTransform();
141 }
[4256]142
[6002]143
[4256]144 //Check to see if we're using our own stops or referencing someone else's
145 if (getPres(sty.setName("xlink:href")))
146 {
[6002]147 try
148 {
[4256]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);
[6002]153 } catch (Exception e)
[4256]154 {
155 throw new SVGException("Could not resolve relative URL in Gradient: " + sty.getStringValue() + ", " + getXMLBase(), e);
156 }
157 }
158 }
[6002]159
[4256]160 public float[] getStopFractions()
161 {
162 if (stopRef != null)
163 {
[6002]164 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
[4256]165 return grad.getStopFractions();
166 }
167
[6002]168 if (stopFractions != null)
169 {
170 return stopFractions;
171 }
[4256]172
173 stopFractions = new float[stops.size()];
174 int idx = 0;
175 for (Iterator it = stops.iterator(); it.hasNext();)
176 {
[6002]177 Stop stop = (Stop) it.next();
[4256]178 float val = stop.offset;
[6002]179 if (idx != 0 && val < stopFractions[idx - 1])
180 {
181 val = stopFractions[idx - 1];
182 }
[4256]183 stopFractions[idx++] = val;
184 }
185
186 return stopFractions;
187 }
188
189 public Color[] getStopColors()
190 {
191 if (stopRef != null)
192 {
[6002]193 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
[4256]194 return grad.getStopColors();
195 }
196
[6002]197 if (stopColors != null)
198 {
199 return stopColors;
200 }
[4256]201
202 stopColors = new Color[stops.size()];
203 int idx = 0;
204 for (Iterator it = stops.iterator(); it.hasNext();)
205 {
[6002]206 Stop stop = (Stop) it.next();
[4256]207 int stopColorVal = stop.color.getRGB();
[6002]208 Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255));
[4256]209 stopColors[idx++] = stopColor;
210 }
211
212 return stopColors;
213 }
[6002]214
[4256]215 public void setStops(Color[] colors, float[] fractions)
216 {
217 if (colors.length != fractions.length)
218 {
219 throw new IllegalArgumentException();
220 }
[6002]221
[4256]222 this.stopColors = colors;
223 this.stopFractions = fractions;
224 stopRef = null;
225 }
[6002]226
[4256]227 private int clamp(int val, int min, int max)
228 {
[6002]229 if (val < min)
230 {
231 return min;
232 }
233 if (val > max)
234 {
235 return max;
236 }
[4256]237 return val;
238 }
[6002]239
[4256]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 /**
[6002]251 * Updates all attributes in this diagram associated with a time event. Ie,
252 * all attributes with track information.
253 *
[4256]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
[6002]267
[4256]268 if (getPres(sty.setName("spreadMethod")))
269 {
270 int newVal;
271 strn = sty.getStringValue().toLowerCase();
[6002]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 }
[4256]282 if (spreadMethod != newVal)
283 {
284 spreadMethod = newVal;
285 stateChange = true;
286 }
287 }
[6002]288
[4256]289 if (getPres(sty.setName("gradientUnits")))
290 {
291 int newVal;
292 strn = sty.getStringValue().toLowerCase();
[6002]293 if (strn.equals("userspaceonuse"))
294 {
295 newVal = GU_USER_SPACE_ON_USE;
296 } else
297 {
298 newVal = GU_OBJECT_BOUNDING_BOX;
299 }
[4256]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
[6002]317
[4256]318 //Check to see if we're using our own stops or referencing someone else's
319 if (getPres(sty.setName("xlink:href")))
320 {
[6002]321 try
322 {
[4256]323 URI newVal = sty.getURIValue(getXMLBase());
324 if ((newVal == null && stopRef != null) || !newVal.equals(stopRef))
325 {
326 stopRef = newVal;
327 stateChange = true;
328 }
[6002]329 } catch (Exception e)
[4256]330 {
[6002]331 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
332 "Could not parse xlink:href", e);
[4256]333 }
334 }
[6002]335
[4256]336 //Check stops, if any
337 for (Iterator it = stops.iterator(); it.hasNext();)
338 {
[6002]339 Stop stop = (Stop) it.next();
[4256]340 if (stop.updateTime(curTime))
341 {
342 stateChange = true;
343 stopFractions = null;
344 stopColors = null;
345 }
346 }
[6002]347
[4256]348 return stateChange;
349 }
350}
Note: See TracBrowser for help on using the repository browser.