fonts now rendered immediately on-demand
[org.ibex.core.git] / src / org / ibex / Box.java
index 90fd499..c2f16dc 100644 (file)
@@ -152,6 +152,8 @@ public final class Box extends JSScope implements Scheduler.Task {
     private short col = 0;
     public LENGTH x = 0;
     public LENGTH y = 0;
+    public LENGTH ax = 0;   // FEATURE: roll these into x/y; requires lots of changes
+    public LENGTH ay = 0;   // FEATURE: roll these into x/y; requires lots of changes; perhaps y()?
     public LENGTH width = 0;
     public LENGTH height = 0;
     private LENGTH contentwidth = 0;      // == max(minwidth, textwidth, sum(child.contentwidth))
@@ -175,9 +177,10 @@ public final class Box extends JSScope implements Scheduler.Task {
         // as external events have occured, check the state of box
         if (texture != null) {
             if (texture.isLoaded) { minwidth = min(texture.width, maxwidth); minheight = min(texture.height, maxheight); }
-            else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res); }
+            else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
+        } else {
+            Log.warn(Box.class, "perform() called with null texture");
         }
-
         MARK_REPACK;
         MARK_REFLOW;
         MARK_RESIZE;
@@ -209,48 +212,86 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     // Reflow ////////////////////////////////////////////////////////////////////////////////////////
 
-    // static stuff so we don't have to keep reallocating
-    private static int[] numRowsInCol = new int[65535];
-    private static LENGTH[] colWidth = new LENGTH[65535];
-    private static LENGTH[] colMaxWidth = new LENGTH[65535];
-    private static LENGTH[] rowHeight = new LENGTH[65535];
-    private static LENGTH[] rowMaxHeight = new LENGTH[65535];
-    static { for(int i=0; i<rowMaxHeight.length; i++) { rowMaxHeight[i] = MAX_LENGTH; colMaxWidth[i] = MAX_LENGTH; } }
-
     Box nextPackedSibling() { Box b = nextSibling(); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); }
     Box firstPackedChild() { Box b = getChild(0); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); }
 
+    private static Box[] frontier = new Box[65535];
+    private static int[] frontier_content = new int[65535];
+
     /** pack the boxes into rows and columns; also computes contentwidth */
     void repack() {
         for(Box child = getChild(0); child != null; child = child.nextSibling()) child.repack();
-
-        //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan 
+        contentwidth = 0;
+        contentheight = 0;
+        if (treeSize() == 0) { constrain(); return; }
+        //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan contentheight/contentwidth contentwidth/contentheight
         if (test(FIXED) == COLS) {
-            short r = 0;
-            for(Box child = firstPackedChild(); child != null; r++) {
-                for(short c=0, numclear=0; child != null && c < cols; c++) {
-                    if (numRowsInCol[c] > r) { numclear = 0; continue; }
-                    if (c != 0 && c + min(cols, child.colspan) - numclear > cols) break;
-                    if (++numclear < min(cols, child.colspan)) continue;
-                    for(int i=c - numclear + 1; i <= c; i++) numRowsInCol[i] += child.rowspan;
-                    child.col = (short)(c - numclear + 1); child.row = r;
-                    rows = (short)max(rows, child.row + child.rowspan);
-                    child = child.nextPackedSibling();
-                    numclear = 0;
+            int childnum = 0;
+            Box lastpacked = null;
+            int maxfront = 0;
+            int maxrow = 0;
+            int rowwidth = 0;
+            for(Box child = getChild(0); child != null; child = child.nextSibling(), childnum++) {
+                if (!(child.test(PACKED) && child.test(VISIBLE))) continue;
+                int col = lastpacked == null ? 0 : (lastpacked.col + lastpacked.colspan);
+                int row = lastpacked == null ? 0 : lastpacked.row;
+                int colspan = min(cols, child.colspan);
+                for(int i=0; i<maxfront; i++) {
+                    if (col + colspan > cols) {
+                        row++;
+                        for(; i>0; i--) row = max(row, frontier[i].row + frontier[i].rowspan);
+                        col = 0;
+                        rowwidth = 0;
+                        continue;
+                    }
+                    Box front = frontier[i];       // FIXME: O(nlgn)
+                    if (front.row + front.rowspan <= row) {
+                        frontier[i] = frontier[maxfront-1];
+                        frontier_content[i] = frontier_content[maxfront-1];
+                        maxfront--;
+                        frontier[maxfront] = null;
+                        frontier_content[maxfront] = 0;
+                        i--;
+                        continue;
+                    }
+                    if ((front.col <= col && front.col + front.colspan > col) ||
+                        (front.col < (col+colspan) && front.col + front.colspan >= (col+colspan))) {
+                        col = front.col + front.colspan;
+                        rowwidth += front.contentwidth; // FIXME: suspect
+                        i = -1;
+                        continue;
+                    }
+                    break;
                 }
+                child.col = (short)col;
+                child.row = (short)row;
+                maxrow = max(maxrow, child.row + child.rowspan);
+                contentwidth = max(contentwidth, rowwidth);
+                rowwidth = 0;
+                lastpacked = child;
+                for(int i=0; i<maxfront; i++) {
+                    frontier_content[maxfront] =
+                        max(frontier_content[maxfront], frontier_content[i] + child.contentheight);
+                    contentheight = 
+                        max(contentheight, frontier_content[i] + child.contentheight);
+                }
+                frontier[maxfront++] = child;
             }
-            for(int i=0; i<cols; i++) numRowsInCol[i] = 0;
+            rows = (short)maxrow;
+            for(int i=0; i<maxfront; i++) frontier[i] = null;
+            for(int i=0; i<maxfront; i++) frontier_content[i] = 0;
         }
         //#end
+        constrain();
+    }
 
-        //#repeat contentwidth/contentheight colWidth/rowHeight colspan/rowspan col/row cols/rows minwidth/minheight \
-        //        textwidth/textheight maxwidth/maxheight
-        contentwidth = 0;
-        for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
-            colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
-        for(int i=0; i<cols; i++) { contentwidth += colWidth[i]; colWidth[i] = 0; }
-        contentwidth = bound(minwidth, max(font == null || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
-        //#end               
+    void constrain() {
+        //#repeat contentwidth/contentheight contentheight/contentwidth minwidth/minheight row/col col/row \
+        //        textwidth/textheight maxwidth/maxheight cols/rows rows/cols colspan/rowspan rowspan/colspan
+        contentwidth = bound(minwidth,
+                             max(contentwidth, font == null || text == null ? 0 : font.textwidth(text)),
+                             maxwidth);
+        //#end
     }
     
     void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
@@ -287,68 +328,180 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
     }
 
-    void resize_children() {
-
-        //#repeat col/row colspan/rowspan contentwidth/contentheight x/y width/height colMaxWidth/rowMaxHeight colWidth/rowHeight \
-        //        HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight colWidth/rowHeight x_slack/y_slack
-        // PHASE 1: compute column min/max sizes
-        int x_slack = width;
-        for(int i=0; i<cols; i++) x_slack -= colWidth[i];
-        for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
-            for(int i=child.col; i < child.col + child.colspan; i++) {
-                x_slack += colWidth[i];
-                colWidth[i] = max(colWidth[i], child.contentwidth / child.colspan);
-                x_slack -= colWidth[i];
-                colMaxWidth[i] = min(colMaxWidth[i], child.test(HSHRINK) ? child.contentwidth : child.maxwidth) / child.colspan;
+    private static float[] coeff = null;
+    private static LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex(100, 100, 300);
+    private static LinearProgramming.Simplex lp = new LinearProgramming.Simplex(100, 100, 300);
+    private static int[] regions = new int[65535];
+    private static int[] regions_v = new int[65535];
+
+    void place_children() {
+        int numkids = 0; for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) numkids++;
+        int numregions = 0, numregions_v = 0;
+        //#repeat col/row colspan/rowspan contentwidth/contentheight width/height HSHRINK/VSHRINK numregions/numregions_v \
+        //        maxwidth/maxheight cols/rows minwidth/minheight lp_h/lp lp_h/lp easy_width/easy_height regions/regions_v
+        if (numkids > 0 && cols > 1) do {
+            // FIXME: numboxes^2, and damn ugly to boot
+            for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) {
+                int target = c.col;
+                for(boolean stop = false;;) {
+                    for(int i=0; i<=numregions; i++) {
+                        if (i == numregions) { regions[numregions++] = target; break; }
+                        if (target == regions[i]) break;
+                        if (target < regions[i]) { int tmp = target; target = regions[i]; regions[i] = tmp; }
+                    }
+                    if (stop) break;
+                    stop = true;
+                    target = min(cols, c.col+c.colspan);
+                }
             }
-        
-        // PHASE 2: hand out slack
-        for(int startslack = 0; x_slack > 0 && cols > 0 && startslack != x_slack;) {
-            int increment = max(1, x_slack / cols);
-            startslack = x_slack;
-            for(short col=0; col < cols; col++) {
-                // FIXME: double check this
-                int diff = min(min(colMaxWidth[col], colWidth[col] + increment) - colWidth[col], x_slack);
-                x_slack -= diff;
-                colWidth[col] += diff;
+            if (regions[numregions-1] == cols) numregions--;
+            else regions[numregions] = cols;
+
+            /* boolean easy_width = contentwidth >= width; */
+            /*
+            for(Box c = firstPackedChild(); easy_width && c != null; c = c.nextPackedSibling()) {
+                if (c.contentwidth == c.maxwidth) continue;
+                if (c.maxwidth == Integer.MAX_VALUE) continue;
+                easy_width = false;
             }
-        }   
-        //#end
+            if (easy_width) for(int i=0; i<cols; i++) {
+                easy_width = false;
+                boolean good = true;
+                for(Box c = firstPackedChild(); good && c != null; c = c.nextPackedSibling())
+                    if (c.col <= i && c.col + c.colspan > i && c.maxwidth < Integer.MAX_VALUE)
+                        good = false;
+                if (good) { easy_width = true; break; }
+            }
+            if (easy_width) break;
+            */
+            int nc = numregions * 2 + numkids + 1;
+            if (coeff == null || nc+1>coeff.length) coeff = new float[nc+1];
+            lp_h.init(nc);
+
+            for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
+            coeff[numregions*2+numkids] = (float)10000.0;               // priority 1: sum of columns no greater than parent
+            for(int i=numregions*2; i<numregions*2+numkids; i++) coeff[i] = (float)100.0;  // priority 2: honor maxwidths
+            for(int i=numregions; i<numregions*2; i++) coeff[i] = (float)(0.1);            // priority 3: equalize columns
+            lp_h.setObjective(coeff, false);
+            
+            for(int i=0; i<numregions; i++) lp_h.set_lowbo(i+1, (float)0.0); // invariant: columns cannot have negative size
+
+            // invariant: columns must be at least as large as parent
+            for(int i=0; i<coeff.length; i++) coeff[i] = (i<numregions) ? (float)(regions[i+1] - regions[i]) : (float)0.0;
+            lp_h.add_constraint(coeff, LinearProgramming.GE, (float)width);
+
+            // priority 1: sum of columns as close to parent's width as possible
+            for(int i=0; i<coeff.length; i++) coeff[i] = (i<numregions) ? (float)(regions[i+1] - regions[i]) : (float)0.0;
+            coeff[numregions*2+numkids] = (float)-1.0;
+            lp_h.add_constraint(coeff, LinearProgramming.EQ, (float)width);
+
+            int childnum = 0;
+            for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling()) {
+
+                // invariant: honor minwidths
+                for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
+                for(int r=0; r<numregions; r++)
+                    if (regions[r] >= child.col && regions[r+1] <= min(child.col+child.colspan,cols))
+                        coeff[r] = (float)(regions[r+1] - regions[r]);
+                lp_h.add_constraint(coeff, LinearProgramming.GE, (float)child.contentwidth);
+
+                // priority 2: honor maxwidths
+                int child_maxwidth = child.test(HSHRINK) ? min(child.maxwidth, child.contentwidth) : child.maxwidth;
+                if (child_maxwidth < Integer.MAX_VALUE) {
+                    for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
+                    for(int r=0; r<numregions; r++)
+                        if (regions[r] >= child.col && regions[r+1] <= min(child.col+child.colspan,cols))
+                            coeff[r] = (float)(regions[r+1] - regions[r]);
+                    coeff[numregions*2+childnum] = (float)-1.0;
+                    lp_h.add_constraint(coeff, LinearProgramming.LE, (float)child_maxwidth);
+                }
 
-        // Phase 3: assign childrens' actual sizes
+                childnum++;
+            }
+
+            // priority 3: equalize columns
+            float avg = ((float)width)/((float)numregions);
+            for(int r=0; r<numregions; r++) {
+                float weight = (float)(regions[r+1] - regions[r]);
+                for(int k=0; k<coeff.length; k++) coeff[k] = (float)(k==r?weight:k==(numregions+r)?-1.0:0.0);
+                lp_h.add_constraint(coeff, LinearProgramming.LE, avg * weight);
+                for(int k=0; k<coeff.length; k++) coeff[k] = (float)(k==r?weight:k==(numregions+r)?1.0:0.0);
+                lp_h.add_constraint(coeff, LinearProgramming.GE, avg * weight);
+            }
+
+            try {
+                int result = lp_h.solve();
+                switch(result) {
+                    case LinearProgramming.UNBOUNDED:
+                        Log.warn(this, "simplex solver claims unboundedness; this should never happen");
+                        break;
+                    case LinearProgramming.INFEASIBLE:
+                        Log.debug(this, "simplex solver claims infeasibility; this should never happen");
+                        break;
+                    case LinearProgramming.MILP_FAIL:
+                        Log.warn(this, "simplex solver claims MILP_FAIL; this should never happen");
+                        break;
+                    case LinearProgramming.RUNNING:
+                        Log.warn(this, "simplex solver still RUNNING; this should never happen");
+                        break;
+                    case LinearProgramming.FAILURE:
+                        Log.warn(this, "simplex solver claims FAILURE; this should never happen");
+                        break;
+                }
+            } catch (Error e) {
+                Log.warn(this, "got an Error in simplex solver; not sure why this happens");
+                return;
+            }
+
+        } while(false);
+        //#end
+        
         for(Box child = getChild(0); child != null; child = child.nextSibling()) {
             if (!child.test(VISIBLE)) continue;
             int child_width, child_height, child_x, child_y;
             if (!child.test(PACKED)) {
-                child_x = child.x;
-                child_y = child.y;
-                child_width = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - child_x);
-                child_height = child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - child_y);
+                child_width = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - Math.abs(child.ax));
+                child_height = child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - Math.abs(child.ay));
                 child_width = max(child.minwidth, child_width);
                 child_height = max(child.minheight, child_height);
+                int gap_x = width - child_width;
+                int gap_y = height - child_height;
+                child_x = child.ax + (child.test(ALIGN_RIGHT) ? gap_x : !child.test(ALIGN_LEFT) ? gap_x / 2 : 0);
+                child_y = child.ay + (child.test(ALIGN_BOTTOM) ? gap_y : !child.test(ALIGN_TOP) ? gap_y / 2 : 0);
             } else {
-                int unbounded;
+                int diff;
                 //#repeat col/row colspan/rowspan contentwidth/contentheight width/height colMaxWidth/rowMaxHeight \
                 //        child_x/child_y x/y HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight x_slack/y_slack \
-                //        colWidth/rowHeight child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP
-                unbounded = 0;
-                for(int i = child.col; i < child.col + child.colspan; i++) unbounded += colWidth[i];
-                child_width = min(unbounded, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);
-                child_x = test(ALIGN_RIGHT) ? x_slack : test(ALIGN_LEFT) ? 0 : x_slack / 2;
-                for(int i=0; i < child.col; i++) child_x += colWidth[i];
-                if (child_width > unbounded) child_x -= (child_width - unbounded) / 2;
+                //        child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP lp_h/lp easy_width/easy_height \
+                //        numregions/numregions_v regions/regions_v
+                child_width = 0;
+                child_x = 0;
+                if (cols == 1) {
+                    child_x = 0;
+                    child_width = width;
+                    /*
+                } else if (easy_width) {
+                    */
+                } else {
+                    for(int r=0; r<numregions; r++) {
+                        if (regions[r] >= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) {
+                            child_width += Math.round(lp_h.solution[lp_h.rows+r+1] * (regions[r+1] - regions[r]));
+                        } else if (regions[r+1] <= child.col) {
+                            child_x += Math.round(lp_h.solution[lp_h.rows+r+1] * (regions[r+1] - regions[r]));
+                        }
+                    }
+                }
+                diff = (child_width - (child.test(HSHRINK) ? child.contentwidth : min(child_width, child.maxwidth)));
+                child_x += (child.test(ALIGN_RIGHT) ? diff : child.test(ALIGN_LEFT) ? 0 : diff / 2);
+                child_width -= diff;
                 //#end
             }
             child.resize(child_x, child_y, child_width, child_height);
         }
 
-        // cleanup
-        for(int i=0; i<cols; i++) { colWidth[i] = 0; colMaxWidth[i] = MAX_LENGTH; }
-        for(int i=0; i<rows; i++) { rowHeight[i] = 0; rowMaxHeight[i] = MAX_LENGTH; }
-
         for(Box child = getChild(0); child != null; child = child.nextSibling())
-            if (test(VISIBLE))
-                child.resize_children();
+            if (child.test(VISIBLE) && child.treeSize() > 0)
+                child.place_children();
     }
 
 
@@ -362,10 +515,9 @@ public final class Box extends JSScope implements Scheduler.Task {
         int globaly = parenty + (parent == null ? 0 : y);
 
         // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
-
         if (test(CLIP)) {
-            cx1 = max(cx1, parent == null ? 0 : globalx);
-            cy1 = max(cy1, parent == null ? 0 : globaly);
+            cx1 = max(cx1, globalx);
+            cy1 = max(cy1, globaly);
             cx2 = min(cx2, globalx + width);
             cy2 = min(cy2, globaly + height);
             if (cx2 <= cx1 || cy2 <= cy1) return;
@@ -379,10 +531,14 @@ public final class Box extends JSScope implements Scheduler.Task {
             for(int x = globalx; x < cx2; x += texture.width)
                 for(int y = globaly; y < cy2; y += texture.height)
                     buf.drawPicture(texture, x, y, cx1, cy1, cx2, cy2);
-
-        if (text != null && !text.equals("") && font != null)
-            if (font.rasterizeGlyphs(text, buf, strokecolor, globalx, globaly, cx1, cy1, cx2, cy2, null) == -1)
-                font.rasterizeGlyphs(text, buf, strokecolor, globalx, globaly, cx1, cy1, cx2, cy2, this);
+        if (text != null && !text.equals("") && font != null) {
+            int gap_x = width - font.textwidth(text);
+            int gap_y = height - font.textheight(text);
+            int text_x = globalx + (test(ALIGN_RIGHT) ? gap_x : !test(ALIGN_LEFT) ? gap_x/2 : 0);
+            int text_y = globaly + (test(ALIGN_BOTTOM) ? gap_y : !test(ALIGN_TOP) ? gap_y/2 : 0);
+            font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2);
+        }
 
         for(Box b = getChild(0); b != null; b = b.nextSibling())
             b.render(globalx, globaly, cx1, cy1, cx2, cy2, buf, null);
@@ -397,13 +553,28 @@ public final class Box extends JSScope implements Scheduler.Task {
     public int localToGlobalY(int y) { return parent == null ? y : parent.globalToLocalY(y + this.y); }
     
     public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        if (nargs != 1 || !"indexof".equals(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
-        Box b = (Box)a0;
-        if (b.parent != this)
-            return (redirect == null || redirect == this) ?
-                N(-1) :
-                redirect.callMethod(method, a0, a1, a2, rest, nargs);
-        return N(b.getIndexInParent());
+        switch (nargs) {
+            case 1: {
+                //#switch(method)
+                case "indexof":
+                    Box b = (Box)a0;
+                    if (b.parent != this)
+                        return (redirect == null || redirect == this) ?
+                            N(-1) :
+                            redirect.callMethod(method, a0, a1, a2, rest, nargs);
+                    return N(b.getIndexInParent());
+
+                case "distanceto":
+                    Box b = (Box)a0;
+                    JS ret = new JS();
+                    ret.put("x", N(b.localToGlobalX(0) - localToGlobalX(0)));
+                    ret.put("y", N(b.localToGlobalY(0) - localToGlobalY(0)));
+                    return ret;
+
+                //#end
+            }
+        }
+        return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
 
     public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
@@ -427,6 +598,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         //#switch(name)
         case "surface": return parent == null ? null : parent.getAndTriggerTraps("surface");
         case "indexof": return METHOD;
+        case "distanceto": return METHOD;
         case "text": return text;
         case "path": throw new JSExn("cannot read from the path property");
         case "fill": return colorToString(fillcolor);
@@ -515,8 +687,8 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "minheight": CHECKSET_INT(minheight); MARK_RESIZE;
                          if (parent == null && getSurface() != null)
                              getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
-        case "colspan": CHECKSET_SHORT(colspan); MARK_REPACK_parent;
-        case "rowspan": CHECKSET_SHORT(rowspan); MARK_REPACK_parent;
+        case "colspan": if (toInt(value) <= 0) return; CHECKSET_SHORT(colspan); MARK_REPACK_parent;
+        case "rowspan": if (toInt(value) <= 0) return; CHECKSET_SHORT(rowspan); MARK_REPACK_parent;
         case "rows": CHECKSET_SHORT(rows); if (rows==0){set(FIXED, COLS);if(cols==0)cols=1;} else set(FIXED, ROWS); MARK_REPACK;
         case "cols": CHECKSET_SHORT(cols); if (cols==0){set(FIXED, ROWS);if(rows==0)rows=1;} else set(FIXED, COLS); MARK_REPACK;
         case "clip": CHECKSET_FLAG(CLIP); if (parent == null) dirty(); else parent.dirty();
@@ -536,15 +708,36 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "Minimized": if (parent == null && getSurface() != null) getSurface().minimized = toBoolean(value);  // FEATURE
         case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = toBoolean(value);  // FEATURE
         case "Close": if (parent == null && getSurface() != null) getSurface().dispose(true);
-        case "redirect": if (redirect == this) redirect = (Box)value; else Log.info(this, "redirect can only be set once");
+        case "redirect":
+            if (value == null) { redirect = null; return; }
+            for(Box cur = (Box)value; cur != null; cur = cur.parent)
+                if (cur == redirect) {
+                    redirect = (Box)value;
+                    return;
+                }
+            JS.error("redirect can only be set to a descendant of its current value");
         case "font":
             if(!(value instanceof Stream)) throw new JSExn("You can only put streams to the font property");
             font = value == null ? null : Font.getFont((Stream)value, font == null ? 10 : font.pointsize);
             MARK_RESIZE;
             dirty();
         case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); MARK_RESIZE; dirty();
-        case "x": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(x); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(x); dirty(); MARK_RESIZE; dirty(); }
-        case "y": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(y); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(y); dirty(); MARK_RESIZE; dirty(); }
+        case "x": if (parent==null && Surface.fromBox(this)!=null) {
+            CHECKSET_INT(x);
+        } else {
+            if (test(PACKED) && parent != null) return;
+            dirty(); CHECKSET_INT(ax);
+            dirty(); MARK_RESIZE;
+            dirty();
+        }
+        case "y": if (parent==null && Surface.fromBox(this)!=null) {
+            CHECKSET_INT(y);
+        } else {
+            if (test(PACKED) && parent != null) return;
+            dirty(); CHECKSET_INT(ay);
+            dirty(); MARK_RESIZE;
+            dirty();
+        }
         case "titlebar":
             if (getSurface() != null && value != null) getSurface().setTitleBarText(JS.toString(value));
             super.put(name,value);
@@ -564,8 +757,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "KeyPressed":    if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "KeyReleased":   if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Move":          if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
-        case "Enter":         if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
-        case "Leave":         if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
 
         case "HScroll":       if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
             parent.putAndTriggerTraps(name, N(((Number)value).floatValue() * ((float)parent.fontSize()) / ((float)fontSize())));
@@ -594,6 +785,8 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "SizeChange":    return;
         case "childadded":    return;
         case "childremoved":  return;
+        case "Enter":         return;
+        case "Leave":         return;
 
         case "thisbox":       if (value == null) removeSelf();
 
@@ -656,6 +849,7 @@ public final class Box extends JSScope implements Scheduler.Task {
             fillcolor = newfillcolor;
         } else if(value instanceof JS) {
             texture = Picture.load((JS)value, this);
+            if (texture != null && texture.isLoaded) perform();
         } else {
             throw new JSExn("fill must be null, a String, or a stream, not a " + value.getClass());
         }