View Javadoc

1   // Copyright 2001-2004 FreeHEP.
2   package org.freehep.graphicsio.swf;
3   
4   import java.io.IOException;
5   import java.util.Vector;
6   
7   import org.freehep.graphicsio.CubicToQuadPathConstructor;
8   
9   /**
10   * @author Mark Donszelmann
11   * @version $Id: SWFPathConstructor.java 8584 2006-08-10 23:06:37Z duns $
12   */
13  public class SWFPathConstructor extends CubicToQuadPathConstructor implements
14          SWFConstants {
15      private Vector path;
16  
17      // to avoid rounding errors we keep all values in TWIPS.
18      private int x0, y0;
19  
20      private int xc, yc;
21  
22      private int stroke, fill0, fill1;
23  
24      public SWFPathConstructor(Vector path, int stroke, int fill0, int fill1) {
25          // resolution equals .5 twips
26          this(path, stroke, fill0, fill1, 0.5 / TWIPS);
27      }
28  
29      public SWFPathConstructor(Vector path, int stroke, int fill0, int fill1,
30              double resolution) {
31          super(resolution);
32          this.path = path;
33          this.stroke = stroke;
34          this.fill0 = fill0;
35          this.fill1 = fill1;
36      }
37  
38      public void move(double x, double y) throws IOException {
39          x0 = toInt(x);
40          y0 = toInt(y);
41          xc = x0;
42          yc = y0;
43          path.add(new SWFShape.ShapeRecord(true, xc, yc, fill0, fill1, stroke));
44          super.move(x, y);
45      }
46  
47      public void line(double x, double y) throws IOException {
48          int dx = toInt(x) - xc;
49          int dy = toInt(y) - yc;
50          path.add(new SWFShape.EdgeRecord(dx, dy));
51          xc += dx;
52          yc += dy;
53          super.line(x, y);
54      }
55  
56      public void quad(double x1, double y1, double x2, double y2)
57              throws IOException {
58          int cdx = toInt(x1) - xc;
59          int cdy = toInt(y1) - yc;
60          int dx = toInt(x2 - x1);
61          int dy = toInt(y2 - y1);
62          path.add(new SWFShape.EdgeRecord(cdx, cdy, dx, dy));
63          xc += dx + cdx;
64          yc += dy + cdy;
65  
66          // important for cubic method
67          currentX = x2;
68          currentY = y2;
69      }
70  
71      public void closePath(double xd0, double yd0) throws IOException {
72          if ((xc != x0) || (yc != y0)) {
73              // due to rounding we may miss the closing, so we do it by hand
74              path.add(new SWFShape.EdgeRecord(x0 - xc, y0 - yc));
75          }
76          super.closePath(xd0, yd0);
77      }
78  
79      private int toInt(double d) {
80          return (int) (d * TWIPS);
81      }
82  }