Skip to content

Commit 03c809e

Browse files
committed
Improve table auto layout
DEVSIX-1252
1 parent 8b87371 commit 03c809e

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/TableWidths.java

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
124124
// set percent only to cells without one
125125
for (int i = cell.getCol(); i < cell.getCol() + cell.getColspan(); i++) {
126126
if (!widths[i].isPercent) {
127-
widths[i].setPercents(percentAddition / pointColumns).setFixed(true);
127+
widths[i].setPercents(percentAddition / pointColumns);
128128
}
129129
}
130130
}
@@ -145,7 +145,9 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
145145
for (int i = cell.getCol(); i < cell.getCol() + cell.getColspan(); i++) {
146146
if (!widths[i].isPercent) {
147147
colspanRemain -= widths[i].width;
148-
if (!widths[i].isFixed) flexibleCols++;
148+
if (!widths[i].isFixed){
149+
flexibleCols++;
150+
}
149151
} else {
150152
colspanRemain = -1;
151153
break;
@@ -155,16 +157,18 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
155157
if (flexibleCols > 0) {
156158
// check min width in columns
157159
for (int i = cell.getCol(); i < cell.getCol() + cell.getColspan(); i++) {
158-
if (!widths[i].isFixed && widths[i].checkCollision(colspanRemain / flexibleCols)) {
160+
if (widths[i].isFlexible() && widths[i].checkCollision(colspanRemain / flexibleCols)) {
159161
widths[i].setPoints(widths[i].min).setFixed(true);
160-
if ((colspanRemain -= widths[i].min) <= 0 || flexibleCols-- <= 0) {
162+
colspanRemain -= widths[i].min;
163+
flexibleCols--;
164+
if (colspanRemain <= 0 || flexibleCols <= 0) {
161165
break;
162166
}
163167
}
164168
}
165169
if (colspanRemain > 0 && flexibleCols > 0) {
166170
for (int k = cell.getCol(); k < cell.getCol() + cell.getColspan(); k++) {
167-
if (!widths[k].isFixed) {
171+
if (widths[k].isFlexible()) {
168172
widths[k].addPoints(colspanRemain / flexibleCols).setFixed(true);
169173
}
170174
}
@@ -177,26 +181,20 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
177181
}
178182
}
179183
}
180-
} else if (!widths[cell.getCol()].isFixed) {
184+
} else if (widths[cell.getCol()].isFlexible()) {
181185
//if there is no information, try to set max width
182186
int flexibleCols = 0;
183187
float remainWidth = 0;
184188
for (int i = cell.getCol(); i < cell.getCol() + cell.getColspan(); i++) {
185-
if (!widths[i].isFixed && !widths[i].isPercent) {
189+
if (widths[i].isFlexible()) {
186190
remainWidth += widths[i].max - widths[i].width;
187191
flexibleCols++;
188192
}
189193
}
190-
if (remainWidth > 0) {
191-
if (flexibleCols > 0) {
192-
for (int i = cell.getCol(); i < cell.getCol() + cell.getColspan(); i++) {
193-
if (!widths[i].isFixed && !widths[i].isPercent) {
194-
widths[i].addPoints(remainWidth / flexibleCols);
195-
}
196-
}
197-
} else {
198-
for (int k = cell.getCol(); k < cell.getCol() + cell.getColspan(); k++) {
199-
widths[k].addPoints(remainWidth / cell.getColspan());
194+
if (remainWidth > 0) { // flexibleCols > 0 too
195+
for (int i = cell.getCol(); i < cell.getCol() + cell.getColspan(); i++) {
196+
if (widths[i].isFlexible()) {
197+
widths[i].addPoints(remainWidth / flexibleCols);
200198
}
201199
}
202200
}
@@ -217,19 +215,17 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
217215
UnitValue colWidth = getTable().getColumnWidth(i);
218216
if (colWidth.getValue() >= 0) {
219217
if (colWidth.isPercentValue()) {
220-
if (!widths[i].isPercent && widths[i].isFixed && widths[i].width > widths[i].min) {
221-
widths[i].max = widths[i].width;
222-
widths[i].setFixed(false);
223-
}
224218
if (!widths[i].isPercent) {
219+
if (widths[i].isFixed && widths[i].width > widths[i].min) {
220+
widths[i].max = widths[i].width;
221+
}
225222
widths[i].setPercents(colWidth.getValue());
226223
}
227-
228224
} else if (!widths[i].isPercent && colWidth.getValue() >= widths[i].min) {
229225
if (widths[i].isFixed) {
230226
widths[i].setPoints(colWidth.getValue());
231227
} else {
232-
widths[i].resetPoints(colWidth.getValue());
228+
widths[i].resetPoints(colWidth.getValue()).setFixed(true);
233229
}
234230
}
235231
}
@@ -256,7 +252,7 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
256252
warn100percent();
257253
} else if (sumOfPercents >= 100) {
258254
widths[i].resetPoints(widths[i].min);
259-
minTableWidth += widths[i].width;
255+
minTableWidth += widths[i].min;
260256
warn100percent();
261257
} else {
262258
sumOfPercents += widths[i].width;
@@ -277,6 +273,7 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
277273
tableWidthBasedOnPercents = Math.max(widths[i].max * 100 / widths[i].width, tableWidthBasedOnPercents);
278274
}
279275
}
276+
280277
if (tableWidthBasedOnPercents <= tableWidth) {
281278
tableWidth = tableWidthBasedOnPercents;
282279
//we don't need more space, columns are done based on column's max width.
@@ -375,33 +372,30 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
375372
float extraWidth = tableWidth - totalPercent - minTotalNonPercent;
376373
if (fixedAddition > 0 && (extraWidth < fixedAddition || flexibleAddition == 0)) {
377374
for (int i = 0; i < numberOfColumns; i++) {
378-
if (!widths[i].isPercent && widths[i].isFixed) {
375+
//only points could be fixed
376+
if (widths[i].isFixed) {
379377
widths[i].finalWidth += (widths[i].width - widths[i].min) * extraWidth / fixedAddition;
380378
}
381379
}
382380
} else {
383381
extraWidth -= fixedAddition;
384382
if (extraWidth < flexibleAddition) {
385383
for (int i = 0; i < numberOfColumns; i++) {
386-
if (!widths[i].isPercent) {
387-
if (widths[i].isFixed) {
388-
widths[i].finalWidth = widths[i].width;
389-
} else {
390-
widths[i].finalWidth += (widths[i].width - widths[i].min) * extraWidth / flexibleAddition;
391-
}
384+
if (widths[i].isFixed) {
385+
widths[i].finalWidth = widths[i].width;
386+
} else if (!widths[i].isPercent) {
387+
widths[i].finalWidth += (widths[i].width - widths[i].min) * extraWidth / flexibleAddition;
392388
}
393389
}
394390
} else {
395391
float totalFixed = 0;
396392
float totalFlexible = 0;
397393
for (int i = 0; i < numberOfColumns; i++) {
398-
if (!widths[i].isPercent) {
399-
if (widths[i].isFixed) {
400-
widths[i].finalWidth = widths[i].width;
401-
totalFixed += widths[i].width;
402-
} else {
403-
totalFlexible += widths[i].width;
404-
}
394+
if (widths[i].isFixed) {
395+
widths[i].finalWidth = widths[i].width;
396+
totalFixed += widths[i].width;
397+
} else if (!widths[i].isPercent) {
398+
totalFlexible += widths[i].width;
405399
}
406400
}
407401
extraWidth = tableWidth - totalPercent - totalFixed;
@@ -635,6 +629,7 @@ ColumnWidthData setPercents(float percent) {
635629
isPercent = true;
636630
width = percent;
637631
}
632+
isFixed = false;
638633
return this;
639634
}
640635

@@ -649,6 +644,10 @@ ColumnWidthData setFixed(boolean fixed) {
649644
return this;
650645
}
651646

647+
boolean isFlexible() {
648+
return !this.isFixed && !this.isPercent;
649+
}
650+
652651
/**
653652
* Check collusion between min value and point width
654653
*

0 commit comments

Comments
 (0)