source: josm/trunk/src/com/kitfox/svg/RadialGradient.java@ 10865

Last change on this file since 10865 was 10787, checked in by Don-vip, 8 years ago

fix #13291 - upgrade to svgSalamander v1.1.0 (patched)

now detects two invalid SVG files: presets/sport/volleyball.svg and presets/shop/diy_store.svg

  • Property svn:eol-style set to native
File size: 6.5 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, 1:55 AM
35 */
36package com.kitfox.svg;
37
38import java.awt.MultipleGradientPaint;
39import java.awt.Paint;
40import java.awt.RadialGradientPaint;
41import java.awt.geom.AffineTransform;
42import java.awt.geom.Point2D;
43import java.awt.geom.Rectangle2D;
44import java.util.Arrays;
45
46import com.kitfox.svg.xml.StyleAttribute;
47
48/**
49 * @author Mark McKay
50 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
51 */
52public class RadialGradient extends Gradient
53{
54 public static final String TAG_NAME = "radialgradient";
55
56 float cx = 0.5f;
57 float cy = 0.5f;
58 boolean hasFocus = false;
59 float fx = 0f;
60 float fy = 0f;
61 float r = 0.5f;
62
63 /**
64 * Creates a new instance of RadialGradient
65 */
66 public RadialGradient()
67 {
68 }
69
70 @Override
71 public String getTagName()
72 {
73 return TAG_NAME;
74 }
75
76 @Override
77 protected void build() throws SVGException
78 {
79 super.build();
80
81 StyleAttribute sty = new StyleAttribute();
82
83 if (getPres(sty.setName("cx")))
84 {
85 cx = sty.getFloatValueWithUnits();
86 }
87
88 if (getPres(sty.setName("cy")))
89 {
90 cy = sty.getFloatValueWithUnits();
91 }
92
93 hasFocus = false;
94 if (getPres(sty.setName("fx")))
95 {
96 fx = sty.getFloatValueWithUnits();
97 hasFocus = true;
98 }
99
100 if (getPres(sty.setName("fy")))
101 {
102 fy = sty.getFloatValueWithUnits();
103 hasFocus = true;
104 }
105
106 if (getPres(sty.setName("r")))
107 {
108 r = sty.getFloatValueWithUnits();
109 }
110 }
111
112 @Override
113 public Paint getPaint(Rectangle2D bounds, AffineTransform xform)
114 {
115 MultipleGradientPaint.CycleMethod method;
116 switch (spreadMethod)
117 {
118 default:
119 case SM_PAD:
120 method = MultipleGradientPaint.CycleMethod.NO_CYCLE;
121 break;
122 case SM_REPEAT:
123 method = MultipleGradientPaint.CycleMethod.REPEAT;
124 break;
125 case SM_REFLECT:
126 method = MultipleGradientPaint.CycleMethod.REFLECT;
127 break;
128 }
129
130 Paint paint;
131 Point2D.Float pt1 = new Point2D.Float(cx, cy);
132 Point2D.Float pt2 = hasFocus ? new Point2D.Float(fx, fy) : pt1;
133 if (gradientUnits == GU_USER_SPACE_ON_USE)
134 {
135 paint = new RadialGradientPaint(
136 pt1,
137 r,
138 pt2,
139 getStopFractions(),
140 getStopColors(),
141 method,
142 MultipleGradientPaint.ColorSpaceType.SRGB,
143 gradientTransform);
144 } else
145 {
146 AffineTransform viewXform = new AffineTransform();
147 viewXform.translate(bounds.getX(), bounds.getY());
148 viewXform.scale(bounds.getWidth(), bounds.getHeight());
149
150 viewXform.concatenate(gradientTransform);
151
152 paint = new RadialGradientPaint(
153 pt1,
154 r,
155 pt2,
156 getStopFractions(),
157 getStopColors(),
158 method,
159 MultipleGradientPaint.ColorSpaceType.SRGB,
160 viewXform);
161 }
162
163 return paint;
164 }
165
166 /**
167 * Updates all attributes in this diagram associated with a time event. Ie,
168 * all attributes with track information.
169 *
170 * @return - true if this node has changed state as a result of the time
171 * update
172 */
173 @Override
174 public boolean updateTime(double curTime) throws SVGException
175 {
176// if (trackManager.getNumTracks() == 0) return false;
177 boolean changeState = super.updateTime(curTime);
178
179 //Get current values for parameters
180 StyleAttribute sty = new StyleAttribute();
181 boolean shapeChange = false;
182
183 if (getPres(sty.setName("cx")))
184 {
185 float newVal = sty.getFloatValueWithUnits();
186 if (newVal != cx)
187 {
188 cx = newVal;
189 shapeChange = true;
190 }
191 }
192
193 if (getPres(sty.setName("cy")))
194 {
195 float newVal = sty.getFloatValueWithUnits();
196 if (newVal != cy)
197 {
198 cy = newVal;
199 shapeChange = true;
200 }
201 }
202
203 if (getPres(sty.setName("fx")))
204 {
205 float newVal = sty.getFloatValueWithUnits();
206 if (newVal != fx)
207 {
208 fx = newVal;
209 shapeChange = true;
210 }
211 }
212
213 if (getPres(sty.setName("fy")))
214 {
215 float newVal = sty.getFloatValueWithUnits();
216 if (newVal != fy)
217 {
218 fy = newVal;
219 shapeChange = true;
220 }
221 }
222
223 if (getPres(sty.setName("r")))
224 {
225 float newVal = sty.getFloatValueWithUnits();
226 if (newVal != r)
227 {
228 r = newVal;
229 shapeChange = true;
230 }
231 }
232
233 return changeState;
234 }
235}
Note: See TracBrowser for help on using the repository browser.