2003/09/19 08:33:46
[org.ibex.core.git] / src / org / xwt / Box.java.pp
index 4da565a..245a2c1 100644 (file)
@@ -62,7 +62,7 @@ import org.xwt.translators.*;
  *  position.
  *
  *  A note on coordinates: the Box class represents regions
- *  internally as x,y,w,h tuples, even though the DoubleBuffer class
+ *  internally as x,y,w,h tuples, even though the PixelBuffer class
  *  uses x1,y1,x2,y2 tuples.
  */
 public final class Box extends JS.Scope {
@@ -126,7 +126,8 @@ public final class Box extends JS.Scope {
     private LENGTH hpad = 0;
     private LENGTH vpad = 0;
     private String text = null;
-    private String font = null;
+    private Res font = null;
+    private int fontsize = 10;
     private LENGTH textwidth = 0;
     private LENGTH textheight = 0;
 
@@ -405,7 +406,7 @@ public final class Box extends JS.Scope {
     // Rendering Pipeline /////////////////////////////////////////////////////////////////////
 
     /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
-    void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
+    void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
         if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
         int globalx = parentx + (parent == null ? 0 : x);
         int globaly = parenty + (parent == null ? 0 : y);
@@ -428,7 +429,7 @@ public final class Box extends JS.Scope {
             else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
 
        if (text != null && !text.equals(""))
-            renderText(x, y, clipx, clipy, clipw, cliph, buf);
+            renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
 
         // now subtract the pad region from the clip region before proceeding
         clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
@@ -440,7 +441,7 @@ public final class Box extends JS.Scope {
             b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf);   
     }
 
-    void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
+    void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
         //buf.setClip(x, y, w + x, h + y);
         System.out.println("draw " + clipx + " " + clipy + " " + (clipx + clipw) + " " + (clipy + cliph));
         buf.drawPicture(image,
@@ -449,7 +450,7 @@ public final class Box extends JS.Scope {
         //buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
     }
 
-    void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, DoubleBuffer buf) {
+    void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
         int iw = image.getWidth();
         int ih = image.getHeight();
         for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
@@ -471,31 +472,21 @@ public final class Box extends JS.Scope {
         }
     }
 
-    void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
-         /*
-       // hack because (believe it or not) libgcj doesn't support UTF16.
-       byte[] b = new byte[text.length() * 2 + 2];
-       for(int i=0; i<text.length(); i++) {
-           b[i * 2] = (byte)((((short)text.charAt(i)) & 0xff00) >> 8);
-           b[i * 2 + 1] = (byte)(((short)text.charAt(i)) & 0xff);
-       }
-       b[text.length()] = 0;
-       b[text.length() + 1] = 0;
-         */
-            /*
-       try {
-           ImageDecoder id = org.xwt.translators.Font.render(new FileInputStream("COMIC.TTF"), 24, text, false);
-           Picture p = Platform.createPicture(id);
-            // FIXME: clipping (don't use setClip)
-           buf.drawPicture(p,
-                            x + hpad, y + vpad,
-                            x + hpad + p.getWidth(), y + vpad + p.getHeight(),
+    void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
+        for(int i=0; i<text.length(); i++) {
+            // FIXME: clipping
+            char c = text.charAt(i);
+            Glyph g = Glyph.getGlyph(font, fontsize, c);
+            System.out.println("rendering glyph for " + c + " as " + g + " @ " + (x+hpad) + ", " + (y+vpad));
+           buf.drawPicture(g.p,
+                            x + hpad,
+                            y + vpad + g.max_ascent - g.baseline,
+                            x + hpad + g.p.getWidth(),
+                            y + vpad + g.max_ascent - g.baseline + g.p.getHeight(),
                             0, 0,
-                            p.getWidth(), p.getHeight());
-       } catch (Exception e) {
-           Log.log(this, e);
-       }
-            */
+                            g.p.getWidth(), g.p.getHeight());
+            x += g.advance;
+        }
     }
 
 
@@ -515,15 +506,19 @@ public final class Box extends JS.Scope {
         } else if ("apply".equals(method)) {
             if (checkOnly) return Boolean.TRUE;
             if (args.elementAt(0) instanceof Res) {
+            try {
                 Res res = (Res)args.elementAt(0);
-                res = res.addExtension(".xwt");
-                Template t = Template.buildTemplate(res, "fromResource");
+                //                res = res.addExtension(".xwt");
+                Template t = Template.buildTemplate(res.getInputStream(), "fromResource");
                 if (ThreadMessage.suspendThread()) try {
                     JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
                     t.apply(this, null, null, callback, 0, t.numUnits());
                 } finally {
                     ThreadMessage.resumeThread();
                 }
+            } catch (IOException e) {
+                Log.log(this, e);
+            }
             } else if (args.elementAt(0) instanceof String) {
                 String templatename = (String)args.elementAt(0);
                 Template t = Template.getTemplate(templatename, null);
@@ -986,8 +981,8 @@ public final class Box extends JS.Scope {
                                 Log.log(this, "invalid color " + s);
                                 return;
                             }
-                        else if (SVG.colors.get(s) != null)
-                            newcolor = 0xFF000000 | ((Integer)SVG.colors.get(s)).intValue();
+                        else if (org.xwt.translators.SVG.colors.get(s) != null)
+                            newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
                         if (newcolor == b.fillcolor) return;
                         b.fillcolor = newcolor;
                         b.dirty();
@@ -1016,10 +1011,13 @@ public final class Box extends JS.Scope {
                            b.textwidth = b.textheight = 0;
                        } else {
                            try {
-                               ImageDecoder id = org.xwt.translators.Font.render(new FileInputStream("COMIC.TTF"), 24, b.text, true);
-                                if (id.getWidth() != b.textwidth || id.getHeight() != b.textheight) MARK_FOR_REFLOW_b;
-                               b.textwidth = id.getWidth();
-                               b.textheight = id.getHeight();
+                                MARK_FOR_REFLOW_b;
+                                b.textwidth = 0;
+                                for(int i=0; i<b.text.length(); i++) {
+                                    Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
+                                    b.textwidth += g.advance;
+                                    b.textheight = g.max_ascent + g.max_descent;
+                                }
                            } catch (Exception e) {
                                Log.log(this, e);
                            }
@@ -1030,8 +1028,18 @@ public final class Box extends JS.Scope {
             specialBoxProperties.put("font", new SpecialBoxProperty() {
                     public Object get(Box b) { return b.font; }
                     public void put(Box b, Object value) {
-                        b.font = value == null ? null : value.toString();
-                        // FIXME: translate value into a resource
+                        // FIXME: translate value into a resource if it is a string
+                        b.font = value == null ? null : (Res)value;
+                        MARK_FOR_REFLOW_b;
+                        b.flags |= FONT_CHANGED_FLAG;
+                        b.dirty();
+                    } });
+        
+            specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
+                    public Object get(Box b) { return b.font; }
+                    public void put(Box b, Object value) {
+                        if (b.fontsize == stoi(value)) return;
+                        b.fontsize = stoi(value);
                         MARK_FOR_REFLOW_b;
                         b.flags |= FONT_CHANGED_FLAG;
                         b.dirty();