Skip to content

Commit c6b1a6d

Browse files
committed
Fix indented rendering of the list item with 'position: inside' in case it goes to the next page
DEVSIX-6982
1 parent 8614e72 commit c6b1a6d

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343

4444
import org.slf4j.Logger;
4545
import org.slf4j.LoggerFactory;
46+
4647
import java.util.Collections;
4748
import java.util.List;
4849

@@ -54,6 +55,7 @@ public class ListItemRenderer extends DivRenderer {
5455

5556
/**
5657
* Creates a ListItemRenderer from its corresponding layout object.
58+
*
5759
* @param modelElement the {@link com.itextpdf.layout.element.ListItem} which this object should manage
5860
*/
5961
public ListItemRenderer(ListItem modelElement) {
@@ -182,7 +184,7 @@ public void draw(DrawContext drawContext) {
182184
applyBorderBox(occupiedArea.getBBox(), true);
183185
applyMargins(occupiedArea.getBBox(), true);
184186

185-
ListSymbolAlignment listSymbolAlignment = (ListSymbolAlignment)parent.<ListSymbolAlignment>getProperty(Property.LIST_SYMBOL_ALIGNMENT,
187+
ListSymbolAlignment listSymbolAlignment = (ListSymbolAlignment) parent.<ListSymbolAlignment>getProperty(Property.LIST_SYMBOL_ALIGNMENT,
186188
isRtl ? ListSymbolAlignment.LEFT : ListSymbolAlignment.RIGHT);
187189
float dxPosition = x - symbolRenderer.getOccupiedArea().getBBox().getX();
188190
if (listSymbolAlignment == ListSymbolAlignment.RIGHT) {
@@ -210,7 +212,7 @@ public void draw(DrawContext drawContext) {
210212

211213
// symbols are not drawn here, because they are in page margins
212214
if (!isRtl && symbolRenderer.getOccupiedArea().getBBox().getRight() > effectiveArea.getLeft()
213-
|| isRtl && symbolRenderer.getOccupiedArea().getBBox().getLeft() < effectiveArea.getRight()) {
215+
|| isRtl && symbolRenderer.getOccupiedArea().getBBox().getLeft() < effectiveArea.getRight()) {
214216
beginElementOpacityApplying(drawContext);
215217
symbolRenderer.draw(drawContext);
216218
endElementOpacityApplying(drawContext);
@@ -261,20 +263,23 @@ private void applyListSymbolPosition() {
261263
ParagraphRenderer paragraphRenderer = (ParagraphRenderer) childRenderers.get(0);
262264
// TODO DEVSIX-6876 LIST_SYMBOL_INDENT is not inherited
263265
Float symbolIndent = this.getPropertyAsFloat(Property.LIST_SYMBOL_INDENT);
264-
265-
if (symbolRenderer instanceof LineRenderer) {
266-
if (symbolIndent != null) {
267-
symbolRenderer.getChildRenderers().get(1).setProperty(isRtl ? Property.MARGIN_LEFT : Property.MARGIN_RIGHT, UnitValue.createPointValue((float) symbolIndent));
268-
}
269-
for (IRenderer childRenderer: symbolRenderer.getChildRenderers()) {
266+
if (symbolRenderer instanceof LineRenderer) {
267+
if (symbolIndent != null) {
268+
symbolRenderer.getChildRenderers().get(1).setProperty(isRtl ? Property.MARGIN_LEFT : Property.MARGIN_RIGHT, UnitValue.createPointValue((float) symbolIndent));
269+
}
270+
if (!paragraphRenderer.childRenderers.contains(symbolRenderer.getChildRenderers().get(1))) {
271+
for (IRenderer childRenderer : symbolRenderer.getChildRenderers()) {
270272
paragraphRenderer.childRenderers.add(0, childRenderer);
271273
}
272-
} else {
273-
if (symbolIndent != null) {
274-
symbolRenderer.setProperty(isRtl ? Property.MARGIN_LEFT : Property.MARGIN_RIGHT, UnitValue.createPointValue((float) symbolIndent));
275-
}
274+
}
275+
} else {
276+
if (symbolIndent != null) {
277+
symbolRenderer.setProperty(isRtl ? Property.MARGIN_LEFT : Property.MARGIN_RIGHT, UnitValue.createPointValue((float) symbolIndent));
278+
}
279+
if (!paragraphRenderer.childRenderers.contains(symbolRenderer)) {
276280
paragraphRenderer.childRenderers.add(0, symbolRenderer);
277281
}
282+
}
278283
symbolAddedInside = true;
279284
} else if (childRenderers.size() > 0 && childRenderers.get(0) instanceof ImageRenderer) {
280285
IRenderer paragraphRenderer = renderSymbolInNeutralParagraph();
@@ -322,11 +327,11 @@ private float[] calculateAscenderDescender() {
322327
Property.FONT_SIZE));
323328
}
324329
float[] ascenderDescender = TextRenderer.calculateAscenderDescender(listItemFont);
325-
return new float[] {
330+
return new float[]{
326331
fontSize.getValue() * FontProgram.convertTextSpaceToGlyphSpace(ascenderDescender[0]),
327332
fontSize.getValue() * FontProgram.convertTextSpaceToGlyphSpace(ascenderDescender[1])
328333
};
329334
}
330-
return new float[] {0, 0};
335+
return new float[]{0, 0};
331336
}
332337
}

layout/src/test/java/com/itextpdf/layout/renderer/ListRendererUnitTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,11 @@ public void symbolPositioningInsideDrawnOnceTest() {
9595
}
9696

9797
@Test
98-
// TODO: DEVSIX-6982 update this test after the ticket will be resolved
99-
public void symbolPositioningInsideAfterPagebreakTest() {
98+
public void symbolPositioningInsideAfterPageBreakTest() {
10099
List modelElement = new List();
101100
modelElement.setNextRenderer(new ListRenderer(modelElement));
102101

103-
for (int i = 0; i <25 ; i++) {
102+
for (int i = 0; i < 25; i++) {
104103
String s = "listitem " + i;
105104
ListItem listItem = (ListItem) new ListItem().add(new Paragraph(s));
106105
modelElement.add(listItem);
@@ -121,8 +120,8 @@ public void symbolPositioningInsideAfterPagebreakTest() {
121120
Pattern regex = Pattern.compile("^.-.*?-.*$");
122121
java.util.List<IRenderer> childRenderers = listRenderer.getChildRenderers();
123122

124-
// Assertion needs to be changed to assertEquals after fix.
125-
Assert.assertNotEquals(childRenderers.stream().filter(listitem -> regex.matcher(listitem.toString()).matches()).collect(Collectors.toList()).size(), 0);
123+
Assert.assertEquals(0, childRenderers.stream()
124+
.filter(listitem -> regex.matcher(listitem.toString()).matches()).count());
126125
}
127126

128127
private static class ListRendererCreatingNotifyingListSymbols extends ListRenderer {

0 commit comments

Comments
 (0)