+ Vec.sortInts(regions, 0, numregions);
+ int j = 0;
+ int newnumregions = numregions;
+ for(int i=1; i<numregions; i++) {
+ if (regions[j] != regions[i]) j++;
+ else newnumregions--;
+ regions[j] = regions[i];
+ }
+ numregions = newnumregions;
+ if (regions[numregions-1] == cols) numregions--;
+ else regions[numregions] = cols;
+
+ int target = findMinimum ? 0 : Math.max(width, contentwidth);
+ // priority 0: (inviolable) honor minwidths
+ // priority 1: sum of columns no greater than parent
+ // priority 2: honor maxwidths
+ // priority 3: equalize columns
+ float targetColumnSize = target == 0 ? 0 : this.targetColumnSize;
+ float last_columnsize = 0;
+ float last_total = 0;
+ float total;
+ boolean first = true;
+ while(true) {
+ total = (float)0.0;
+ for(int r=0; r<numregions; r++) total += (sizes[r] = (float)(targetColumnSize * (regions[r+1]-regions[r])));
+ int minregion = 0;
+ for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
+ for(int r=(child.col==0?0:minregion); r<numregions; r++) {
+ if (regions[r+1] < child.col) continue;
+ if (regions[r] >= min(child.col+child.colspan,cols)) { minregion = r; break; }
+ total -= sizes[r];
+ int child_maxwidth = child.test(HSHRINK)?child.contentwidth:child.maxwidth;
+ if (sizes[r] <= (float)(targetColumnSize*(regions[r+1]-regions[r])))
+ if ((child.colspan * targetColumnSize) > (child_maxwidth + (float)0.5))
+ sizes[r] = (float)Math.min(sizes[r], (regions[r+1]-regions[r])*(child_maxwidth/child.colspan));
+ if ((child.colspan * targetColumnSize) < (child.contentwidth - (float)0.5))
+ sizes[r] = (float)Math.max(sizes[r], (regions[r+1]-regions[r])*(child.contentwidth/child.colspan));
+ total += sizes[r];
+ }
+ float save = targetColumnSize;
+ if (Math.abs(total - target) <= (float)1.0) break;
+ if (!first) {
+ if (Math.abs(total - last_total) <= (float)1.0) break;
+ } else {
+ last_columnsize = ((total - target) / (float)cols) + targetColumnSize;
+ }
+ if (total < target) targetColumnSize += Math.abs((last_columnsize - targetColumnSize) / (float)1.1);
+ else if (total > target) targetColumnSize -= Math.abs((last_columnsize - targetColumnSize) / (float)1.1);
+ last_columnsize = save;
+ last_total = total;
+ first = false;
+ }
+ if (findMinimum) contentwidth = Math.round(total);
+ else this.targetColumnSize = targetColumnSize;
+ } while(false);