Skip to content

Commit c6116de

Browse files
ars18wrwiText-CI
authored andcommitted
Add a test which demonstrates margin-collapse processing with a custom renderer.
DEVSIX-2901
1 parent 1748f09 commit c6116de

File tree

1 file changed

+93
-13
lines changed

1 file changed

+93
-13
lines changed

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

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,32 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.kernel.colors.ColorConstants;
4646
import com.itextpdf.kernel.colors.DeviceRgb;
47+
import com.itextpdf.kernel.geom.Rectangle;
4748
import com.itextpdf.kernel.pdf.PdfDocument;
4849
import com.itextpdf.kernel.pdf.PdfWriter;
4950
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
5051
import com.itextpdf.kernel.utils.CompareTool;
5152
import com.itextpdf.layout.element.Div;
5253
import com.itextpdf.layout.element.Paragraph;
5354
import com.itextpdf.layout.element.Text;
55+
import com.itextpdf.layout.layout.LayoutArea;
56+
import com.itextpdf.layout.layout.LayoutContext;
57+
import com.itextpdf.layout.layout.LayoutResult;
5458
import com.itextpdf.layout.property.Property;
59+
import com.itextpdf.layout.renderer.DivRenderer;
60+
import com.itextpdf.layout.renderer.IRenderer;
5561
import com.itextpdf.test.ExtendedITextTest;
5662
import com.itextpdf.test.annotations.type.IntegrationTest;
5763
import org.junit.Assert;
5864
import org.junit.BeforeClass;
65+
import org.junit.Rule;
5966
import org.junit.Test;
6067
import org.junit.experimental.categories.Category;
68+
import org.junit.rules.ExpectedException;
6169

6270
import java.io.IOException;
71+
import java.util.ArrayList;
72+
import java.util.List;
6373

6474
@Category(IntegrationTest.class)
6575
public class CollapsingMarginsTest extends ExtendedITextTest {
@@ -71,6 +81,9 @@ public static void beforeClass() {
7181
createOrClearDestinationFolder(destinationFolder);
7282
}
7383

84+
@Rule
85+
public ExpectedException junitExpectedException = ExpectedException.none();
86+
7487
@Test
7588
public void collapsingMarginsTest01() throws IOException, InterruptedException {
7689
String outFileName = destinationFolder + "collapsingMarginsTest01.pdf";
@@ -104,14 +117,14 @@ public void collapsingMarginsTest01() throws IOException, InterruptedException {
104117
Div div1 = new Div();
105118
Div div2 = new Div();
106119

107-
div1.add(p).setBackgroundColor(new DeviceRgb(65,151,29));
108-
div2.add(p).setBackgroundColor(new DeviceRgb(209,247,29));
120+
div1.add(p).setBackgroundColor(new DeviceRgb(65, 151, 29));
121+
div2.add(p).setBackgroundColor(new DeviceRgb(209, 247, 29));
109122

110123
div1.setMarginBottom(20);
111124
div2.setMarginTop(150);
112125
div2.setMarginBottom(150);
113126

114-
Div div = new Div().setMarginTop(20).setMarginBottom(10).setBackgroundColor(new DeviceRgb(78,151,205));
127+
Div div = new Div().setMarginTop(20).setMarginBottom(10).setBackgroundColor(new DeviceRgb(78, 151, 205));
115128
div.add(div1);
116129
div.add(div2);
117130
doc.add(div);
@@ -160,14 +173,14 @@ public void collapsingMarginsTest02() throws IOException, InterruptedException {
160173
Div div1 = new Div();
161174
Div div2 = new Div();
162175

163-
div1.add(p).setBackgroundColor(new DeviceRgb(65,151,29));
164-
div2.add(p).setBackgroundColor(new DeviceRgb(209,247,29));
176+
div1.add(p).setBackgroundColor(new DeviceRgb(65, 151, 29));
177+
div2.add(p).setBackgroundColor(new DeviceRgb(209, 247, 29));
165178

166179
div1.setMarginBottom(40);
167180
div2.setMarginTop(20);
168181
div2.setMarginBottom(150);
169182

170-
Div div = new Div().setMarginTop(20).setMarginBottom(10).setBackgroundColor(new DeviceRgb(78,151,205));
183+
Div div = new Div().setMarginTop(20).setMarginBottom(10).setBackgroundColor(new DeviceRgb(78, 151, 205));
171184
div.add(div1);
172185
div.add(div2);
173186
doc.add(div);
@@ -215,8 +228,8 @@ public void collapsingMarginsTest03() throws IOException, InterruptedException {
215228
Div div1 = new Div();
216229
Div div2 = new Div();
217230

218-
div1.add(p).setBackgroundColor(new DeviceRgb(65,151,29));
219-
div2.add(p).setBackgroundColor(new DeviceRgb(209,247,29));
231+
div1.add(p).setBackgroundColor(new DeviceRgb(65, 151, 29));
232+
div2.add(p).setBackgroundColor(new DeviceRgb(209, 247, 29));
220233

221234
div1.setMarginBottom(80);
222235
div2.setMarginTop(80);
@@ -267,14 +280,14 @@ public void collapsingMarginsTest04() throws IOException, InterruptedException {
267280
p.add(new Text("small text").setFontSize(5.1f));
268281
p.add(
269282
"\nAnd is always as nobly requited;\n" +
270-
"Then battle for Freedom wherever you can,\n" +
271-
"And, if not shot or hanged, you'll get knighted.");
283+
"Then battle for Freedom wherever you can,\n" +
284+
"And, if not shot or hanged, you'll get knighted.");
272285

273286
Div div1 = new Div();
274287
Div div2 = new Div();
275288

276-
div1.add(p).setBackgroundColor(new DeviceRgb(65,151,29));
277-
div2.add(p).setBackgroundColor(new DeviceRgb(209,247,29));
289+
div1.add(p).setBackgroundColor(new DeviceRgb(65, 151, 29));
290+
div2.add(p).setBackgroundColor(new DeviceRgb(209, 247, 29));
278291

279292
div1.setMarginBottom(80);
280293
div2.setMarginTop(80);
@@ -320,7 +333,7 @@ public void collapsingMarginsTest05() throws IOException, InterruptedException {
320333
p.setMarginTop(80);
321334
Div div = new Div();
322335

323-
div.add(p).setBackgroundColor(new DeviceRgb(65,151,29));
336+
div.add(p).setBackgroundColor(new DeviceRgb(65, 151, 29));
324337

325338
doc.add(div);
326339

@@ -342,4 +355,71 @@ private void drawPageBorders(PdfDocument pdfDocument, int pageNum) {
342355
canvas.restoreState();
343356
}
344357
}
358+
359+
@Test
360+
// TODO DEVSIX-2901 the exception should not be thrown
361+
public void columnRendererTest() throws IOException, InterruptedException {
362+
junitExpectedException.expect(ArrayIndexOutOfBoundsException.class);
363+
String outFileName = destinationFolder + "columnRendererTest.pdf";
364+
String cmpFileName = sourceFolder + "cmp_columnRendererTest.pdf";
365+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
366+
367+
String textByron =
368+
"When a man hath no freedom to fight for at home,\n" +
369+
" Let him combat for that of his neighbours;\n" +
370+
"Let him think of the glories of Greece and of Rome,\n" +
371+
" And get knocked on the head for his labours.\n" +
372+
"\n" +
373+
"To do good to Mankind is the chivalrous plan,\n" +
374+
" And is always as nobly requited;\n" +
375+
"Then battle for Freedom wherever you can,\n" +
376+
" And, if not shot or hanged, you'll get knighted.";
377+
378+
Document doc = new Document(pdfDocument);
379+
doc.setProperty(Property.COLLAPSING_MARGINS, true);
380+
381+
382+
Paragraph p = new Paragraph();
383+
for (int i = 0; i < 10; i++) {
384+
p.add(textByron);
385+
}
386+
387+
Div div = new Div().add(p);
388+
List<Rectangle> areas = new ArrayList<>();
389+
areas.add(new Rectangle(30, 30, 150, 600));
390+
areas.add(new Rectangle(200, 30, 150, 600));
391+
areas.add(new Rectangle(370, 30, 150, 600));
392+
div.setNextRenderer(new CustomColumnDocumentRenderer(div, areas));
393+
394+
doc.add(div);
395+
396+
doc.close();
397+
398+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
399+
}
400+
401+
private static class CustomColumnDocumentRenderer extends DivRenderer {
402+
private List<Rectangle> areas;
403+
404+
public CustomColumnDocumentRenderer(Div modelElement, List<Rectangle> areas) {
405+
super(modelElement);
406+
this.areas = areas;
407+
}
408+
409+
@Override
410+
public LayoutResult layout(LayoutContext layoutContext) {
411+
LayoutResult result = super.layout(layoutContext);
412+
return result;
413+
}
414+
415+
@Override
416+
public List<Rectangle> initElementAreas(LayoutArea area) {
417+
return areas;
418+
}
419+
420+
@Override
421+
public IRenderer getNextRenderer() {
422+
return new CustomColumnDocumentRenderer((Div) modelElement, areas);
423+
}
424+
}
345425
}

0 commit comments

Comments
 (0)