1 /********************************************** 2 * Copyright (C) 2010 Lukas Laag 3 * This file is part of lib-gwt-svg. 4 * 5 * libgwtsvg is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * libgwtsvg is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/ 17 **********************************************/ 18 /* 19 * Copyright (c) 2004 World Wide Web Consortium, 20 * 21 * (Massachusetts Institute of Technology, European Research Consortium for 22 * Informatics and Mathematics, Keio University). All Rights Reserved. This 23 * work is distributed under the W3C(r) Software License [1] in the hope that 24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 26 * 27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 28 */ 29 30 package org.vectomatic.dom.svg; 31 32 import com.google.gwt.core.client.JavaScriptException; 33 import com.google.gwt.core.client.JavaScriptObject; 34 35 /** 36 * <p>Many of SVG's graphics operations utilize 2x3 matrices of the form:</p> 37 * <pre> 38 * [a c e] 39 * [b d f]</pre> <p>which, when expanded into a 3x3 matrix for the purposes 40 * of matrix arithmetic, become:</p> <pre> 41 * [a c e] 42 * [b d f] 43 * [0 0 1]</pre> 44 */ 45 public class OMSVGMatrix extends JavaScriptObject { 46 protected OMSVGMatrix() { 47 } 48 49 // Implementation of the svg::SVGMatrix W3C IDL interface 50 /** 51 * The <var>a</var> component of the matrix. 52 */ 53 public final native float getA() /*-{ 54 return this.a; 55 }-*/; 56 /** 57 * The <var>a</var> component of the matrix. 58 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 59 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 60 * attribute</a>. 61 */ 62 public final native void setA(float value) throws JavaScriptException /*-{ 63 this.a = value; 64 }-*/; 65 /** 66 * The <var>b</var> component of the matrix. 67 */ 68 public final native float getB() /*-{ 69 return this.b; 70 }-*/; 71 /** 72 * The <var>b</var> component of the matrix. 73 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 74 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 75 * attribute</a>. 76 */ 77 public final native void setB(float value) throws JavaScriptException /*-{ 78 this.b = value; 79 }-*/; 80 /** 81 * The <var>c</var> component of the matrix. 82 */ 83 public final native float getC() /*-{ 84 return this.c; 85 }-*/; 86 /** 87 * The <var>c</var> component of the matrix. 88 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 89 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 90 * attribute</a>. 91 */ 92 public final native void setC(float value) throws JavaScriptException /*-{ 93 this.c = value; 94 }-*/; 95 /** 96 * The <var>d</var> component of the matrix. 97 */ 98 public final native float getD() /*-{ 99 return this.d; 100 }-*/; 101 /** 102 * The <var>d</var> component of the matrix. 103 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 104 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 105 * attribute</a>. 106 */ 107 public final native void setD(float value) throws JavaScriptException /*-{ 108 this.d = value; 109 }-*/; 110 /** 111 * The <var>e</var> component of the matrix. 112 */ 113 public final native float getE() /*-{ 114 return this.e; 115 }-*/; 116 /** 117 * The <var>e</var> component of the matrix. 118 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 119 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 120 * attribute</a>. 121 */ 122 public final native void setE(float value) throws JavaScriptException /*-{ 123 this.e = value; 124 }-*/; 125 /** 126 * The <var>f</var> component of the matrix. 127 */ 128 public final native float getF() /*-{ 129 return this.f; 130 }-*/; 131 /** 132 * The <var>f</var> component of the matrix. 133 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised on an attempt 134 * to change the value of a <a href="svgdom.html#ReadOnlyNodes">read only 135 * attribute</a>. 136 */ 137 public final native void setF(float value) throws JavaScriptException /*-{ 138 this.f = value; 139 }-*/; 140 /** 141 * Performs matrix multiplication. This matrix is post-multiplied by another 142 * matrix, returning the resulting new matrix. 143 * @param secondMatrix The matrix which is post-multiplied to this matrix. 144 * @return The resulting matrix. 145 */ 146 public final native OMSVGMatrix multiply(OMSVGMatrix secondMatrix) /*-{ 147 return this.multiply(secondMatrix); 148 }-*/; 149 /** 150 * Returns the inverse matrix. 151 * @return The inverse matrix. 152 * @throws SVGException(SVG_MATRIX_NOT_INVERTABLE) Raised if this matrix is 153 * not invertable. 154 */ 155 public final native OMSVGMatrix inverse() throws JavaScriptException /*-{ 156 return this.inverse(); 157 }-*/; 158 /** 159 * Post-multiplies a translation transformation on the current matrix and 160 * returns the resulting matrix. 161 * @param x The distance to translate along the x-axis. 162 * @param y The distance to translate along the y-axis. 163 * @return The resulting matrix. 164 */ 165 public final native OMSVGMatrix translate(float x, float y) /*-{ 166 return this.translate(x, y); 167 }-*/; 168 /** 169 * Post-multiplies a uniform scale transformation on the current matrix and 170 * returns the resulting matrix. 171 * @param scaleFactor Scale factor in both X and Y. 172 * @return The resulting matrix. 173 */ 174 public final native OMSVGMatrix scale(float scaleFactor) /*-{ 175 return this.scale(scaleFactor); 176 }-*/; 177 /** 178 * Post-multiplies a non-uniform scale transformation on the current matrix 179 * and returns the resulting matrix. 180 * @param scaleFactorX Scale factor in X. 181 * @param scaleFactorY Scale factor in Y. 182 * @return The resulting matrix. 183 */ 184 public final native OMSVGMatrix scaleNonUniform(float scaleFactorX, float scaleFactorY) /*-{ 185 return this.scaleNonUniform(scaleFactorX, scaleFactorY); 186 }-*/; 187 /** 188 * Post-multiplies a rotation transformation on the current matrix and returns 189 * the resulting matrix. 190 * @param angle Rotation angle. 191 * @return The resulting matrix. 192 */ 193 public final native OMSVGMatrix rotate(float angle) /*-{ 194 return this.rotate(angle); 195 }-*/; 196 /** 197 * Post-multiplies a rotation transformation on the current matrix and returns 198 * the resulting matrix. The rotation angle is determined by taking (+/-) 199 * atan(y/x). The direction of the vector (x, y) determines whether the positive 200 * or negative angle value is used. 201 * @param x The X coordinate of the vector (x,y). Must not be zero. 202 * @param y The Y coordinate of the vector (x,y). Must not be zero. 203 * @return The resulting matrix. 204 * @throws SVGException(SVG_INVALID_VALUE_ERR) Raised if one of the parameters 205 * has an invalid value. 206 */ 207 public final native OMSVGMatrix rotateFromVector(float x, float y) throws JavaScriptException /*-{ 208 return this.rotateFromVector(x, y); 209 }-*/; 210 /** 211 * Post-multiplies the transformation [-1 0 0 1 0 0] and returns the resulting 212 * matrix. 213 * @return The resulting matrix. 214 */ 215 public final native OMSVGMatrix flipX() /*-{ 216 return this.flipX(); 217 }-*/; 218 /** 219 * Post-multiplies the transformation [1 0 0 -1 0 0] and returns the resulting 220 * matrix. 221 * @return The resulting matrix. 222 */ 223 public final native OMSVGMatrix flipY() /*-{ 224 return this.flipY(); 225 }-*/; 226 /** 227 * Post-multiplies a skewX transformation on the current matrix and returns 228 * the resulting matrix. 229 * @param angle Skew angle. 230 * @return The resulting matrix. 231 */ 232 public final native OMSVGMatrix skewX(float angle) /*-{ 233 return this.skewX(angle); 234 }-*/; 235 /** 236 * Post-multiplies a skewY transformation on the current matrix and returns 237 * the resulting matrix. 238 * @param angle Skew angle. 239 * @return The resulting matrix. 240 */ 241 public final native OMSVGMatrix skewY(float angle) /*-{ 242 return this.skewY(angle); 243 }-*/; 244 245 // Helper methods 246 /** 247 * Returns a textual description of the matrix for debugging purposes. 248 * @return a textual description of the matrix. 249 */ 250 public final String getDescription() { 251 StringBuilder builder = new StringBuilder(); 252 builder.append(getA()); 253 builder.append(" "); 254 builder.append(getB()); 255 builder.append(" "); 256 builder.append(getC()); 257 builder.append(" "); 258 builder.append(getD()); 259 builder.append(" "); 260 builder.append(getE()); 261 builder.append(" "); 262 builder.append(getF()); 263 return builder.toString(); 264 } 265 /** 266 * Returns true if this matrix is the identity matrix 267 * @return 268 * true if this matrix is the identity matrix 269 */ 270 public final boolean isIdentity() { 271 return getA() == 1 && getB() == 0 && getC() == 0 && getD() == 1 && getE() == 0 && getF() == 0; 272 } 273 }