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.util.Iterator;
022: import java.util.LinkedList;
023:
024: import org.w3c.dom.svg.SVGPathSeg;
025:
026: /**
027: * A handler class that generates an array of shorts and an array floats from
028: * parsing path data.
029: *
030: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
031: * @version $Id: PathArrayProducer.java 475685 2006-11-16 11:16:05Z cam $
032: */
033: public class PathArrayProducer implements PathHandler {
034:
035: /**
036: * List of <code>float[]</code> objects.
037: */
038: protected LinkedList ps;
039:
040: /**
041: * The current <code>float[]</code> object.
042: */
043: protected float[] p;
044:
045: /**
046: * List of <code>short[]</code> objects.
047: */
048: protected LinkedList cs;
049:
050: /**
051: * The current <code>short[]</code> object.
052: */
053: protected short[] c;
054:
055: /**
056: * The index in which to store the next path command.
057: */
058: protected int cindex;
059:
060: /**
061: * The index in which to store the next path parameter.
062: */
063: protected int pindex;
064:
065: /**
066: * The total number of path commands accumulated.
067: */
068: protected int ccount;
069:
070: /**
071: * The total number of path parameters accumulated.
072: */
073: protected int pcount;
074:
075: /**
076: * Returns the array of path commands accumulated.
077: */
078: public short[] getPathCommands() {
079: return c;
080: }
081:
082: /**
083: * Returns the array of path parameters accumulated.
084: */
085: public float[] getPathParameters() {
086: return p;
087: }
088:
089: // PathHandler ///////////////////////////////////////////////////////////
090:
091: /**
092: * Implements {@link PathHandler#startPath()}.
093: */
094: public void startPath() throws ParseException {
095: cs = new LinkedList();
096: c = new short[11];
097: ps = new LinkedList();
098: p = new float[11];
099: ccount = 0;
100: pcount = 0;
101: cindex = 0;
102: pindex = 0;
103: }
104:
105: /**
106: * Implements {@link PathHandler#movetoRel(float,float)}.
107: */
108: public void movetoRel(float x, float y) throws ParseException {
109: command(SVGPathSeg.PATHSEG_MOVETO_REL);
110: param(x);
111: param(y);
112: }
113:
114: /**
115: * Implements {@link PathHandler#movetoAbs(float,float)}.
116: */
117: public void movetoAbs(float x, float y) throws ParseException {
118: command(SVGPathSeg.PATHSEG_MOVETO_ABS);
119: param(x);
120: param(y);
121: }
122:
123: /**
124: * Implements {@link PathHandler#closePath()}.
125: */
126: public void closePath() throws ParseException {
127: command(SVGPathSeg.PATHSEG_CLOSEPATH);
128: }
129:
130: /**
131: * Implements {@link PathHandler#linetoRel(float,float)}.
132: */
133: public void linetoRel(float x, float y) throws ParseException {
134: command(SVGPathSeg.PATHSEG_LINETO_REL);
135: param(x);
136: param(y);
137: }
138:
139: /**
140: * Implements {@link PathHandler#linetoAbs(float,float)}.
141: */
142: public void linetoAbs(float x, float y) throws ParseException {
143: command(SVGPathSeg.PATHSEG_LINETO_ABS);
144: param(x);
145: param(y);
146: }
147:
148: /**
149: * Implements {@link PathHandler#linetoHorizontalRel(float)}.
150: */
151: public void linetoHorizontalRel(float x) throws ParseException {
152: command(SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL);
153: param(x);
154: }
155:
156: /**
157: * Implements {@link PathHandler#linetoHorizontalAbs(float)}.
158: */
159: public void linetoHorizontalAbs(float x) throws ParseException {
160: command(SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS);
161: param(x);
162: }
163:
164: /**
165: * Implements {@link PathHandler#linetoVerticalRel(float)}.
166: */
167: public void linetoVerticalRel(float y) throws ParseException {
168: command(SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL);
169: param(y);
170: }
171:
172: /**
173: * Implements {@link PathHandler#linetoVerticalAbs(float)}.
174: */
175: public void linetoVerticalAbs(float y) throws ParseException {
176: command(SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS);
177: param(y);
178: }
179:
180: /**
181: * Implements {@link
182: * PathHandler#curvetoCubicRel(float,float,float,float,float,float)}.
183: */
184: public void curvetoCubicRel(float x1, float y1, float x2, float y2,
185: float x, float y) throws ParseException {
186: command(SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL);
187: param(x1);
188: param(y1);
189: param(x2);
190: param(y2);
191: param(x);
192: param(y);
193: }
194:
195: /**
196: * Implements {@link
197: * PathHandler#curvetoCubicAbs(float,float,float,float,float,float)}.
198: */
199: public void curvetoCubicAbs(float x1, float y1, float x2, float y2,
200: float x, float y) throws ParseException {
201: command(SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS);
202: param(x1);
203: param(y1);
204: param(x2);
205: param(y2);
206: param(x);
207: param(y);
208: }
209:
210: /**
211: * Implements {@link
212: * PathHandler#curvetoCubicSmoothRel(float,float,float,float)}.
213: */
214: public void curvetoCubicSmoothRel(float x2, float y2, float x,
215: float y) throws ParseException {
216: command(SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL);
217: param(x2);
218: param(y2);
219: param(x);
220: param(y);
221: }
222:
223: /**
224: * Implements {@link
225: * PathHandler#curvetoCubicSmoothAbs(float,float,float,float)}.
226: */
227: public void curvetoCubicSmoothAbs(float x2, float y2, float x,
228: float y) throws ParseException {
229: command(SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS);
230: param(x2);
231: param(y2);
232: param(x);
233: param(y);
234: }
235:
236: /**
237: * Implements {@link
238: * PathHandler#curvetoQuadraticRel(float,float,float,float)}.
239: */
240: public void curvetoQuadraticRel(float x1, float y1, float x, float y)
241: throws ParseException {
242: command(SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL);
243: param(x1);
244: param(y1);
245: param(x);
246: param(y);
247: }
248:
249: /**
250: * Implements {@link
251: * PathHandler#curvetoQuadraticAbs(float,float,float,float)}.
252: */
253: public void curvetoQuadraticAbs(float x1, float y1, float x, float y)
254: throws ParseException {
255: command(SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS);
256: param(x1);
257: param(y1);
258: param(x);
259: param(y);
260: }
261:
262: /**
263: * Implements {@link PathHandler#curvetoQuadraticSmoothRel(float,float)}.
264: */
265: public void curvetoQuadraticSmoothRel(float x, float y)
266: throws ParseException {
267: command(SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL);
268: param(x);
269: param(y);
270: }
271:
272: /**
273: * Implements {@link PathHandler#curvetoQuadraticSmoothAbs(float,float)}.
274: */
275: public void curvetoQuadraticSmoothAbs(float x, float y)
276: throws ParseException {
277: command(SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS);
278: param(x);
279: param(y);
280: }
281:
282: /**
283: * Implements {@link
284: * PathHandler#arcRel(float,float,float,boolean,boolean,float,float)}.
285: */
286: public void arcRel(float rx, float ry, float xAxisRotation,
287: boolean largeArcFlag, boolean sweepFlag, float x, float y)
288: throws ParseException {
289: command(SVGPathSeg.PATHSEG_ARC_REL);
290: param(rx);
291: param(ry);
292: param(xAxisRotation);
293: param(largeArcFlag ? 1 : 0);
294: param(sweepFlag ? 1 : 0);
295: param(x);
296: param(y);
297: }
298:
299: /**
300: * Implements {@link
301: * PathHandler#arcAbs(float,float,float,boolean,boolean,float,float)}.
302: */
303: public void arcAbs(float rx, float ry, float xAxisRotation,
304: boolean largeArcFlag, boolean sweepFlag, float x, float y)
305: throws ParseException {
306: command(SVGPathSeg.PATHSEG_ARC_ABS);
307: param(rx);
308: param(ry);
309: param(xAxisRotation);
310: param(largeArcFlag ? 1 : 0);
311: param(sweepFlag ? 1 : 0);
312: param(x);
313: param(y);
314: }
315:
316: /**
317: * Adds a path command to the list.
318: */
319: protected void command(short val) throws ParseException {
320: if (cindex == c.length) {
321: cs.add(c);
322: c = new short[c.length * 2 + 1];
323: cindex = 0;
324: }
325: c[cindex++] = val;
326: ccount++;
327: }
328:
329: /**
330: * Adds a path parameter to the list.
331: */
332: protected void param(float val) throws ParseException {
333: if (pindex == p.length) {
334: ps.add(p);
335: p = new float[p.length * 2 + 1];
336: pindex = 0;
337: }
338: p[pindex++] = val;
339: pcount++;
340: }
341:
342: /**
343: * Implements {@link PathHandler#endPath()}.
344: */
345: public void endPath() throws ParseException {
346: short[] allCommands = new short[ccount];
347: int pos = 0;
348: Iterator it = cs.iterator();
349: while (it.hasNext()) {
350: short[] a = (short[]) it.next();
351: System.arraycopy(a, 0, allCommands, pos, a.length);
352: pos += a.length;
353: }
354: System.arraycopy(c, 0, allCommands, pos, cindex);
355: cs.clear();
356: c = allCommands;
357:
358: float[] allParams = new float[pcount];
359: pos = 0;
360: it = ps.iterator();
361: while (it.hasNext()) {
362: float[] a = (float[]) it.next();
363: System.arraycopy(a, 0, allParams, pos, a.length);
364: pos += a.length;
365: }
366: System.arraycopy(p, 0, allParams, pos, pindex);
367: ps.clear();
368: p = allParams;
369: }
370: }
|