KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > AbstractSVGMatrix


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.svg;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21 import java.awt.geom.NoninvertibleTransformException JavaDoc;
22
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.svg.SVGException;
25 import org.w3c.dom.svg.SVGMatrix;
26
27 /**
28  * This class provides an abstract implementation of the {@link SVGMatrix}
29  * interface.
30  *
31  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
32  * @version $Id: AbstractSVGMatrix.java,v 1.6 2004/08/18 07:13:13 vhardy Exp $
33  */

34 public abstract class AbstractSVGMatrix implements SVGMatrix {
35
36     /**
37      * The transform used to implement flipX.
38      */

39     protected final static AffineTransform JavaDoc FLIP_X_TRANSFORM =
40         new AffineTransform JavaDoc(-1, 0, 0, 1, 0, 0);
41
42     /**
43      * The transform used to implement flipX.
44      */

45     protected final static AffineTransform JavaDoc FLIP_Y_TRANSFORM =
46         new AffineTransform JavaDoc(1, 0, 0, -1, 0, 0);
47
48     /**
49      * Returns the associated AffineTransform.
50      */

51     protected abstract AffineTransform JavaDoc getAffineTransform();
52     
53     /**
54      * Implements {@link SVGMatrix#getA()}.
55      */

56     public float getA() {
57         return (float)getAffineTransform().getScaleX();
58     }
59
60     /**
61      * Implements {@link SVGMatrix#setA(float)}.
62      */

63     public void setA(float a) throws DOMException JavaDoc {
64         AffineTransform JavaDoc at = getAffineTransform();
65         at.setTransform(a,
66                         at.getShearY(),
67                         at.getShearX(),
68                         at.getScaleY(),
69                         at.getTranslateX(),
70                         at.getTranslateY());
71     }
72
73     /**
74      * Implements {@link SVGMatrix#getB()}.
75      */

76     public float getB() {
77         return (float)getAffineTransform().getShearY();
78     }
79
80     /**
81      * Implements {@link SVGMatrix#setB(float)}.
82      */

83     public void setB(float b) throws DOMException JavaDoc {
84         AffineTransform JavaDoc at = getAffineTransform();
85         at.setTransform(at.getScaleX(),
86                         b,
87                         at.getShearX(),
88                         at.getScaleY(),
89                         at.getTranslateX(),
90                         at.getTranslateY());
91     }
92
93     /**
94      * Implements {@link SVGMatrix#getC()}.
95      */

96     public float getC() {
97         return (float)getAffineTransform().getShearX();
98     }
99
100     /**
101      * Implements {@link SVGMatrix#setC(float)}.
102      */

103     public void setC(float c) throws DOMException JavaDoc {
104         AffineTransform JavaDoc at = getAffineTransform();
105         at.setTransform(at.getScaleX(),
106                         at.getShearY(),
107                         c,
108                         at.getScaleY(),
109                         at.getTranslateX(),
110                         at.getTranslateY());
111     }
112
113     /**
114      * Implements {@link SVGMatrix#getD()}.
115      */

116     public float getD() {
117         return (float)getAffineTransform().getScaleY();
118     }
119
120     /**
121      * Implements {@link SVGMatrix#setD(float)}.
122      */

123     public void setD(float d) throws DOMException JavaDoc {
124         AffineTransform JavaDoc at = getAffineTransform();
125         at.setTransform(at.getScaleX(),
126                         at.getShearY(),
127                         at.getShearX(),
128                         d,
129                         at.getTranslateX(),
130                         at.getTranslateY());
131     }
132
133     /**
134      * Implements {@link SVGMatrix#getE()}.
135      */

136     public float getE() {
137         return (float)getAffineTransform().getTranslateX();
138     }
139
140     /**
141      * Implements {@link SVGMatrix#setE(float)}.
142      */

143     public void setE(float e) throws DOMException JavaDoc {
144         AffineTransform JavaDoc at = getAffineTransform();
145         at.setTransform(at.getScaleX(),
146                         at.getShearY(),
147                         at.getShearX(),
148                         at.getScaleY(),
149                         e,
150                         at.getTranslateY());
151     }
152
153     /**
154      * Implements {@link SVGMatrix#getF()}.
155      */

156     public float getF() {
157         return (float)getAffineTransform().getTranslateY();
158     }
159
160     /**
161      * Implements {@link SVGMatrix#setF(float)}.
162      */

163     public void setF(float f) throws DOMException JavaDoc {
164         AffineTransform JavaDoc at = getAffineTransform();
165         at.setTransform(at.getScaleX(),
166                         at.getShearY(),
167                         at.getShearX(),
168                         at.getScaleY(),
169                         at.getTranslateX(),
170                         f);
171     }
172
173     /**
174      * Implements {@link SVGMatrix#multiply(SVGMatrix)}.
175      */

176     public SVGMatrix multiply(SVGMatrix secondMatrix) {
177         AffineTransform JavaDoc at = new AffineTransform JavaDoc(secondMatrix.getA(),
178                                                  secondMatrix.getB(),
179                                                  secondMatrix.getC(),
180                                                  secondMatrix.getD(),
181                                                  secondMatrix.getE(),
182                                                  secondMatrix.getF());
183         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
184         tr.concatenate(at);
185         return new SVGOMMatrix(tr);
186     }
187
188     /**
189      * Implements {@link SVGMatrix#inverse()}.
190      */

191     public SVGMatrix inverse() throws SVGException {
192         try {
193             return new SVGOMMatrix(getAffineTransform().createInverse());
194         } catch (NoninvertibleTransformException JavaDoc e) {
195             throw new SVGOMException(SVGException.SVG_MATRIX_NOT_INVERTABLE,
196                                      e.getMessage());
197         }
198     }
199
200     /**
201      * Implements {@link SVGMatrix#translate(float,float)}.
202      */

203     public SVGMatrix translate(float x, float y) {
204         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
205         tr.translate(x, y);
206         return new SVGOMMatrix(tr);
207     }
208
209     /**
210      * Implements {@link SVGMatrix#scale(float)}.
211      */

212     public SVGMatrix scale(float scaleFactor) {
213         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
214         tr.scale(scaleFactor, scaleFactor);
215         return new SVGOMMatrix(tr);
216     }
217
218     /**
219      * Implements {@link SVGMatrix#scaleNonUniform(float,float)}.
220      */

221     public SVGMatrix scaleNonUniform (float scaleFactorX, float scaleFactorY) {
222         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
223         tr.scale(scaleFactorX, scaleFactorY);
224         return new SVGOMMatrix(tr);
225     }
226
227     /**
228      * Implements {@link SVGMatrix#rotate(float)}.
229      */

230     public SVGMatrix rotate(float angle) {
231         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
232         tr.rotate(angle);
233         return new SVGOMMatrix(tr);
234     }
235
236     /**
237      * Implements {@link SVGMatrix#rotateFromVector(float,float)}.
238      */

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 JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
244         tr.rotate(Math.atan2(y, x));
245         return new SVGOMMatrix(tr);
246     }
247
248     /**
249      * Implements {@link SVGMatrix#flipX()}.
250      */

251     public SVGMatrix flipX() {
252         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
253         tr.concatenate(FLIP_X_TRANSFORM);
254         return new SVGOMMatrix(tr);
255     }
256
257     /**
258      * Implements {@link SVGMatrix#flipY()}.
259      */

260     public SVGMatrix flipY() {
261         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
262         tr.concatenate(FLIP_Y_TRANSFORM);
263         return new SVGOMMatrix(tr);
264     }
265
266     /**
267      * Implements {@link SVGMatrix#skewX(float)}.
268      */

269     public SVGMatrix skewX(float angle) {
270         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)getAffineTransform().clone();
271         tr.concatenate
272             (AffineTransform.getShearInstance(Math.tan(Math.PI * angle / 180),
273                                               0));
274         return new SVGOMMatrix(tr);
275     }
276
277     /**
278      * Implements {@link SVGMatrix#skewY(float)}.
279      */

280     public SVGMatrix skewY(float angle) {
281         AffineTransform JavaDoc tr = (AffineTransform JavaDoc)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