bugfix in url parsing for HTTP.java
[org.ibex.core.git] / src / org / ibex / graphics / SVG.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.graphics;
3 import java.util.*;
4
5
6 // FIXME: offer a "subpixel" mode where we pass floats to the Platform and don't do any snapping
7 // FIXME: fracture when realizing instead of when parsing?
8
9 /*
10     v1.0
11     - textpath
12     - gradients
13     - patterns
14     - clipping/masking
15     - filters (filtering of a group must be performed AFTER the group is assembled; sep. canvas)
16
17     v1.1
18     - bump caps [requires Paint that can fill circles...] [remember to distinguish between closed/unclosed]
19     - line joins
20         - mitre    (hard)
21         - bevel    (easy)
22         - bump     (easy, but requires 'round' Paint)
23     - subtree sharing? otherwise the memory consumption might be outrageous... clone="" attribute?
24     - better clipping
25         - intersect clip regions (linearity)
26         - clip on trapezoids, not pixels
27     - faster gradients and patterns:
28         - transform each corner of the trapezoid and then interpolate
29 */
30
31 // FIXME: need to support style sheets and the 'style=' attribute
32 // FIXME: need to convert markers into subboxes
33 public class SVG {
34
35     /*
36     public static void parseNode(String name, String[] keys, Object[] vals, Template t) {
37         Hash h = new Hash();
38         for(int i=0; i<keys.length; i++) if (vals[i] != null) h.put(keys[i], vals[i]);
39
40         Hash props = new Hash();
41         props.put("transform", h.get("transform"));
42         props.put("fill", h.get("fill"));
43         props.put("stroke", h.get("stroke"));
44         if ("visible".equals(h.get("overflow")) || "auto".equals(h.get("overflow")))
45             Log.info(VectorGraphics.class, "warning: overflow={auto|visible} not supported; ignoring");
46         if (h.get("display") != null) props.put("invisible", new Boolean("none".equals(h.get("display"))));
47
48
49         // FIXME: "the automatic transformation that is created due to
50         // a viewBox does not affect the x, y, width and height
51         // attributes".  Also, transform+viewbox together?
52
53         if (h.get("preserveAspectRatio") != null) {
54             StringTokenizer st = new StringTokenizer((String)h.get("preserveAspectRatio"), " ");
55             String align = st.nextToken();
56             if ("defer".equals(align)) align = st.nextToken();
57             if (!align.equals("none")) {
58                 // FIXME, need to beef up XWT's align property
59                 align = "";
60                 if (align.startsWith("yMin")) align = "top";
61                 else if (align.startsWith("yMax")) align = "bottom";
62                 if (align.startsWith("xMin")) align += "left";
63                 else if (align.startsWith("xMax")) align += "right";
64                 props.put("align", align);
65             }
66             // FIXME: need to implement scaling property on boxes, also size-to-viewbox
67             props.put("scaling", "uniform");
68             if (st.hasMoreTokens()) {
69                 String meetOrSlice = st.nextToken();
70                 if (meetOrSlice.equals("meet")) props.put("scaling", "meet");          // keep within viewport
71                 else if (meetOrSlice.equals("slice")) props.put("scaling", "slice");   // expand beyond viewport
72             }
73         }
74
75         // FIXME: insert an extra layer of boxen and put this transform on the inner layer
76         if (h.get("viewBox") != null) {
77             PathTokenizer pt = new PathTokenizer(h.get("viewBox").toString());
78             String transform = (String)props.get("transform");
79             if (transform == null) transform = "";
80             transform = "translate(" + (-1 * pt.parseFloat()) + ", " + (-1 * pt.parseFloat()) + ") " + 
81                 "scale(" + pt.parseFloat() + "%, " + pt.parseFloat() + "%) ";
82         }
83         
84         String path = (String)h.get("d");
85         if (name.equals("g")) {
86             path = null;
87
88         } else if (name.equals("font")) {
89             VectorGraphics.Font f = currentFont = new VectorGraphics.Font();
90             if (h.get("horiz-origin-x") != null) f.horiz_origin_x = Float.parseFloat(h.get("horiz-origin-x").toString());
91             if (h.get("horiz-origin-y") != null) f.horiz_origin_y = Float.parseFloat(h.get("horiz-origin-y").toString());
92             if (h.get("horiz-adv-x") != null) f.horiz_adv_x = Float.parseFloat(h.get("horiz-adv-x").toString());
93             if (h.get("vert-origin-x") != null) f.vert_origin_x = Float.parseFloat(h.get("vert-origin-x").toString());
94             if (h.get("vert-origin-y") != null) f.vert_origin_y = Float.parseFloat(h.get("vert-origin_y").toString());
95             if (h.get("vert-adv-y") != null) f.vert_adv_y = Float.parseFloat(h.get("vert-adv-y").toString());
96
97         } else if (name.equals("hkern")) {
98         //FIXME
99
100         } else if (name.equals("vkern")) {
101         //FIXME
102
103         } else if (name.equals("font-face")) {
104         //FIXME
105
106         } else if (name.equals("glyph") || name.equals("missing-glyph")) {
107             String glyphName = name.equals("missing-glyph") ? "missing-glyph" : (String)h.get("glyph-name");
108             VectorGraphics.Font.Glyph g = new VectorGraphics.Font.Glyph(glyphName, (String)h.get("unicode"), t, currentFont);
109             if (h.get("horiz-adv-x") != null) g.horiz_adv_x = Float.parseFloat(h.get("horiz-adv-x").toString());
110             if (h.get("vert-origin-x") != null) g.vert_origin_x = Float.parseFloat(h.get("vert-origin-x").toString());
111             if (h.get("vert-origin-y") != null) g.vert_origin_y = Float.parseFloat(h.get("vert-origin-y").toString());
112             if (h.get("vert-adv-y") != null) g.vert_adv_y = Float.parseFloat(h.get("vert-adv-y").toString());
113             if ("v".equals(h.get("orientation"))) g.isVerticallyOriented = true;
114
115         } else if (name.equals("svg")) {
116             // FIXME: handle percentages
117             // FIXME: what if these aren't provided?
118             // FIXME (in general)
119             float x = Float.parseFloat(h.get("x").toString());
120             float y = Float.parseFloat(h.get("y").toString());
121             float width = Float.parseFloat(h.get("width").toString());
122             float height = Float.parseFloat(h.get("height").toString());
123             h.put("viewBox", x + ", " + y + ", " + (x + width) + ", " + (y + height));
124             path = "";
125             
126         } else if (name.equals("path")) {
127             path = h.get("d").toString();
128             
129         } else if (name.equals("rect")) {
130             float x = Float.parseFloat(h.get("x").toString());
131             float y = Float.parseFloat(h.get("y").toString());
132             float width = Float.parseFloat(h.get("width").toString());
133             float height = Float.parseFloat(h.get("height").toString());
134             float rx = Float.parseFloat(h.get("rx").toString());
135             float ry = Float.parseFloat(h.get("ry").toString());
136             path =
137                 "M" + (x + rx) + "," + y +
138                 "H" + (x + width - rx) +
139                 "A" + rx + "," + rx + ",0,0,1," + (x + width) + "," + (y + ry) +
140                 "V" + (y + width - ry) +
141                 "A" + rx + "," + rx + ",0,0,1," + (x + width - rx) + "," +
142                 (y + height) +
143                 "H" + (x + rx) +
144                 "A" + rx + "," + rx + ",0,0,1," + x + "," + (y + height - ry) +
145                 "V" + (y + ry) +
146                 "A" + rx + "," + rx + ",0,0,1," + (x + rx) + "," + (y + ry) +
147                 "Z";
148             
149         } else if (name.equals("circle")) {
150             float r = Float.parseFloat(h.get("r").toString());
151             float cx = Float.parseFloat(h.get("cx").toString());
152             float cy = Float.parseFloat(h.get("cy").toString());
153             path = "A " + r + " " + r + " 1 1 " + cx + " " + cy;
154             
155         } else if (name.equals("ellipse")) {
156             float rx = Float.parseFloat(h.get("rx").toString());
157             float ry = Float.parseFloat(h.get("ry").toString());
158             float cx = Float.parseFloat(h.get("cx").toString());
159             float cy = Float.parseFloat(h.get("cy").toString());
160             path = "A " + rx + " " + ry + " 1 1 " + cx + " " + cy;
161             
162         } else if (name.equals("line")) {
163             float x1 = Float.parseFloat(h.get("x1").toString());
164             float y1 = Float.parseFloat(h.get("y1").toString());
165             float x2 = Float.parseFloat(h.get("x2").toString());
166             float y2 = Float.parseFloat(h.get("y2").toString());
167             path = "M " + x1 + " " + y1 + " L " + x2 + " " + y2;
168                 
169         } else if (name.equals("polyline") || name.equals("polygon")) {
170             StringTokenizer st = new StringTokenizer(h.get("points").toString(), ", ", false);
171             String s = "M ";
172             while(st.hasMoreTokens()) s += st.nextToken() + " " + st.nextToken() + " ";
173             path = s + (name.equals("polygon") ? "z" : "");
174
175         } else {
176             Log.info(VectorGraphics.class, "unknown element in VectorGraphics namespace: " + name);
177         }
178         props.put("path", path);
179         t.keys = new String[props.size()];
180         System.arraycopy(props.keys(), 0, t.keys, 0, t.keys.length);
181         t.vals = new String[props.size()];
182         for(int i=0; i<t.keys.length; i++) t.vals[i] = props.get(t.keys[i]);
183         
184
185         // FIXME!!!!
186         if (h.get("viewBox") != null) {
187         StringTokenizer st = new StringTokenizer(h.get("viewBox").toString(), ", ", false);
188         if (t.transform == null) t.transform = "";
189         Point p1, p2;
190         VectorGraphics.RasterPath.fromString(path).getBoundingBox(p1, p2);
191         
192         float minx = st.parseFloat();
193         float miny = st.parseFloat();
194         float width = st.parseFloat();
195         float height = st.parseFloat();
196         t.transform += "translate(" + (-1 * p1.x) + ", " + (-1 * p1.y) + ") " +
197         "scale(" + ((p2.x - p1.x) / width) + ", " + ((p2.y - p1.y) / height) + ") " + 
198         "translate(" + minx + ", " + miny + ") ";
199         
200         // FIXME: preserveAspectRatio
201         }
202
203     }
204     */
205
206     /*
207     public static class Font {
208         Font() { }
209         float horiz_origin_x = 0,  horiz_origin_y = 0, horiz_adv_x = 0;
210         float vert_origin_x = 0, vert_origin_y = 0, vert_adv_y = 0;
211
212         // FIXME: avoid using substring() in here ore creating any objects
213         public void render(String text, DoubleBuffer buf, int x, int y, int fillcolor, int strokecolor, int size) {
214             // FIXME: points, not pixels
215             Affine a = buf.a;
216             float scaleFactor = (float)(1.0/1000.0) * (float)size;
217             for(int pos=0; pos<text.length(); pos++) { 
218                 Glyph g;
219                 for(g = (Glyph)glyphByUnicode.get(text.substring(pos, pos+1));
220                     g != null && !g.unicode.equals(text.substring(pos, pos + g.unicode.length()));
221                     g = g.next);
222                 if (g == null) {
223                     g = (Glyph)glyphByName.get("missing-glyph");
224                 } else {
225                     pos += g.unicode.length() - 1;
226                 }
227                 if (g != null) {
228                     System.out.println("  " + g.unicode);
229                     g.render(buf, x, y, fillcolor, strokecolor, scaleFactor);
230                     x += (int)(g.horiz_adv_x * size / 1000.0);
231                 } else {
232                     x += (int)(horiz_adv_x * size / 1000.0);
233                 }
234             }
235             buf.setTransform(a);
236         }
237
238         / ** all glyphs, keyed by their <tt>name</tt> property * /
239         Hashtable glyphByName = new Hashtable();
240
241         / ** linked list of glyphs, stored by the first character of their <tt>unicode</tt> property * /
242         Hashtable glyphByUnicode = new Hashtable();
243
244         // a Glyph in an VectorGraphics font
245         public static class Glyph {
246
247             // FIXME: lang attribute
248             boolean isVerticallyOriented = false;
249             Template t = null;
250             Box b = null;
251
252             float horiz_adv_x = 0;
253             float vert_origin_x = 0;
254             float vert_origin_y = 0;
255             float vert_adv_y = 0;
256
257             String unicode = null;
258
259             // forms the linked list in glyphByUnicode; glyphs appear in the order specified in the font
260             public Glyph next = null;
261
262             Glyph(String name, String unicode, Template t, VectorGraphics.Font f) {
263                 if (unicode != null)
264                     if (f.glyphByUnicode.get(unicode.substring(0, 1)) == null) {
265                         f.glyphByUnicode.put(unicode.substring(0, 1), this);
266                     } else {
267                         Glyph g;
268                         for(g = (Glyph)f.glyphByUnicode.get(unicode.substring(0, 1)); g.next != null; g = g.next);
269                         g.next = this;
270                     }
271                 if (name != null) f.glyphByUnicode.put(name, this);
272                 this.unicode = unicode;
273                 this.t = t;
274                 horiz_adv_x = f.horiz_adv_x;
275                 vert_origin_x = f.vert_origin_x;
276                 vert_origin_y = f.vert_origin_y;
277                 vert_adv_y = f.vert_adv_y;
278             }
279             public void render(DoubleBuffer buf, int x, int y, int fillcolor, int strokecolor, float scaleFactor) {
280                 // FEATURE: make b double-buffered for increased performance
281                 if (b == null) {
282                     b = new Box(t, new org.ibex.util.Vec(), new org.ibex.util.Vec(), null, 0, 0);
283                     b.put("absolute", Boolean.TRUE);
284                     b.prerender();
285                     t = null;
286                 }
287                 // FIXME
288                 b.put("width", new Integer(1000));
289                 b.put("height", new Integer(1000));
290                 b.fillcolor = fillcolor;
291                 b.strokecolor = strokecolor;
292
293                 // we toss an extra flip on the ctm so that fonts stick "up" instead of down
294                 b.render(0, 0, buf.getWidth(), buf.getHeight(), buf,
295                          Affine.flip(false, true).multiply(Affine.scale(scaleFactor, scaleFactor).multiply(Affine.translate(x, y))).multiply(buf.a));
296             }
297         }
298     }
299     */
300
301 }