fixed bug 522
[org.ibex.core.git] / src / org / ibex / Box.java
index 2ada11f..bc77085 100644 (file)
@@ -245,7 +245,7 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     void constrain() {
         //#repeat contentwidth/contentheight colspan/rowspan col/row cols/rows minwidth/minheight \
-        //        textwidth/textheight maxwidth/maxheight cols/rows
+        //        textwidth/textheight maxwidth/maxheight cols/rows rowspan/colspan row/col rows/cols
         // FIXME: inefficient
         contentwidth = 0;
         for(int i=0; i<rows; i++) {
@@ -294,24 +294,24 @@ public final class Box extends JSScope implements Scheduler.Task {
     }
 
     private static float[] coeff = null;
-    private static LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex(50, 50, 300);
-    private static LinearProgramming.Simplex lp_v = new LinearProgramming.Simplex(50, 50, 300);
+    private static LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex(100, 100, 300);
+    private static LinearProgramming.Simplex lp_v = new LinearProgramming.Simplex(100, 100, 300);
 
     void place_children() {
         int numkids = 0; for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) numkids++;
-        //#repeat col/row colspan/rowspan contentwidth/contentheight width/height \
+        //#repeat col/row colspan/rowspan contentwidth/contentheight width/height HSHRINK/VSHRINK \
         //        maxwidth/maxheight cols/rows minwidth/minheight lp_h/lp_v lp_h/lp_v
         do {
             int nc = numkids * 2 + cols * 3 + 1 + 2;
             if (coeff == null || nc+1>coeff.length) coeff = new float[nc+1];
-            lp_h.init(nc, nc);
+            lp_h.init(nc);
 
             // objective function
             for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
-            coeff[cols*2+numkids] = (float)-100000.0;                             // priority 1: sum of columns equals parent
-            for(int i=cols*2; i<cols*2+numkids; i++) coeff[i] = (float)-1000.0;   // priority 2: honor maxwidths
-            for(int i=cols; i<cols*2; i++) coeff[i] = (float)(-1.0);              // priority 3: equalize columns
-            lp_h.setObjective(coeff, true);
+            coeff[cols*2+numkids] = (float)10000.0;                              // priority 1: sum of columns equals parent
+            for(int i=cols*2; i<cols*2+numkids; i++) coeff[i] = (float)100.0;    // priority 2: honor maxwidths
+            for(int i=cols; i<cols*2; i++) coeff[i] = (float)(1.0);              // priority 3: equalize columns
+            lp_h.setObjective(coeff, false);
 
             // priority 1: sum of columns at least as big as parent
             for(int i=0; i<coeff.length; i++) coeff[i] = (i<cols) ? (float)1.0 : (float)0.0;
@@ -326,23 +326,28 @@ public final class Box extends JSScope implements Scheduler.Task {
                 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(coeff, LinearProgramming.GE, (float)child.contentwidth);
-                if (child.maxwidth < Integer.MAX_VALUE) {
+                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] = (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(coeff, LinearProgramming.EQ, (float)child.maxwidth);
+                    lp_h.add_constraint(coeff, LinearProgramming.EQ, (float)child_maxwidth);
                 }
                 for(int j=0; j<coeff.length; j++) coeff[j] = (float)0.0;
                 childnum++;
             }
 
             // priority 3: equalize columns
+            for(int i=0 ; i<cols; i++) lp_h.set_lowbo(i+1, (float)0.0);
             for(int i=0 ; i<cols; i++) {
-                lp_h.set_lowbo(i+1, (float)0.0);
-                lp_h.bound_difference(i, cols+i, ((float)width)/((float)cols), LinearProgramming.LE, coeff);
-                lp_h.bound_sum(       i, cols+i, ((float)width)/((float)cols), LinearProgramming.GE, coeff);
+                for(int j=0 ; j<i; j++) {
+                    for(int k=0; k<coeff.length; k++) coeff[k] = (float)(k==i?1.0:k==j?-1.0:k==(cols+1)?-1.0:0.0);
+                    lp_h.add_constraint(coeff, LinearProgramming.LE, 0);
+                    for(int k=0; k<coeff.length; k++) coeff[k] = (float)(k==i?1.0:k==j?-1.0:k==(cols+1)?1.0:0.0);
+                    lp_h.add_constraint(coeff, LinearProgramming.GE, 0);
+                }
             }
-
+            for(int i=0; i<coeff.length; i++) coeff[i] = (float)0.0;
             lp_h.solve();
         } while(false);
         //#end
@@ -727,6 +732,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());
         }