001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.dom.svg;
020:
021: import java.awt.geom.AffineTransform;
022: import java.awt.geom.NoninvertibleTransformException;
023:
024: import org.w3c.dom.DOMException;
025: import org.w3c.dom.svg.SVGException;
026: import org.w3c.dom.svg.SVGMatrix;
027:
028: /**
029: * This class provides an abstract implementation of the {@link SVGMatrix}
030: * interface.
031: *
032: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
033: * @version $Id: AbstractSVGMatrix.java 501495 2007-01-30 18:00:36Z dvholten $
034: */
035: public abstract class AbstractSVGMatrix implements SVGMatrix {
036:
037: /**
038: * The transform used to implement flipX.
039: */
040: protected static final AffineTransform FLIP_X_TRANSFORM = new AffineTransform(
041: -1, 0, 0, 1, 0, 0);
042:
043: /**
044: * The transform used to implement flipX.
045: */
046: protected static final AffineTransform FLIP_Y_TRANSFORM = new AffineTransform(
047: 1, 0, 0, -1, 0, 0);
048:
049: /**
050: * Returns the associated AffineTransform.
051: */
052: protected abstract AffineTransform getAffineTransform();
053:
054: /**
055: * Implements {@link SVGMatrix#getA()}.
056: */
057: public float getA() {
058: return (float) getAffineTransform().getScaleX();
059: }
060:
061: /**
062: * Implements {@link SVGMatrix#setA(float)}.
063: */
064: public void setA(float a) throws DOMException {
065: AffineTransform at = getAffineTransform();
066: at.setTransform(a, at.getShearY(), at.getShearX(), at
067: .getScaleY(), at.getTranslateX(), at.getTranslateY());
068: }
069:
070: /**
071: * Implements {@link SVGMatrix#getB()}.
072: */
073: public float getB() {
074: return (float) getAffineTransform().getShearY();
075: }
076:
077: /**
078: * Implements {@link SVGMatrix#setB(float)}.
079: */
080: public void setB(float b) throws DOMException {
081: AffineTransform at = getAffineTransform();
082: at.setTransform(at.getScaleX(), b, at.getShearX(), at
083: .getScaleY(), at.getTranslateX(), at.getTranslateY());
084: }
085:
086: /**
087: * Implements {@link SVGMatrix#getC()}.
088: */
089: public float getC() {
090: return (float) getAffineTransform().getShearX();
091: }
092:
093: /**
094: * Implements {@link SVGMatrix#setC(float)}.
095: */
096: public void setC(float c) throws DOMException {
097: AffineTransform at = getAffineTransform();
098: at.setTransform(at.getScaleX(), at.getShearY(), c, at
099: .getScaleY(), at.getTranslateX(), at.getTranslateY());
100: }
101:
102: /**
103: * Implements {@link SVGMatrix#getD()}.
104: */
105: public float getD() {
106: return (float) getAffineTransform().getScaleY();
107: }
108:
109: /**
110: * Implements {@link SVGMatrix#setD(float)}.
111: */
112: public void setD(float d) throws DOMException {
113: AffineTransform at = getAffineTransform();
114: at.setTransform(at.getScaleX(), at.getShearY(), at.getShearX(),
115: d, at.getTranslateX(), at.getTranslateY());
116: }
117:
118: /**
119: * Implements {@link SVGMatrix#getE()}.
120: */
121: public float getE() {
122: return (float) getAffineTransform().getTranslateX();
123: }
124:
125: /**
126: * Implements {@link SVGMatrix#setE(float)}.
127: */
128: public void setE(float e) throws DOMException {
129: AffineTransform at = getAffineTransform();
130: at.setTransform(at.getScaleX(), at.getShearY(), at.getShearX(),
131: at.getScaleY(), e, at.getTranslateY());
132: }
133:
134: /**
135: * Implements {@link SVGMatrix#getF()}.
136: */
137: public float getF() {
138: return (float) getAffineTransform().getTranslateY();
139: }
140:
141: /**
142: * Implements {@link SVGMatrix#setF(float)}.
143: */
144: public void setF(float f) throws DOMException {
145: AffineTransform at = getAffineTransform();
146: at.setTransform(at.getScaleX(), at.getShearY(), at.getShearX(),
147: at.getScaleY(), at.getTranslateX(), f);
148: }
149:
150: /**
151: * Implements {@link SVGMatrix#multiply(SVGMatrix)}.
152: */
153: public SVGMatrix multiply(SVGMatrix secondMatrix) {
154: AffineTransform at = new AffineTransform(secondMatrix.getA(),
155: secondMatrix.getB(), secondMatrix.getC(), secondMatrix
156: .getD(), secondMatrix.getE(), secondMatrix
157: .getF());
158: AffineTransform tr = (AffineTransform) getAffineTransform()
159: .clone();
160: tr.concatenate(at);
161: return new SVGOMMatrix(tr);
162: }
163:
164: /**
165: * Implements {@link SVGMatrix#inverse()}.
166: */
167: public SVGMatrix inverse() throws SVGException {
168: try {
169: return new SVGOMMatrix(getAffineTransform().createInverse());
170: } catch (NoninvertibleTransformException e) {
171: throw new SVGOMException(
172: SVGException.SVG_MATRIX_NOT_INVERTABLE, e
173: .getMessage());
174: }
175: }
176:
177: /**
178: * Implements {@link SVGMatrix#translate(float,float)}.
179: */
180: public SVGMatrix translate(float x, float y) {
181: AffineTransform tr = (AffineTransform) getAffineTransform()
182: .clone();
183: tr.translate(x, y);
184: return new SVGOMMatrix(tr);
185: }
186:
187: /**
188: * Implements {@link SVGMatrix#scale(float)}.
189: */
190: public SVGMatrix scale(float scaleFactor) {
191: AffineTransform tr = (AffineTransform) getAffineTransform()
192: .clone();
193: tr.scale(scaleFactor, scaleFactor);
194: return new SVGOMMatrix(tr);
195: }
196:
197: /**
198: * Implements {@link SVGMatrix#scaleNonUniform(float,float)}.
199: */
200: public SVGMatrix scaleNonUniform(float scaleFactorX,
201: float scaleFactorY) {
202: AffineTransform tr = (AffineTransform) getAffineTransform()
203: .clone();
204: tr.scale(scaleFactorX, scaleFactorY);
205: return new SVGOMMatrix(tr);
206: }
207:
208: /**
209: * Implements {@link SVGMatrix#rotate(float)}.
210: */
211: public SVGMatrix rotate(float angle) {
212: AffineTransform tr = (AffineTransform) getAffineTransform()
213: .clone();
214: tr.rotate(Math.toRadians(angle));
215: return new SVGOMMatrix(tr);
216: }
217:
218: /**
219: * Implements {@link SVGMatrix#rotateFromVector(float,float)}.
220: */
221: public SVGMatrix rotateFromVector(float x, float y)
222: throws SVGException {
223: if (x == 0 || y == 0) {
224: throw new SVGOMException(
225: SVGException.SVG_INVALID_VALUE_ERR, "");
226: }
227: AffineTransform tr = (AffineTransform) getAffineTransform()
228: .clone();
229: tr.rotate(Math.atan2(y, x));
230: return new SVGOMMatrix(tr);
231: }
232:
233: /**
234: * Implements {@link SVGMatrix#flipX()}.
235: */
236: public SVGMatrix flipX() {
237: AffineTransform tr = (AffineTransform) getAffineTransform()
238: .clone();
239: tr.concatenate(FLIP_X_TRANSFORM);
240: return new SVGOMMatrix(tr);
241: }
242:
243: /**
244: * Implements {@link SVGMatrix#flipY()}.
245: */
246: public SVGMatrix flipY() {
247: AffineTransform tr = (AffineTransform) getAffineTransform()
248: .clone();
249: tr.concatenate(FLIP_Y_TRANSFORM);
250: return new SVGOMMatrix(tr);
251: }
252:
253: /**
254: * Implements {@link SVGMatrix#skewX(float)}.
255: */
256: public SVGMatrix skewX(float angleDeg) {
257: AffineTransform tr = (AffineTransform) getAffineTransform()
258: .clone();
259: tr.concatenate(AffineTransform.getShearInstance(Math.tan(Math
260: .toRadians(angleDeg)), 0));
261: return new SVGOMMatrix(tr);
262: }
263:
264: /**
265: * Implements {@link SVGMatrix#skewY(float)}.
266: */
267: public SVGMatrix skewY(float angleDeg) {
268: AffineTransform tr = (AffineTransform) getAffineTransform()
269: .clone();
270: tr.concatenate(AffineTransform.getShearInstance(0, Math
271: .tan(Math.toRadians(angleDeg))));
272: return new SVGOMMatrix(tr);
273: }
274: }
|