1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
6 // NOTE: we have to make sure that a box is never smaller than the
7 // bounding box of its path unless the user specifically forced
10 // FIXME: fracture when realizing instead of when parsing?
13 - Base64 "data:" URL support
18 - filters (filtering of a group must be performed AFTER the group is assembled; sep. canvas)
22 - markers (do this in the parser; remember the order: fill, stroke, marker)
23 - bump caps [requires Paint that can fill circles...] [remember to distinguish between closed/unclosed]
27 - bump (easy, but requires 'round' Paint)
28 - subtree sharing? otherwise the memory consumption might be outrageous... clone="" attribute?
30 - intersect clip regions (linearity)
31 - clip on trapezoids, not pixels
32 - faster gradients and patterns:
33 - transform each corner of the trapezoid and then interpolate
37 /** XWT's fully conformant Static SVG Viewer; see SVG spec, section G.7 */
38 public final class VectorGraphics {
40 // Public entry points /////////////////////////////////////////////////////////////////
43 public static SVG.Font currentFont = null;
45 public static VectorPath parseVectorPath(String s) {
46 PathTokenizer t = new PathTokenizer(s);
47 VectorPath ret = new VectorPath();
48 char last_command = 'M';
50 while(t.hasMoreTokens()) {
51 char command = t.parseCommand();
52 if (first && command != 'M') throw new RuntimeException("the first command of a path must be 'M'");
54 boolean relative = Character.toLowerCase(command) == command;
55 command = Character.toLowerCase(command);
56 ret.parseSingleCommandAndArguments(t, command, relative);
57 last_command = command;
62 public static void parseNode(String name, String[] keys, Object[] vals, Template t) {
64 for(int i=0; i<keys.length; i++) if (vals[i] != null) h.put(keys[i], vals[i]);
66 Hash props = new Hash();
67 props.put("transform", h.get("transform"));
68 props.put("fill", h.get("fill"));
69 props.put("stroke", h.get("stroke"));
70 if ("visible".equals(h.get("overflow")) || "auto".equals(h.get("overflow")))
71 Log.log(SVG.class, "warning: overflow={auto|visible} not supported; ignoring");
72 if (h.get("display") != null) props.put("invisible", new Boolean("none".equals(h.get("display"))));
75 // FIXME: "the automatic transformation that is created due to
76 // a viewBox does not affect the x, y, width and height
77 // attributes". Also, transform+viewbox together?
79 if (h.get("preserveAspectRatio") != null) {
80 StringTokenizer st = new StringTokenizer((String)h.get("preserveAspectRatio"), " ");
81 String align = st.nextToken();
82 if ("defer".equals(align)) align = st.nextToken();
83 if (!align.equals("none")) {
84 // FIXME, need to beef up XWT's align property
86 if (align.startsWith("yMin")) align = "top";
87 else if (align.startsWith("yMax")) align = "bottom";
88 if (align.startsWith("xMin")) align += "left";
89 else if (align.startsWith("xMax")) align += "right";
90 props.put("align", align);
92 // FIXME: need to implement scaling property on boxes, also size-to-viewbox
93 props.put("scaling", "uniform");
94 if (st.hasMoreTokens()) {
95 String meetOrSlice = st.nextToken();
96 if (meetOrSlice.equals("meet")) props.put("scaling", "meet"); // keep within viewport
97 else if (meetOrSlice.equals("slice")) props.put("scaling", "slice"); // expand beyond viewport
101 // FIXME: insert an extra layer of boxen and put this transform on the inner layer
102 if (h.get("viewBox") != null) {
103 PathTokenizer pt = new PathTokenizer(h.get("viewBox").toString());
104 String transform = (String)props.get("transform");
105 if (transform == null) transform = "";
106 transform = "translate(" + (-1 * pt.parseFloat()) + ", " + (-1 * pt.parseFloat()) + ") " +
107 "scale(" + pt.parseFloat() + "%, " + pt.parseFloat() + "%) ";
110 String path = (String)h.get("d");
111 if (name.equals("g")) {
114 } else if (name.equals("font")) {
115 SVG.Font f = currentFont = new SVG.Font();
116 if (h.get("horiz-origin-x") != null) f.horiz_origin_x = Float.parseFloat(h.get("horiz-origin-x").toString());
117 if (h.get("horiz-origin-y") != null) f.horiz_origin_y = Float.parseFloat(h.get("horiz-origin-y").toString());
118 if (h.get("horiz-adv-x") != null) f.horiz_adv_x = Float.parseFloat(h.get("horiz-adv-x").toString());
119 if (h.get("vert-origin-x") != null) f.vert_origin_x = Float.parseFloat(h.get("vert-origin-x").toString());
120 if (h.get("vert-origin-y") != null) f.vert_origin_y = Float.parseFloat(h.get("vert-origin_y").toString());
121 if (h.get("vert-adv-y") != null) f.vert_adv_y = Float.parseFloat(h.get("vert-adv-y").toString());
123 } else if (name.equals("hkern")) {
126 } else if (name.equals("vkern")) {
129 } else if (name.equals("font-face")) {
132 } else if (name.equals("glyph") || name.equals("missing-glyph")) {
133 String glyphName = name.equals("missing-glyph") ? "missing-glyph" : (String)h.get("glyph-name");
134 SVG.Font.Glyph g = new SVG.Font.Glyph(glyphName, (String)h.get("unicode"), t, currentFont);
135 if (h.get("horiz-adv-x") != null) g.horiz_adv_x = Float.parseFloat(h.get("horiz-adv-x").toString());
136 if (h.get("vert-origin-x") != null) g.vert_origin_x = Float.parseFloat(h.get("vert-origin-x").toString());
137 if (h.get("vert-origin-y") != null) g.vert_origin_y = Float.parseFloat(h.get("vert-origin-y").toString());
138 if (h.get("vert-adv-y") != null) g.vert_adv_y = Float.parseFloat(h.get("vert-adv-y").toString());
139 if ("v".equals(h.get("orientation"))) g.isVerticallyOriented = true;
141 } else if (name.equals("svg")) {
142 // FIXME: handle percentages
143 // FIXME: what if these aren't provided?
144 // FIXME (in general)
145 float x = Float.parseFloat(h.get("x").toString());
146 float y = Float.parseFloat(h.get("y").toString());
147 float width = Float.parseFloat(h.get("width").toString());
148 float height = Float.parseFloat(h.get("height").toString());
149 h.put("viewBox", x + ", " + y + ", " + (x + width) + ", " + (y + height));
152 } else if (name.equals("path")) {
153 path = h.get("d").toString();
155 } else if (name.equals("rect")) {
156 float x = Float.parseFloat(h.get("x").toString());
157 float y = Float.parseFloat(h.get("y").toString());
158 float width = Float.parseFloat(h.get("width").toString());
159 float height = Float.parseFloat(h.get("height").toString());
160 float rx = Float.parseFloat(h.get("rx").toString());
161 float ry = Float.parseFloat(h.get("ry").toString());
163 "M" + (x + rx) + "," + y +
164 "H" + (x + width - rx) +
165 "A" + rx + "," + rx + ",0,0,1," + (x + width) + "," + (y + ry) +
166 "V" + (y + width - ry) +
167 "A" + rx + "," + rx + ",0,0,1," + (x + width - rx) + "," +
170 "A" + rx + "," + rx + ",0,0,1," + x + "," + (y + height - ry) +
172 "A" + rx + "," + rx + ",0,0,1," + (x + rx) + "," + (y + ry) +
175 } else if (name.equals("circle")) {
176 float r = Float.parseFloat(h.get("r").toString());
177 float cx = Float.parseFloat(h.get("cx").toString());
178 float cy = Float.parseFloat(h.get("cy").toString());
179 path = "A " + r + " " + r + " 1 1 " + cx + " " + cy;
181 } else if (name.equals("ellipse")) {
182 float rx = Float.parseFloat(h.get("rx").toString());
183 float ry = Float.parseFloat(h.get("ry").toString());
184 float cx = Float.parseFloat(h.get("cx").toString());
185 float cy = Float.parseFloat(h.get("cy").toString());
186 path = "A " + rx + " " + ry + " 1 1 " + cx + " " + cy;
188 } else if (name.equals("line")) {
189 float x1 = Float.parseFloat(h.get("x1").toString());
190 float y1 = Float.parseFloat(h.get("y1").toString());
191 float x2 = Float.parseFloat(h.get("x2").toString());
192 float y2 = Float.parseFloat(h.get("y2").toString());
193 path = "M " + x1 + " " + y1 + " L " + x2 + " " + y2;
195 } else if (name.equals("polyline") || name.equals("polygon")) {
196 StringTokenizer st = new StringTokenizer(h.get("points").toString(), ", ", false);
198 while(st.hasMoreTokens()) s += st.nextToken() + " " + st.nextToken() + " ";
199 path = s + (name.equals("polygon") ? "z" : "");
202 Log.log(SVG.class, "unknown element in SVG namespace: " + name);
204 props.put("path", path);
205 t.keys = new String[props.size()];
206 System.arraycopy(props.keys(), 0, t.keys, 0, t.keys.length);
207 t.vals = new String[props.size()];
208 for(int i=0; i<t.keys.length; i++) t.vals[i] = props.get(t.keys[i]);
212 if (h.get("viewBox") != null) {
213 StringTokenizer st = new StringTokenizer(h.get("viewBox").toString(), ", ", false);
214 if (t.transform == null) t.transform = "";
216 SVG.RasterPath.fromString(path).getBoundingBox(p1, p2);
218 float minx = st.parseFloat();
219 float miny = st.parseFloat();
220 float width = st.parseFloat();
221 float height = st.parseFloat();
222 t.transform += "translate(" + (-1 * p1.x) + ", " + (-1 * p1.y) + ") " +
223 "scale(" + ((p2.x - p1.x) / width) + ", " + ((p2.y - p1.y) / height) + ") " +
224 "translate(" + minx + ", " + miny + ") ";
226 // FIXME: preserveAspectRatio
232 public static class Font {
234 float horiz_origin_x = 0, horiz_origin_y = 0, horiz_adv_x = 0;
235 float vert_origin_x = 0, vert_origin_y = 0, vert_adv_y = 0;
237 // FIXME: avoid using substring() in here ore creating any objects
238 public void render(String text, DoubleBuffer buf, int x, int y, int fillcolor, int strokecolor, int size) {
239 // FIXME: points, not pixels
241 float scaleFactor = (float)(1.0/1000.0) * (float)size;
242 for(int pos=0; pos<text.length(); pos++) {
244 for(g = (Glyph)glyphByUnicode.get(text.substring(pos, pos+1));
245 g != null && !g.unicode.equals(text.substring(pos, pos + g.unicode.length()));
248 g = (Glyph)glyphByName.get("missing-glyph");
250 pos += g.unicode.length() - 1;
253 System.out.println(" " + g.unicode);
254 g.render(buf, x, y, fillcolor, strokecolor, scaleFactor);
255 x += (int)(g.horiz_adv_x * size / 1000.0);
257 x += (int)(horiz_adv_x * size / 1000.0);
263 / ** all glyphs, keyed by their <tt>name</tt> property * /
264 Hashtable glyphByName = new Hashtable();
266 / ** linked list of glyphs, stored by the first character of their <tt>unicode</tt> property * /
267 Hashtable glyphByUnicode = new Hashtable();
269 / ** a Glyph in an SVG font * /
270 public static class Glyph {
272 // FIXME: lang attribute
273 boolean isVerticallyOriented = false;
277 float horiz_adv_x = 0;
278 float vert_origin_x = 0;
279 float vert_origin_y = 0;
280 float vert_adv_y = 0;
282 String unicode = null;
284 / ** forms the linked list in glyphByUnicode; glyphs appear in the order specified in the font * /
285 public Glyph next = null;
287 Glyph(String name, String unicode, Template t, SVG.Font f) {
289 if (f.glyphByUnicode.get(unicode.substring(0, 1)) == null) {
290 f.glyphByUnicode.put(unicode.substring(0, 1), this);
293 for(g = (Glyph)f.glyphByUnicode.get(unicode.substring(0, 1)); g.next != null; g = g.next);
296 if (name != null) f.glyphByUnicode.put(name, this);
297 this.unicode = unicode;
299 horiz_adv_x = f.horiz_adv_x;
300 vert_origin_x = f.vert_origin_x;
301 vert_origin_y = f.vert_origin_y;
302 vert_adv_y = f.vert_adv_y;
304 public void render(DoubleBuffer buf, int x, int y, int fillcolor, int strokecolor, float scaleFactor) {
305 // FEATURE: make b double-buffered for increased performance
307 b = new Box(t, new org.xwt.util.Vec(), new org.xwt.util.Vec(), null, 0, 0);
308 b.put("absolute", Boolean.TRUE);
313 b.put("width", new Integer(1000));
314 b.put("height", new Integer(1000));
315 b.fillcolor = fillcolor;
316 b.strokecolor = strokecolor;
318 // we toss an extra flip on the ctm so that fonts stick "up" instead of down
319 b.render(0, 0, buf.getWidth(), buf.getHeight(), buf,
320 Affine.flip(false, true).multiply(Affine.scale(scaleFactor, scaleFactor).multiply(Affine.translate(x, y))).multiply(buf.a));
325 // Affine //////////////////////////////////////////////////////////////////////////////
327 / ** an affine transform; all operations are destructive * /
328 public static final class Affine {
333 public float a, b, c, d, e, f;
335 Affine(float _a, float _b, float _c, float _d, float _e, float _f) { a = _a; b = _b; c = _c; d = _d; e = _e; f = _f; }
336 public String toString() { return "[ " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + " ]"; }
337 public Affine copy() { return new Affine(a, b, c, d, e, f); }
338 public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); }
339 public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); }
340 public static Affine shear(float degrees) {
341 return new Affine(1, 0, (float)Math.tan(degrees * (float)(Math.PI / 180.0)), 1, 0, 0); }
342 public static Affine translate(float tx, float ty) { return new Affine(1, 0, 0, 1, tx, ty); }
343 public static Affine flip(boolean horiz, boolean vert) { return new Affine(horiz ? -1 : 1, 0, 0, vert ? -1 : 1, 0, 0); }
344 public float multiply_px(float x, float y) { return x * a + y * c + e; }
345 public float multiply_py(float x, float y) { return x * b + y * d + f; }
347 public static Affine rotate(float degrees) {
348 float s = (float)Math.sin(degrees * (float)(Math.PI / 180.0));
349 float c = (float)Math.cos(degrees * (float)(Math.PI / 180.0));
350 return new Affine(c, s, -s, c, 0, 0);
353 / ** this = this * a * /
354 public Affine multiply(Affine A) {
355 float _a = this.a * A.a + this.b * A.c;
356 float _b = this.a * A.b + this.b * A.d;
357 float _c = this.c * A.a + this.d * A.c;
358 float _d = this.c * A.b + this.d * A.d;
359 float _e = this.e * A.a + this.f * A.c + A.e;
360 float _f = this.e * A.b + this.f * A.d + A.f;
361 a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
365 public void invert() {
366 float det = 1 / (a * d - b * c);
368 float _b = -1 * b * det;
369 float _c = -1 * c * det;
371 float _e = -1 * e * a - f * c;
372 float _f = -1 * e * b - f * d;
373 a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
378 // PathTokenizer //////////////////////////////////////////////////////////////////////////////
380 public static final float PX_PER_INCH = 72;
381 public static final float INCHES_PER_CM = (float)0.3937;
382 public static final float INCHES_PER_MM = INCHES_PER_CM / 10;
384 public static class PathTokenizer {
385 // FIXME: check array bounds exception for improperly terminated string
388 char lastCommand = 'M';
389 public PathTokenizer(String s) { this.s = s; }
390 private void consumeWhitespace() {
391 while(i < s.length() && (Character.isWhitespace(s.charAt(i)))) i++;
392 if (i < s.length() && s.charAt(i) == ',') i++;
393 while(i < s.length() && (Character.isWhitespace(s.charAt(i)))) i++;
395 public boolean hasMoreTokens() { consumeWhitespace(); return i < s.length(); }
396 public char parseCommand() {
398 char c = s.charAt(i);
399 if (!Character.isLetter(c)) return lastCommand;
401 return lastCommand = c;
403 public float parseFloat() {
406 float multiplier = 1;
407 for(; i < s.length(); i++) {
408 char c = s.charAt(i);
409 if (Character.isWhitespace(c) || c == ',' || (c == '-' && i != start)) break;
410 if (!((c >= '0' && c <= '9') || c == '.' || c == 'e' || c == 'E' || c == '-')) {
411 if (c == '%') { // FIXME
412 } else if (s.regionMatches(i, "pt", 0, i+2)) { // FIXME
413 } else if (s.regionMatches(i, "em", 0, i+2)) { // FIXME
414 } else if (s.regionMatches(i, "pc", 0, i+2)) { // FIXME
415 } else if (s.regionMatches(i, "ex", 0, i+2)) { // FIXME
416 } else if (s.regionMatches(i, "mm", 0, i+2)) { i += 2; multiplier = INCHES_PER_MM * PX_PER_INCH; break;
417 } else if (s.regionMatches(i, "cm", 0, i+2)) { i += 2; multiplier = INCHES_PER_CM * PX_PER_INCH; break;
418 } else if (s.regionMatches(i, "in", 0, i+2)) { i += 2; multiplier = PX_PER_INCH; break;
419 } else if (s.regionMatches(i, "px", 0, i+2)) { i += 2; break;
420 } else if (Character.isLetter(c)) break;
421 throw new RuntimeException("didn't expect character \"" + c + "\" in a numeric constant");
424 if (start == i) throw new RuntimeException("FIXME");
425 return Float.parseFloat(s.substring(start, i)) * multiplier;
430 // Abstract Path //////////////////////////////////////////////////////////////////////////////
432 / ** an abstract path; may contain splines and arcs * /
433 public static class VectorPath {
435 // the number of vertices on this path
438 // the vertices of the path
439 float[] x = new float[DEFAULT_PATHLEN];
440 float[] y = new float[DEFAULT_PATHLEN];
442 // the type of each edge; type[i] is the type of the edge from x[i],y[i] to x[i+1],y[i+1]
443 byte[] type = new byte[DEFAULT_PATHLEN];
445 // bezier control points
446 float[] c1x = new float[DEFAULT_PATHLEN]; // or rx (arcto)
447 float[] c1y = new float[DEFAULT_PATHLEN]; // or ry (arcto)
448 float[] c2x = new float[DEFAULT_PATHLEN]; // or x-axis-rotation (arcto)
449 float[] c2y = new float[DEFAULT_PATHLEN]; // or large-arc << 1 | sweep (arcto)
451 boolean closed = false;
453 static final byte TYPE_MOVETO = 0;
454 static final byte TYPE_LINETO = 1;
455 static final byte TYPE_ARCTO = 2;
456 static final byte TYPE_CUBIC = 3;
457 static final byte TYPE_QUADRADIC = 4;
459 / ** Creates a concrete vector path transformed through the given matrix. * /
460 public RasterPath realize(Affine a) {
462 RasterPath ret = new RasterPath();
463 int NUMSTEPS = 5; // FIXME
465 ret.x[0] = (int)Math.round(a.multiply_px(x[0], y[0]));
466 ret.y[0] = (int)Math.round(a.multiply_py(x[0], y[0]));
468 for(int i=1; i<numvertices; i++) {
469 if (type[i] == TYPE_LINETO) {
472 ret.x[ret.numvertices] = (int)Math.round(a.multiply_px(rx, ry));
473 ret.y[ret.numvertices] = (int)Math.round(a.multiply_py(rx, ry));
474 ret.edges[ret.numedges++] = ret.numvertices - 1; ret.numvertices++;
476 } else if (type[i] == TYPE_MOVETO) {
479 ret.x[ret.numvertices] = (int)Math.round(a.multiply_px(rx, ry));
480 ret.y[ret.numvertices] = (int)Math.round(a.multiply_py(rx, ry));
483 } else if (type[i] == TYPE_ARCTO) {
487 float fa = ((int)c2y[i]) >> 1;
488 float fs = ((int)c2y[i]) & 1;
494 // F.6.5: given x1,y1,x2,y2,fa,fs, compute cx,cy,theta1,dtheta
495 float x1_ = (float)Math.cos(phi) * (x1 - x2) / 2 + (float)Math.sin(phi) * (y1 - y2) / 2;
496 float y1_ = -1 * (float)Math.sin(phi) * (x1 - x2) / 2 + (float)Math.cos(phi) * (y1 - y2) / 2;
497 float tmp = (float)Math.sqrt((rx * rx * ry * ry - rx * rx * y1_ * y1_ - ry * ry * x1_ * x1_) /
498 (rx * rx * y1_ * y1_ + ry * ry * x1_ * x1_));
499 float cx_ = (fa == fs ? -1 : 1) * tmp * (rx * y1_ / ry);
500 float cy_ = (fa == fs ? -1 : 1) * -1 * tmp * (ry * x1_ / rx);
501 float cx = (float)Math.cos(phi) * cx_ - (float)Math.sin(phi) * cy_ + (x1 + x2) / 2;
502 float cy = (float)Math.sin(phi) * cx_ + (float)Math.cos(phi) * cy_ + (y1 + y2) / 2;
504 // F.6.4 Conversion from center to endpoint parameterization
505 float ux = 1, uy = 0, vx = (x1_ - cx_) / rx, vy = (y1_ - cy_) / ry;
506 float det = ux * vy - uy * vx;
507 float theta1 = (det < 0 ? -1 : 1) *
508 (float)Math.acos((ux * vx + uy * vy) /
509 ((float)Math.sqrt(ux * ux + uy * uy) * (float)Math.sqrt(vx * vx + vy * vy)));
510 ux = (x1_ - cx_) / rx; uy = (y1_ - cy_) / ry;
511 vx = (-1 * x1_ - cx_) / rx; vy = (-1 * y1_ - cy_) / ry;
512 det = ux * vy - uy * vx;
513 float dtheta = (det < 0 ? -1 : 1) *
514 (float)Math.acos((ux * vx + uy * vy) /
515 ((float)Math.sqrt(ux * ux + uy * uy) * (float)Math.sqrt(vx * vx + vy * vy)));
516 dtheta = dtheta % (float)(2 * Math.PI);
518 if (fs == 0 && dtheta > 0) theta1 -= 2 * PI;
519 if (fs == 1 && dtheta < 0) theta1 += 2 * PI;
521 if (fa == 1 && dtheta < 0) dtheta = 2 * PI + dtheta;
522 else if (fa == 1 && dtheta > 0) dtheta = -1 * (2 * PI - dtheta);
524 // FIXME: integrate F.6.6
525 // FIXME: isn't quite ending where it should...
527 // F.6.3: Parameterization alternatives
528 float theta = theta1;
529 for(int j=0; j<NUMSTEPS; j++) {
530 float rasterx = rx * (float)Math.cos(theta) * (float)Math.cos(phi) -
531 ry * (float)Math.sin(theta) * (float)Math.sin(phi) + cx;
532 float rastery = rx * (float)Math.cos(theta) * (float)Math.sin(phi) +
533 ry * (float)Math.cos(phi) * (float)Math.sin(theta) + cy;
534 ret.x[ret.numvertices] = (int)Math.round(a.multiply_px(rasterx, rastery));
535 ret.y[ret.numvertices] = (int)Math.round(a.multiply_py(rasterx, rastery));
536 ret.edges[ret.numedges++] = ret.numvertices - 1; ret.numvertices++;
537 theta += dtheta / NUMSTEPS;
540 } else if (type[i] == TYPE_CUBIC) {
542 float ax = x[i+1] - 3 * c2x[i] + 3 * c1x[i] - x[i];
543 float bx = 3 * c2x[i] - 6 * c1x[i] + 3 * x[i];
544 float cx = 3 * c1x[i] - 3 * x[i];
546 float ay = y[i+1] - 3 * c2y[i] + 3 * c1y[i] - y[i];
547 float by = 3 * c2y[i] - 6 * c1y[i] + 3 * y[i];
548 float cy = 3 * c1y[i] - 3 * y[i];
551 for(float t=0; t<1; t += 1 / (float)NUMSTEPS) {
552 float rx = ax * t * t * t + bx * t * t + cx * t + dx;
553 float ry = ay * t * t * t + by * t * t + cy * t + dy;
554 ret.x[ret.numvertices] = (int)Math.round(a.multiply_px(rx, ry));
555 ret.y[ret.numvertices] = (int)Math.round(a.multiply_py(rx, ry));
556 ret.edges[ret.numedges++] = ret.numvertices - 1; ret.numvertices++;
560 } else if (type[i] == TYPE_QUADRADIC) {
562 float bx = x[i+1] - 2 * c1x[i] + x[i];
563 float cx = 2 * c1x[i] - 2 * x[i];
565 float by = y[i+1] - 2 * c1y[i] + y[i];
566 float cy = 2 * c1y[i] - 2 * y[i];
569 for(float t=0; t<1; t += 1 / (float)NUMSTEPS) {
570 float rx = bx * t * t + cx * t + dx;
571 float ry = by * t * t + cy * t + dy;
572 ret.x[ret.numvertices] = (int)Math.round(a.multiply_px(rx, ry));
573 ret.y[ret.numvertices] = (int)Math.round(a.multiply_py(rx, ry));
574 ret.edges[ret.numedges++] = ret.numvertices - 1; ret.numvertices++;
581 if (ret.numedges > 0) ret.sort(0, ret.numedges - 1, false);
585 protected void parseSingleCommandAndArguments(PathTokenizer t, char command, boolean relative) {
586 if (numvertices == 0 && command != 'm') throw new RuntimeException("first command MUST be an 'm'");
587 if (numvertices > x.length - 2) {
588 float[] new_x = new float[x.length * 2]; System.arraycopy(x, 0, new_x, 0, x.length); x = new_x;
589 float[] new_y = new float[y.length * 2]; System.arraycopy(y, 0, new_y, 0, y.length); y = new_y;
594 type[numvertices-1] = TYPE_LINETO;
595 for(where = numvertices - 1; where > 0; where--)
596 if (type[where - 1] == TYPE_MOVETO) break;
597 x[numvertices] = x[where];
598 y[numvertices] = y[where];
605 if (numvertices > 0) type[numvertices-1] = TYPE_MOVETO;
606 x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
607 y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
612 case 'l': case 'h': case 'v': {
613 type[numvertices-1] = TYPE_LINETO;
614 float first = t.parseFloat(), second;
615 if (command == 'h') {
616 second = relative ? 0 : y[numvertices - 1];
617 } else if (command == 'v') {
618 second = first; first = relative ? 0 : x[numvertices - 1];
620 second = t.parseFloat();
622 x[numvertices] = first + (relative ? x[numvertices - 1] : 0);
623 y[numvertices] = second + (relative ? y[numvertices - 1] : 0);
629 type[numvertices-1] = TYPE_ARCTO;
630 c1x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
631 c1y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
632 c2x[numvertices-1] = (t.parseFloat() / 360) * 2 * PI;
633 c2y[numvertices-1] = (((int)t.parseFloat()) << 1) | (int)t.parseFloat();
634 x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
635 y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
640 case 's': case 'c': {
641 type[numvertices-1] = TYPE_CUBIC;
642 if (command == 'c') {
643 c1x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
644 c1y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
645 } else if (numvertices > 1 && type[numvertices-2] == TYPE_CUBIC) {
646 c1x[numvertices-1] = 2 * x[numvertices - 1] - c2x[numvertices-2];
647 c1y[numvertices-1] = 2 * y[numvertices - 1] - c2y[numvertices-2];
649 c1x[numvertices-1] = x[numvertices-1];
650 c1y[numvertices-1] = y[numvertices-1];
652 c2x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
653 c2y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
654 x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
655 y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
660 case 't': case 'q': {
661 type[numvertices-1] = TYPE_QUADRADIC;
662 if (command == 'q') {
663 c1x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
664 c1y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
665 } else if (numvertices > 1 && type[numvertices-2] == TYPE_QUADRADIC) {
666 c1x[numvertices-1] = 2 * x[numvertices - 1] - c1x[numvertices-2];
667 c1y[numvertices-1] = 2 * y[numvertices - 1] - c1y[numvertices-2];
669 c1x[numvertices-1] = x[numvertices-1];
670 c1y[numvertices-1] = y[numvertices-1];
672 x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
673 y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
682 // invariant: after this loop, no two lines intersect other than at a vertex
684 int index = numvertices - 2;
685 for(int i=0; i<Math.min(numvertices - 3, index); i++) {
686 for(int j = index; j < numvertices - 1; j++) {
688 // I'm not sure how to deal with vertical lines...
689 if (x[i+1] == x[i] || x[j+1] == x[j]) continue;
691 float islope = (y[i+1] - y[i]) / (x[i+1] - x[i]);
692 float jslope = (y[j+1] - y[j]) / (x[j+1] - x[j]);
693 if (islope == jslope) continue; // parallel lines can't intersect
695 float _x = (islope * x[i] - jslope * x[j] + y[j] - y[i]) / (islope - jslope);
696 float _y = islope * (_x - x[i]) + y[i];
698 if (_x > Math.min(x[i+1], x[i]) && _x < Math.max(x[i+1], x[i]) &&
699 _x > Math.min(x[j+1], x[j]) && _x < Math.max(x[j+1], x[j])) {
700 // FIXME: something's not right in here. See if we can do without fracturing line 'i'.
701 for(int k = ++numvertices; k>i; k--) { x[k] = x[k - 1]; y[k] = y[k - 1]; }
704 x[numvertices] = x[numvertices - 1]; x[numvertices - 1] = _x;
705 y[numvertices] = y[numvertices - 1]; y[numvertices - 1] = _y;
706 edges[numedges++] = numvertices - 1; numvertices++;
708 break; // actually 'continue' the outermost loop
720 // Concrete Vector Path //////////////////////////////////////////////////////////////////////////////
722 / ** a vector path * /
723 public static class RasterPath {
725 // the vertices of this path
726 int[] x = new int[DEFAULT_PATHLEN];
727 int[] y = new int[DEFAULT_PATHLEN];
731 * A list of the vertices on this path which *start* an edge (rather than a moveto), sorted by increasing y.
732 * example: x[edges[1]],y[edges[1]] - x[edges[i]+1],y[edges[i]+1] is the second-topmost edge
733 * note that if x[i],y[i] - x[i+1],y[i+1] is a MOVETO, then no element in edges will be equal to i
735 int[] edges = new int[DEFAULT_PATHLEN];
738 / ** FIXME: if a path is closed "manually" you get caps on the ends; otherwise you get a marker... * /
739 boolean closed = false;
741 / ** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 * /
742 int sort(int left, int right, boolean partition) {
745 middle = (left + right) / 2;
746 int s = edges[right]; edges[right] = edges[middle]; edges[middle] = s;
747 for (i = left - 1, j = right; ; ) {
748 while (y[edges[++i]] < y[edges[right]]);
749 while (j > left && y[edges[--j]] > y[edges[right]]);
751 s = edges[i]; edges[i] = edges[j]; edges[j] = s;
753 s = edges[right]; edges[right] = edges[i]; edges[i] = s;
756 if (left >= right) return 0;
757 int p = sort(left, right, true);
758 sort(left, p - 1, false);
759 sort(p + 1, right, false);
764 / ** finds the x value at which the line intercepts the line y=_y * /
765 private int intercept(int i, float _y, boolean includeTop, boolean includeBottom) {
766 if (includeTop ? (_y < Math.min(y[i], y[i+1])) : (_y <= Math.min(y[i], y[i+1])))
767 return Integer.MIN_VALUE;
768 if (includeBottom ? (_y > Math.max(y[i], y[i+1])) : (_y >= Math.max(y[i], y[i+1])))
769 return Integer.MIN_VALUE;
770 return (int)Math.round((((float)(x[i + 1] - x[i])) /
771 ((float)(y[i + 1] - y[i])) ) * ((float)(_y - y[i])) + x[i]);
774 / ** fill the interior of the path * /
775 public void fill(DoubleBuffer buf, RasterPath pen, Paint paint) {
776 if (numedges == 0) return;
777 int y0 = y[edges[0]], y1 = y0;
778 boolean useEvenOdd = false;
780 // we iterate over all endpoints in increasing y-coordinate order
781 for(int index = 1; index<numedges; index++) {
784 // we now examine the horizontal band between y=y0 and y=y1
786 y1 = y[edges[index]];
787 if (y0 == y1) continue;
789 // within this band, we iterate over all edges
790 int x0 = Integer.MIN_VALUE;
791 int leftSegment = -1;
793 int x1 = Integer.MAX_VALUE;
794 int rightSegment = Integer.MAX_VALUE;
795 for(int i=0; i<numedges; i++) {
796 if (y[edges[i]] == y[edges[i]+1]) continue; // ignore horizontal lines; they are irrelevant.
797 // we order the segments by the x-coordinate of their midpoint;
798 // since segments cannot intersect, this is a well-ordering
799 int i0 = intercept(edges[i], y0, true, false);
800 int i1 = intercept(edges[i], y1, false, true);
801 if (i0 == Integer.MIN_VALUE || i1 == Integer.MIN_VALUE) continue;
802 int midpoint = i0 + i1;
803 if (midpoint < x0) continue;
804 if (midpoint == x0 && i <= leftSegment) continue;
805 if (midpoint > x1) continue;
806 if (midpoint == x1 && i >= rightSegment) continue;
810 if (leftSegment == rightSegment || rightSegment == Integer.MAX_VALUE) break;
811 if (leftSegment != -1)
812 if ((useEvenOdd && count % 2 != 0) || (!useEvenOdd && count != 0))
813 paint.fillTrapezoid(intercept(edges[leftSegment], y0, true, true),
814 intercept(edges[rightSegment], y0, true, true), y0,
815 intercept(edges[leftSegment], y1, true, true),
816 intercept(edges[rightSegment], y1, true, true), y1,
818 if (useEvenOdd) count++;
819 else count += (y[edges[rightSegment]] < y[edges[rightSegment]+1]) ? -1 : 1;
820 leftSegment = rightSegment; x0 = x1;
825 / ** stroke the outline of the path * /
826 public void stroke(DoubleBuffer buf, int width, int color, boolean mitre,
827 String dashArray, int dashOffset, float segLength) {
828 if (dashArray != null) {
831 float actualLength = 0;
832 for(int i=0; i<numvertices; i++) {
833 // skip over MOVETOs -- they do not contribute to path length
834 if (x[i] == x[i+1] && y[i] == y[i+1]) continue;
835 if (x[i+1] == x[i+2] && y[i+1] == y[i+2]) continue;
840 actualLength += java.lang.Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
842 ratio = actualLength / segLength;
844 PathTokenizer pt = new PathTokenizer(dashArray);
845 Vector v = new Vector();
846 while (pt.hasMoreTokens()) v.addElement(new Float(pt.parseFloat()));
847 float[] dashes = new float[v.size() % 2 == 0 ? v.size() : 2 * v.size()];
848 for(int i=0; i<dashes.length; i++) dashes[i] = ((Float)v.elementAt(i % v.size())).floatValue();
850 int dashpos = dashOffset;
851 boolean on = dashpos % 2 == 0;
852 for(int i=0; i<numvertices; i++) {
853 // skip over MOVETOs -- they do not contribute to path length
854 if (x[i] == x[i+1] && y[i] == y[i+1]) continue;
855 if (x[i+1] == x[i+2] && y[i+1] == y[i+2]) continue;
857 int x2 = (int)x[i + 1];
859 int y2 = (int)y[i + 1];
860 float segmentLength = (float)java.lang.Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
861 int _x1 = x1, _y1 = y1;
864 pos = Math.min(segmentLength, pos + dashes[dashpos] * ratio);
865 if (pos != segmentLength) dashpos = (dashpos + 1) % dashes.length;
866 int _x2 = (int)((x2 * pos + x1 * (segmentLength - pos)) / segmentLength);
867 int _y2 = (int)((y2 * pos + y1 * (segmentLength - pos)) / segmentLength);
868 if (on) buf.drawLine(_x1, _y1, _x2, _y2, width, color);
870 _x1 = _x2; _y1 = _y2;
871 } while(pos < segmentLength);
875 for(int i=0; i<numedges; i++)
876 buf.drawLine((int)x[edges[i]],
877 (int)y[edges[i]], (int)x[edges[i]+1], (int)y[edges[i]+1], width, color);
883 // Paint //////////////////////////////////////////////////////////////////////////////
885 public static interface Paint {
887 fillTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, DoubleBuffer buf);
890 public static class SingleColorPaint implements Paint {
892 public SingleColorPaint(int color) { this.color = color; }
893 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, DoubleBuffer buf) {
894 buf.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
898 public static abstract class GradientPaint extends Paint {
899 public GradientPaint(boolean reflect, boolean repeat, Affine gradientTransform,
900 int[] stop_colors, float[] stop_offsets) {
901 this.reflect = reflect; this.repeat = repeat;
902 this.gradientTransform = gradientTransform;
903 this.stop_colors = stop_colors;
904 this.stop_offsets = stop_offsets;
906 Affine gradientTransform = Affine.identity();
907 boolean useBoundingBox = false; // FIXME not supported
908 boolean patternUseBoundingBox = false; // FIXME not supported
910 // it's invalid for both of these to be true
911 boolean reflect = false; // FIXME not supported
912 boolean repeat = false; // FIXME not supported
914 float[] stop_offsets;
916 public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, DoubleBuffer buf) {
918 Affine inverse = a.copy().invert();
919 float slope1 = (tx3 - tx1) / (ty2 - ty1);
920 float slope2 = (tx4 - tx2) / (ty2 - ty1);
921 for(float y=ty1; y<ty2; y++) {
922 float _x1 = (y - ty1) * slope1 + tx1;
923 float _x2 = (y - ty1) * slope2 + tx2;
924 if (_x1 > _x2) { float _x0 = _x1; _x1 = _x2; _x2 = _x0; }
926 for(float x=_x1; x<_x2; x++) {
928 float distance = isLinear ?
929 // length of projection of <x,y> onto the gradient vector == {<x,y> \dot {grad \over |grad|}}
930 (x * (x2 - x1) + y * (y2 - y1)) / (float)Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) :
932 // radial form is simple! FIXME, not quite right
933 (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
935 // FIXME: offsets are 0..1, not 0..length(gradient)
936 int i = 0; for(; i<stop_offsets.length; i++) if (distance < stop_offsets[i]) break;
938 // FIXME: handle points beyond the bounds
939 if (i < 0 || i >= stop_offsets.length) continue;
941 // gradate from offsets[i - 1] to offsets[i]
942 float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
944 int a = (int)((((stop_colors[i] >> 24) & 0xff) - ((stop_colors[i - 1] >> 24) & 0xff)) * percentage) +
945 ((stop_colors[i - 1] >> 24) & 0xff);
946 int r = (int)((((stop_colors[i] >> 16) & 0xff) - ((stop_colors[i - 1] >> 16) & 0xff)) * percentage) +
947 ((stop_colors[i - 1] >> 16) & 0xff);
948 int g = (int)((((stop_colors[i] >> 8) & 0xff) - ((stop_colors[i - 1] >> 8) & 0xff)) * percentage) +
949 ((stop_colors[i - 1] >> 8) & 0xff);
950 int b = (int)((((stop_colors[i] >> 0) & 0xff) - ((stop_colors[i - 1] >> 0) & 0xff)) * percentage) +
951 ((stop_colors[i - 1] >> 0) & 0xff);
952 int argb = (a << 24) | (r << 16) | (g << 8) | b;
953 buf.drawPoint((int)x, (int)Math.floor(y), argb);
959 public static class LinearGradientPaint extends GradientPaint {
960 public LinearGradientPaint(float x1, float y1, float x2, float y2, boolean reflect, boolean repeat,
961 Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
962 super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
963 this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
965 float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
968 public static class RadialGradientPaint extends GradientPaint {
969 public RadialGradientPaint(float cx, float cy, float fx, float fy, float r, boolean reflect, boolean repeat,
970 Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
971 super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
972 this.cx = cx; this.cy = cy; this.fx = fx; this.fy = fy; this.r = r;
975 float cx, cy, r, fx, fy;
980 // Private Constants //////////////////////////////////////////////////////////////////////////////////
982 private static final int DEFAULT_PATHLEN = 1000;
984 / ** Copied verbatim from the SVG specification * /
985 public static Hashtable colors = new Hashtable(400);
987 colors.put("aliceblue", new Integer((240 << 16) | (248 << 8) | 255));
988 colors.put("antiquewhite", new Integer((250 << 16) | (235 << 8) | 215));
989 colors.put("aqua", new Integer((0 << 16) | (255 << 8) | 255));
990 colors.put("aquamarine", new Integer((127 << 16) | (255 << 8) | 212));
991 colors.put("azure", new Integer((240 << 16) | (255 << 8) | 255));
992 colors.put("beige", new Integer((245 << 16) | (245 << 8) | 220));
993 colors.put("bisque", new Integer((255 << 16) | (228 << 8) | 196));
994 colors.put("black", new Integer((0 << 16) | (0 << 8) | 0));
995 colors.put("blanchedalmond", new Integer((255 << 16) | (235 << 8) | 205));
996 colors.put("blue", new Integer((0 << 16) | (0 << 8) | 255));
997 colors.put("blueviolet", new Integer((138 << 16) | (43 << 8) | 226));
998 colors.put("brown", new Integer((165 << 16) | (42 << 8) | 42));
999 colors.put("burlywood", new Integer((222 << 16) | (184 << 8) | 135));
1000 colors.put("cadetblue", new Integer((95 << 16) | (158 << 8) | 160));
1001 colors.put("chartreuse", new Integer((127 << 16) | (255 << 8) | 0));
1002 colors.put("chocolate", new Integer((210 << 16) | (105 << 8) | 30));
1003 colors.put("coral", new Integer((255 << 16) | (127 << 8) | 80));
1004 colors.put("cornflowerblue", new Integer((100 << 16) | (149 << 8) | 237));
1005 colors.put("cornsilk", new Integer((255 << 16) | (248 << 8) | 220));
1006 colors.put("crimson", new Integer((220 << 16) | (20 << 8) | 60));
1007 colors.put("cyan", new Integer((0 << 16) | (255 << 8) | 255));
1008 colors.put("darkblue", new Integer((0 << 16) | (0 << 8) | 139));
1009 colors.put("darkcyan", new Integer((0 << 16) | (139 << 8) | 139));
1010 colors.put("darkgoldenrod", new Integer((184 << 16) | (134 << 8) | 11));
1011 colors.put("darkgray", new Integer((169 << 16) | (169 << 8) | 169));
1012 colors.put("darkgreen", new Integer((0 << 16) | (100 << 8) | 0));
1013 colors.put("darkgrey", new Integer((169 << 16) | (169 << 8) | 169));
1014 colors.put("darkkhaki", new Integer((189 << 16) | (183 << 8) | 107));
1015 colors.put("darkmagenta", new Integer((139 << 16) | (0 << 8) | 139));
1016 colors.put("darkolivegreen", new Integer((85 << 16) | (107 << 8) | 47));
1017 colors.put("darkorange", new Integer((255 << 16) | (140 << 8) | 0));
1018 colors.put("darkorchid", new Integer((153 << 16) | (50 << 8) | 204));
1019 colors.put("darkred", new Integer((139 << 16) | (0 << 8) | 0));
1020 colors.put("darksalmon", new Integer((233 << 16) | (150 << 8) | 122));
1021 colors.put("darkseagreen", new Integer((143 << 16) | (188 << 8) | 143));
1022 colors.put("darkslateblue", new Integer((72 << 16) | (61 << 8) | 139));
1023 colors.put("darkslategray", new Integer((47 << 16) | (79 << 8) | 79));
1024 colors.put("darkslategrey", new Integer((47 << 16) | (79 << 8) | 79));
1025 colors.put("darkturquoise", new Integer((0 << 16) | (206 << 8) | 209));
1026 colors.put("darkviolet", new Integer((148 << 16) | (0 << 8) | 211));
1027 colors.put("deeppink", new Integer((255 << 16) | (20 << 8) | 147));
1028 colors.put("deepskyblue", new Integer((0 << 16) | (191 << 8) | 255));
1029 colors.put("dimgray", new Integer((105 << 16) | (105 << 8) | 105));
1030 colors.put("dimgrey", new Integer((105 << 16) | (105 << 8) | 105));
1031 colors.put("dodgerblue", new Integer((30 << 16) | (144 << 8) | 255));
1032 colors.put("firebrick", new Integer((178 << 16) | (34 << 8) | 34));
1033 colors.put("floralwhite", new Integer((255 << 16) | (250 << 8) | 240));
1034 colors.put("forestgreen", new Integer((34 << 16) | (139 << 8) | 34));
1035 colors.put("fuchsia", new Integer((255 << 16) | (0 << 8) | 255));
1036 colors.put("gainsboro", new Integer((220 << 16) | (220 << 8) | 220));
1037 colors.put("ghostwhite", new Integer((248 << 16) | (248 << 8) | 255));
1038 colors.put("gold", new Integer((255 << 16) | (215 << 8) | 0));
1039 colors.put("goldenrod", new Integer((218 << 16) | (165 << 8) | 32));
1040 colors.put("gray", new Integer((128 << 16) | (128 << 8) | 128));
1041 colors.put("grey", new Integer((128 << 16) | (128 << 8) | 128));
1042 colors.put("green", new Integer((0 << 16) | (128 << 8) | 0));
1043 colors.put("greenyellow", new Integer((173 << 16) | (255 << 8) | 47));
1044 colors.put("honeydew", new Integer((240 << 16) | (255 << 8) | 240));
1045 colors.put("hotpink", new Integer((255 << 16) | (105 << 8) | 180));
1046 colors.put("indianred", new Integer((205 << 16) | (92 << 8) | 92));
1047 colors.put("indigo", new Integer((75 << 16) | (0 << 8) | 130));
1048 colors.put("ivory", new Integer((255 << 16) | (255 << 8) | 240));
1049 colors.put("khaki", new Integer((240 << 16) | (230 << 8) | 140));
1050 colors.put("lavender", new Integer((230 << 16) | (230 << 8) | 250));
1051 colors.put("lavenderblush", new Integer((255 << 16) | (240 << 8) | 245));
1052 colors.put("lawngreen", new Integer((124 << 16) | (252 << 8) | 0));
1053 colors.put("lemonchiffon", new Integer((255 << 16) | (250 << 8) | 205));
1054 colors.put("lightblue", new Integer((173 << 16) | (216 << 8) | 230));
1055 colors.put("lightcoral", new Integer((240 << 16) | (128 << 8) | 128));
1056 colors.put("lightcyan", new Integer((224 << 16) | (255 << 8) | 255));
1057 colors.put("lightgoldenrodyellow", new Integer((250 << 16) | (250 << 8) | 210));
1058 colors.put("lightgray", new Integer((211 << 16) | (211 << 8) | 211));
1059 colors.put("lightgreen", new Integer((144 << 16) | (238 << 8) | 144));
1060 colors.put("lightgrey", new Integer((211 << 16) | (211 << 8) | 211));
1061 colors.put("lightpink", new Integer((255 << 16) | (182 << 8) | 193));
1062 colors.put("lightsalmon", new Integer((255 << 16) | (160 << 8) | 122));
1063 colors.put("lightseagreen", new Integer((32 << 16) | (178 << 8) | 170));
1064 colors.put("lightskyblue", new Integer((135 << 16) | (206 << 8) | 250));
1065 colors.put("lightslategray", new Integer((119 << 16) | (136 << 8) | 153));
1066 colors.put("lightslategrey", new Integer((119 << 16) | (136 << 8) | 153));
1067 colors.put("lightsteelblue", new Integer((176 << 16) | (196 << 8) | 222));
1068 colors.put("lightyellow", new Integer((255 << 16) | (255 << 8) | 224));
1069 colors.put("lime", new Integer((0 << 16) | (255 << 8) | 0));
1070 colors.put("limegreen", new Integer((50 << 16) | (205 << 8) | 50));
1071 colors.put("linen", new Integer((250 << 16) | (240 << 8) | 230));
1072 colors.put("magenta", new Integer((255 << 16) | (0 << 8) | 255));
1073 colors.put("maroon", new Integer((128 << 16) | (0 << 8) | 0));
1074 colors.put("mediumaquamarine", new Integer((102 << 16) | (205 << 8) | 170));
1075 colors.put("mediumblue", new Integer((0 << 16) | (0 << 8) | 205));
1076 colors.put("mediumorchid", new Integer((186 << 16) | (85 << 8) | 211));
1077 colors.put("mediumpurple", new Integer((147 << 16) | (112 << 8) | 219));
1078 colors.put("mediumseagreen", new Integer((60 << 16) | (179 << 8) | 113));
1079 colors.put("mediumslateblue", new Integer((123 << 16) | (104 << 8) | 238));
1080 colors.put("mediumspringgreen", new Integer((0 << 16) | (250 << 8) | 154));
1081 colors.put("mediumturquoise", new Integer((72 << 16) | (209 << 8) | 204));
1082 colors.put("mediumvioletred", new Integer((199 << 16) | (21 << 8) | 133));
1083 colors.put("midnightblue", new Integer((25 << 16) | (25 << 8) | 112));
1084 colors.put("mintcream", new Integer((245 << 16) | (255 << 8) | 250));
1085 colors.put("mistyrose", new Integer((255 << 16) | (228 << 8) | 225));
1086 colors.put("moccasin", new Integer((255 << 16) | (228 << 8) | 181));
1087 colors.put("navajowhite", new Integer((255 << 16) | (222 << 8) | 173));
1088 colors.put("navy", new Integer((0 << 16) | (0 << 8) | 128));
1089 colors.put("oldlace", new Integer((253 << 16) | (245 << 8) | 230));
1090 colors.put("olive", new Integer((128 << 16) | (128 << 8) | 0));
1091 colors.put("olivedrab", new Integer((107 << 16) | (142 << 8) | 35));
1092 colors.put("orange", new Integer((255 << 16) | (165 << 8) | 0));
1093 colors.put("orangered", new Integer((255 << 16) | (69 << 8) | 0));
1094 colors.put("orchid", new Integer((218 << 16) | (112 << 8) | 214));
1095 colors.put("palegoldenrod", new Integer((238 << 16) | (232 << 8) | 170));
1096 colors.put("palegreen", new Integer((152 << 16) | (251 << 8) | 152));
1097 colors.put("paleturquoise", new Integer((175 << 16) | (238 << 8) | 238));
1098 colors.put("palevioletred", new Integer((219 << 16) | (112 << 8) | 147));
1099 colors.put("papayawhip", new Integer((255 << 16) | (239 << 8) | 213));
1100 colors.put("peachpuff", new Integer((255 << 16) | (218 << 8) | 185));
1101 colors.put("peru", new Integer((205 << 16) | (133 << 8) | 63));
1102 colors.put("pink", new Integer((255 << 16) | (192 << 8) | 203));
1103 colors.put("plum", new Integer((221 << 16) | (160 << 8) | 221));
1104 colors.put("powderblue", new Integer((176 << 16) | (224 << 8) | 230));
1105 colors.put("purple", new Integer((128 << 16) | (0 << 8) | 128));
1106 colors.put("red", new Integer((255 << 16) | (0 << 8) | 0));
1107 colors.put("rosybrown", new Integer((188 << 16) | (143 << 8) | 143));
1108 colors.put("royalblue", new Integer((65 << 16) | (105 << 8) | 225));
1109 colors.put("saddlebrown", new Integer((139 << 16) | (69 << 8) | 19));
1110 colors.put("salmon", new Integer((250 << 16) | (128 << 8) | 114));
1111 colors.put("sandybrown", new Integer((244 << 16) | (164 << 8) | 96));
1112 colors.put("seagreen", new Integer((46 << 16) | (139 << 8) | 87));
1113 colors.put("seashell", new Integer((255 << 16) | (245 << 8) | 238));
1114 colors.put("sienna", new Integer((160 << 16) | (82 << 8) | 45));
1115 colors.put("silver", new Integer((192 << 16) | (192 << 8) | 192));
1116 colors.put("skyblue", new Integer((135 << 16) | (206 << 8) | 235));
1117 colors.put("slateblue", new Integer((106 << 16) | (90 << 8) | 205));
1118 colors.put("slategray", new Integer((112 << 16) | (128 << 8) | 144));
1119 colors.put("slategrey", new Integer((112 << 16) | (128 << 8) | 144));
1120 colors.put("snow", new Integer((255 << 16) | (250 << 8) | 250));
1121 colors.put("springgreen", new Integer((0 << 16) | (255 << 8) | 127));
1122 colors.put("steelblue", new Integer((70 << 16) | (130 << 8) | 180));
1123 colors.put("tan", new Integer((210 << 16) | (180 << 8) | 140));
1124 colors.put("teal", new Integer((0 << 16) | (128 << 8) | 128));
1125 colors.put("thistle", new Integer((216 << 16) | (191 << 8) | 216));
1126 colors.put("tomato", new Integer((255 << 16) | (99 << 8) | 71));
1127 colors.put("turquoise", new Integer((64 << 16) | (224 << 8) | 208));
1128 colors.put("violet", new Integer((238 << 16) | (130 << 8) | 238));
1129 colors.put("wheat", new Integer((245 << 16) | (222 << 8) | 179));
1130 colors.put("white", new Integer((255 << 16) | (255 << 8) | 255));
1131 colors.put("whitesmoke", new Integer((245 << 16) | (245 << 8) | 245));
1132 colors.put("yellow", new Integer((255 << 16) | (255 << 8) | 0));
1133 colors.put("yellowgreen", new Integer((154 << 16) | (205 << 8) | 50));
1136 private static final float PI = (float)Math.PI;