Skip to content

Commit 6a80e7e

Browse files
author
Kate Ivanova
committed
Add new tests on orphans and widows
1. Case both orphans and widows being handled on one paragraph. 2. Case using orphans or widows on page split. DEVSIX-3818
1 parent 8ae1279 commit 6a80e7e

File tree

5 files changed

+163
-6
lines changed

5 files changed

+163
-6
lines changed

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.layout;
2424

2525
import com.itextpdf.io.LogMessageConstant;
26-
import com.itextpdf.kernel.colors.Color;
27-
import com.itextpdf.kernel.colors.ColorConstants;
28-
import com.itextpdf.kernel.colors.DeviceGray;
29-
import com.itextpdf.kernel.geom.PageSize;
3026
import com.itextpdf.kernel.geom.Rectangle;
3127
import com.itextpdf.kernel.pdf.PdfDocument;
3228
import com.itextpdf.kernel.pdf.PdfWriter;
3329
import com.itextpdf.kernel.utils.CompareTool;
3430
import com.itextpdf.layout.element.Paragraph;
35-
import com.itextpdf.layout.property.TextAlignment;
3631
import com.itextpdf.layout.layout.LayoutArea;
3732
import com.itextpdf.layout.layout.LayoutContext;
3833
import com.itextpdf.layout.layout.LayoutResult;
@@ -49,7 +44,6 @@ This file is part of the iText (R) project.
4944

5045
import java.io.ByteArrayOutputStream;
5146
import java.io.IOException;
52-
import java.util.ArrayList;
5347
import java.util.List;
5448
import org.junit.Assert;
5549
import org.junit.BeforeClass;
@@ -361,6 +355,32 @@ public void twoLinesParagraphMin3Widows() throws IOException, InterruptedExcepti
361355
runOrphansWidowsBiggerThanLinesCount("twoLinesParagraphMin3Widows", false, false);
362356
}
363357

358+
@Test
359+
public void orphansAndWidowsTest() throws IOException, InterruptedException {
360+
runOrphansAndWidows("orphansAndWidowsTest");
361+
}
362+
363+
@Test
364+
public void widowsControlOnPagesTest() throws IOException, InterruptedException {
365+
String testName = "widowsControlOnPagesTest";
366+
367+
Paragraph testPara = new Paragraph();
368+
testPara.setWidowsControl(new ParagraphWidowsControl
369+
(3,1, true));
370+
371+
runTestOnPage(testName, testPara, false);
372+
}
373+
374+
@Test
375+
public void orphansControlOnPagesTest() throws IOException, InterruptedException {
376+
String testName = "orphansControlOnPagesTest";
377+
378+
Paragraph testPara = new Paragraph();
379+
testPara.setOrphansControl(new ParagraphOrphansControl(3));
380+
381+
runTestOnPage(testName, testPara, true);
382+
}
383+
364384
private static void runMaxHeightLimit(String fileName, boolean orphans) throws IOException, InterruptedException {
365385
String outPdf = destinationFolder + fileName + ".pdf";
366386
String cmpPdf = sourceFolder + "cmp_" + fileName + ".pdf";
@@ -491,6 +511,34 @@ private static void runMinThreeWidowsTest(String testName, int linesLeft, boolea
491511
runTest(testName, linesLeft, false, testPara);
492512
}
493513

514+
private static void runOrphansAndWidows(String testName)
515+
throws InterruptedException, IOException {
516+
String outPdf = destinationFolder + testName + ".pdf";
517+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
518+
519+
Paragraph testPara = new Paragraph();
520+
testPara
521+
.setOrphansControl(new ParagraphOrphansControl(3))
522+
.setWidowsControl(new ParagraphWidowsControl
523+
(3, 1, true));
524+
525+
OrphansWidowsTestUtil.produceOrphansAndWidowsTestCase(outPdf, testPara);
526+
527+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff_"));
528+
}
529+
530+
private static void runTestOnPage(String testName, Paragraph testPara, boolean orphans)
531+
throws InterruptedException, IOException {
532+
String outPdf = destinationFolder + testName + ".pdf";
533+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
534+
535+
int linesLeft = 1;
536+
537+
OrphansWidowsTestUtil.produceOrphansOrWidowsTestCase(outPdf, linesLeft, orphans, testPara);
538+
539+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff_"));
540+
}
541+
494542
private static void runTest(String testName, int linesLeft, boolean orphans, Paragraph testPara)
495543
throws InterruptedException, IOException {
496544
runTest(testName, linesLeft, orphans, testPara, false);

layout/src/test/java/com/itextpdf/layout/testutil/OrphansWidowsTestUtil.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.layout.property.ParagraphWidowsControl;
5050
import com.itextpdf.layout.property.Property;
5151
import com.itextpdf.layout.property.UnitValue;
52+
import com.itextpdf.layout.renderer.DocumentRenderer;
5253
import com.itextpdf.layout.renderer.ParagraphRenderer;
5354

5455
import java.io.FileNotFoundException;
@@ -510,6 +511,114 @@ public static void produceOrphansWidowsUnexpectedWidthOfNextAreaTestCase(String
510511
document.close();
511512
}
512513

514+
public static void produceOrphansOrWidowsTestCase(String outPdf, int linesLeft, boolean orphans,
515+
Paragraph testPara) throws FileNotFoundException {
516+
Document doc = new Document(new PdfDocument(new PdfWriter(outPdf)));
517+
518+
PageSize pageSize = new PageSize(PageSize.A4.getWidth(), PageSize.A5.getHeight());
519+
doc.getPdfDocument().setDefaultPageSize(pageSize);
520+
521+
testPara.setMargin(0).setBackgroundColor(new DeviceRgb(232, 232, 232));
522+
testPara.add(PARA_TEXT);
523+
524+
String orphansOrWidows = orphans ? "orphans" : "widows";
525+
String description = "Test " + orphansOrWidows + ".\n" + " This block height is adjusted in"
526+
+ " such way as to leave " + (String.valueOf(linesLeft)) + " line(s) on area break.\n"
527+
+ " Configuration is identified by the file name.\n Reference example"
528+
+ " without " + orphansOrWidows + " control can be found on the next page.";
529+
530+
float effectiveWidth;
531+
float effectiveHeight;
532+
533+
doc.setRenderer(new DocumentRenderer(doc));
534+
535+
Rectangle effectiveArea = doc.getPageEffectiveArea(pageSize);
536+
effectiveWidth = effectiveArea.getWidth();
537+
effectiveHeight = effectiveArea.getHeight();
538+
539+
float linesHeight = calculateHeightForLinesNum(doc, testPara, effectiveWidth, linesLeft, orphans);
540+
float adjustmentHeight = effectiveHeight - linesHeight - LINES_SPACE_EPS;
541+
542+
doc.add(new Paragraph()
543+
.add(new Text(description))
544+
.setMargin(0)
545+
.setBorder(new SolidBorder(1))
546+
.setHeight(adjustmentHeight));
547+
548+
doc.add(testPara);
549+
doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
550+
551+
doc.add(new Paragraph("Reference example without orphans/widows control.")
552+
.setMargin(0)
553+
.setBorder(new SolidBorder(1))
554+
.setHeight(adjustmentHeight));
555+
556+
doc.add(new Paragraph(PARA_TEXT).setMargin(0).setBackgroundColor(new DeviceRgb(232, 232, 232)));
557+
558+
doc.close();
559+
}
560+
561+
public static void produceOrphansAndWidowsTestCase(String outPdf, Paragraph testPara) throws FileNotFoundException {
562+
Document doc = new Document(new PdfDocument(new PdfWriter(outPdf)));
563+
564+
PageSize pageSize = new PageSize(PageSize.A4.getWidth(), PageSize.A5.getHeight());
565+
doc.getPdfDocument().setDefaultPageSize(pageSize);
566+
567+
Rectangle[] columns = initUniformColumns(pageSize, 2);
568+
doc.setRenderer(new ColumnDocumentRenderer(doc, columns));
569+
570+
String paraText = "A one line string\n";
571+
572+
testPara.setMargin(0).setBackgroundColor(new DeviceRgb(232, 232, 232));
573+
testPara.add(paraText);
574+
575+
float linesHeight = calculateHeightForLinesNum(doc, testPara, columns[1].getWidth(), 1, true);
576+
float adjustmentHeight = columns[0].getHeight() - linesHeight - LINES_SPACE_EPS;
577+
578+
String description = "Test orphans and widows case at once. This block height"
579+
+ " is adjusted in such way that both orphans and widows cases occur.\n "
580+
+ "The following paragraph contains as many fitting in one line text strings as needed"
581+
+ " to reproduce the case with both orphans and widows\n"
582+
+ "Reference example without orphans and widows"
583+
+ " control can be found on the next page";
584+
585+
doc.add(new Paragraph(description)
586+
.setMargin(0)
587+
.setBorder(new SolidBorder(1))
588+
.setHeight(adjustmentHeight));
589+
590+
Paragraph tempPara = new Paragraph().setMargin(0);
591+
for (int i = 0; i < 50; i++) {
592+
tempPara.add(paraText);
593+
}
594+
595+
ParagraphRenderer renderer = (ParagraphRenderer) tempPara.createRendererSubTree().setParent(doc.getRenderer());
596+
LayoutResult layoutRes = renderer.layout(new LayoutContext(new LayoutArea
597+
(1, new Rectangle( columns[1].getWidth(), columns[1].getHeight()))));
598+
int numberOfLines = ((ParagraphRenderer) layoutRes.getSplitRenderer()).getLines().size();
599+
for (int i = 0; i <= numberOfLines; i++) {
600+
testPara.add(paraText);
601+
}
602+
doc.add(testPara);
603+
doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
604+
605+
doc.add(new Paragraph("Reference example without orphans and widows control.")
606+
.setMargin(0)
607+
.setBorder(new SolidBorder(1))
608+
.setHeight(adjustmentHeight));
609+
610+
Paragraph paragraph = new Paragraph();
611+
for (int i = 0; i <= numberOfLines + 1; i++) {
612+
paragraph.add(paraText);
613+
}
614+
paragraph.setMargin(0).setBackgroundColor(new DeviceRgb(232, 232, 232));
615+
doc.add(paragraph);
616+
617+
doc.add(new Paragraph(paraText).setMargin(0).setBackgroundColor(new DeviceRgb(232, 232, 232)));
618+
619+
doc.close();
620+
}
621+
513622
public static float calculateHeightForLinesNum(Document doc, Paragraph p, float width, float linesNum,
514623
boolean orphans) {
515624
ParagraphRenderer renderer = (ParagraphRenderer) p.createRendererSubTree().setParent(doc.getRenderer());

0 commit comments

Comments
 (0)