-
- // PHASE 2: hand out slack
- for(int startslack = 0; x_slack > 0 && cols > 0 && startslack != x_slack;) {
- if (eligible == 0) break;
- int increment = x_slack / eligible;
- eligible = 0;
- 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);
- if (colWidth[col] + diff < colMinWidth[col]) diff = colMinWidth[col] - colWidth[col];
- if (diff == 0) continue;
- eligible++;
- x_slack -= diff;
- colWidth[col] += diff;
+ lp_h.setObjective(coeff, false);
+
+ // 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.EQ, (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);
+ }
+ }
+
+ try { switch(lp_h.solve()) {
+ case Simplex.UNBOUNDED: Log.warn(this, "simplex claims unboundedness; this should never happen"); break;
+ case Simplex.INFEASIBLE: Log.debug(this, "simplex claims infeasibility; this should never happen"); break;
+ case Simplex.MILP_FAIL: Log.warn(this, "simplex claims MILP_FAIL; this should never happen"); break;
+ case Simplex.RUNNING: Log.warn(this, "simplex still RUNNING; this should never happen"); break;
+ case Simplex.FAILURE: Log.warn(this, "simplex 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;