new linear constraint solver for layout
[org.ibex.core.git] / src / org / ibex / Box.java
index 524cb7c..3892ce9 100644 (file)
@@ -214,11 +214,6 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     // 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(); }
@@ -247,11 +242,12 @@ public final class Box extends JSScope implements Scheduler.Task {
         //#end
 
         //#repeat contentwidth/contentheight colWidth/rowHeight colspan/rowspan col/row cols/rows minwidth/minheight \
-        //        textwidth/textheight maxwidth/maxheight
+        //        textwidth/textheight maxwidth/maxheight cols/rows
         contentwidth = 0;
+        int[] colWidth = new int[cols];
         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; }
+        for(int i=0; i<cols; i++) contentwidth += colWidth[i];
         contentwidth = bound(minwidth, max(font == null || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
         //#end               
     }
@@ -290,35 +286,61 @@ 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;
+    void place_children() {
+        int numkids = 0; for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) numkids++;
+        int nc = numkids * 2 + cols * 3 + 1 + 2;
+        if (coeff == null || nc+1>coeff.length) coeff = new float[nc+1];
+        LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex();
+        LinearProgramming.Problem lpr_h = new LinearProgramming.Problem(nc, nc);
+        LinearProgramming.Simplex lp_v = new LinearProgramming.Simplex();
+        LinearProgramming.Problem lpr_v = new LinearProgramming.Problem(nc, nc);
+
+        //#repeat col/row colspan/rowspan contentwidth/contentheight width/height colMaxWidth/rowMaxHeight colWidth/rowHeight \
+        //        HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight colWidth/rowHeight x_slack/y_slack lp_h/lp_v lpr_h/lpr_v
+        do {
+            // objective function
+            coeff[cols*2+numkids] = coeff[cols*2+numkids+1] = (float)-10000.0;    // attempt to make sum of columns equal to parent width
+            for(int i=cols*2; i<cols*2+numkids; i++) coeff[i] = (float)100.0;     // second priority: try to honor maxwidths
+            for(int i=cols; i<cols*2; i++) coeff[i] = (float)(-1.0);              // third priority: try to make all columns similar size
+            lp_h.set_obj_fn(lpr_h, coeff);
+            lp_h.set_maxim(lpr_h);
+
+            // top priority: try to match the parent's width
+            for(int i=0; i<coeff.length; i++) coeff[i] = (i<cols) ? (float)1.0 : (float)0.0;
+            coeff[cols*2+numkids] = (float)-1.0;
+            lp_h.add_constraint(lpr_h, coeff, LinearProgramming.LE, (float)width);
+
+            for(int i=0; i<coeff.length; i++) coeff[i] = (i<cols) ? (float)1.0 : (float)0.0;
+            coeff[cols*2+numkids+1] = (float)1.0;
+            lp_h.add_constraint(lpr_h, coeff, LinearProgramming.GE, (float)width);
+
+            // obey minwidth, second priority: try to obey maxwidth (if relevant)
+            int childnum = 0;
+            for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling()) {
+                for(int i=0; i<coeff.length; i++) coeff[i] = (i>=child.col && i<min(child.colspan+child.col, cols)) ? (float)1.0 : (float)0.0;
+                lp_h.add_constraint(lpr_h, coeff, LinearProgramming.GE, (float)child.minwidth);
+                if (child.maxwidth < Integer.MAX_VALUE) {
+                    for(int i=0; i<coeff.length; i++)
+                        coeff[i] = (i>=child.col && i<min(child.colspan+child.col, cols)) ? (float)1.0 : (float)0.0;
+                    coeff[cols*2+childnum] = (float)1.0;
+                    lp_h.add_constraint(lpr_h, coeff, LinearProgramming.EQ, (float)child.maxwidth);
+                }
+                for(int j=0; j<coeff.length; j++) coeff[j] = (float)0.0;
+                childnum++;
             }
-        
-        // 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;
+
+            // third priority: try to make columns similar size
+            for(int i=0 ; i<cols; i++) {
+                lp_h.set_lowbo(lpr_h, i+1, (float)0.0);
+                lp_h.bound_difference(lpr_h, i, cols+i, ((float)width)/((float)cols), LinearProgramming.LE, coeff);
+                lp_h.bound_sum(       lpr_h, i, cols+i, ((float)width)/((float)cols), LinearProgramming.GE, coeff);
             }
-        }   
-        //#end
 
-        // Phase 3: assign childrens' actual sizes
+            lp_h.solve(lpr_h);
+        } 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;
@@ -332,28 +354,23 @@ public final class Box extends JSScope implements Scheduler.Task {
                 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;
                 //#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;
+                //        colWidth/rowHeight child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP lpr_h/lpr_v
+                child_width = 0;
+                child_x = 0;
+                for(int i=0; i < child.col; i++) child_x += Math.round(lpr_h.solution[lpr_h.rows+i+1]);
+                for(int i = child.col; i < child.col + child.colspan; i++) child_width += Math.round(lpr_h.solution[lpr_h.rows+i+1]);
+                child_x += (child_width - min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth)) / 2;
+                child_width = min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);
                 //#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();
     }
 
 
@@ -685,7 +702,6 @@ 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) perform();
         } else {
             throw new JSExn("fill must be null, a String, or a stream, not a " + value.getClass());
         }