cd62d8960be75c649084d3cd4c750cf6e5f78a3f
[org.ibex.core.git] / src / org / xwt / VectorGraphics.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3 import org.xwt.util.*;
4 import java.util.*;
5
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
8 //       it to be so.
9
10 // FIXME: fracture when realizing instead of when parsing?
11 /*
12     v1.0
13     - Base64 "data:" URL support
14     - textpath
15     - gradients
16     - patterns
17     - clipping/masking
18     - filters (filtering of a group must be performed AFTER the group is assembled; sep. canvas)
19     - style sheets
20
21     v1.1
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]
24     - line joins
25         - mitre    (hard)
26         - bevel    (easy)
27         - bump     (easy, but requires 'round' Paint)
28     - subtree sharing? otherwise the memory consumption might be outrageous...  clone="" attribute?
29     - better clipping
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
34
35  */
36
37 /** XWT's fully conformant Static SVG Viewer; see SVG spec, section G.7 */
38 public final class VectorGraphics {
39     /*
40     // Public entry points /////////////////////////////////////////////////////////////////
41
42     // FIXME
43     public static SVG.Font currentFont = null;
44
45     public static VectorPath parseVectorPath(String s) {
46         PathTokenizer t = new PathTokenizer(s);
47         VectorPath ret = new VectorPath();
48         char last_command = 'M';
49         boolean first = true;
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'");
53             first = false;
54             boolean relative = Character.toLowerCase(command) == command;
55             command = Character.toLowerCase(command);
56             ret.parseSingleCommandAndArguments(t, command, relative);
57             last_command = command;
58         }
59         return ret;
60     }
61
62     public static void parseNode(String name, String[] keys, Object[] vals, Template t) {
63         Hash h = new Hash();
64         for(int i=0; i<keys.length; i++) if (vals[i] != null) h.put(keys[i], vals[i]);
65
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"))));
73
74
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?
78
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
85                 align = "";
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);
91             }
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
98             }
99         }
100
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() + "%) ";
108         }
109         
110         String path = (String)h.get("d");
111         if (name.equals("g")) {
112             path = null;
113
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());
122
123         } else if (name.equals("hkern")) {
124         //FIXME
125
126         } else if (name.equals("vkern")) {
127         //FIXME
128
129         } else if (name.equals("font-face")) {
130         //FIXME
131
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;
140
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));
150             path = "";
151             
152         } else if (name.equals("path")) {
153             path = h.get("d").toString();
154             
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());
162             path =
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) + "," +
168                 (y + height) +
169                 "H" + (x + rx) +
170                 "A" + rx + "," + rx + ",0,0,1," + x + "," + (y + height - ry) +
171                 "V" + (y + ry) +
172                 "A" + rx + "," + rx + ",0,0,1," + (x + rx) + "," + (y + ry) +
173                 "Z";
174             
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;
180             
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;
187             
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;
194                 
195         } else if (name.equals("polyline") || name.equals("polygon")) {
196             StringTokenizer st = new StringTokenizer(h.get("points").toString(), ", ", false);
197             String s = "M ";
198             while(st.hasMoreTokens()) s += st.nextToken() + " " + st.nextToken() + " ";
199             path = s + (name.equals("polygon") ? "z" : "");
200
201         } else {
202             Log.log(SVG.class, "unknown element in SVG namespace: " + name);
203         }
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]);
209         
210
211         // FIXME!!!!
212         if (h.get("viewBox") != null) {
213         StringTokenizer st = new StringTokenizer(h.get("viewBox").toString(), ", ", false);
214         if (t.transform == null) t.transform = "";
215         Point p1, p2;
216         SVG.RasterPath.fromString(path).getBoundingBox(p1, p2);
217         
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 + ") ";
225         
226         // FIXME: preserveAspectRatio
227         }
228
229     }
230     
231     
232     public static class Font {
233         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;
236
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
240             Affine a = buf.a;
241             float scaleFactor = (float)(1.0/1000.0) * (float)size;
242             for(int pos=0; pos<text.length(); pos++) { 
243                 Glyph g;
244                 for(g = (Glyph)glyphByUnicode.get(text.substring(pos, pos+1));
245                     g != null && !g.unicode.equals(text.substring(pos, pos + g.unicode.length()));
246                     g = g.next);
247                 if (g == null) {
248                     g = (Glyph)glyphByName.get("missing-glyph");
249                 } else {
250                     pos += g.unicode.length() - 1;
251                 }
252                 if (g != null) {
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);
256                 } else {
257                     x += (int)(horiz_adv_x * size / 1000.0);
258                 }
259             }
260             buf.setTransform(a);
261         }
262
263         / ** all glyphs, keyed by their <tt>name</tt> property * /
264         Hashtable glyphByName = new Hashtable();
265
266         / ** linked list of glyphs, stored by the first character of their <tt>unicode</tt> property * /
267         Hashtable glyphByUnicode = new Hashtable();
268
269         / ** a Glyph in an SVG font * /
270         public static class Glyph {
271
272             // FIXME: lang attribute
273             boolean isVerticallyOriented = false;
274             Template t = null;
275             Box b = null;
276
277             float horiz_adv_x = 0;
278             float vert_origin_x = 0;
279             float vert_origin_y = 0;
280             float vert_adv_y = 0;
281
282             String unicode = null;
283
284             / ** forms the linked list in glyphByUnicode; glyphs appear in the order specified in the font * /
285             public Glyph next = null;
286
287             Glyph(String name, String unicode, Template t, SVG.Font f) {
288                 if (unicode != null)
289                     if (f.glyphByUnicode.get(unicode.substring(0, 1)) == null) {
290                         f.glyphByUnicode.put(unicode.substring(0, 1), this);
291                     } else {
292                         Glyph g;
293                         for(g = (Glyph)f.glyphByUnicode.get(unicode.substring(0, 1)); g.next != null; g = g.next);
294                         g.next = this;
295                     }
296                 if (name != null) f.glyphByUnicode.put(name, this);
297                 this.unicode = unicode;
298                 this.t = t;
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;
303             }
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
306                 if (b == null) {
307                     b = new Box(t, new org.xwt.util.Vec(), new org.xwt.util.Vec(), null, 0, 0);
308                     b.put("absolute", Boolean.TRUE);
309                     b.prerender();
310                     t = null;
311                 }
312                 // FIXME
313                 b.put("width", new Integer(1000));
314                 b.put("height", new Integer(1000));
315                 b.fillcolor = fillcolor;
316                 b.strokecolor = strokecolor;
317
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));
321             }
322         }
323     }
324
325     // Affine //////////////////////////////////////////////////////////////////////////////
326
327     / ** an affine transform; all operations are destructive * /
328     public static final class Affine {
329
330         //  [ a b e ]
331         //  [ c d f ]
332         //  [ 0 0 1 ]
333         public float a, b, c, d, e, f;
334
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; }
346
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);
351         }
352
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;
362             return this;
363         }
364
365         public void invert() {
366             float det = 1 / (a * d - b * c);
367             float _a = d * det;
368             float _b = -1 * b * det;
369             float _c = -1 * c * det;
370             float _d = a * 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;
374         }
375     }
376
377
378     // PathTokenizer //////////////////////////////////////////////////////////////////////////////
379
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;
383
384     public static class PathTokenizer {
385         // FIXME: check array bounds exception for improperly terminated string
386         String s;
387         int i = 0;
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++;
394         }
395         public boolean hasMoreTokens() { consumeWhitespace(); return i < s.length(); }
396         public char parseCommand() {
397             consumeWhitespace();
398             char c = s.charAt(i);
399             if (!Character.isLetter(c)) return lastCommand;
400             i++;
401             return lastCommand = c;
402         }
403         public float parseFloat() {
404             consumeWhitespace();
405             int start = i;
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");
422                 }
423             }
424             if (start == i) throw new RuntimeException("FIXME");
425             return Float.parseFloat(s.substring(start, i)) * multiplier;
426         }
427     }
428
429
430     // Abstract Path //////////////////////////////////////////////////////////////////////////////
431
432     / ** an abstract path; may contain splines and arcs * /
433     public static class VectorPath {
434
435         // the number of vertices on this path
436         int numvertices = 0;
437
438         // the vertices of the path
439         float[] x = new float[DEFAULT_PATHLEN];
440         float[] y = new float[DEFAULT_PATHLEN];
441
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];
444
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)
450
451         boolean closed = false;
452
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;
458
459         / ** Creates a concrete vector path transformed through the given matrix. * /
460         public RasterPath realize(Affine a) {
461
462             RasterPath ret = new RasterPath();
463             int NUMSTEPS = 5;  // FIXME
464             ret.numvertices = 1;
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]));
467
468             for(int i=1; i<numvertices; i++) {
469                 if (type[i] == TYPE_LINETO) {
470                     float rx = x[i];
471                     float ry = y[i];
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++;
475
476                 } else if (type[i] == TYPE_MOVETO) {
477                     float rx = x[i];
478                     float ry = y[i];
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));
481                     ret.numvertices++;
482
483                 } else if (type[i] == TYPE_ARCTO) {
484                     float rx = c1x[i];
485                     float ry = c1y[i];
486                     float phi = c2x[i];
487                     float fa = ((int)c2y[i]) >> 1;
488                     float fs = ((int)c2y[i]) & 1;
489                     float x1 = x[i];
490                     float y1 = y[i];
491                     float x2 = x[i+1];
492                     float y2 = y[i+1];
493
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;
503
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);
517
518                     if (fs == 0 && dtheta > 0) theta1 -= 2 * PI; 
519                     if (fs == 1 && dtheta < 0) theta1 += 2 * PI;
520
521                     if (fa == 1 && dtheta < 0) dtheta = 2 * PI + dtheta;
522                     else if (fa == 1 && dtheta > 0) dtheta = -1 * (2 * PI - dtheta);
523
524                     // FIXME: integrate F.6.6
525                     // FIXME: isn't quite ending where it should...
526
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;
538                     }
539
540                 } else if (type[i] == TYPE_CUBIC) {
541
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];
545                     float dx = 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];
549                     float dy = y[i];
550                     
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++;
557                     }
558
559
560                 } else if (type[i] == TYPE_QUADRADIC) {
561
562                     float bx = x[i+1] - 2 * c1x[i] + x[i];
563                     float cx = 2 * c1x[i] - 2 * x[i];
564                     float dx = x[i];
565                     float by = y[i+1] - 2 * c1y[i] + y[i];
566                     float cy = 2 * c1y[i] - 2 * y[i];
567                     float dy = y[i];
568                         
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++;
575                     }
576
577                 }
578
579             }
580             
581             if (ret.numedges > 0) ret.sort(0, ret.numedges - 1, false);
582             return ret;
583         }
584
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;
590             }
591             switch(command) {
592             case 'z': {
593                 int where;
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];
599                 numvertices++;
600                 closed = true;
601                 break;
602             }
603
604             case 'm': {
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);
608                 numvertices++;
609                 break;
610             }
611
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];
619                 } else {
620                     second = t.parseFloat();
621                 }
622                 x[numvertices] = first + (relative ? x[numvertices - 1] : 0);
623                 y[numvertices] = second + (relative ? y[numvertices - 1] : 0);
624                 numvertices++;
625                 break;
626             }
627             
628             case 'a': {
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);
636                 numvertices++;
637                 break;
638             }
639
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];
648                 } else {
649                     c1x[numvertices-1] = x[numvertices-1];
650                     c1y[numvertices-1] = y[numvertices-1];
651                 }
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);
656                 numvertices++;
657                 break;
658             }
659
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];
668                 } else {
669                     c1x[numvertices-1] = x[numvertices-1];
670                     c1y[numvertices-1] = y[numvertices-1];
671                 }
672                 x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
673                 y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
674                 numvertices++;
675                 break;
676             }
677
678             default:
679                 // FIXME
680             }
681             / *
682             // invariant: after this loop, no two lines intersect other than at a vertex
683             // FIXME: cleanup
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++) {
687
688                     // I'm not sure how to deal with vertical lines...
689                     if (x[i+1] == x[i] || x[j+1] == x[j]) continue;
690                         
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
694                         
695                     float _x = (islope * x[i] - jslope * x[j] + y[j] - y[i]) / (islope - jslope);
696                     float _y = islope * (_x - x[i]) + y[i];
697                         
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]; }
702                         x[i+1] = _x;
703                         y[i+1] = _y;
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++;
707                         index++;
708                         break;  // actually 'continue' the outermost loop
709                     }
710                 }
711             }
712             * /
713
714         }
715     }
716
717
718
719
720     // Concrete Vector Path //////////////////////////////////////////////////////////////////////////////
721     
722     / ** a vector path * /
723     public static class RasterPath {
724
725         // the vertices of this path
726         int[] x = new int[DEFAULT_PATHLEN];
727         int[] y = new int[DEFAULT_PATHLEN];
728         int numvertices = 0;
729
730         / **
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
734          * /
735         int[] edges = new int[DEFAULT_PATHLEN];
736         int numedges = 0;
737
738         / ** FIXME: if a path is closed "manually" you get caps on the ends; otherwise you get a marker... * /
739         boolean closed = false;
740         
741         / ** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 * /
742         int sort(int left, int right, boolean partition) {
743             if (partition) {
744                 int i, j, middle;
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]]);
750                     if (i >= j) break;
751                     s = edges[i]; edges[i] = edges[j]; edges[j] = s;
752                 }
753                 s = edges[right]; edges[right] = edges[i]; edges[i] = s;
754                 return i;
755             } else {
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);
760                 return 0;
761             }
762         }
763
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]);
772         }
773
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;
779
780             // we iterate over all endpoints in increasing y-coordinate order
781             for(int index = 1; index<numedges; index++) {
782                 int count = 0;
783
784                 // we now examine the horizontal band between y=y0 and y=y1
785                 y0 = y1;
786                 y1 = y[edges[index]];
787                 if (y0 == y1) continue;
788  
789                 // within this band, we iterate over all edges
790                 int x0 = Integer.MIN_VALUE;
791                 int leftSegment = -1;
792                 while(true) {
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;
807                         rightSegment = i;
808                         x1 = midpoint;
809                     }
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,
817                                                 buf);
818                     if (useEvenOdd) count++;
819                     else count += (y[edges[rightSegment]] < y[edges[rightSegment]+1]) ? -1 : 1;
820                     leftSegment = rightSegment; x0 = x1;
821                 }
822             }
823         }
824         
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) {
829                 float ratio = 1;
830                 if (segLength > 0) {
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;
836                         int x1 = x[i];
837                         int x2 = x[i + 1];
838                         int y1 = y[i];
839                         int y2 = y[i + 1];
840                         actualLength += java.lang.Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
841                     }
842                     ratio = actualLength / segLength;
843                 }
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();
849                 float length = 0;
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;
856                     int x1 = (int)x[i];
857                     int x2 = (int)x[i + 1];
858                     int y1 = (int)y[i];
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;
862                     float pos = 0;
863                     do {
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);
869                         on = !on;
870                         _x1 = _x2; _y1 = _y2;
871                     } while(pos < segmentLength);
872                 }
873
874             } else {
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);
878             }
879         }
880     }
881
882
883     // Paint //////////////////////////////////////////////////////////////////////////////
884
885     public static interface Paint {
886         public abstract void
887             fillTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, DoubleBuffer buf);
888     }
889
890     public static class SingleColorPaint implements Paint {
891         int color;
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);
895         }
896     }
897     / *
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;
905         }
906         Affine gradientTransform = Affine.identity();
907         boolean useBoundingBox = false;            // FIXME not supported
908         boolean patternUseBoundingBox = false;     // FIXME not supported
909
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
913         int[] stop_colors;
914         float[] stop_offsets;
915
916         public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, DoubleBuffer buf) {
917             Affine a = buf.a;
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; }
925
926                 for(float x=_x1; x<_x2; x++) {
927                     
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)) :
931                         
932                         // radial form is simple! FIXME, not quite right
933                         (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
934                         
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;
937
938                     // FIXME: handle points beyond the bounds
939                     if (i < 0 || i >= stop_offsets.length) continue;
940
941                     // gradate from offsets[i - 1] to offsets[i]
942                     float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
943
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);
954                 }
955             }
956         }
957     }
958
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;
964         }
965         float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
966     }
967
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;
973         }
974             
975         float cx, cy, r, fx, fy;
976
977     }
978     * /
979
980     // Private Constants //////////////////////////////////////////////////////////////////////////////////
981
982     private static final int DEFAULT_PATHLEN = 1000;
983
984     / ** Copied verbatim from the SVG specification * /
985     public static Hashtable colors = new Hashtable(400);
986     static {
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));
1134     }
1135
1136     private static final float PI = (float)Math.PI;
1137     */
1138 }