1 18 package org.apache.batik.gvt.svg12; 19 20 import java.awt.Dimension ; 21 import java.awt.Graphics2D ; 22 import java.awt.Shape ; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.geom.Rectangle2D ; 25 import java.lang.ref.SoftReference ; 26 27 import org.apache.batik.bridge.BridgeContext; 28 import org.apache.batik.bridge.GVTBuilder; 29 import org.apache.batik.gvt.AbstractGraphicsNode; 30 import org.apache.batik.gvt.GraphicsNode; 31 import org.apache.batik.util.SVGConstants; 32 import org.w3c.dom.Element ; 33 34 41 public class MultiResGraphicsNode 42 extends AbstractGraphicsNode implements SVGConstants { 43 44 SoftReference [] srcs; 45 Element [] srcElems; 46 Dimension [] minSz; 47 Dimension [] maxSz; 48 Rectangle2D bounds; 49 50 BridgeContext ctx; 51 52 Element multiImgElem; 53 54 public MultiResGraphicsNode(Element multiImgElem, 55 Rectangle2D bounds, 56 Element [] srcElems, 57 Dimension [] minSz, 58 Dimension [] maxSz, 59 BridgeContext ctx) { 60 61 this.multiImgElem = multiImgElem; 62 this.srcElems = new Element [srcElems.length]; 63 this.minSz = new Dimension [srcElems.length]; 64 this.maxSz = new Dimension [srcElems.length]; 65 this.ctx = ctx; 66 67 for (int i=0; i<srcElems.length; i++) { 68 this.srcElems[i] = srcElems[i]; 69 this.minSz[i] = minSz[i]; 70 this.maxSz[i] = maxSz[i]; 71 } 72 73 this.srcs = new SoftReference [srcElems.length]; 74 this.bounds = bounds; 75 } 76 77 82 public void primitivePaint(Graphics2D g2d) { 83 AffineTransform at = g2d.getTransform(); 85 86 double scx = Math.sqrt(at.getShearY()*at.getShearY()+ 87 at.getScaleX()*at.getScaleX()); 88 double scy = Math.sqrt(at.getShearX()*at.getShearX()+ 89 at.getScaleY()*at.getScaleY()); 90 91 GraphicsNode gn = null; 92 int idx =-1; 93 double w = bounds.getWidth()*scx; 94 double minDist = calcDist(w, minSz[0], maxSz[0]); 95 int minIdx = 0; 96 for (int i=0; i<minSz.length; i++) { 98 double dist = calcDist(w, minSz[i], maxSz[i]); 99 if (dist < minDist) { 101 minDist = dist; 102 minIdx = i; 103 } 104 105 if (((minSz[i] == null) || (w >= minSz[i].width)) && 106 ((maxSz[i] == null) || (w <= maxSz[i].width))) { 107 if ((idx == -1) || (minIdx == i)) { 111 idx = i; 112 } 113 } 114 } 115 116 if (idx == -1) 117 idx = minIdx; 118 gn = getGraphicsNode(idx); 119 if (gn == null) return; 120 121 Rectangle2D gnBounds = gn.getBounds(); 124 if (gnBounds == null) return; 125 126 double gnDevW = gnBounds.getWidth()*scx; 127 double gnDevH = gnBounds.getHeight()*scy; 128 double gnDevX = gnBounds.getX()*scx; 129 double gnDevY = gnBounds.getY()*scy; 130 double gnDevX0, gnDevX1, gnDevY0, gnDevY1; 131 if (gnDevW < 0) { 132 gnDevX0 = gnDevX+gnDevW; 133 gnDevX1 = gnDevX; 134 } else { 135 gnDevX0 = gnDevX; 136 gnDevX1 = gnDevX+gnDevW; 137 } 138 if (gnDevH < 0) { 139 gnDevY0 = gnDevY+gnDevH; 140 gnDevY1 = gnDevY; 141 } else { 142 gnDevY0 = gnDevY; 143 gnDevY1 = gnDevY+gnDevH; 144 } 145 gnDevW = (int)(Math.ceil(gnDevX1)-Math.floor(gnDevX0)); 148 gnDevH = (int)(Math.ceil(gnDevY1)-Math.floor(gnDevY0)); 149 scx = (gnDevW/gnBounds.getWidth())/scx; 150 scy = (gnDevH/gnBounds.getHeight())/scy; 151 152 AffineTransform nat = g2d.getTransform(); 155 nat = new AffineTransform (nat.getScaleX()*scx, nat.getShearY()*scx, 156 nat.getShearX()*scy, nat.getScaleY()*scy, 157 nat.getTranslateX(), nat.getTranslateY()); 158 g2d.setTransform(nat); 159 160 164 gn.paint(g2d); 165 } 166 167 public double calcDist(double loc, Dimension min, Dimension max) { 179 if (min == null) { 180 if (max == null) 181 return 10E10; else 183 return Math.abs(loc-max.width); 184 } else { 185 if (max == null) 186 return Math.abs(loc-min.width); 187 else { 188 double mid = (max.width+min.width)/2.0; 189 return Math.abs(loc-mid); 190 } 191 } 192 } 193 194 197 public Rectangle2D getPrimitiveBounds() { 198 return bounds; 199 } 200 201 public Rectangle2D getGeometryBounds(){ 202 return bounds; 203 } 204 205 public Rectangle2D getSensitiveBounds(){ 206 return bounds; 207 } 208 209 212 public Shape getOutline() { 213 return bounds; 214 } 215 216 public GraphicsNode getGraphicsNode(int idx) { 217 if (srcs[idx] != null) { 218 Object o = srcs[idx].get(); 219 if (o != null) 220 return (GraphicsNode)o; 221 } 222 223 try { 224 GVTBuilder builder = ctx.getGVTBuilder(); 225 GraphicsNode gn; 226 gn = builder.build(ctx, srcElems[idx]); 227 srcs[idx] = new SoftReference (gn); 228 return gn; 229 } catch (Exception ex) { ex.printStackTrace(); } 230 231 return null; 232 } 233 } 234 235 | Popular Tags |