Skip to content

Commit afca8df

Browse files
Evgeniy PrudnikoviText-CI
authored andcommitted
Consider the margins of this document while obtaining list item's effective area
DEVSIX-6371
1 parent ddbd7bc commit afca8df

11 files changed

+165
-5
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.kernel.pdf.PdfDocument;
5050
import com.itextpdf.kernel.pdf.PdfPage;
5151
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
52-
import com.itextpdf.layout.Document;
5352
import com.itextpdf.layout.element.ListItem;
5453
import com.itextpdf.layout.element.Paragraph;
5554
import com.itextpdf.layout.layout.LayoutContext;
@@ -364,8 +363,20 @@ private Rectangle obtainEffectiveArea(DrawContext drawContext) {
364363
pageSize = pdfDocument.getDefaultPageSize();
365364
}
366365

367-
Document document = new Document(pdfDocument);
368-
return new Rectangle(pageSize).applyMargins(document.getTopMargin(), document.getRightMargin(), document.getBottomMargin(),
369-
document.getLeftMargin(), false);
366+
RootRenderer rootRenderer = this.getRootRenderer();
367+
//TODO DEVSIX-6372 Obtaining DocumentRenderer's margins results in a ClassCastException
368+
Float[] margins = new Float[] {rootRenderer.<Float>getProperty(Property.MARGIN_TOP),
369+
rootRenderer.<Float>getProperty(Property.MARGIN_RIGHT),
370+
rootRenderer.<Float>getProperty(Property.MARGIN_BOTTOM),
371+
rootRenderer.<Float>getProperty(Property.MARGIN_LEFT)};
372+
for (int i = 0; i < margins.length; i++) {
373+
margins[i] = replaceIfNull(margins[i], 0f);
374+
}
375+
return new Rectangle(pageSize)
376+
.applyMargins((float) margins[0], (float) margins[1], (float) margins[2], (float) margins[3], false);
377+
}
378+
379+
private static float replaceIfNull(Float value, float defaultValue) {
380+
return (float) (null == value ? defaultValue : value);
370381
}
371382
}

layout/src/test/java/com/itextpdf/layout/CanvasTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ This file is part of the iText (R) project.
6262
import com.itextpdf.layout.element.IBlockElement;
6363
import com.itextpdf.layout.element.Image;
6464
import com.itextpdf.layout.element.Link;
65+
import com.itextpdf.layout.element.ListItem;
6566
import com.itextpdf.layout.element.Paragraph;
67+
import com.itextpdf.layout.properties.ListNumberingType;
68+
import com.itextpdf.layout.properties.Property;
6669
import com.itextpdf.layout.testutil.TestConfigurationEvent;
6770
import com.itextpdf.layout.testutil.TestProductEvent;
6871
import com.itextpdf.test.ExtendedITextTest;
@@ -144,6 +147,63 @@ public void canvasWithPageLinkTest() throws IOException, InterruptedException {
144147
Assert.assertNull(new CompareTool().compareByContent(out, cmp, DESTINATION_FOLDER));
145148
}
146149

150+
@Test
151+
public void listItemWithoutMarginsInCanvasTest() throws IOException, InterruptedException {
152+
String testName = "listItemWithoutMarginsInCanvasTest";
153+
String out = DESTINATION_FOLDER + testName + ".pdf";
154+
String cmp = SOURCE_FOLDER + "cmp_" + testName + ".pdf";
155+
PdfDocument pdf = new PdfDocument(new PdfWriter(out));
156+
PdfPage page = pdf.addNewPage();
157+
Rectangle pageSize = page.getPageSize();
158+
159+
Canvas canvas = new Canvas(page, pageSize);
160+
com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List();
161+
list.setListSymbol(ListNumberingType.DECIMAL);
162+
list.add(new ListItem("list item 1"));
163+
list.add(new ListItem("list item 2"));
164+
canvas.add(list);
165+
canvas.close();
166+
pdf.close();
167+
168+
Assert.assertNull(new CompareTool().compareByContent(out, cmp, DESTINATION_FOLDER));
169+
}
170+
171+
@Test
172+
public void notApplyingMarginsInCanvasTest() throws IOException, InterruptedException {
173+
String testName = "notApplyingMarginsInCanvasTest";
174+
String out = DESTINATION_FOLDER + testName + ".pdf";
175+
String cmp = SOURCE_FOLDER + "cmp_" + testName + ".pdf";
176+
PdfDocument pdf = new PdfDocument(new PdfWriter(out));
177+
PdfPage page = pdf.addNewPage();
178+
Rectangle pageSize = page.getPageSize();
179+
180+
Canvas canvas = new Canvas(page, pageSize);
181+
canvas.setProperty(Property.MARGIN_LEFT, 36);
182+
canvas.add(new Paragraph("Hello"));
183+
canvas.close();
184+
pdf.close();
185+
186+
Assert.assertNull(new CompareTool().compareByContent(out, cmp, DESTINATION_FOLDER));
187+
}
188+
189+
@Test
190+
public void nullableMarginsInCanvasRendererTest() throws IOException, InterruptedException {
191+
String testName = "nullableMarginsInCanvasRenderer";
192+
String out = DESTINATION_FOLDER + testName + ".pdf";
193+
String cmp = SOURCE_FOLDER + "cmp_" + testName + ".pdf";
194+
PdfDocument pdf = new PdfDocument(new PdfWriter(out));
195+
PdfPage page = pdf.addNewPage();
196+
Rectangle pageSize = page.getPageSize();
197+
198+
Canvas canvas = new Canvas(page, pageSize);
199+
canvas.setProperty(Property.MARGIN_LEFT, null);
200+
canvas.add(new Paragraph("Hello"));
201+
canvas.close();
202+
pdf.close();
203+
204+
Assert.assertNull(new CompareTool().compareByContent(out, cmp, DESTINATION_FOLDER));
205+
}
206+
147207
@Test
148208
public void canvasWithPageEnableTaggingTest01() throws IOException, InterruptedException {
149209
String testName = "canvasWithPageEnableTaggingTest01";

layout/src/test/java/com/itextpdf/layout/ListTest.java

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,86 @@ public void listItemTest02() throws IOException, InterruptedException {
460460
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
461461
}
462462

463+
@Test
464+
public void listItemWithoutMarginsTest() throws IOException, InterruptedException {
465+
String outFileName = destinationFolder + "listItemWithoutMarginsTest.pdf";
466+
String cmpFileName = sourceFolder + "cmp_listItemWithoutMarginsTest.pdf";
467+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
468+
Document document = new Document(pdf);
469+
document.setMargins(0, 0, 0, 0);
470+
471+
List list = new List();
472+
list.setListSymbol(ListNumberingType.DECIMAL);
473+
list.add(new ListItem("list item 1"));
474+
list.add(new ListItem("list item 2"));
475+
476+
document.add(list);
477+
document.close();
478+
479+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
480+
}
481+
482+
@Test
483+
public void listItemBigMarginsTest() throws IOException, InterruptedException {
484+
String outFileName = destinationFolder + "listItemBigMarginsTest.pdf";
485+
String cmpFileName = sourceFolder + "cmp_listItemBigMarginsTest.pdf";
486+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
487+
Document document = new Document(pdf);
488+
int margin = 100;
489+
document.setMargins(margin, margin, margin, margin);
490+
491+
List list = new List();
492+
list.setListSymbol(ListNumberingType.DECIMAL);
493+
list.add(new ListItem("list item 1"));
494+
list.add(new ListItem("list item 2"));
495+
496+
document.add(list);
497+
document.close();
498+
499+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
500+
}
501+
502+
@Test
503+
public void maxMarginWidthWhereTheBulletIsNotDrawnTest() throws IOException, InterruptedException {
504+
String outFileName = destinationFolder + "maxMarginWidthWhereTheBulletIsNotDrawn.pdf";
505+
String cmpFileName = sourceFolder + "cmp_maxMarginWidthWhereTheBulletIsNotDrawn.pdf";
506+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
507+
Document document = new Document(pdf);
508+
int margin = 50;
509+
document.setMargins(margin, margin, margin, margin);
510+
511+
List list = new List();
512+
list.setListSymbol(ListNumberingType.DECIMAL);
513+
list.add(new ListItem("list item 1"));
514+
list.add(new ListItem("list item 2"));
515+
Float marginLeft = document.<Float>getDefaultProperty(Property.MARGIN_LEFT);
516+
list.setFixedPosition((float) marginLeft, 780, 200);
517+
document.add(list);
518+
document.close();
519+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
520+
}
521+
522+
@Test
523+
public void initialMarginWidthWhereTheBulletIsDrawnTest() throws IOException, InterruptedException {
524+
String outFileName = destinationFolder + "initialMarginWidthWhereTheBulletIsDrawn.pdf";
525+
String cmpFileName = sourceFolder + "cmp_initialMarginWidthWhereTheBulletIsDrawn.pdf";
526+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
527+
Document document = new Document(pdf);
528+
int margin = 49;
529+
document.setMargins(margin, margin, margin, margin);
530+
531+
List list = new List();
532+
list.setListSymbol(ListNumberingType.DECIMAL);
533+
list.add(new ListItem("list item 1"));
534+
list.add(new ListItem("list item 2"));
535+
Float marginLeft = document.<Float>getDefaultProperty(Property.MARGIN_LEFT);
536+
list.setFixedPosition((float) marginLeft, 780, 200);
537+
document.add(list);
538+
document.close();
539+
540+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
541+
}
542+
463543
@LogMessages(messages = {
464544
@LogMessage(messageTemplate = IoLogMessageConstant.CLIP_ELEMENT, count = 4)
465545
})
@@ -471,7 +551,6 @@ public void listWithSetHeightProperties01() throws IOException, InterruptedExcep
471551
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
472552
Document doc = new Document(pdfDoc);
473553

474-
475554
doc.add(new Paragraph("Default layout:"));
476555
ListItem item = new ListItem();
477556
ListItem nestedItem = new ListItem();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,4 +687,14 @@ public void nullChildTest() {
687687
AssertUtil.doesNotThrow(() -> renderer.linkRenderToDocument(divRenderer, doc.getPdfDocument()));
688688
}
689689
}
690+
691+
@Test
692+
//TODO DEVSIX-6372 Obtaining DocumentRenderer's margins results in a ClassCastException
693+
public void obtainingMarginsErrorTest() {
694+
PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
695+
Document document = new Document(doc);
696+
RootRenderer renderer = document.getRenderer();
697+
Rectangle rect = new Rectangle(0, 0);
698+
Assert.assertThrows(ClassCastException.class, () -> renderer.applyMargins(rect, false));
699+
}
690700
}

0 commit comments

Comments
 (0)