1 18 package org.apache.batik.dom.svg; 19 20 import java.awt.geom.AffineTransform ; 21 import java.awt.geom.NoninvertibleTransformException ; 22 23 import org.apache.batik.css.engine.SVGCSSEngine; 24 import org.w3c.dom.DOMException ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.svg.SVGElement; 27 import org.w3c.dom.svg.SVGException; 28 import org.w3c.dom.svg.SVGFitToViewBox; 29 import org.w3c.dom.svg.SVGMatrix; 30 import org.w3c.dom.svg.SVGRect; 31 32 38 public class SVGLocatableSupport { 39 42 public SVGLocatableSupport() { 43 } 44 45 49 public static SVGElement getNearestViewportElement(Element e) { 50 Element elt = e; 51 while (elt != null) { 52 elt = SVGCSSEngine.getParentCSSStylableElement(elt); 53 if (elt instanceof SVGFitToViewBox) { 54 break; 55 } 56 } 57 return (SVGElement)elt; 58 } 59 60 64 public static SVGElement getFarthestViewportElement(Element elt) { 65 return (SVGElement)elt.getOwnerDocument().getDocumentElement(); 66 } 67 68 71 public static SVGRect getBBox(Element elt) { 72 final SVGOMElement svgelt = (SVGOMElement)elt; 73 SVGContext svgctx = svgelt.getSVGContext(); 74 if (svgctx == null) return null; 75 if (svgctx.getBBox() == null) return null; 76 77 return new SVGRect() { 78 public float getX() { 79 return (float)svgelt.getSVGContext().getBBox().getX(); 80 } 81 public void setX(float x) throws DOMException { 82 throw svgelt.createDOMException 83 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 84 "readonly.rect", null); 85 } 86 public float getY() { 87 return (float)svgelt.getSVGContext().getBBox().getY(); 88 } 89 public void setY(float y) throws DOMException { 90 throw svgelt.createDOMException 91 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 92 "readonly.rect", null); 93 } 94 public float getWidth() { 95 return (float)svgelt.getSVGContext().getBBox().getWidth(); 96 } 97 public void setWidth(float width) throws DOMException { 98 throw svgelt.createDOMException 99 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 100 "readonly.rect", null); 101 } 102 public float getHeight() { 103 return (float)svgelt.getSVGContext().getBBox().getHeight(); 104 } 105 public void setHeight(float height) throws DOMException { 106 throw svgelt.createDOMException 107 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 108 "readonly.rect", null); 109 } 110 }; 111 } 112 113 116 public static SVGMatrix getCTM(Element elt) { 117 final SVGOMElement svgelt = (SVGOMElement)elt; 118 return new AbstractSVGMatrix() { 119 protected AffineTransform getAffineTransform() { 120 return svgelt.getSVGContext().getCTM(); 121 } 122 }; 123 } 124 125 128 public static SVGMatrix getScreenCTM(Element elt) { 129 final SVGOMElement svgelt = (SVGOMElement)elt; 130 return new AbstractSVGMatrix() { 131 protected AffineTransform getAffineTransform() { 132 SVGContext context = svgelt.getSVGContext(); 133 AffineTransform ret = context.getGlobalTransform(); 134 AffineTransform scrnTrans = context.getScreenTransform(); 135 if (scrnTrans != null) 136 ret.preConcatenate(scrnTrans); 137 return ret; 138 } 139 }; 140 } 141 142 146 public static SVGMatrix getTransformToElement(Element elt, 147 SVGElement element) 148 throws SVGException { 149 final SVGOMElement currentElt = (SVGOMElement)elt; 150 final SVGOMElement targetElt = (SVGOMElement)element; 151 return new AbstractSVGMatrix() { 152 protected AffineTransform getAffineTransform() { 153 AffineTransform cat = 154 currentElt.getSVGContext().getGlobalTransform(); 155 if (cat == null) { 156 cat = new AffineTransform (); 157 } 158 AffineTransform tat = 159 targetElt.getSVGContext().getGlobalTransform(); 160 if (tat == null) { 161 tat = new AffineTransform (); 162 } 163 AffineTransform at = new AffineTransform (cat); 164 try { 165 at.preConcatenate(tat.createInverse()); 166 return at; 167 } catch (NoninvertibleTransformException ex) { 168 throw currentElt.createSVGException 169 (SVGException.SVG_MATRIX_NOT_INVERTABLE, 170 "noninvertiblematrix", 171 null); 172 } 173 } 174 }; 175 } 176 } 177 | Popular Tags |