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.
5 package org.ibex.graphics;
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?
18 - filters (filtering of a group must be performed AFTER the group is assembled; sep. canvas)
21 - bump caps [requires Paint that can fill circles...] [remember to distinguish between closed/unclosed]
25 - bump (easy, but requires 'round' Paint)
26 - subtree sharing? otherwise the memory consumption might be outrageous... clone="" attribute?
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
34 // FIXME: need to support style sheets and the 'style=' attribute
35 // FIXME: need to convert markers into subboxes
39 public static void parseNode(String name, String[] keys, Object[] vals, Template t) {
41 for(int i=0; i<keys.length; i++) if (vals[i] != null) h.put(keys[i], vals[i]);
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"))));
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?
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
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);
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
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() + "%) ";
87 String path = (String)h.get("d");
88 if (name.equals("g")) {
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());
100 } else if (name.equals("hkern")) {
103 } else if (name.equals("vkern")) {
106 } else if (name.equals("font-face")) {
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;
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));
129 } else if (name.equals("path")) {
130 path = h.get("d").toString();
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());
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) + "," +
147 "A" + rx + "," + rx + ",0,0,1," + x + "," + (y + height - ry) +
149 "A" + rx + "," + rx + ",0,0,1," + (x + rx) + "," + (y + ry) +
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;
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;
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;
172 } else if (name.equals("polyline") || name.equals("polygon")) {
173 StringTokenizer st = new StringTokenizer(h.get("points").toString(), ", ", false);
175 while(st.hasMoreTokens()) s += st.nextToken() + " " + st.nextToken() + " ";
176 path = s + (name.equals("polygon") ? "z" : "");
179 Log.info(VectorGraphics.class, "unknown element in VectorGraphics namespace: " + name);
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]);
189 if (h.get("viewBox") != null) {
190 StringTokenizer st = new StringTokenizer(h.get("viewBox").toString(), ", ", false);
191 if (t.transform == null) t.transform = "";
193 VectorGraphics.RasterPath.fromString(path).getBoundingBox(p1, p2);
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 + ") ";
203 // FIXME: preserveAspectRatio
210 public static class 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;
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
219 float scaleFactor = (float)(1.0/1000.0) * (float)size;
220 for(int pos=0; pos<text.length(); pos++) {
222 for(g = (Glyph)glyphByUnicode.get(text.substring(pos, pos+1));
223 g != null && !g.unicode.equals(text.substring(pos, pos + g.unicode.length()));
226 g = (Glyph)glyphByName.get("missing-glyph");
228 pos += g.unicode.length() - 1;
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);
235 x += (int)(horiz_adv_x * size / 1000.0);
241 / ** all glyphs, keyed by their <tt>name</tt> property * /
242 Hashtable glyphByName = new Hashtable();
244 / ** linked list of glyphs, stored by the first character of their <tt>unicode</tt> property * /
245 Hashtable glyphByUnicode = new Hashtable();
247 // a Glyph in an VectorGraphics font
248 public static class Glyph {
250 // FIXME: lang attribute
251 boolean isVerticallyOriented = false;
255 float horiz_adv_x = 0;
256 float vert_origin_x = 0;
257 float vert_origin_y = 0;
258 float vert_adv_y = 0;
260 String unicode = null;
262 // forms the linked list in glyphByUnicode; glyphs appear in the order specified in the font
263 public Glyph next = null;
265 Glyph(String name, String unicode, Template t, VectorGraphics.Font f) {
267 if (f.glyphByUnicode.get(unicode.substring(0, 1)) == null) {
268 f.glyphByUnicode.put(unicode.substring(0, 1), this);
271 for(g = (Glyph)f.glyphByUnicode.get(unicode.substring(0, 1)); g.next != null; g = g.next);
274 if (name != null) f.glyphByUnicode.put(name, this);
275 this.unicode = unicode;
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;
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
285 b = new Box(t, new org.ibex.util.Vec(), new org.ibex.util.Vec(), null, 0, 0);
286 b.put("absolute", Boolean.TRUE);
291 b.put("width", new Integer(1000));
292 b.put("height", new Integer(1000));
293 b.fillcolor = fillcolor;
294 b.strokecolor = strokecolor;
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));