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

Last change on this file since 13376 was 11525, checked in by Don-vip, 7 years ago

see #14319 - update to latest version of svgSalamander (2017-01-07, patched)

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