+ }
+ if (regions[numregions-1] == cols) numregions--;
+ else regions[numregions] = cols;
+ }
+ //#end
+
+ void solve(boolean findMinimum) {
+ int numkids = 0; for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) numkids++;
+ //#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 \
+ // computeRegions/computeRegions_v
+ if (numkids > 0 && cols > 1) do {
+ computeRegions();
+ int nc = numregions * 3 + numkids * 2 + 3;
+ 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;
+ if (!findMinimum) {
+ 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
+ } else {
+ coeff[numregions*2+numkids] = (float)1.0;
+ }
+ lp_h.setObjective(coeff, false);
+
+ for(int i=0; i<numregions; i++) {
+ for(int j=0; j<coeff.length; j++) coeff[j] = j==i ? (float)1.0 : (float)0.0;
+ lp_h.add_constraint(coeff, Simplex.GE, (float)0.0);
+ }
+
+ // 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;
+ if (!findMinimum) lp_h.add_constraint(coeff, Simplex.LE, (float)width);
+ else lp_h.add_constraint(coeff, Simplex.LE, (float)0);
+
+ 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, Simplex.GE, (float)child.contentwidth);
+ if (!findMinimum) {
+ // 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, Simplex.LE, (float)child_maxwidth);
+ }
+ }
+ childnum++;
+ }
+
+ if (!findMinimum) {
+ // 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, Simplex.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, Simplex.GE, avg * weight);
+ }