Changeset 6002 in josm for trunk/src/com/kitfox/svg/Group.java
- Timestamp:
- 2013-06-11T01:01:28+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/Group.java
r4256 r6002 1 1 /* 2 * Stop.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, 1:56 AM 26 35 */ 27 28 36 package com.kitfox.svg; 29 37 30 38 import com.kitfox.svg.xml.StyleAttribute; 31 39 import java.awt.Graphics2D; 32 import java.awt.Rectangle;33 40 import java.awt.Shape; 34 41 import java.awt.geom.AffineTransform; … … 40 47 import java.util.List; 41 48 42 43 49 /** 44 50 * @author Mark McKay … … 47 53 public class Group extends ShapeElement 48 54 { 49 55 public static final String TAG_NAME = "group"; 56 50 57 //Cache bounding box for faster clip testing 51 58 Rectangle2D boundingBox; 52 59 Shape cachedShape; 53 60 54 //Cache clip bounds 55 final Rectangle clipBounds = new Rectangle(); 56 57 /** Creates a new instance of Stop */ 58 public Group() { 59 } 60 61 /* 62 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) 63 { 64 //Load style string 65 super.loaderStartElement(helper, attrs, parent); 66 67 //String transform = attrs.getValue("transform"); 68 } 69 */ 61 /** 62 * Creates a new instance of Stop 63 */ 64 public Group() 65 { 66 } 67 68 public String getTagName() 69 { 70 return TAG_NAME; 71 } 70 72 71 73 /** … … 76 78 { 77 79 super.loaderAddChild(helper, child); 78 79 // members.add(child);80 80 } 81 81 82 82 protected boolean outsideClip(Graphics2D g) throws SVGException 83 83 { 84 g.getClipBounds(clipBounds); 84 Shape clip = g.getClip(); 85 if (clip == null) 86 { 87 return false; 88 } 89 //g.getClipBounds(clipBounds); 85 90 Rectangle2D rect = getBoundingBox(); 86 91 87 //if (rect == null) 88 //{ 89 // rect = getBoundingBox(); 90 //} 91 92 // if (rect.intersects(clipBounds)) 93 if (rect.intersects(clipBounds)) 92 if (clip.intersects(rect)) 94 93 { 95 94 return false; … … 107 106 { 108 107 xform.inverseTransform(point, xPoint); 109 } 110 catch (NoninvertibleTransformException ex) 108 } catch (NoninvertibleTransformException ex) 111 109 { 112 110 throw new SVGException(ex); 113 111 } 114 112 } 115 116 113 114 117 115 for (Iterator it = children.iterator(); it.hasNext();) 118 116 { 119 SVGElement ele = (SVGElement) it.next();120 if (ele instanceof RenderableElement) 121 { 122 RenderableElement rendEle = (RenderableElement) ele;123 117 SVGElement ele = (SVGElement) it.next(); 118 if (ele instanceof RenderableElement) 119 { 120 RenderableElement rendEle = (RenderableElement) ele; 121 124 122 rendEle.pick(xPoint, boundingBox, retVec); 125 123 } … … 134 132 ltw.concatenate(xform); 135 133 } 136 137 134 135 138 136 for (Iterator it = children.iterator(); it.hasNext();) 139 137 { 140 SVGElement ele = (SVGElement) it.next();141 if (ele instanceof RenderableElement) 142 { 143 RenderableElement rendEle = (RenderableElement) ele;144 138 SVGElement ele = (SVGElement) it.next(); 139 if (ele instanceof RenderableElement) 140 { 141 RenderableElement rendEle = (RenderableElement) ele; 142 145 143 rendEle.pick(pickArea, ltw, boundingBox, retVec); 146 144 } … … 154 152 if (getStyle(styleAttrib.setName("visibility"))) 155 153 { 156 if (!styleAttrib.getStringValue().equals("visible")) return; 157 } 158 154 if (!styleAttrib.getStringValue().equals("visible")) 155 { 156 return; 157 } 158 } 159 159 160 //Do not process offscreen groups 160 161 boolean ignoreClip = diagram.ignoringClipHeuristic(); 161 if (!ignoreClip && outsideClip(g)) return; 162 if (!ignoreClip && outsideClip(g)) 163 { 164 return; 165 } 162 166 163 167 beginLayer(g); … … 165 169 Iterator it = children.iterator(); 166 170 167 try 168 { 169 g.getClipBounds(clipBounds); 170 } 171 catch (Exception e) 172 { 173 //For some reason, getClipBounds can throw a null pointer exception for 174 // some types of Graphics2D 175 ignoreClip = true; 176 } 177 171 // try 172 // { 173 // g.getClipBounds(clipBounds); 174 // } 175 // catch (Exception e) 176 // { 177 // //For some reason, getClipBounds can throw a null pointer exception for 178 // // some types of Graphics2D 179 // ignoreClip = true; 180 // } 181 182 Shape clip = g.getClip(); 178 183 while (it.hasNext()) 179 184 { 180 SVGElement ele = (SVGElement) it.next();181 if (ele instanceof RenderableElement) 182 { 183 RenderableElement rendEle = (RenderableElement) ele;185 SVGElement ele = (SVGElement) it.next(); 186 if (ele instanceof RenderableElement) 187 { 188 RenderableElement rendEle = (RenderableElement) ele; 184 189 185 190 // if (shapeEle == null) continue; … … 188 193 { 189 194 //Skip if clipping area is outside our bounds 190 if (!ignoreClip && !rendEle.getBoundingBox().intersects(clipBounds)) 195 if (!ignoreClip && clip != null 196 && !clip.intersects(rendEle.getBoundingBox())) 191 197 { 192 198 continue; … … 201 207 } 202 208 203 204 209 /** 205 210 * Retrieves the cached bounding box of this group … … 207 212 public Shape getShape() 208 213 { 209 if (cachedShape == null) calcShape(); 214 if (cachedShape == null) 215 { 216 calcShape(); 217 } 210 218 return cachedShape; 211 219 } … … 217 225 for (Iterator it = children.iterator(); it.hasNext();) 218 226 { 219 SVGElement ele = (SVGElement) it.next();227 SVGElement ele = (SVGElement) it.next(); 220 228 221 229 if (ele instanceof ShapeElement) 222 230 { 223 ShapeElement shpEle = (ShapeElement) ele;231 ShapeElement shpEle = (ShapeElement) ele; 224 232 Shape shape = shpEle.getShape(); 225 233 if (shape != null) … … 238 246 public Rectangle2D getBoundingBox() throws SVGException 239 247 { 240 if (boundingBox == null) calcBoundingBox(); 248 if (boundingBox == null) 249 { 250 calcBoundingBox(); 251 } 241 252 // calcBoundingBox(); 242 253 return boundingBox; … … 245 256 /** 246 257 * Recalculates the bounding box by taking the union of the bounding boxes 247 * of all children. 258 * of all children. Caches the result. 248 259 */ 249 260 public void calcBoundingBox() throws SVGException … … 254 265 for (Iterator it = children.iterator(); it.hasNext();) 255 266 { 256 SVGElement ele = (SVGElement) it.next();257 258 if (ele instanceof RenderableElement) 259 { 260 RenderableElement rendEle = (RenderableElement) ele;267 SVGElement ele = (SVGElement) it.next(); 268 269 if (ele instanceof RenderableElement) 270 { 271 RenderableElement rendEle = (RenderableElement) ele; 261 272 Rectangle2D bounds = rendEle.getBoundingBox(); 262 273 if (bounds != null) 263 274 { 264 if (retRect == null) retRect = bounds; 265 else retRect = retRect.createUnion(bounds); 275 if (retRect == null) 276 { 277 retRect = bounds; 278 } else 279 { 280 retRect = retRect.createUnion(bounds); 281 } 266 282 } 267 283 } … … 274 290 275 291 //If no contents, use degenerate rectangle 276 if (retRect == null) retRect = new Rectangle2D.Float(); 292 if (retRect == null) 293 { 294 retRect = new Rectangle2D.Float(); 295 } 277 296 278 297 boundingBox = boundsToParent(retRect); … … 287 306 while (it.hasNext()) 288 307 { 289 SVGElement ele = (SVGElement) it.next();308 SVGElement ele = (SVGElement) it.next(); 290 309 boolean updateVal = ele.updateTime(curTime); 291 310 … … 293 312 294 313 //Update our shape if shape aware children change 295 if (ele instanceof ShapeElement) cachedShape = null; 296 if (ele instanceof RenderableElement) boundingBox = null; 314 if (ele instanceof ShapeElement) 315 { 316 cachedShape = null; 317 } 318 if (ele instanceof RenderableElement) 319 { 320 boundingBox = null; 321 } 297 322 } 298 323
Note:
See TracChangeset
for help on using the changeset viewer.