Changeset 6002 in josm for trunk/src/com/kitfox/svg/PatternSVG.java
- Timestamp:
- 2013-06-11T01:01:28+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/PatternSVG.java
r4256 r6002 1 1 /* 2 * Gradient.java 3 * 4 * 5 * The Salamander Project - 2D and 3D graphics libraries in Java 6 * Copyright (C) 2004 Mark McKay 7 * 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 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 24 33 * 25 34 * Created on January 26, 2004, 3:25 AM 26 35 */ 27 28 36 package com.kitfox.svg; 29 37 38 import com.kitfox.svg.pattern.PatternPaint; 30 39 import com.kitfox.svg.xml.StyleAttribute; 31 import java.net.*; 32 import java.util.*; 33 import java.awt.geom.*; 34 import java.awt.*; 35 import java.awt.image.*; 36 37 import com.kitfox.svg.pattern.*; 38 import com.kitfox.svg.xml.*; 39 import org.xml.sax.*; 40 import java.awt.Graphics2D; 41 import java.awt.Paint; 42 import java.awt.RenderingHints; 43 import java.awt.TexturePaint; 44 import java.awt.geom.AffineTransform; 45 import java.awt.geom.Point2D; 46 import java.awt.geom.Rectangle2D; 47 import java.awt.image.BufferedImage; 48 import java.net.URI; 49 import java.util.Iterator; 50 import java.util.logging.Level; 51 import java.util.logging.Logger; 40 52 41 53 /** … … 43 55 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 44 56 */ 45 public class PatternSVG extends FillElement { 46 57 public class PatternSVG extends FillElement 58 { 59 public static final String TAG_NAME = "pattern"; 60 47 61 public static final int GU_OBJECT_BOUNDING_BOX = 0; 48 62 public static final int GU_USER_SPACE_ON_USE = 1; 49 50 63 int gradientUnits = GU_OBJECT_BOUNDING_BOX; 51 52 64 float x; 53 65 float y; 54 66 float width; 55 67 float height; 56 57 68 AffineTransform patternXform = new AffineTransform(); 58 69 Rectangle2D.Float viewBox; 59 60 70 Paint texPaint; 61 71 62 /** Creates a new instance of Gradient */ 63 public PatternSVG() { 64 } 65 /* 66 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) 67 { 68 //Load style string 69 super.loaderStartElement(helper, attrs, parent); 70 71 String href = attrs.getValue("xlink:href"); 72 //If we have a link to another pattern, initialize ourselves with it's values 73 if (href != null) 74 { 75 //System.err.println("Gradient.loaderStartElement() href '" + href + "'"); 76 try { 77 URI src = getXMLBase().resolve(href); 78 // URL url = srcUrl.toURL(); 79 // URL url = new URL(helper.docRoot, href); 80 PatternSVG patSrc = (PatternSVG)helper.universe.getElement(src); 81 82 gradientUnits = patSrc.gradientUnits; 83 x = patSrc.x; 84 y = patSrc.y; 85 width = patSrc.width; 86 height = patSrc.height; 87 viewBox = patSrc.viewBox; 88 patternXform.setTransform(patSrc.patternXform); 89 members.addAll(patSrc.members); 90 } 91 catch (Exception e) 92 { 93 e.printStackTrace(); 94 } 95 } 96 97 98 String gradientUnits = attrs.getValue("gradientUnits"); 99 100 if (gradientUnits != null) 101 { 102 if (gradientUnits.toLowerCase().equals("userspaceonuse")) this.gradientUnits = GU_USER_SPACE_ON_USE; 103 else this.gradientUnits = GU_OBJECT_BOUNDING_BOX; 104 } 105 106 String patternTransform = attrs.getValue("patternTransform"); 107 if (patternTransform != null) 108 { 109 patternXform = parseTransform(patternTransform); 110 } 111 112 String x = attrs.getValue("x"); 113 String y = attrs.getValue("y"); 114 String width = attrs.getValue("width"); 115 String height = attrs.getValue("height"); 116 117 if (x != null) this.x = XMLParseUtil.parseFloat(x); 118 if (y != null) this.y = XMLParseUtil.parseFloat(y); 119 if (width != null) this.width = XMLParseUtil.parseFloat(width); 120 if (height != null) this.height = XMLParseUtil.parseFloat(height); 121 122 String viewBoxStrn = attrs.getValue("viewBox"); 123 if (viewBoxStrn != null) 124 { 125 float[] dim = XMLParseUtil.parseFloatList(viewBoxStrn); 126 viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]); 127 } 128 } 129 */ 72 /** 73 * Creates a new instance of Gradient 74 */ 75 public PatternSVG() 76 { 77 } 78 79 public String getTagName() 80 { 81 return TAG_NAME; 82 } 83 130 84 /** 131 85 * Called after the start element but before the end element to indicate … … 135 89 { 136 90 super.loaderAddChild(helper, child); 137 138 // members.add(child); 139 } 140 91 } 92 141 93 protected void build() throws SVGException 142 94 { 143 95 super.build(); 144 96 145 97 StyleAttribute sty = new StyleAttribute(); 146 147 98 99 //Load style string 148 100 String href = null; 149 if (getPres(sty.setName("xlink:href"))) href = sty.getStringValue(); 101 if (getPres(sty.setName("xlink:href"))) 102 { 103 href = sty.getStringValue(); 104 } 150 105 //String href = attrs.getValue("xlink:href"); 151 106 //If we have a link to another pattern, initialize ourselves with it's values … … 153 108 { 154 109 //System.err.println("Gradient.loaderStartElement() href '" + href + "'"); 155 try { 110 try 111 { 156 112 URI src = getXMLBase().resolve(href); 157 PatternSVG patSrc = (PatternSVG)diagram.getUniverse().getElement(src); 113 PatternSVG patSrc = (PatternSVG) diagram.getUniverse().getElement(src); 158 114 159 115 gradientUnits = patSrc.gradientUnits; … … 165 121 patternXform.setTransform(patSrc.patternXform); 166 122 children.addAll(patSrc.children); 123 } catch (Exception e) 124 { 125 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 126 "Could not parse xlink:href", e); 167 127 } 168 catch (Exception e)169 {170 e.printStackTrace();171 }172 128 } 173 129 174 130 String gradientUnits = ""; 175 if (getPres(sty.setName("gradientUnits"))) gradientUnits = sty.getStringValue().toLowerCase(); 176 if (gradientUnits.equals("userspaceonuse")) this.gradientUnits = GU_USER_SPACE_ON_USE; 177 else this.gradientUnits = GU_OBJECT_BOUNDING_BOX; 131 if (getPres(sty.setName("gradientUnits"))) 132 { 133 gradientUnits = sty.getStringValue().toLowerCase(); 134 } 135 if (gradientUnits.equals("userspaceonuse")) 136 { 137 this.gradientUnits = GU_USER_SPACE_ON_USE; 138 } else 139 { 140 this.gradientUnits = GU_OBJECT_BOUNDING_BOX; 141 } 178 142 179 143 String patternTransform = ""; 180 if (getPres(sty.setName("patternTransform"))) patternTransform = sty.getStringValue(); 144 if (getPres(sty.setName("patternTransform"))) 145 { 146 patternTransform = sty.getStringValue(); 147 } 181 148 patternXform = parseTransform(patternTransform); 182 149 183 184 if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits(); 185 186 if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits(); 187 188 if (getPres(sty.setName("width"))) width = sty.getFloatValueWithUnits(); 189 190 if (getPres(sty.setName("height"))) height = sty.getFloatValueWithUnits(); 191 150 151 if (getPres(sty.setName("x"))) 152 { 153 x = sty.getFloatValueWithUnits(); 154 } 155 156 if (getPres(sty.setName("y"))) 157 { 158 y = sty.getFloatValueWithUnits(); 159 } 160 161 if (getPres(sty.setName("width"))) 162 { 163 width = sty.getFloatValueWithUnits(); 164 } 165 166 if (getPres(sty.setName("height"))) 167 { 168 height = sty.getFloatValueWithUnits(); 169 } 170 192 171 if (getPres(sty.setName("viewBox"))) 193 172 { … … 195 174 viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]); 196 175 } 197 176 198 177 preparePattern(); 199 178 } 200 201 /* 202 public void loaderEndElement(SVGLoaderHelper helper) 203 { 204 build(); 205 } 206 */ 207 179 180 /* 181 public void loaderEndElement(SVGLoaderHelper helper) 182 { 183 build(); 184 } 185 */ 208 186 protected void preparePattern() throws SVGException 209 187 { 210 188 //For now, treat all fills as UserSpaceOnUse. Otherwise, we'll need 211 189 // a different paint for every object. 212 int tileWidth = (int)width; 213 int tileHeight = (int)height; 190 int tileWidth = (int) width; 191 int tileHeight = (int) height; 214 192 215 193 float stretchX = 1f, stretchY = 1f; … … 217 195 { 218 196 //Scale our source tile so that we can have nice sampling from it. 219 float xlateX = (float)patternXform.getTranslateX(); 220 float xlateY = (float)patternXform.getTranslateY(); 197 float xlateX = (float) patternXform.getTranslateX(); 198 float xlateY = (float) patternXform.getTranslateY(); 221 199 222 200 Point2D.Float pt = new Point2D.Float(), pt2 = new Point2D.Float(); … … 226 204 pt2.x -= xlateX; 227 205 pt2.y -= xlateY; 228 stretchX = (float)Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / width; 206 stretchX = (float) Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / width; 229 207 230 208 pt.setLocation(height, 0); … … 232 210 pt2.x -= xlateX; 233 211 pt2.y -= xlateY; 234 stretchY = (float)Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / height; 212 stretchY = (float) Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / height; 235 213 236 214 tileWidth *= stretchX; … … 238 216 } 239 217 240 if (tileWidth == 0 || tileHeight == 0) 218 if (tileWidth == 0 || tileHeight == 0) 241 219 { 242 220 //Use defaults if tile has degenerate size 243 221 return; 244 222 } 245 223 246 224 BufferedImage buf = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_INT_ARGB); 247 225 Graphics2D g = buf.createGraphics(); … … 251 229 for (Iterator it = children.iterator(); it.hasNext();) 252 230 { 253 SVGElement ele = (SVGElement)it.next(); 231 SVGElement ele = (SVGElement) it.next(); 254 232 if (ele instanceof RenderableElement) 255 233 { … … 259 237 { 260 238 xform.translate(-x, -y); 261 } 262 else 239 } else 263 240 { 264 241 xform.scale(tileWidth / viewBox.width, tileHeight / viewBox.height); … … 267 244 268 245 g.setTransform(xform); 269 ((RenderableElement)ele).render(g); 246 ((RenderableElement) ele).render(g); 270 247 } 271 248 } … … 280 257 { 281 258 texPaint = new TexturePaint(buf, new Rectangle2D.Float(x, y, width, height)); 282 } 283 else 259 } else 284 260 { 285 261 patternXform.scale(1 / stretchX, 1 / stretchY); … … 294 270 295 271 /** 296 * Updates all attributes in this diagram associated with a time event. 297 * Ie, all attributes with track information. 272 * Updates all attributes in this diagram associated with a time event. Ie, 273 * all attributes with track information. 274 * 298 275 * @return - true if this node has changed state as a result of the time 299 276 * update
Note:
See TracChangeset
for help on using the changeset viewer.