1 18 package org.apache.batik.gvt.filter; 19 20 import java.awt.Composite ; 21 import java.awt.Graphics2D ; 22 import java.awt.color.ColorSpace ; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.geom.Rectangle2D ; 25 import java.awt.image.RenderedImage ; 26 import java.awt.image.renderable.RenderContext ; 27 import java.util.Map ; 28 29 import org.apache.batik.ext.awt.image.GraphicsUtil; 30 import org.apache.batik.ext.awt.image.SVGComposite; 31 import org.apache.batik.ext.awt.image.renderable.AbstractRable; 32 import org.apache.batik.ext.awt.image.renderable.Filter; 33 import org.apache.batik.ext.awt.image.renderable.PaintRable; 34 import org.apache.batik.ext.awt.image.rendered.CachableRed; 35 import org.apache.batik.ext.awt.image.rendered.TranslateRed; 36 import org.apache.batik.gvt.GraphicsNode; 37 38 46 public class GraphicsNodeRable8Bit 47 extends AbstractRable 48 implements GraphicsNodeRable, PaintRable { 49 50 private AffineTransform cachedGn2dev = null; 51 private AffineTransform cachedUsr2dev = null; 52 private CachableRed cachedRed = null; 53 private Rectangle2D cachedBounds = null; 54 57 private boolean usePrimitivePaint = true; 58 59 64 public boolean getUsePrimitivePaint() { 65 return usePrimitivePaint; 66 } 67 68 73 public void setUsePrimitivePaint(boolean usePrimitivePaint) { 74 this.usePrimitivePaint = usePrimitivePaint; 75 } 76 77 80 private GraphicsNode node; 81 82 85 public GraphicsNode getGraphicsNode(){ 86 return node; 87 } 88 89 92 public void setGraphicsNode(GraphicsNode node){ 93 if(node == null){ 94 throw new IllegalArgumentException (); 95 } 96 97 this.node = node; 98 } 99 100 103 public void clearCache() { 104 cachedRed = null; 105 cachedUsr2dev = null; 106 cachedGn2dev = null; 107 cachedBounds = null; 108 } 109 110 113 public GraphicsNodeRable8Bit(GraphicsNode node){ 114 if(node == null) 115 throw new IllegalArgumentException (); 116 117 this.node = node; 118 this.usePrimitivePaint = true; 119 } 120 121 125 public GraphicsNodeRable8Bit(GraphicsNode node, 126 Map props){ 127 super((Filter)null, props); 128 129 if(node == null) 130 throw new IllegalArgumentException (); 131 132 this.node = node; 133 this.usePrimitivePaint = true; 134 } 135 136 141 public GraphicsNodeRable8Bit(GraphicsNode node, 142 boolean usePrimitivePaint){ 143 if(node == null) 144 throw new IllegalArgumentException (); 145 146 this.node = node; 147 this.usePrimitivePaint = usePrimitivePaint; 148 } 149 150 153 public Rectangle2D getBounds2D(){ 154 if (usePrimitivePaint){ 155 Rectangle2D primitiveBounds = node.getPrimitiveBounds(); 156 if(primitiveBounds == null) 157 return new Rectangle2D.Double (0, 0, 0, 0); 158 159 return (Rectangle2D )(primitiveBounds.clone()); 160 } 161 162 Rectangle2D bounds = node.getBounds(); 168 if(bounds == null){ 169 return new Rectangle2D.Double (0, 0, 0, 0); 170 } 171 172 AffineTransform at = node.getTransform(); 173 if (at != null){ 174 bounds = at.createTransformedShape(bounds).getBounds2D(); 175 } 176 return bounds; 177 } 178 179 186 public boolean isDynamic(){ 187 return false; 188 } 189 190 200 public boolean paintRable(Graphics2D g2d) { 201 Composite c = g2d.getComposite(); 204 if (!SVGComposite.OVER.equals(c)) 205 return false; 206 207 ColorSpace g2dCS = GraphicsUtil.getDestinationColorSpace(g2d); 208 if ((g2dCS == null) || 209 (g2dCS != ColorSpace.getInstance(ColorSpace.CS_sRGB))){ 210 return false; 212 } 213 214 GraphicsNode gn = getGraphicsNode(); 216 if (getUsePrimitivePaint()){ 217 gn.primitivePaint(g2d); 218 } 219 else{ 220 gn.paint(g2d); 221 } 222 223 return true; 225 } 226 227 242 public RenderedImage createRendering(RenderContext renderContext){ 243 AffineTransform usr2dev = renderContext.getTransform(); 245 246 AffineTransform gn2dev; 247 if (usr2dev == null) { 248 usr2dev = new AffineTransform (); 249 gn2dev = usr2dev; 250 } else { 251 gn2dev = (AffineTransform )usr2dev.clone(); 252 } 253 254 AffineTransform gn2usr = node.getTransform(); 256 if (gn2usr != null) { 257 gn2dev.concatenate(gn2usr); 258 } 259 260 Rectangle2D bounds2D = getBounds2D(); 261 262 if ((cachedBounds != null) && 263 (cachedGn2dev != null) && 264 (cachedBounds.equals(bounds2D)) && 265 (gn2dev.getScaleX() == cachedGn2dev.getScaleX()) && 266 (gn2dev.getScaleY() == cachedGn2dev.getScaleY()) && 267 (gn2dev.getShearX() == cachedGn2dev.getShearX()) && 268 (gn2dev.getShearY() == cachedGn2dev.getShearY())) 269 { 270 double deltaX = (usr2dev.getTranslateX() - 272 cachedUsr2dev.getTranslateX()); 273 double deltaY = (usr2dev.getTranslateY() - 274 cachedUsr2dev.getTranslateY()); 275 276 if ((deltaX ==0) && (deltaY == 0)) 279 return cachedRed; 281 282 284 if ((deltaX == (int)deltaX) && 286 (deltaY == (int)deltaY)) { 287 return new TranslateRed 288 (cachedRed, 289 (int)Math.round(cachedRed.getMinX()+deltaX), 290 (int)Math.round(cachedRed.getMinY()+deltaY)); 291 } 292 } 293 294 if (false) { 296 System.out.println("Not using Cached Red: " + usr2dev); 297 System.out.println("Old: " + cachedUsr2dev); 298 } 299 300 if((bounds2D.getWidth() > 0) && 301 (bounds2D.getHeight() > 0)) { 302 cachedUsr2dev = (AffineTransform )usr2dev.clone(); 303 cachedGn2dev = gn2dev; 304 cachedBounds = bounds2D; 305 cachedRed = new GraphicsNodeRed8Bit 306 (node, usr2dev, usePrimitivePaint, 307 renderContext.getRenderingHints()); 308 return cachedRed; 309 } 310 311 cachedUsr2dev = null; 312 cachedGn2dev = null; 313 cachedBounds = null; 314 cachedRed = null; 315 return null; 316 } 317 } 318 | Popular Tags |