X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2FBox.java;h=c2f16dc9276301df906375224a40bfa955d9db37;hp=e77935ddcafddc91824da9bb40aa6d41dce449b7;hb=580e7997d6f1e66c51114a050ff695bb15ed4e19;hpb=950336bc09f4d2a4057118d5e16db7bf0e7f2211 diff --git a/src/org/ibex/Box.java b/src/org/ibex/Box.java index e77935d..c2f16dc 100644 --- a/src/org/ibex/Box.java +++ b/src/org/ibex/Box.java @@ -212,51 +212,86 @@ public final class Box extends JSScope implements Scheduler.Task { // Reflow //////////////////////////////////////////////////////////////////////////////////////// - // static stuff so we don't have to keep reallocating - private static int[] numRowsInCol = new int[65535]; - 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(); } + private static Box[] frontier = new Box[65535]; + private static int[] frontier_content = new int[65535]; + /** pack the boxes into rows and columns; also computes contentwidth */ void repack() { for(Box child = getChild(0); child != null; child = child.nextSibling()) child.repack(); - - //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan + contentwidth = 0; + contentheight = 0; + if (treeSize() == 0) { constrain(); return; } + //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan contentheight/contentwidth contentwidth/contentheight if (test(FIXED) == COLS) { - short r = 0; - for(Box child = firstPackedChild(); child != null; r++) { - for(short c=0, numclear=0; child != null && c < cols; c++) { - if (numRowsInCol[c] > r) { numclear = 0; continue; } - if (c != 0 && c + min(cols, child.colspan) - numclear > cols) break; - if (++numclear < min(cols, child.colspan)) continue; - for(int i=c - numclear + 1; i <= c; i++) numRowsInCol[i] += child.rowspan; - child.col = (short)(c - numclear + 1); child.row = r; - rows = (short)max(rows, child.row + child.rowspan); - child = child.nextPackedSibling(); - numclear = 0; + int childnum = 0; + Box lastpacked = null; + int maxfront = 0; + int maxrow = 0; + int rowwidth = 0; + for(Box child = getChild(0); child != null; child = child.nextSibling(), childnum++) { + if (!(child.test(PACKED) && child.test(VISIBLE))) continue; + int col = lastpacked == null ? 0 : (lastpacked.col + lastpacked.colspan); + int row = lastpacked == null ? 0 : lastpacked.row; + int colspan = min(cols, child.colspan); + for(int i=0; i cols) { + row++; + for(; i>0; i--) row = max(row, frontier[i].row + frontier[i].rowspan); + col = 0; + rowwidth = 0; + continue; + } + Box front = frontier[i]; // FIXME: O(nlgn) + if (front.row + front.rowspan <= row) { + frontier[i] = frontier[maxfront-1]; + frontier_content[i] = frontier_content[maxfront-1]; + maxfront--; + frontier[maxfront] = null; + frontier_content[maxfront] = 0; + i--; + continue; + } + if ((front.col <= col && front.col + front.colspan > col) || + (front.col < (col+colspan) && front.col + front.colspan >= (col+colspan))) { + col = front.col + front.colspan; + rowwidth += front.contentwidth; // FIXME: suspect + i = -1; + continue; + } + break; + } + child.col = (short)col; + child.row = (short)row; + maxrow = max(maxrow, child.row + child.rowspan); + contentwidth = max(contentwidth, rowwidth); + rowwidth = 0; + lastpacked = child; + for(int i=0; i i)) - rowcontentwidth += child.contentwidth; - contentwidth = max(contentwidth, rowcontentwidth); - } - contentwidth = bound(minwidth, max(font == null || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth); - //#end + //#repeat contentwidth/contentheight contentheight/contentwidth minwidth/minheight row/col col/row \ + // textwidth/textheight maxwidth/maxheight cols/rows rows/cols colspan/rowspan rowspan/colspan + contentwidth = bound(minwidth, + max(contentwidth, font == null || text == null ? 0 : font.textwidth(text)), + maxwidth); + //#end } void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) { @@ -295,60 +330,129 @@ public final class Box extends JSScope implements Scheduler.Task { private static float[] coeff = null; private static LinearProgramming.Simplex lp_h = new LinearProgramming.Simplex(100, 100, 300); - private static LinearProgramming.Simplex lp_v = new LinearProgramming.Simplex(100, 100, 300); + private static LinearProgramming.Simplex lp = new LinearProgramming.Simplex(100, 100, 300); + private static int[] regions = new int[65535]; + private static int[] regions_v = new int[65535]; 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 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; + 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 (numkids > 0 && cols > 1) do { + // FIXME: numboxes^2, and damn ugly to boot + for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) { + int target = c.col; + for(boolean stop = false;;) { + for(int i=0; i<=numregions; i++) { + if (i == numregions) { regions[numregions++] = target; break; } + if (target == regions[i]) break; + if (target < regions[i]) { int tmp = target; target = regions[i]; regions[i] = tmp; } + } + if (stop) break; + stop = true; + target = min(cols, c.col+c.colspan); + } + } + if (regions[numregions-1] == cols) numregions--; + else regions[numregions] = cols; + + /* boolean easy_width = contentwidth >= width; */ + /* + 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 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); - // objective function for(int i=0; i=child.col && i= 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=child.col && i= 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); } - for(int j=0; j= 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 - (child.test(HSHRINK) ? child.contentwidth : min(child_width, 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); + child_width -= diff; //#end } child.resize(child_x, child_y, child_width, child_height); @@ -419,8 +537,7 @@ public final class Box extends JSScope implements Scheduler.Task { int gap_y = height - font.textheight(text); int text_x = globalx + (test(ALIGN_RIGHT) ? gap_x : !test(ALIGN_LEFT) ? gap_x/2 : 0); int text_y = globaly + (test(ALIGN_BOTTOM) ? gap_y : !test(ALIGN_TOP) ? gap_y/2 : 0); - if (font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, null) == -1) - font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, this); + font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2); } for(Box b = getChild(0); b != null; b = b.nextSibling()) @@ -570,8 +687,8 @@ public final class Box extends JSScope implements Scheduler.Task { case "minheight": CHECKSET_INT(minheight); MARK_RESIZE; if (parent == null && getSurface() != null) getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight); - case "colspan": CHECKSET_SHORT(colspan); MARK_REPACK_parent; - case "rowspan": CHECKSET_SHORT(rowspan); MARK_REPACK_parent; + case "colspan": if (toInt(value) <= 0) return; CHECKSET_SHORT(colspan); MARK_REPACK_parent; + case "rowspan": if (toInt(value) <= 0) return; CHECKSET_SHORT(rowspan); MARK_REPACK_parent; case "rows": CHECKSET_SHORT(rows); if (rows==0){set(FIXED, COLS);if(cols==0)cols=1;} else set(FIXED, ROWS); MARK_REPACK; case "cols": CHECKSET_SHORT(cols); if (cols==0){set(FIXED, ROWS);if(rows==0)rows=1;} else set(FIXED, COLS); MARK_REPACK; case "clip": CHECKSET_FLAG(CLIP); if (parent == null) dirty(); else parent.dirty();