001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on January 26, 2004, 3:25 AM 035 */ 036package com.kitfox.svg; 037 038import com.kitfox.svg.pattern.PatternPaint; 039import com.kitfox.svg.xml.StyleAttribute; 040import java.awt.Graphics2D; 041import java.awt.Paint; 042import java.awt.RenderingHints; 043import java.awt.TexturePaint; 044import java.awt.geom.AffineTransform; 045import java.awt.geom.Point2D; 046import java.awt.geom.Rectangle2D; 047import java.awt.image.BufferedImage; 048import java.net.URI; 049import java.util.logging.Level; 050import java.util.logging.Logger; 051 052/** 053 * @author Mark McKay 054 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 055 */ 056public class PatternSVG extends FillElement 057{ 058 public static final String TAG_NAME = "pattern"; 059 060 public static final int GU_OBJECT_BOUNDING_BOX = 0; 061 public static final int GU_USER_SPACE_ON_USE = 1; 062 int gradientUnits = GU_OBJECT_BOUNDING_BOX; 063 float x; 064 float y; 065 float width; 066 float height; 067 AffineTransform patternXform = new AffineTransform(); 068 Rectangle2D.Float viewBox; 069 Paint texPaint; 070 071 /** 072 * Creates a new instance of Gradient 073 */ 074 public PatternSVG() 075 { 076 } 077 078 @Override 079 public String getTagName() 080 { 081 return TAG_NAME; 082 } 083 084 /** 085 * Called after the start element but before the end element to indicate 086 * each child tag that has been processed 087 */ 088 @Override 089 public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException 090 { 091 super.loaderAddChild(helper, child); 092 } 093 094 @Override 095 protected void build() throws SVGException 096 { 097 super.build(); 098 099 StyleAttribute sty = new StyleAttribute(); 100 101 //Load style string 102 String href = null; 103 if (getPres(sty.setName("xlink:href"))) 104 { 105 href = sty.getStringValue(); 106 } 107 //String href = attrs.getValue("xlink:href"); 108 //If we have a link to another pattern, initialize ourselves with it's values 109 if (href != null) 110 { 111//System.err.println("Gradient.loaderStartElement() href '" + href + "'"); 112 try 113 { 114 URI src = getXMLBase().resolve(href); 115 PatternSVG patSrc = (PatternSVG) diagram.getUniverse().getElement(src); 116 117 gradientUnits = patSrc.gradientUnits; 118 x = patSrc.x; 119 y = patSrc.y; 120 width = patSrc.width; 121 height = patSrc.height; 122 viewBox = patSrc.viewBox; 123 patternXform.setTransform(patSrc.patternXform); 124 children.addAll(patSrc.children); 125 } catch (Exception e) 126 { 127 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 128 "Could not parse xlink:href", e); 129 } 130 } 131 132 String gradientUnits = ""; 133 if (getPres(sty.setName("gradientUnits"))) 134 { 135 gradientUnits = sty.getStringValue().toLowerCase(); 136 } 137 if (gradientUnits.equals("userspaceonuse")) 138 { 139 this.gradientUnits = GU_USER_SPACE_ON_USE; 140 } else 141 { 142 this.gradientUnits = GU_OBJECT_BOUNDING_BOX; 143 } 144 145 String patternTransform = ""; 146 if (getPres(sty.setName("patternTransform"))) 147 { 148 patternTransform = sty.getStringValue(); 149 } 150 patternXform = parseTransform(patternTransform); 151 152 153 if (getPres(sty.setName("x"))) 154 { 155 x = sty.getFloatValueWithUnits(); 156 } 157 158 if (getPres(sty.setName("y"))) 159 { 160 y = sty.getFloatValueWithUnits(); 161 } 162 163 if (getPres(sty.setName("width"))) 164 { 165 width = sty.getFloatValueWithUnits(); 166 } 167 168 if (getPres(sty.setName("height"))) 169 { 170 height = sty.getFloatValueWithUnits(); 171 } 172 173 if (getPres(sty.setName("viewBox"))) 174 { 175 float[] dim = sty.getFloatList(); 176 viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]); 177 } 178 179 preparePattern(); 180 } 181 182 /* 183 public void loaderEndElement(SVGLoaderHelper helper) 184 { 185 build(); 186 } 187 */ 188 protected void preparePattern() throws SVGException 189 { 190 //For now, treat all fills as UserSpaceOnUse. Otherwise, we'll need 191 // a different paint for every object. 192 int tileWidth = (int) width; 193 int tileHeight = (int) height; 194 195 float stretchX = 1f, stretchY = 1f; 196 if (!patternXform.isIdentity()) 197 { 198 //Scale our source tile so that we can have nice sampling from it. 199 float xlateX = (float) patternXform.getTranslateX(); 200 float xlateY = (float) patternXform.getTranslateY(); 201 202 Point2D.Float pt = new Point2D.Float(), pt2 = new Point2D.Float(); 203 204 pt.setLocation(width, 0); 205 patternXform.transform(pt, pt2); 206 pt2.x -= xlateX; 207 pt2.y -= xlateY; 208 stretchX = (float) Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / width; 209 210 pt.setLocation(height, 0); 211 patternXform.transform(pt, pt2); 212 pt2.x -= xlateX; 213 pt2.y -= xlateY; 214 stretchY = (float) Math.sqrt(pt2.x * pt2.x + pt2.y * pt2.y) * 1.5f / height; 215 216 tileWidth *= stretchX; 217 tileHeight *= stretchY; 218 } 219 220 if (tileWidth == 0 || tileHeight == 0) 221 { 222 //Use defaults if tile has degenerate size 223 return; 224 } 225 226 BufferedImage buf = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_INT_ARGB); 227 Graphics2D g = buf.createGraphics(); 228 g.setClip(0, 0, tileWidth, tileHeight); 229 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 230 231 for (SVGElement ele : children) { 232 if (ele instanceof RenderableElement) 233 { 234 AffineTransform xform = new AffineTransform(); 235 236 if (viewBox == null) 237 { 238 xform.translate(-x, -y); 239 } else 240 { 241 xform.scale(tileWidth / viewBox.width, tileHeight / viewBox.height); 242 xform.translate(-viewBox.x, -viewBox.y); 243 } 244 245 g.setTransform(xform); 246 ((RenderableElement) ele).render(g); 247 } 248 } 249 250 g.dispose(); 251 252//try { 253//javax.imageio.ImageIO.write(buf, "png", new java.io.File("c:\\tmp\\texPaint.png")); 254//} catch (Exception e ) {} 255 256 if (patternXform.isIdentity()) 257 { 258 texPaint = new TexturePaint(buf, new Rectangle2D.Float(x, y, width, height)); 259 } else 260 { 261 patternXform.scale(1 / stretchX, 1 / stretchY); 262 texPaint = new PatternPaint(buf, patternXform); 263 } 264 } 265 266 @Override 267 public Paint getPaint(Rectangle2D bounds, AffineTransform xform) 268 { 269 return texPaint; 270 } 271 272 /** 273 * Updates all attributes in this diagram associated with a time event. Ie, 274 * all attributes with track information. 275 * 276 * @return - true if this node has changed state as a result of the time 277 * update 278 */ 279 @Override 280 public boolean updateTime(double curTime) throws SVGException 281 { 282 //Patterns don't change state 283 return false; 284 } 285}