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

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

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

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