Changeset 6002 in josm for trunk/src/com/kitfox/svg/Gradient.java
- Timestamp:
- 2013-06-11T01:01:28+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/Gradient.java
r4256 r6002 1 1 /* 2 * Gradient.java 2 * SVG Salamander 3 * Copyright (c) 2004, Mark McKay 4 * All rights reserved. 3 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: 4 9 * 5 * The Salamander Project - 2D and 3D graphics libraries in Java 6 * Copyright (C) 2004 Mark McKay 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. 7 17 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * 22 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 23 * projects can be found at http://www.kitfox.com 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 24 33 * 25 34 * Created on January 26, 2004, 3:25 AM 26 35 */ 27 28 36 package com.kitfox.svg; 29 37 30 import java.net.*; 31 import java.util.*; 32 import java.awt.geom.*; 33 import java.awt.*; 34 35 import com.kitfox.svg.xml.*; 38 import com.kitfox.svg.xml.StyleAttribute; 39 import java.awt.Color; 40 import java.awt.geom.AffineTransform; 41 import java.net.URI; 42 import java.util.ArrayList; 43 import java.util.Iterator; 44 import java.util.logging.Level; 45 import java.util.logging.Logger; 36 46 37 47 /** … … 41 51 abstract public class Gradient extends FillElement 42 52 { 43 53 public static final String TAG_NAME = "gradient"; 54 44 55 public static final int SM_PAD = 0; 45 56 public static final int SM_REPEAT = 1; 46 57 public static final int SM_REFLECT = 2; 47 48 58 int spreadMethod = SM_PAD; 49 50 59 public static final int GU_OBJECT_BOUNDING_BOX = 0; 51 60 public static final int GU_USER_SPACE_ON_USE = 1; 52 53 61 protected int gradientUnits = GU_OBJECT_BOUNDING_BOX; 54 55 62 //Either this gradient contains a list of stops, or it will take it's 56 63 // stops from the referenced gradient 57 64 ArrayList stops = new ArrayList(); 58 65 URI stopRef = null; 59 // Gradient stopRef = null;60 61 66 protected AffineTransform gradientTransform = null; 62 67 63 68 //Cache arrays of stop values here 64 69 float[] stopFractions; 65 70 Color[] stopColors; 66 71 67 /** Creates a new instance of Gradient */ 68 public Gradient() { 69 } 70 72 /** 73 * Creates a new instance of Gradient 74 */ 75 public Gradient() 76 { 77 } 78 79 public String getTagName() 80 { 81 return TAG_NAME; 82 } 83 71 84 /** 72 85 * Called after the start element but before the end element to indicate … … 77 90 super.loaderAddChild(helper, child); 78 91 79 if (!(child instanceof Stop)) return; 80 appendStop((Stop)child); 92 if (!(child instanceof Stop)) 93 { 94 return; 95 } 96 appendStop((Stop) child); 81 97 } 82 98 … … 84 100 { 85 101 super.build(); 86 102 87 103 StyleAttribute sty = new StyleAttribute(); 88 104 String strn; 89 105 90 106 if (getPres(sty.setName("spreadMethod"))) 91 107 { 92 108 strn = sty.getStringValue().toLowerCase(); 93 if (strn.equals("repeat")) spreadMethod = SM_REPEAT; 94 else if (strn.equals("reflect")) spreadMethod = SM_REFLECT; 95 else spreadMethod = SM_PAD; 109 if (strn.equals("repeat")) 110 { 111 spreadMethod = SM_REPEAT; 112 } else if (strn.equals("reflect")) 113 { 114 spreadMethod = SM_REFLECT; 115 } else 116 { 117 spreadMethod = SM_PAD; 118 } 96 119 } 97 120 … … 99 122 { 100 123 strn = sty.getStringValue().toLowerCase(); 101 if (strn.equals("userspaceonuse")) gradientUnits = GU_USER_SPACE_ON_USE; 102 else gradientUnits = GU_OBJECT_BOUNDING_BOX; 103 } 104 105 if (getPres(sty.setName("gradientTransform"))) gradientTransform = parseTransform(sty.getStringValue()); 124 if (strn.equals("userspaceonuse")) 125 { 126 gradientUnits = GU_USER_SPACE_ON_USE; 127 } else 128 { 129 gradientUnits = GU_OBJECT_BOUNDING_BOX; 130 } 131 } 132 133 if (getPres(sty.setName("gradientTransform"))) 134 { 135 gradientTransform = parseTransform(sty.getStringValue()); 136 } 106 137 //If we still don't have one, set it to identity 107 if (gradientTransform == null) gradientTransform = new AffineTransform(); 108 109 138 if (gradientTransform == null) 139 { 140 gradientTransform = new AffineTransform(); 141 } 142 143 110 144 //Check to see if we're using our own stops or referencing someone else's 111 145 if (getPres(sty.setName("xlink:href"))) 112 146 { 113 try { 147 try 148 { 114 149 stopRef = sty.getURIValue(getXMLBase()); 115 150 //System.err.println("Gradient: " + sty.getStringValue() + ", " + getXMLBase() + ", " + src); 116 151 // URI src = getXMLBase().resolve(href); 117 152 // stopRef = (Gradient)diagram.getUniverse().getElement(src); 118 } 119 catch (Exception e) 153 } catch (Exception e) 120 154 { 121 155 throw new SVGException("Could not resolve relative URL in Gradient: " + sty.getStringValue() + ", " + getXMLBase(), e); … … 123 157 } 124 158 } 125 159 126 160 public float[] getStopFractions() 127 161 { 128 162 if (stopRef != null) 129 163 { 130 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);164 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef); 131 165 return grad.getStopFractions(); 132 166 } 133 167 134 if (stopFractions != null) return stopFractions; 168 if (stopFractions != null) 169 { 170 return stopFractions; 171 } 135 172 136 173 stopFractions = new float[stops.size()]; … … 138 175 for (Iterator it = stops.iterator(); it.hasNext();) 139 176 { 140 Stop stop = (Stop) it.next();177 Stop stop = (Stop) it.next(); 141 178 float val = stop.offset; 142 if (idx != 0 && val < stopFractions[idx - 1]) val = stopFractions[idx - 1]; 179 if (idx != 0 && val < stopFractions[idx - 1]) 180 { 181 val = stopFractions[idx - 1]; 182 } 143 183 stopFractions[idx++] = val; 144 184 } … … 151 191 if (stopRef != null) 152 192 { 153 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef);193 Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef); 154 194 return grad.getStopColors(); 155 195 } 156 196 157 if (stopColors != null) return stopColors; 197 if (stopColors != null) 198 { 199 return stopColors; 200 } 158 201 159 202 stopColors = new Color[stops.size()]; … … 161 204 for (Iterator it = stops.iterator(); it.hasNext();) 162 205 { 163 Stop stop = (Stop) it.next();206 Stop stop = (Stop) it.next(); 164 207 int stopColorVal = stop.color.getRGB(); 165 Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255));208 Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255)); 166 209 stopColors[idx++] = stopColor; 167 210 } … … 169 212 return stopColors; 170 213 } 171 214 172 215 public void setStops(Color[] colors, float[] fractions) 173 216 { … … 176 219 throw new IllegalArgumentException(); 177 220 } 178 221 179 222 this.stopColors = colors; 180 223 this.stopFractions = fractions; 181 224 stopRef = null; 182 225 } 183 226 184 227 private int clamp(int val, int min, int max) 185 228 { 186 if (val < min) return min; 187 if (val > max) return max; 229 if (val < min) 230 { 231 return min; 232 } 233 if (val > max) 234 { 235 return max; 236 } 188 237 return val; 189 238 } 190 239 191 240 public void setStopRef(URI grad) 192 241 { … … 200 249 201 250 /** 202 * Updates all attributes in this diagram associated with a time event. 203 * Ie, all attributes with track information. 251 * Updates all attributes in this diagram associated with a time event. Ie, 252 * all attributes with track information. 253 * 204 254 * @return - true if this node has changed state as a result of the time 205 255 * update … … 214 264 boolean shapeChange = false; 215 265 String strn; 216 266 217 267 218 268 if (getPres(sty.setName("spreadMethod"))) … … 220 270 int newVal; 221 271 strn = sty.getStringValue().toLowerCase(); 222 if (strn.equals("repeat")) newVal = SM_REPEAT; 223 else if (strn.equals("reflect")) newVal = SM_REFLECT; 224 else newVal = SM_PAD; 272 if (strn.equals("repeat")) 273 { 274 newVal = SM_REPEAT; 275 } else if (strn.equals("reflect")) 276 { 277 newVal = SM_REFLECT; 278 } else 279 { 280 newVal = SM_PAD; 281 } 225 282 if (spreadMethod != newVal) 226 283 { … … 229 286 } 230 287 } 231 288 232 289 if (getPres(sty.setName("gradientUnits"))) 233 290 { 234 291 int newVal; 235 292 strn = sty.getStringValue().toLowerCase(); 236 if (strn.equals("userspaceonuse")) newVal = GU_USER_SPACE_ON_USE; 237 else newVal = GU_OBJECT_BOUNDING_BOX; 293 if (strn.equals("userspaceonuse")) 294 { 295 newVal = GU_USER_SPACE_ON_USE; 296 } else 297 { 298 newVal = GU_OBJECT_BOUNDING_BOX; 299 } 238 300 if (newVal != gradientUnits) 239 301 { … … 253 315 } 254 316 255 317 256 318 //Check to see if we're using our own stops or referencing someone else's 257 319 if (getPres(sty.setName("xlink:href"))) 258 320 { 259 try { 321 try 322 { 260 323 URI newVal = sty.getURIValue(getXMLBase()); 261 324 if ((newVal == null && stopRef != null) || !newVal.equals(stopRef)) … … 264 327 stateChange = true; 265 328 } 266 } 267 catch (Exception e)268 {269 e.printStackTrace();270 } 271 } 272 329 } catch (Exception e) 330 { 331 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 332 "Could not parse xlink:href", e); 333 } 334 } 335 273 336 //Check stops, if any 274 337 for (Iterator it = stops.iterator(); it.hasNext();) 275 338 { 276 Stop stop = (Stop) it.next();339 Stop stop = (Stop) it.next(); 277 340 if (stop.updateTime(curTime)) 278 341 { … … 282 345 } 283 346 } 284 347 285 348 return stateChange; 286 349 } 287 288 350 }
Note:
See TracChangeset
for help on using the changeset viewer.