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.parser;
020:
021: import java.awt.geom.AffineTransform;
022: import java.io.Reader;
023:
024: /**
025: * This class provides an implementation of the PathHandler that initializes
026: * an AffineTransform from the value of a 'transform' attribute.
027: *
028: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
029: * @version $Id: AWTTransformProducer.java 501495 2007-01-30 18:00:36Z dvholten $
030: */
031: public class AWTTransformProducer implements TransformListHandler {
032: /**
033: * The value of the current affine transform.
034: */
035: protected AffineTransform affineTransform;
036:
037: /**
038: * Utility method for creating an AffineTransform.
039: * @param r The reader used to read the transform specification.
040: */
041: public static AffineTransform createAffineTransform(Reader r)
042: throws ParseException {
043: TransformListParser p = new TransformListParser();
044: AWTTransformProducer th = new AWTTransformProducer();
045:
046: p.setTransformListHandler(th);
047: p.parse(r);
048:
049: return th.getAffineTransform();
050: }
051:
052: /**
053: * Utility method for creating an AffineTransform.
054: * @param s The transform specification.
055: */
056: public static AffineTransform createAffineTransform(String s)
057: throws ParseException {
058: TransformListParser p = new TransformListParser();
059: AWTTransformProducer th = new AWTTransformProducer();
060:
061: p.setTransformListHandler(th);
062: p.parse(s);
063:
064: return th.getAffineTransform();
065: }
066:
067: /**
068: * Returns the AffineTransform object initialized during the last parsing.
069: * @return the transform or null if this handler has not been used by
070: * a parser.
071: */
072: public AffineTransform getAffineTransform() {
073: return affineTransform;
074: }
075:
076: /**
077: * Implements {@link TransformListHandler#startTransformList()}.
078: */
079: public void startTransformList() throws ParseException {
080: affineTransform = new AffineTransform();
081: }
082:
083: /**
084: * Implements {@link
085: * TransformListHandler#matrix(float,float,float,float,float,float)}.
086: */
087: public void matrix(float a, float b, float c, float d, float e,
088: float f) throws ParseException {
089: affineTransform.concatenate(new AffineTransform(a, b, c, d, e,
090: f));
091: }
092:
093: /**
094: * Implements {@link TransformListHandler#rotate(float)}.
095: */
096: public void rotate(float theta) throws ParseException {
097: affineTransform.concatenate(AffineTransform
098: .getRotateInstance(Math.toRadians(theta)));
099: }
100:
101: /**
102: * Implements {@link TransformListHandler#rotate(float,float,float)}.
103: */
104: public void rotate(float theta, float cx, float cy)
105: throws ParseException {
106: AffineTransform at = AffineTransform.getRotateInstance(Math
107: .toRadians(theta), cx, cy);
108: affineTransform.concatenate(at);
109: }
110:
111: /**
112: * Implements {@link TransformListHandler#translate(float)}.
113: */
114: public void translate(float tx) throws ParseException {
115: AffineTransform at = AffineTransform
116: .getTranslateInstance(tx, 0);
117: affineTransform.concatenate(at);
118: }
119:
120: /**
121: * Implements {@link TransformListHandler#translate(float,float)}.
122: */
123: public void translate(float tx, float ty) throws ParseException {
124: AffineTransform at = AffineTransform.getTranslateInstance(tx,
125: ty);
126: affineTransform.concatenate(at);
127: }
128:
129: /**
130: * Implements {@link TransformListHandler#scale(float)}.
131: */
132: public void scale(float sx) throws ParseException {
133: affineTransform.concatenate(AffineTransform.getScaleInstance(
134: sx, sx));
135: }
136:
137: /**
138: * Implements {@link TransformListHandler#scale(float,float)}.
139: */
140: public void scale(float sx, float sy) throws ParseException {
141: affineTransform.concatenate(AffineTransform.getScaleInstance(
142: sx, sy));
143: }
144:
145: /**
146: * Implements {@link TransformListHandler#skewX(float)}.
147: */
148: public void skewX(float skx) throws ParseException {
149: affineTransform.concatenate(AffineTransform.getShearInstance(
150: Math.tan(Math.toRadians(skx)), 0));
151: }
152:
153: /**
154: * Implements {@link TransformListHandler#skewY(float)}.
155: */
156: public void skewY(float sky) throws ParseException {
157: affineTransform.concatenate(AffineTransform.getShearInstance(0,
158: Math.tan(Math.toRadians(sky))));
159: }
160:
161: /**
162: * Implements {@link TransformListHandler#endTransformList()}.
163: */
164: public void endTransformList() throws ParseException {
165: }
166: }
|