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 */ 034package com.kitfox.svg; 035 036import com.kitfox.svg.xml.StyleAttribute; 037import java.awt.Graphics2D; 038import java.awt.Rectangle; 039import java.awt.Shape; 040import java.awt.geom.AffineTransform; 041import java.awt.geom.PathIterator; 042import java.awt.geom.Rectangle2D; 043import java.util.ArrayList; 044 045/** 046 * 047 * @author kitfox 048 */ 049public class Marker extends Group 050{ 051 public static final String TAG_NAME = "marker"; 052 053 AffineTransform viewXform; 054 AffineTransform markerXform; 055 Rectangle2D viewBox; 056 float refX; 057 float refY; 058 float markerWidth = 1; 059 float markerHeight = 1; 060 float orient = Float.NaN; 061 boolean markerUnitsStrokeWidth = true; //if set to false 'userSpaceOnUse' is assumed 062 063 @Override 064 public String getTagName() 065 { 066 return TAG_NAME; 067 } 068 069 @Override 070 protected void build() throws SVGException 071 { 072 super.build(); 073 074 StyleAttribute sty = new StyleAttribute(); 075 076 if (getPres(sty.setName("refX"))) 077 { 078 refX = sty.getFloatValueWithUnits(); 079 } 080 if (getPres(sty.setName("refY"))) 081 { 082 refY = sty.getFloatValueWithUnits(); 083 } 084 if (getPres(sty.setName("markerWidth"))) 085 { 086 markerWidth = sty.getFloatValueWithUnits(); 087 } 088 if (getPres(sty.setName("markerHeight"))) 089 { 090 markerHeight = sty.getFloatValueWithUnits(); 091 } 092 093 if (getPres(sty.setName("orient"))) 094 { 095 if ("auto".equals(sty.getStringValue())) 096 { 097 orient = Float.NaN; 098 } else 099 { 100 orient = sty.getFloatValue(); 101 } 102 } 103 104 if (getPres(sty.setName("viewBox"))) 105 { 106 float[] dim = sty.getFloatList(); 107 viewBox = new Rectangle2D.Float(dim[0], dim[1], dim[2], dim[3]); 108 } 109 110 if (viewBox == null) 111 { 112 viewBox = new Rectangle(0, 0, 1, 1); 113 } 114 115 if (getPres(sty.setName("markerUnits"))) 116 { 117 String markerUnits = sty.getStringValue(); 118 if (markerUnits != null && markerUnits.equals("userSpaceOnUse")) 119 { 120 markerUnitsStrokeWidth = false; 121 } 122 } 123 124 //Transform pattern onto unit square 125 viewXform = new AffineTransform(); 126 viewXform.scale(1.0 / viewBox.getWidth(), 1.0 / viewBox.getHeight()); 127 viewXform.translate(-viewBox.getX(), -viewBox.getY()); 128 129 markerXform = new AffineTransform(); 130 markerXform.scale(markerWidth, markerHeight); 131 markerXform.concatenate(viewXform); 132 markerXform.translate(-refX, -refY); 133 } 134 135 @Override 136 protected boolean outsideClip(Graphics2D g) throws SVGException 137 { 138 Shape clip = g.getClip(); 139 Rectangle2D rect = super.getBoundingBox(); 140 if (clip == null || clip.intersects(rect)) 141 { 142 return false; 143 } 144 145 return true; 146 147 } 148 149 @Override 150 public void render(Graphics2D g) throws SVGException 151 { 152 AffineTransform oldXform = g.getTransform(); 153 g.transform(markerXform); 154 155 super.render(g); 156 157 g.setTransform(oldXform); 158 } 159 160 public void render(Graphics2D g, MarkerPos pos, float strokeWidth) throws SVGException 161 { 162 AffineTransform cacheXform = g.getTransform(); 163 164 g.translate(pos.x, pos.y); 165 if (markerUnitsStrokeWidth) 166 { 167 g.scale(strokeWidth, strokeWidth); 168 } 169 170 g.rotate(Math.atan2(pos.dy, pos.dx)); 171 172 g.transform(markerXform); 173 174 super.render(g); 175 176 g.setTransform(cacheXform); 177 } 178 179 @Override 180 public Shape getShape() 181 { 182 Shape shape = super.getShape(); 183 return markerXform.createTransformedShape(shape); 184 } 185 186 @Override 187 public Rectangle2D getBoundingBox() throws SVGException 188 { 189 Rectangle2D rect = super.getBoundingBox(); 190 return markerXform.createTransformedShape(rect).getBounds2D(); 191 } 192 193 /** 194 * Updates all attributes in this diagram associated with a time event. Ie, 195 * all attributes with track information. 196 * 197 * @return - true if this node has changed state as a result of the time 198 * update 199 */ 200 @Override 201 public boolean updateTime(double curTime) throws SVGException 202 { 203 boolean changeState = super.updateTime(curTime); 204 205 build(); 206 207 //Marker properties do not change 208 return changeState; 209 } 210 211 //-------------------------------- 212 public static final int MARKER_START = 0; 213 public static final int MARKER_MID = 1; 214 public static final int MARKER_END = 2; 215 216 public static class MarkerPos 217 { 218 219 int type; 220 double x; 221 double y; 222 double dx; 223 double dy; 224 225 public MarkerPos(int type, double x, double y, double dx, double dy) 226 { 227 this.type = type; 228 this.x = x; 229 this.y = y; 230 this.dx = dx; 231 this.dy = dy; 232 } 233 } 234 235 public static class MarkerLayout 236 { 237 238 private ArrayList<MarkerPos> markerList = new ArrayList<MarkerPos>(); 239 boolean started = false; 240 241 public void layout(Shape shape) 242 { 243 double px = 0; 244 double py = 0; 245 double[] coords = new double[6]; 246 for (PathIterator it = shape.getPathIterator(null); 247 !it.isDone(); it.next()) 248 { 249 switch (it.currentSegment(coords)) 250 { 251 case PathIterator.SEG_MOVETO: 252 px = coords[0]; 253 py = coords[1]; 254 started = false; 255 break; 256 case PathIterator.SEG_CLOSE: 257 started = false; 258 break; 259 case PathIterator.SEG_LINETO: 260 { 261 double x = coords[0]; 262 double y = coords[1]; 263 markerIn(px, py, x - px, y - py); 264 markerOut(x, y, x - px, y - py); 265 px = x; 266 py = y; 267 break; 268 } 269 case PathIterator.SEG_QUADTO: 270 { 271 double k0x = coords[0]; 272 double k0y = coords[1]; 273 double x = coords[2]; 274 double y = coords[3]; 275 276 277 //Best in tangent 278 if (px != k0x || py != k0y) 279 { 280 markerIn(px, py, k0x - px, k0y - py); 281 } else 282 { 283 markerIn(px, py, x - px, y - py); 284 } 285 286 //Best out tangent 287 if (x != k0x || y != k0y) 288 { 289 markerOut(x, y, x - k0x, y - k0y); 290 } else 291 { 292 markerOut(x, y, x - px, y - py); 293 } 294 295 markerIn(px, py, k0x - px, k0y - py); 296 markerOut(x, y, x - k0x, y - k0y); 297 px = x; 298 py = y; 299 break; 300 } 301 case PathIterator.SEG_CUBICTO: 302 { 303 double k0x = coords[0]; 304 double k0y = coords[1]; 305 double k1x = coords[2]; 306 double k1y = coords[3]; 307 double x = coords[4]; 308 double y = coords[5]; 309 310 //Best in tangent 311 if (px != k0x || py != k0y) 312 { 313 markerIn(px, py, k0x - px, k0y - py); 314 } else if (px != k1x || py != k1y) 315 { 316 markerIn(px, py, k1x - px, k1y - py); 317 } else 318 { 319 markerIn(px, py, x - px, y - py); 320 } 321 322 //Best out tangent 323 if (x != k1x || y != k1y) 324 { 325 markerOut(x, y, x - k1x, y - k1y); 326 } else if (x != k0x || y != k0y) 327 { 328 markerOut(x, y, x - k0x, y - k0y); 329 } else 330 { 331 markerOut(x, y, x - px, y - py); 332 } 333 px = x; 334 py = y; 335 break; 336 } 337 } 338 } 339 340 for (int i = 1; i < markerList.size(); ++i) 341 { 342 MarkerPos prev = (MarkerPos) markerList.get(i - 1); 343 MarkerPos cur = (MarkerPos) markerList.get(i); 344 345 if (cur.type == MARKER_START) 346 { 347 prev.type = MARKER_END; 348 } 349 } 350 MarkerPos last = (MarkerPos) markerList.get(markerList.size() - 1); 351 last.type = MARKER_END; 352 } 353 354 private void markerIn(double x, double y, double dx, double dy) 355 { 356 if (started == false) 357 { 358 started = true; 359 markerList.add(new MarkerPos(MARKER_START, x, y, dx, dy)); 360 } 361 } 362 363 private void markerOut(double x, double y, double dx, double dy) 364 { 365 markerList.add(new MarkerPos(MARKER_MID, x, y, dx, dy)); 366 } 367 368 /** 369 * @return the markerList 370 */ 371 public ArrayList<MarkerPos> getMarkerList() 372 { 373 return markerList; 374 } 375 } 376}