Skip to content

Commit 2b3980f

Browse files
committed
Process paragraph margins correctly. Fixes in overflow with floats processing. Delete unnecessary layout methods.
DEVSIX-992
1 parent 707e41a commit 2b3980f

File tree

3 files changed

+43
-81
lines changed

3 files changed

+43
-81
lines changed

layout/src/main/java/com/itextpdf/layout/element/BlockElement.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -399,45 +399,4 @@ public T setMinHeight(float minHeight) {
399399
setProperty(Property.MIN_HEIGHT, minHeight);
400400
return (T) (Object) this;
401401
}
402-
403-
/**
404-
* Sets the overflow-x value.
405-
*
406-
* @param overflow-x the overflow value
407-
* @return this element
408-
*/
409-
public T setOverflowX(OverflowPropertyValue overflow) {
410-
setProperty(Property.OVERFLOW_X, overflow);
411-
return (T) (Object) this;
412-
}
413-
414-
/**
415-
* Sets the overflow-y value.
416-
*
417-
* @param overflow-y the overflow value
418-
* @return this element
419-
*/
420-
public T setOverflowY(OverflowPropertyValue overflow) {
421-
setProperty(Property.OVERFLOW_Y, overflow);
422-
return (T) (Object) this;
423-
}
424-
425-
/**
426-
* Gets the overflow-x value of the element.
427-
*
428-
* @return the overflow-x value of the element.
429-
*/
430-
public OverflowPropertyValue getOverflowX() {
431-
return this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X);
432-
}
433-
434-
/**
435-
* Gets the overflow-y value of the element.
436-
*
437-
* @return the overflow-y value of the element.
438-
*/
439-
public OverflowPropertyValue getOverflowY() {
440-
return this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_Y);
441-
}
442-
443402
}

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

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,17 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
9595
Rectangle layoutBox = layoutContext.getArea().getBBox().clone();
9696
boolean wasParentsHeightClipped = layoutContext.getArea().isClippedHeight();
9797
List<Rectangle> floatRendererAreas = layoutContext.getFloatRendererAreas();
98+
99+
OverflowPropertyValue oldXOverflow = null;
100+
boolean wasXOverflowChanged = false;
101+
98102
if (floatRendererAreas != null) {
103+
float layoutWidth = layoutBox.getWidth();
99104
FloatingHelper.adjustLineAreaAccordingToFloats(floatRendererAreas, layoutBox);
100-
if (0 != floatRendererAreas.size()) {
101-
setProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT); // TODO
105+
if (layoutWidth > layoutBox.getWidth()) {
106+
oldXOverflow = this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X);
107+
wasXOverflowChanged = true;
108+
setProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
102109
}
103110
}
104111

@@ -132,7 +139,7 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
132139

133140
boolean floatsPlaced = false;
134141
List<IRenderer> overflowFloats = new ArrayList<>();
135-
int lastTabIndex = 0;
142+
int lastTabIndex = 0;
136143

137144
while (childPos < childRenderers.size()) {
138145
IRenderer childRenderer = childRenderers.get(childPos);
@@ -198,14 +205,13 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
198205
// TODO may be process floating spans as inline blocks always?
199206

200207
if (childPos > 0) {
208+
oldXOverflow = this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X);
209+
wasXOverflowChanged = true;
201210
setProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
202211
}
203212
if (overflowFloats.isEmpty() && (!anythingPlaced || floatingBoxFullWidth <= bbox.getWidth())) {
204213
childResult = childRenderer.layout(new LayoutContext(new LayoutArea(layoutContext.getArea().getPageNumber(), layoutContext.getArea().getBBox().clone(), wasParentsHeightClipped), null, floatRendererAreas));
205214
}
206-
if (childPos > 0) {
207-
deleteOwnProperty(Property.OVERFLOW_X);
208-
}
209215
// Get back child width so that it's not lost
210216
if (childWidthWasReplaced) {
211217
if (childRendererHasOwnWidthProperty) {
@@ -222,7 +228,7 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
222228
minChildWidth = ((MinMaxWidthLayoutResult) childResult).getNotNullMinMaxWidth(bbox.getWidth()).getMinWidth();
223229
}
224230
// TODO if percents width was used, max width might be huge
225-
maxChildWidth = ((MinMaxWidthLayoutResult)childResult).getNotNullMinMaxWidth(bbox.getWidth()).getMaxWidth();
231+
maxChildWidth = ((MinMaxWidthLayoutResult) childResult).getNotNullMinMaxWidth(bbox.getWidth()).getMaxWidth();
226232
}
227233
widthHandler.updateMinChildWidth(minChildWidth);
228234
widthHandler.updateMaxChildWidth(maxChildWidth);
@@ -235,8 +241,8 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
235241
LineRenderer[] split = splitNotFittingFloat(childPos, childResult);
236242
IRenderer splitRenderer = childResult.getSplitRenderer();
237243
if (splitRenderer instanceof TextRenderer) {
238-
((TextRenderer)splitRenderer).trimFirst();
239-
((TextRenderer)splitRenderer).trimLast();
244+
((TextRenderer) splitRenderer).trimFirst();
245+
((TextRenderer) splitRenderer).trimLast();
240246
}
241247
// ensure no other thing (like text wrapping the float) will occupy the line
242248
splitRenderer.getOccupiedArea().getBBox().setWidth(layoutContext.getArea().getBBox().getWidth());
@@ -246,8 +252,8 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
246252
floatsPlaced = true;
247253

248254
if (childRenderer instanceof TextRenderer) {
249-
((TextRenderer)childRenderer).trimFirst();
250-
((TextRenderer)childRenderer).trimLast();
255+
((TextRenderer) childRenderer).trimFirst();
256+
((TextRenderer) childRenderer).trimLast();
251257
}
252258

253259
adjustLineOnFloatPlaced(layoutBox, childPos, kidFloatPropertyVal, childRenderer.getOccupiedArea().getBBox());
@@ -269,7 +275,7 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
269275
boolean isInlineBlockChild = isInlineBlockChild(childRenderer);
270276
if (!childWidthWasReplaced) {
271277
if (isInlineBlockChild && childRenderer instanceof AbstractRenderer) {
272-
childBlockMinMaxWidth = ((AbstractRenderer)childRenderer).getMinMaxWidth(MinMaxWidthUtils.getMax());
278+
childBlockMinMaxWidth = ((AbstractRenderer) childRenderer).getMinMaxWidth(MinMaxWidthUtils.getMax());
273279
float childMaxWidth = childBlockMinMaxWidth.getMaxWidth() + MIN_MAX_WIDTH_CORRECTION_EPS;
274280
// Decrease the calculated width by margins, paddings and borders so that even for 100% width the content definitely fits
275281
// TODO DEVSIX-1174 fix depending on box-sizing
@@ -290,12 +296,11 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
290296
}
291297
if (childResult == null) {
292298
if (childPos > 0) {
299+
oldXOverflow = this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X);
300+
wasXOverflowChanged = true;
293301
setProperty(Property.OVERFLOW_X, OverflowPropertyValue.FIT);
294302
}
295303
childResult = childRenderer.layout(new LayoutContext(new LayoutArea(layoutContext.getArea().getPageNumber(), bbox, wasParentsHeightClipped)));
296-
if (childPos > 0) {
297-
deleteOwnProperty(Property.OVERFLOW_X);
298-
}
299304
}
300305

301306
// Get back child width so that it's not lost
@@ -326,12 +331,12 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
326331
childDescent = ((ILeafElementRenderer) childRenderer).getDescent();
327332
} else if (isInlineBlockChild && childResult.getStatus() != LayoutResult.NOTHING) {
328333
if (childRenderer instanceof AbstractRenderer) {
329-
Float yLine = ((AbstractRenderer)childRenderer).getLastYLineRecursively();
334+
Float yLine = ((AbstractRenderer) childRenderer).getLastYLineRecursively();
330335
if (yLine == null) {
331336
childAscent = childRenderer.getOccupiedArea().getBBox().getHeight();
332337
} else {
333-
childAscent = childRenderer.getOccupiedArea().getBBox().getTop() - (float)yLine;
334-
childDescent = -((float)yLine - childRenderer.getOccupiedArea().getBBox().getBottom());
338+
childAscent = childRenderer.getOccupiedArea().getBBox().getTop() - (float) yLine;
339+
childDescent = -((float) yLine - childRenderer.getOccupiedArea().getBBox().getBottom());
335340
}
336341
} else {
337342
childAscent = childRenderer.getOccupiedArea().getBBox().getHeight();
@@ -546,9 +551,9 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
546551
children.add(newRenderer);
547552

548553
// Insert non-text renderers
549-
if (insertAfter.containsKey((TextRenderer)renderer)) {
550-
children.addAll(insertAfter.get((TextRenderer)renderer));
551-
insertAfter.remove((TextRenderer)renderer);
554+
if (insertAfter.containsKey((TextRenderer) renderer)) {
555+
children.addAll(insertAfter.get((TextRenderer) renderer));
556+
insertAfter.remove((TextRenderer) renderer);
552557
}
553558

554559
newRenderer.line = new GlyphLine(newRenderer.line);
@@ -620,15 +625,13 @@ public LineLayoutResult layout(LayoutContext layoutContext) {
620625
result.setMinMaxWidth(minMaxWidth);
621626
}
622627

623-
if (floatRendererAreas != null) {
624-
if (0 != floatRendererAreas.size()) {
625-
deleteOwnProperty(Property.OVERFLOW_X); // TODO
626-
if (null != result.getSplitRenderer()) {
627-
result.getSplitRenderer().deleteOwnProperty(Property.OVERFLOW_X);
628-
}
629-
if (null != result.getOverflowRenderer()) {
630-
result.getOverflowRenderer().deleteOwnProperty(Property.OVERFLOW_X);
631-
}
628+
if (wasXOverflowChanged) {
629+
setProperty(Property.OVERFLOW_X, oldXOverflow);
630+
if (null != result.getSplitRenderer()) {
631+
result.getSplitRenderer().setProperty(Property.OVERFLOW_X, oldXOverflow);
632+
}
633+
if (null != result.getOverflowRenderer()) {
634+
result.getOverflowRenderer().setProperty(Property.OVERFLOW_X, oldXOverflow);
632635
}
633636
}
634637
return result;
@@ -788,7 +791,7 @@ protected LineRenderer adjustChildrenYLine() {
788791
renderer.move(0, actualYLine - renderer.getOccupiedArea().getBBox().getBottom() + descent);
789792
} else {
790793
Float yLine = isInlineBlockChild(renderer) && renderer instanceof AbstractRenderer ? ((AbstractRenderer) renderer).getLastYLineRecursively() : null;
791-
renderer.move(0, actualYLine - (yLine == null ? renderer.getOccupiedArea().getBBox().getBottom() : (float)yLine));
794+
renderer.move(0, actualYLine - (yLine == null ? renderer.getOccupiedArea().getBBox().getBottom() : (float) yLine));
792795
}
793796
}
794797
return this;
@@ -840,7 +843,7 @@ float getTopLeadingIndent(Leading leading) {
840843
case Leading.FIXED:
841844
return (Math.max(leading.getValue(), maxBlockAscent - maxBlockDescent) - occupiedArea.getBBox().getHeight()) / 2;
842845
case Leading.MULTIPLIED:
843-
float fontSize = (float)this.getPropertyAsFloat(Property.FONT_SIZE, 0f);
846+
float fontSize = (float) this.getPropertyAsFloat(Property.FONT_SIZE, 0f);
844847
// In HTML, depending on whether <!DOCTYPE html> is present or not, and if present then depending on the version,
845848
// the behavior id different. In one case, bottom leading indent is added for images, in the other it is not added.
846849
// This is why !containsImage() is present below. Depending on the presence of this !containsImage() condition, the behavior changes
@@ -858,7 +861,7 @@ float getBottomLeadingIndent(Leading leading) {
858861
case Leading.FIXED:
859862
return (Math.max(leading.getValue(), maxBlockAscent - maxBlockDescent) - occupiedArea.getBBox().getHeight()) / 2;
860863
case Leading.MULTIPLIED:
861-
float fontSize = (float)this.getPropertyAsFloat(Property.FONT_SIZE, 0f);
864+
float fontSize = (float) this.getPropertyAsFloat(Property.FONT_SIZE, 0f);
862865
// In HTML, depending on whether <!DOCTYPE html> is present or not, and if present then depending on the version,
863866
// the behavior id different. In one case, bottom leading indent is added for images, in the other it is not added.
864867
// This is why !containsImage() is present below. Depending on the presence of this !containsImage() condition, the behavior changes

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
369369
previousDescent = processedRenderer.getMaxDescent();
370370
}
371371
}
372+
float moveDown = lastLineBottomLeadingIndent;
373+
if ((null == overflowY || OverflowPropertyValue.FIT.equals(overflowY)) && moveDown > occupiedArea.getBBox().getY() - layoutBox.getY()) {
374+
moveDown = occupiedArea.getBBox().getY() - layoutBox.getY();
375+
}
376+
occupiedArea.getBBox().moveDown(moveDown);
377+
occupiedArea.getBBox().setHeight(occupiedArea.getBBox().getHeight() + moveDown);
378+
372379
float overflowPartHeight = getOverflowPartHeight(overflowY, layoutBox);
373380

374381
if (marginsCollapsingEnabled) {
@@ -382,13 +389,6 @@ public LayoutResult layout(LayoutContext layoutContext) {
382389
FloatingHelper.includeChildFloatsInOccupiedArea(floatRendererAreas, this);
383390
}
384391

385-
float moveDown = lastLineBottomLeadingIndent;
386-
if ((null == overflowY || OverflowPropertyValue.FIT.equals(overflowY)) && moveDown > occupiedArea.getBBox().getY() - layoutBox.getY()) {
387-
moveDown = occupiedArea.getBBox().getY() - layoutBox.getY();
388-
}
389-
occupiedArea.getBBox().moveDown(moveDown);
390-
occupiedArea.getBBox().setHeight(occupiedArea.getBBox().getHeight() + moveDown);
391-
392392
IRenderer overflowRenderer = null;
393393
Float blockMinHeight = retrieveMinHeight();
394394
if (null != blockMinHeight && blockMinHeight > occupiedArea.getBBox().getHeight()) {

0 commit comments

Comments
 (0)