// 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(); }
for(int i=0; i<cols; i++) numRowsInCol[i] = 0;
}
//#end
+ constrain();
+ }
- //#repeat contentwidth/contentheight colWidth/rowHeight colspan/rowspan col/row cols/rows minwidth/minheight \
- // textwidth/textheight maxwidth/maxheight
+ void constrain() {
+ //#repeat contentwidth/contentheight colspan/rowspan col/row cols/rows minwidth/minheight \
+ // textwidth/textheight maxwidth/maxheight cols/rows rowspan/colspan row/col rows/cols
+ // FIXME: inefficient
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; }
+ for(int i=0; i<rows; i++) {
+ int rowcontentwidth = 0;
+ for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
+ if (child.row <= i && (child.row + child.rowspan > i))
+ rowcontentwidth += child.contentwidth;
+ contentwidth = max(contentwidth, rowcontentwidth);
+ }
contentwidth = bound(minwidth, max(font == null || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
//#end
}
}
}
- 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);
+ boolean[] breakpoints = new boolean[65535];
+ int[] regions = new int[65535];
+ 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 (cols > 1) do {
+ /* boolean easy_width = contentwidth >= width; */
+ for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) {
+ breakpoints[c.col] = true;
+ breakpoints[min(cols, c.col+c.colspan)] = true;
}
-
- // 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;
+ numregions = 0;
+ // FIXME: depends on cols
+ for(int i=0; i<cols; i++) if (breakpoints[i]) regions[numregions++] = i;
+ regions[numregions] = cols;
+ for(int i=0; i<numkids; i++) breakpoints[i] = false;
+ /*
+ 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;
+ }
+ 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);
+ }
+
+ 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);
}
- }
- //#end
- // Phase 3: assign childrens' actual sizes
+ int result = lp_h.solve();
+ if (result == LinearProgramming.UNBOUNDED)
+ Log.warn(this, "simplex solver claims unboundedness; this should never happen");
+ if (result != LinearProgramming.OPTIMAL)
+ Log.warn(this, "simplex solver claims infeasibility; this should never happen");
+ } 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;
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 - min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth));
+ child_x += (child.test(ALIGN_RIGHT) ? diff : child.test(ALIGN_LEFT) ? 0 : diff / 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();
}
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()); }
//#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);
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 (value == null) { redirect = null; return; }
for(Box cur = (Box)value; cur != null; cur = cur.parent)
if (cur == redirect) {
redirect = (Box)value;
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());
}