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