1
2 package org.freehep.graphicsio.emf;
3
4 import java.awt.Point;
5 import java.awt.Rectangle;
6 import java.io.IOException;
7
8 import org.freehep.graphicsio.QuadToCubicPathConstructor;
9 import org.freehep.graphicsio.emf.gdi.CloseFigure;
10 import org.freehep.graphicsio.emf.gdi.LineTo;
11 import org.freehep.graphicsio.emf.gdi.MoveToEx;
12 import org.freehep.graphicsio.emf.gdi.PolyBezierTo;
13 import org.freehep.graphicsio.emf.gdi.PolyBezierTo16;
14 import org.freehep.graphicsio.emf.gdi.PolylineTo;
15 import org.freehep.graphicsio.emf.gdi.PolylineTo16;
16
17
18
19
20
21 public class EMFPathConstructor extends QuadToCubicPathConstructor implements
22 EMFConstants {
23 private EMFOutputStream os;
24
25 private Rectangle imageBounds;
26
27 private boolean curved;
28
29 private int pointIndex = 0;
30
31 private boolean wide = false;
32
33 private Point[] points = new Point[4];
34
35 public EMFPathConstructor(EMFOutputStream os, Rectangle imageBounds) {
36 super();
37 this.os = os;
38 this.imageBounds = imageBounds;
39 this.curved = false;
40 }
41
42 public void move(double x, double y) throws IOException {
43 flush();
44 os.writeTag(new MoveToEx(new Point(toUnit(x), toUnit(y))));
45 super.move(x, y);
46 }
47
48 private void addPoint(int n, double x, double y) {
49 if (n >= points.length) {
50 Point[] buf = new Point[n << 1];
51 System.arraycopy(points, 0, buf, 0, points.length);
52 points = buf;
53 }
54
55 int ix = toUnit(x);
56 int iy = toUnit(y);
57
58 if (wide || (ix < Short.MIN_VALUE) || (ix > Short.MAX_VALUE)
59 || (iy < Short.MIN_VALUE) || (iy > Short.MAX_VALUE)) {
60 wide = true;
61 }
62
63 if (points[n] == null) {
64 points[n] = new Point(ix, iy);
65 } else {
66 points[n].x = ix;
67 points[n].y = iy;
68 }
69 }
70
71 public void line(double x, double y) throws IOException {
72 if (curved && (pointIndex > 0))
73 flush();
74 curved = false;
75 addPoint(pointIndex++, x, y);
76 super.line(x, y);
77 }
78
79 public void cubic(double x1, double y1, double x2, double y2, double x3,
80 double y3) throws IOException {
81 if (!curved && (pointIndex > 0))
82 flush();
83 curved = true;
84 addPoint(pointIndex++, x1, y1);
85 addPoint(pointIndex++, x2, y2);
86 addPoint(pointIndex++, x3, y3);
87 super.cubic(x1, y1, x2, y2, x3, y3);
88 }
89
90 public void closePath(double x0, double y0) throws IOException {
91 flush();
92 os.writeTag(new CloseFigure());
93 super.closePath(x0, y0);
94 }
95
96 public void flush() throws IOException {
97 if (curved) {
98 if (wide) {
99 os.writeTag(new PolyBezierTo(imageBounds, pointIndex, points));
100 } else {
101 os
102 .writeTag(new PolyBezierTo16(imageBounds, pointIndex,
103 points));
104 }
105 } else if (pointIndex == 1) {
106 os.writeTag(new LineTo(points[0]));
107 } else if (pointIndex > 1) {
108 if (wide) {
109 os.writeTag(new PolylineTo(imageBounds, pointIndex, points));
110 } else {
111 os.writeTag(new PolylineTo16(imageBounds, pointIndex, points));
112 }
113 }
114 pointIndex = 0;
115 wide = false;
116 super.flush();
117 }
118
119 protected int toUnit(double d) {
120 return (int) (d * UNITS_PER_PIXEL * TWIPS);
121 }
122 }