Skip to content

Commit 10ea093

Browse files
committed
Update auto table layout algorithm
DEVSIX-1252
1 parent a8f9bf5 commit 10ea093

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
216216
//TODO add colgroup information.
217217
for (int i = 0; i < numberOfColumns; i++) {
218218
UnitValue colWidth = getTable().getColumnWidth(i);
219-
if (colWidth.getValue() >= 0) {
219+
if (colWidth != null && colWidth.getValue() > 0) {
220220
if (colWidth.isPercentValue()) {
221221
if (!widths[i].isPercent) {
222222
if (widths[i].isFixed && widths[i].width > widths[i].min) {
@@ -341,7 +341,7 @@ float[] autoLayout(float[] minWidths, float[] maxWidths) {
341341
//sum of non fixed non percent columns.
342342
for (int i = 0; i < numberOfColumns; i++) {
343343
if (widths[i].isPercent) {
344-
if (tableWidth * widths[i].width >= widths[i].min) {
344+
if (tableWidth * widths[i].width / 100 >= widths[i].min) {
345345
widths[i].finalWidth = tableWidth * widths[i].width / 100;
346346
totalPercent += widths[i].finalWidth;
347347
} else {
@@ -451,10 +451,9 @@ float[] fixedLayout() {
451451
UnitValue cellWidth = getCellWidth(cell, true);
452452
if (cellWidth != null) {
453453
assert cellWidth.getValue() >= 0;
454-
float width = cellWidth.getValue();
455-
if (cellWidth.isPercentValue()) {
456-
width = tableWidth * width / 100;
457-
}
454+
float width = cellWidth.isPercentValue()
455+
? tableWidth * cellWidth.getValue() / 100
456+
: cellWidth.getValue();
458457
int colspan = cell.getModelElement().getColspan();
459458
for (int j = 0; j < colspan; j++) {
460459
columnWidths[i + j] = width / colspan;
@@ -702,11 +701,16 @@ public String toString() {
702701
}
703702
}
704703

704+
static private final UnitValue ZeroWidth = UnitValue.createPointValue(0);
705+
705706
//TODO DEVSIX-1174, box-sizing property
706707
UnitValue getCellWidth(CellRenderer cell, boolean zeroIsValid) {
707708
UnitValue widthValue = cell.<UnitValue>getProperty(Property.WIDTH);
708709
if (widthValue == null || widthValue.getValue() < 0) return null;
709-
if (!zeroIsValid && widthValue.getValue() == 0) return null;
710+
//zero has special meaning in fixed layout, we shall not add padding to zero value
711+
if (widthValue.getValue() == 0) {
712+
return zeroIsValid ? ZeroWidth : null;
713+
}
710714
if (widthValue == null || widthValue.isPercentValue()) {
711715
return widthValue;
712716
} else {

0 commit comments

Comments
 (0)