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.w3c.dom.DOMException ; 24 import org.w3c.dom.svg.SVGException; 25 import org.w3c.dom.svg.SVGMatrix; 26 27 34 public abstract class AbstractSVGMatrix implements SVGMatrix { 35 36 39 protected final static AffineTransform FLIP_X_TRANSFORM = 40 new AffineTransform (-1, 0, 0, 1, 0, 0); 41 42 45 protected final static AffineTransform FLIP_Y_TRANSFORM = 46 new AffineTransform (1, 0, 0, -1, 0, 0); 47 48 51 protected abstract AffineTransform getAffineTransform(); 52 53 56 public float getA() { 57 return (float)getAffineTransform().getScaleX(); 58 } 59 60 63 public void setA(float a) throws DOMException { 64 AffineTransform at = getAffineTransform(); 65 at.setTransform(a, 66 at.getShearY(), 67 at.getShearX(), 68 at.getScaleY(), 69 at.getTranslateX(), 70 at.getTranslateY()); 71 } 72 73 76 public float getB() { 77 return (float)getAffineTransform().getShearY(); 78 } 79 80 83 public void setB(float b) throws DOMException { 84 AffineTransform at = getAffineTransform(); 85 at.setTransform(at.getScaleX(), 86 b, 87 at.getShearX(), 88 at.getScaleY(), 89 at.getTranslateX(), 90 at.getTranslateY()); 91 } 92 93 96 public float getC() { 97 return (float)getAffineTransform().getShearX(); 98 } 99 100 103 public void setC(float c) throws DOMException { 104 AffineTransform at = getAffineTransform(); 105 at.setTransform(at.getScaleX(), 106 at.getShearY(), 107 c, 108 at.getScaleY(), 109 at.getTranslateX(), 110 at.getTranslateY()); 111 } 112 113 116 public float getD() { 117 return (float)getAffineTransform().getScaleY(); 118 } 119 120 123 public void setD(float d) throws DOMException { 124 AffineTransform at = getAffineTransform(); 125 at.setTransform(at.getScaleX(), 126 at.getShearY(), 127 at.getShearX(), 128 d, 129 at.getTranslateX(), 130 at.getTranslateY()); 131 } 132 133 136 public float getE() { 137 return (float)getAffineTransform().getTranslateX(); 138 } 139 140 143 public void setE(float e) throws DOMException { 144 AffineTransform at = getAffineTransform(); 145 at.setTransform(at.getScaleX(), 146 at.getShearY(), 147 at.getShearX(), 148 at.getScaleY(), 149 e, 150 at.getTranslateY()); 151 } 152 153 156 public float getF() { 157 return (float)getAffineTransform().getTranslateY(); 158 } 159 160 163 public void setF(float f) throws DOMException { 164 AffineTransform at = getAffineTransform(); 165 at.setTransform(at.getScaleX(), 166 at.getShearY(), 167 at.getShearX(), 168 at.getScaleY(), 169 at.getTranslateX(), 170 f); 171 } 172 173 176 public SVGMatrix multiply(SVGMatrix secondMatrix) { 177 AffineTransform at = new AffineTransform (secondMatrix.getA(), 178 secondMatrix.getB(), 179 secondMatrix.getC(), 180 secondMatrix.getD(), 181 secondMatrix.getE(), 182 secondMatrix.getF()); 183 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 184 tr.concatenate(at); 185 return new SVGOMMatrix(tr); 186 } 187 188 191 public SVGMatrix inverse() throws SVGException { 192 try { 193 return new SVGOMMatrix(getAffineTransform().createInverse()); 194 } catch (NoninvertibleTransformException e) { 195 throw new SVGOMException(SVGException.SVG_MATRIX_NOT_INVERTABLE, 196 e.getMessage()); 197 } 198 } 199 200 203 public SVGMatrix translate(float x, float y) { 204 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 205 tr.translate(x, y); 206 return new SVGOMMatrix(tr); 207 } 208 209 212 public SVGMatrix scale(float scaleFactor) { 213 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 214 tr.scale(scaleFactor, scaleFactor); 215 return new SVGOMMatrix(tr); 216 } 217 218 221 public SVGMatrix scaleNonUniform (float scaleFactorX, float scaleFactorY) { 222 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 223 tr.scale(scaleFactorX, scaleFactorY); 224 return new SVGOMMatrix(tr); 225 } 226 227 230 public SVGMatrix rotate(float angle) { 231 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 232 tr.rotate(angle); 233 return new SVGOMMatrix(tr); 234 } 235 236 239 public SVGMatrix rotateFromVector(float x, float y) throws SVGException { 240 if (x == 0 || y == 0) { 241 throw new SVGOMException(SVGException.SVG_INVALID_VALUE_ERR, ""); 242 } 243 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 244 tr.rotate(Math.atan2(y, x)); 245 return new SVGOMMatrix(tr); 246 } 247 248 251 public SVGMatrix flipX() { 252 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 253 tr.concatenate(FLIP_X_TRANSFORM); 254 return new SVGOMMatrix(tr); 255 } 256 257 260 public SVGMatrix flipY() { 261 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 262 tr.concatenate(FLIP_Y_TRANSFORM); 263 return new SVGOMMatrix(tr); 264 } 265 266 269 public SVGMatrix skewX(float angle) { 270 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 271 tr.concatenate 272 (AffineTransform.getShearInstance(Math.tan(Math.PI * angle / 180), 273 0)); 274 return new SVGOMMatrix(tr); 275 } 276 277 280 public SVGMatrix skewY(float angle) { 281 AffineTransform tr = (AffineTransform )getAffineTransform().clone(); 282 tr.concatenate 283 (AffineTransform.getShearInstance(0, 284 Math.tan(Math.PI * 285 angle / 180))); 286 return new SVGOMMatrix(tr); 287 } 288 } 289 | Popular Tags |