source: josm/trunk/src/com/kitfox/svg/LinearGradient.java@ 10787

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