Skip to content

Commit 9f34858

Browse files
Evgeniy prudnikoviText-CI
authored andcommitted
Fixed setting the direction to list item
DEVSIX-5358
1 parent a3cfd4e commit 9f34858

18 files changed

+198
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void draw(DrawContext drawContext) {
135135

136136
// It will be null in case of overflow (only the "split" part will contain symbol renderer.
137137
if (symbolRenderer != null && !symbolAddedInside) {
138-
boolean isRtl = BaseDirection.RIGHT_TO_LEFT.equals(this.<BaseDirection>getProperty(Property.BASE_DIRECTION));
138+
boolean isRtl = BaseDirection.RIGHT_TO_LEFT == this.<BaseDirection>getProperty(Property.BASE_DIRECTION);
139139
symbolRenderer.setParent(this);
140140
float x = isRtl ? occupiedArea.getBBox().getRight() : occupiedArea.getBBox().getLeft();
141141
ListSymbolPosition symbolPosition = (ListSymbolPosition) ListRenderer.getListItemOrListProperty(this, parent, Property.LIST_SYMBOL_POSITION);
@@ -223,7 +223,8 @@ public void draw(DrawContext drawContext) {
223223
symbolRenderer.move(dxPosition, 0);
224224
}
225225

226-
if (symbolRenderer.getOccupiedArea().getBBox().getRight() > parent.getOccupiedArea().getBBox().getLeft()) {
226+
if (!isRtl && symbolRenderer.getOccupiedArea().getBBox().getRight() > parent.getOccupiedArea().getBBox().getLeft()
227+
|| isRtl && symbolRenderer.getOccupiedArea().getBBox().getLeft() < parent.getOccupiedArea().getBBox().getRight()) {
227228
beginElementOpacityApplying(drawContext);
228229
symbolRenderer.draw(drawContext);
229230
endElementOpacityApplying(drawContext);
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package com.itextpdf.layout;
2+
3+
import com.itextpdf.commons.utils.MessageFormatUtil;
4+
import com.itextpdf.io.logs.IoLogMessageConstant;
5+
import com.itextpdf.io.util.UrlUtil;
6+
import com.itextpdf.kernel.colors.ColorConstants;
7+
import com.itextpdf.kernel.pdf.PdfDocument;
8+
import com.itextpdf.kernel.pdf.PdfWriter;
9+
import com.itextpdf.kernel.utils.CompareTool;
10+
import com.itextpdf.layout.element.List;
11+
import com.itextpdf.layout.element.ListItem;
12+
import com.itextpdf.layout.element.Paragraph;
13+
import com.itextpdf.layout.properties.BaseDirection;
14+
import com.itextpdf.layout.properties.ListSymbolAlignment;
15+
import com.itextpdf.layout.properties.ListSymbolPosition;
16+
import com.itextpdf.layout.properties.Property;
17+
import com.itextpdf.test.ExtendedITextTest;
18+
import com.itextpdf.test.annotations.LogMessage;
19+
import com.itextpdf.test.annotations.LogMessages;
20+
import com.itextpdf.test.annotations.type.IntegrationTest;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
import org.junit.experimental.categories.Category;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.Parameterized;
26+
27+
import java.io.FileOutputStream;
28+
import java.io.IOException;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.ArrayList;
31+
32+
@RunWith(Parameterized.class)
33+
@Category(IntegrationTest.class)
34+
public class ListItemPositionAlignmentTest extends ExtendedITextTest {
35+
36+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/ListItemPositionAlignmentTest/";
37+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/ListItemPositionAlignmentTest/";
38+
39+
private static final String PARAMETERS_NAME_PATTERN = "{index}: list-base-direction: {0}; list-item-base-direction: {1};" +
40+
" list-symbol-alignment: {2}; list-symbol-position: {3};";
41+
private static final String RESULTANT_FILE_NAME_PATTERN
42+
= "list-dir-{0}_item-dir-{1}_symbol-align-{2}_symbol-position-{3}";
43+
44+
private static final String HTML_PATTERN =
45+
"<ul style=\"background-color: green; direction: {3}\">"
46+
+ " <li style=\"background-color: blue;\">Usual item</li>"
47+
+ " <li style=\"background-color: yellow; direction: {2}; symbol-alignment:{1}; symbol-position: {0}\">Specific item</li>"
48+
+ "</ul>";
49+
50+
private BaseDirection listBaseDirection;
51+
private BaseDirection listItemBaseDirection;
52+
private ListSymbolAlignment listSymbolAlignment;
53+
private ListSymbolPosition listSymbolPosition;
54+
private Integer comparisonPdfId;
55+
56+
public ListItemPositionAlignmentTest(Object listBaseDirection, Object listItemBaseDirection,
57+
Object listSymbolAlignment, Object listSymbolPosition, Object comparisonPdfId) throws IOException {
58+
this.listBaseDirection = (BaseDirection) listBaseDirection;
59+
this.listItemBaseDirection = (BaseDirection) listItemBaseDirection;
60+
this.listSymbolAlignment = (ListSymbolAlignment) listSymbolAlignment;
61+
this.listSymbolPosition = (ListSymbolPosition) listSymbolPosition;
62+
this.comparisonPdfId = (Integer) comparisonPdfId;
63+
64+
createOrClearDestinationFolder(DESTINATION_FOLDER);
65+
// Create an HTML for this test
66+
createHtml();
67+
}
68+
69+
@Parameterized.Parameters(name = PARAMETERS_NAME_PATTERN)
70+
public static Iterable<Object[]> baseDirectionAndSymbolAlignmentProperties() {
71+
BaseDirection[] directionTestValues = new BaseDirection[]{BaseDirection.LEFT_TO_RIGHT,
72+
BaseDirection.RIGHT_TO_LEFT};
73+
ListSymbolAlignment[] listSymbolAlignmentTestValues = new ListSymbolAlignment[]{ListSymbolAlignment.LEFT,
74+
ListSymbolAlignment.RIGHT};
75+
ListSymbolPosition[] listSymbolPositionTestValues = new ListSymbolPosition[]{ListSymbolPosition.OUTSIDE,
76+
ListSymbolPosition.INSIDE};
77+
java.util.List<Object[]> objectList = new ArrayList<>();
78+
int count = 0;
79+
for (BaseDirection listBA : directionTestValues) {
80+
for (BaseDirection itemBA : directionTestValues) {
81+
for (ListSymbolAlignment listSA : listSymbolAlignmentTestValues) {
82+
for (ListSymbolPosition listSP : listSymbolPositionTestValues) {
83+
objectList.add(new Object[]{listBA, itemBA, listSA, listSP, count});
84+
count++;
85+
}
86+
}
87+
}
88+
}
89+
return objectList;
90+
}
91+
92+
@Test
93+
@LogMessages(messages = {@LogMessage(messageTemplate = IoLogMessageConstant.TYPOGRAPHY_NOT_FOUND, count = 8)})
94+
public void defaultListIemPositionAlignmentTest() throws IOException, InterruptedException {
95+
String fileName = MessageFormatUtil.format(
96+
RESULTANT_FILE_NAME_PATTERN,
97+
formatSymbolPosition(listSymbolPosition),
98+
formatSymbolAlignment(listSymbolAlignment),
99+
formatBaseDirection(listItemBaseDirection),
100+
formatBaseDirection(listBaseDirection));
101+
102+
String outFileName = DESTINATION_FOLDER + "defaultListItemTest" + comparisonPdfId + ".pdf";
103+
String cmpFileName = SOURCE_FOLDER + "cmp_defaultListItemTest" + comparisonPdfId + ".pdf";
104+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
105+
106+
Document document = new Document(pdfDocument);
107+
List list = createTestList();
108+
document.add(list);
109+
110+
document.close();
111+
112+
System.out.println("HTML: " + UrlUtil.getNormalizedFileUriString(DESTINATION_FOLDER + fileName + ".html") + "\n");
113+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, DESTINATION_FOLDER, "diff"));
114+
}
115+
116+
private List createTestList() {
117+
List list = new List();
118+
list.setSymbolIndent(20);
119+
list.setListSymbol("\u2022");
120+
list.setBackgroundColor(ColorConstants.GREEN);
121+
122+
ListItem listItem1 = new ListItem();
123+
listItem1.add(new Paragraph("Usual item"));
124+
listItem1.setBackgroundColor(ColorConstants.BLUE);
125+
126+
list.add(listItem1);
127+
128+
ListItem listItem2 = new ListItem();
129+
listItem2.add(new Paragraph("Specific item"));
130+
listItem2.setBackgroundColor(ColorConstants.YELLOW);
131+
listItem2.setProperty(Property.BASE_DIRECTION, listItemBaseDirection);
132+
listItem2.setProperty(Property.LIST_SYMBOL_ALIGNMENT, listSymbolAlignment);
133+
listItem2.setProperty(Property.LIST_SYMBOL_POSITION, listSymbolPosition);
134+
list.add(listItem2);
135+
list.setProperty(Property.BASE_DIRECTION, listBaseDirection);
136+
return list;
137+
}
138+
139+
private void createHtml() throws IOException {
140+
String fileName = MessageFormatUtil.format(
141+
RESULTANT_FILE_NAME_PATTERN,
142+
formatSymbolPosition(listSymbolPosition),
143+
formatSymbolAlignment(listSymbolAlignment),
144+
formatBaseDirection(listItemBaseDirection),
145+
formatBaseDirection(listBaseDirection));
146+
147+
String htmlString = MessageFormatUtil.format(
148+
HTML_PATTERN,
149+
formatSymbolPosition(listSymbolPosition),
150+
formatSymbolAlignment(listSymbolAlignment),
151+
formatBaseDirection(listItemBaseDirection),
152+
formatBaseDirection(listBaseDirection));
153+
try (FileOutputStream htmlFile =
154+
new FileOutputStream(DESTINATION_FOLDER + fileName + ".html")) {
155+
byte[] htmlBytes = htmlString.getBytes(StandardCharsets.UTF_8);
156+
htmlFile.write(htmlBytes, 0, htmlBytes.length);
157+
}
158+
}
159+
160+
private static String formatBaseDirection(BaseDirection direction) {
161+
switch (direction) {
162+
case LEFT_TO_RIGHT:
163+
return "ltr";
164+
case RIGHT_TO_LEFT:
165+
return "rtl";
166+
default:
167+
Assert.fail("Unexpected base direction");
168+
return null;
169+
}
170+
}
171+
172+
private static String formatSymbolAlignment(ListSymbolAlignment alignment) {
173+
switch (alignment) {
174+
case LEFT:
175+
return "left";
176+
case RIGHT:
177+
return "right";
178+
default:
179+
Assert.fail("Unexpected symbol alignment");
180+
return null;
181+
}
182+
}
183+
184+
private static String formatSymbolPosition(ListSymbolPosition position) {
185+
switch (position) {
186+
case OUTSIDE:
187+
return "outside";
188+
case INSIDE:
189+
return "inside";
190+
default:
191+
Assert.fail("Unexpected symbol position");
192+
return null;
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)