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

Last change on this file since 15912 was 14328, checked in by Don-vip, 6 years ago

see #14319, see #16838 - update to svgSalamander 1.1.2

  • 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 private void buildStops()
163 {
164 ArrayList<Stop> stopList = new ArrayList<>(stops);
165 stopList.sort((o1, o2) -> Float.compare(o1.offset, o2.offset));
166
167 //Remove doubles
168 for (int i = stopList.size() - 2; i > 0; --i)
169 {
170 if (stopList.get(i + 1).offset == stopList.get(i).offset)
171 {
172 stopList.remove(i + 1);
173 }
174 }
175
176 stopFractions = new float[stopList.size()];
177 stopColors = new Color[stopList.size()];
178 int idx = 0;
179 for (Stop stop : stopList)
180 {
181 int stopColorVal = stop.color.getRGB();
182 Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255));
183
184 stopColors[idx] = stopColor;
185 stopFractions[idx] = stop.offset;
186 idx++;
187 }
188 }
189
190 public float[] getStopFractions()
191 {
192 if (stopRef != null)
193 {
194 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
195 return grad.getStopFractions();
196 }
197
198 if (stopFractions != null)
199 {
200 return stopFractions;
201 }
202
203 buildStops();
204
205 return stopFractions;
206 }
207
208 public Color[] getStopColors()
209 {
210 if (stopRef != null)
211 {
212 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);
213 return grad.getStopColors();
214 }
215
216 if (stopColors != null)
217 {
218 return stopColors;
219 }
220
221 buildStops();
222
223 return stopColors;
224 }
225
226 private int clamp(int val, int min, int max)
227 {
228 if (val < min)
229 {
230 return min;
231 }
232 if (val > max)
233 {
234 return max;
235 }
236 return val;
237 }
238
239 public void setStopRef(URI grad)
240 {
241 stopRef = grad;
242 }
243
244 public void appendStop(Stop stop)
245 {
246 stops.add(stop);
247 }
248
249 /**
250 * Updates all attributes in this diagram associated with a time event. Ie,
251 * all attributes with track information.
252 *
253 * @return - true if this node has changed state as a result of the time
254 * update
255 */
256 @Override
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 String strn;
265
266
267 if (getPres(sty.setName("spreadMethod")))
268 {
269 int newVal;
270 strn = sty.getStringValue().toLowerCase();
271 if (strn.equals("repeat"))
272 {
273 newVal = SM_REPEAT;
274 } else if (strn.equals("reflect"))
275 {
276 newVal = SM_REFLECT;
277 } else
278 {
279 newVal = SM_PAD;
280 }
281 if (spreadMethod != newVal)
282 {
283 spreadMethod = newVal;
284 stateChange = true;
285 }
286 }
287
288 if (getPres(sty.setName("gradientUnits")))
289 {
290 int newVal;
291 strn = sty.getStringValue().toLowerCase();
292 if (strn.equals("userspaceonuse"))
293 {
294 newVal = GU_USER_SPACE_ON_USE;
295 } else
296 {
297 newVal = GU_OBJECT_BOUNDING_BOX;
298 }
299 if (newVal != gradientUnits)
300 {
301 gradientUnits = newVal;
302 stateChange = true;
303 }
304 }
305
306 if (getPres(sty.setName("gradientTransform")))
307 {
308 AffineTransform newVal = parseTransform(sty.getStringValue());
309 if (newVal != null && newVal.equals(gradientTransform))
310 {
311 gradientTransform = newVal;
312 stateChange = true;
313 }
314 }
315
316
317 //Check to see if we're using our own stops or referencing someone else's
318 if (getPres(sty.setName("xlink:href")))
319 {
320 try
321 {
322 URI newVal = sty.getURIValue(getXMLBase());
323 if ((newVal == null && stopRef != null) || !newVal.equals(stopRef))
324 {
325 stopRef = newVal;
326 stateChange = true;
327 }
328 } catch (Exception e)
329 {
330 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
331 "Could not parse xlink:href", e);
332 }
333 }
334
335 //Check stops, if any
336 for (Stop stop : stops) {
337 if (stop.updateTime(curTime))
338 {
339 stateChange = true;
340 stopFractions = null;
341 stopColors = null;
342 }
343 }
344
345 return stateChange;
346 }
347}
Note: See TracBrowser for help on using the repository browser.