1 18 package org.apache.batik.gvt.filter; 19 20 import java.awt.Shape ; 21 import java.awt.geom.AffineTransform ; 22 import java.awt.geom.NoninvertibleTransformException ; 23 import java.awt.geom.Rectangle2D ; 24 import java.awt.image.RenderedImage ; 25 import java.awt.image.renderable.RenderContext ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Vector ; 29 30 import org.apache.batik.ext.awt.image.CompositeRule; 31 import org.apache.batik.ext.awt.image.PadMode; 32 import org.apache.batik.ext.awt.image.renderable.AbstractRable; 33 import org.apache.batik.ext.awt.image.renderable.AffineRable8Bit; 34 import org.apache.batik.ext.awt.image.renderable.CompositeRable8Bit; 35 import org.apache.batik.ext.awt.image.renderable.Filter; 36 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit; 37 import org.apache.batik.gvt.CompositeGraphicsNode; 38 import org.apache.batik.gvt.GraphicsNode; 39 40 48 public class BackgroundRable8Bit 49 extends AbstractRable { 50 51 54 private GraphicsNode node; 55 56 59 public GraphicsNode getGraphicsNode(){ 60 return node; 61 } 62 63 66 public void setGraphicsNode(GraphicsNode node){ 67 if(node == null){ 68 throw new IllegalArgumentException (); 69 } 70 71 this.node = node; 72 } 73 74 77 public BackgroundRable8Bit(GraphicsNode node){ 78 if(node == null) 79 throw new IllegalArgumentException (); 80 81 this.node = node; 82 } 83 84 85 static Rectangle2D addBounds(CompositeGraphicsNode cgn, 89 GraphicsNode child, 90 Rectangle2D init) { 91 94 List children = cgn.getChildren(); 95 Iterator i = children.iterator(); 96 Rectangle2D r2d = null; 97 while (i.hasNext()) { 98 GraphicsNode gn = (GraphicsNode)i.next(); 99 if (gn == child) 100 break; 101 102 Rectangle2D cr2d = gn.getBounds(); 104 AffineTransform at = gn.getTransform(); 105 if (at != null) 106 cr2d = at.createTransformedShape(cr2d).getBounds2D(); 107 108 if (r2d == null) r2d = (Rectangle2D )cr2d.clone(); 109 else r2d.add(cr2d); 110 } 111 112 if (r2d == null) { 113 if (init == null) 114 return CompositeGraphicsNode.VIEWPORT; 115 116 return init; 117 } 118 119 if (init == null) 120 return r2d; 121 122 init.add(r2d); 123 124 return init; 125 } 126 127 128 static Rectangle2D getViewportBounds(GraphicsNode gn, 129 GraphicsNode child) { 130 Rectangle2D r2d = null; 132 if (gn instanceof CompositeGraphicsNode) { 133 CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn; 134 r2d = cgn.getBackgroundEnable(); 135 } 136 137 if (r2d == null) 138 r2d = getViewportBounds(gn.getParent(), gn); 140 141 if (r2d == null) 143 return null; 144 145 if (r2d == CompositeGraphicsNode.VIEWPORT) { 147 if (child == null) 149 return (Rectangle2D )gn.getPrimitiveBounds().clone(); 150 151 CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn; 154 return addBounds(cgn, child, null); 155 } 156 157 AffineTransform at = gn.getTransform(); 160 if (at != null) { 161 try { 162 at = at.createInverse(); 163 r2d = at.createTransformedShape(r2d).getBounds2D(); 164 } catch (NoninvertibleTransformException nte) { 165 r2d = null; 167 } 168 } 169 170 if (child != null) { 171 CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn; 173 r2d = addBounds(cgn, child, r2d); 174 } else { 175 Rectangle2D gnb = gn.getPrimitiveBounds(); 176 if (gnb != null) 177 r2d.add(gnb); 178 } 179 180 return r2d; 181 } 182 183 static Rectangle2D getBoundsRecursive(GraphicsNode gn, 187 GraphicsNode child) { 188 189 Rectangle2D r2d = null; 190 if (gn == null) { 191 return null; 193 } 194 195 if (gn instanceof CompositeGraphicsNode) { 196 CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn; 197 r2d = cgn.getBackgroundEnable(); 199 } 200 201 if (r2d != null) 203 return r2d; 204 205 r2d = getBoundsRecursive(gn.getParent(), gn); 207 208 if (r2d == null) { 210 return new Rectangle2D.Float (0, 0, 0, 0); 212 } 213 214 if (r2d == CompositeGraphicsNode.VIEWPORT) 217 return r2d; 218 219 AffineTransform at = gn.getTransform(); 220 if (at != null) { 221 try { 222 at = at.createInverse(); 225 r2d = at.createTransformedShape(r2d).getBounds2D(); 226 } catch (NoninvertibleTransformException nte) { 227 r2d = null; 229 } 230 } 231 232 return r2d; 233 } 234 235 238 public Rectangle2D getBounds2D() { 239 Rectangle2D r2d = getBoundsRecursive(node, null); 241 242 244 if (r2d == CompositeGraphicsNode.VIEWPORT) { 245 r2d = getViewportBounds(node, null); 246 } 248 249 return r2d; 250 } 251 252 261 public Filter getBackground(GraphicsNode gn, 262 GraphicsNode child, 263 Rectangle2D aoi) { 264 if (gn == null) { 265 throw new IllegalArgumentException 266 ("BackgroundImage requested yet no parent has " + 267 "'enable-background:new'"); 268 } 269 270 Rectangle2D r2d = null; 271 if (gn instanceof CompositeGraphicsNode) { 272 CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn; 273 r2d = cgn.getBackgroundEnable(); 274 } 275 276 Vector srcs = new Vector (); 277 if (r2d == null) { 278 Rectangle2D paoi = aoi; 279 AffineTransform at = gn.getTransform(); 280 if (at != null) 281 paoi = at.createTransformedShape(aoi).getBounds2D(); 282 Filter f = getBackground(gn.getParent(), gn, paoi); 283 284 if ((f != null) && f.getBounds2D().intersects(aoi)) { 286 srcs.add(f); 287 } 288 } 289 290 if (child != null) { 291 CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn; 292 List children = cgn.getChildren(); 293 Iterator i = children.iterator(); 294 while (i.hasNext()) { 295 GraphicsNode childGN = (GraphicsNode)i.next(); 296 if (childGN == child) 300 break; 301 302 Rectangle2D cbounds = childGN.getBounds(); 303 307 AffineTransform at = childGN.getTransform(); 308 if (at != null) 309 cbounds = at.createTransformedShape(cbounds).getBounds2D(); 310 311 312 if (aoi.intersects(cbounds)) { 313 srcs.add(childGN.getEnableBackgroundGraphicsNodeRable 314 (true)); 315 } 316 } 317 } 318 319 if (srcs.size() == 0) 320 return null; 321 322 Filter ret = null; 323 if (srcs.size() == 1) 324 ret = (Filter)srcs.get(0); 325 else 326 ret = new CompositeRable8Bit(srcs, CompositeRule.OVER, false); 327 328 if (child != null) { 329 333 AffineTransform at = child.getTransform(); 334 if (at != null) { 335 try { 336 at = at.createInverse(); 337 ret = new AffineRable8Bit(ret, at); 338 } catch (NoninvertibleTransformException nte) { 339 ret = null; 340 } 341 } 342 } 343 344 return ret; 345 } 346 347 354 public boolean isDynamic(){ 355 return false; 356 } 357 358 373 public RenderedImage createRendering(RenderContext renderContext){ 374 375 Rectangle2D r2d = getBounds2D(); 376 377 379 Shape aoi = renderContext.getAreaOfInterest(); 380 if (aoi != null) { 381 Rectangle2D aoiR2d = aoi.getBounds2D(); 382 385 if (r2d.intersects(aoiR2d) == false) 386 return null; 387 388 Rectangle2D.intersect(r2d, aoiR2d, r2d); 389 } 390 391 Filter background = getBackground(node, null, r2d); 392 if ( background == null) 394 return null; 395 396 background = new PadRable8Bit(background, r2d, PadMode.ZERO_PAD); 397 398 399 RenderedImage ri = background.createRendering 400 (new RenderContext (renderContext.getTransform(), r2d, 401 renderContext.getRenderingHints())); 402 return ri; 408 } 409 } 410 | Popular Tags |