Skip to content

Commit 7645d53

Browse files
Remove floats skipping logic in span-wrapping: now floats break text sequence
DEVSIX-1448
1 parent 2af1b9a commit 7645d53

8 files changed

+206
-133
lines changed

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

Lines changed: 44 additions & 65 deletions
Large diffs are not rendered by default.

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

Lines changed: 159 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -361,59 +361,50 @@ public void wordSplitAcrossMultipleRenderersOverflowXVisible() throws IOExceptio
361361
}
362362

363363
@Test
364-
public void wordSplitAcrossMutipleTextRenderersWithinFloatingContainer() throws IOException, InterruptedException {
365-
String outFileName = destinationFolder + "wordSplitAcrossMutipleTextRenderersWithinFloatingContainer.pdf";
366-
String cmpFileName = sourceFolder + "cmp_wordSplitAcrossMutipleTextRenderersWithinFloatingContainer.pdf";
364+
public void wordSplitRenderersWithFittingFloatingElementInBetween() throws IOException, InterruptedException {
365+
String outFileName = destinationFolder + "wordSplitRenderersWithFittingFloatingElementInBetween.pdf";
366+
String cmpFileName = sourceFolder + "cmp_wordSplitRenderersWithFittingFloatingElementInBetween.pdf";
367367

368368
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
369369
Document doc = new Document(pdfDocument);
370370

371371
doc.setFontSize(20);
372372

373-
Text oooooooooover = new Text("oooooooooover")
374-
.setFontColor(ColorConstants.LIGHT_GRAY);
375-
Text flooooo = new Text("flooooo")
376-
.setFontColor(ColorConstants.GRAY);
377-
Text ooooowNextWords = new Text("ooooow next words")
378-
.setFontColor(ColorConstants.DARK_GRAY);
373+
Text reg = new Text("reg").setFontColor(ColorConstants.LIGHT_GRAY);
374+
Text ul = new Text("ul").setFontColor(ColorConstants.DARK_GRAY);
375+
Text aaaaaaaaaaaaaaaaaaaaaaati = new Text("aaaaaaaaaaaaaaaaaaaaaaati").setFontColor(ColorConstants.GRAY);
376+
Text ngAndRestOfText = new Text("ng overflow text renderers with floating elements between them")
377+
.setFontColor(ColorConstants.RED);
379378

380-
Paragraph floatingParagraph = new Paragraph()
381-
.add(oooooooooover)
382-
.add(flooooo)
383-
.add(ooooowNextWords)
379+
380+
Div floatDiv = new Div().setWidth(20).setHeight(60)
381+
.setBackgroundColor(ColorConstants.LIGHT_GRAY)
382+
.setBorder(new SolidBorder(2));
383+
floatDiv.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
384+
385+
Paragraph p = new Paragraph()
386+
.add(reg)
387+
.add(ul)
388+
.add(floatDiv)
389+
.add(aaaaaaaaaaaaaaaaaaaaaaati)
390+
.add(ngAndRestOfText)
384391
.setBackgroundColor(ColorConstants.CYAN)
385392
.setWidth(150)
386393
.setBorder(new SolidBorder(1));
387-
// TODO DEVSIX-1438 bring reviewer's attention: if overflow is set on the div, then forced split occurs.
388-
// is it expected?
389-
floatingParagraph.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
390-
floatingParagraph.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
391-
392-
Text regularText = new Text("regular words regular words regular words regular words regular words regular " +
393-
"words regular words regular words regular words");
394-
Paragraph regularParagraph = new Paragraph(regularText)
395-
.setBackgroundColor(ColorConstants.MAGENTA);
396-
397-
Div div = new Div()
398-
.add(floatingParagraph)
399-
.add(regularParagraph)
400-
.setMaxWidth(300)
401-
.setHeight(300)
402-
.setBackgroundColor(ColorConstants.YELLOW);
403-
404-
div.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
405394

406-
doc.add(div);
395+
p.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
396+
doc.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
407397

398+
doc.add(p);
408399
doc.close();
409400

410401
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
411402
}
412403

413404
@Test
414-
public void wordSplitRenderersWithFloatingElementInsertedInBetween() throws IOException, InterruptedException {
415-
String outFileName = destinationFolder + "wordSplitRenderersWithFloatingElementInsertedInBetween.pdf";
416-
String cmpFileName = sourceFolder + "cmp_wordSplitRenderersWithFloatingElementInsertedInBetween.pdf";
405+
public void wordSplitRenderersWithNotFittingFloatingElementInBetween() throws IOException, InterruptedException {
406+
String outFileName = destinationFolder + "wordSplitRenderersWithNotFittingFloatingElementInBetween.pdf";
407+
String cmpFileName = sourceFolder + "cmp_wordSplitRenderersWithNotFittingFloatingElementInBetween.pdf";
417408

418409
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
419410
Document doc = new Document(pdfDocument);
@@ -437,7 +428,6 @@ public void wordSplitRenderersWithFloatingElementInsertedInBetween() throws IOEx
437428
.setWidth(150)
438429
.setBorder(new SolidBorder(1));
439430

440-
// todo mention that it's crucial to set both overflow and rendering_mode!!!
441431
paragraph.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
442432
paragraph.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
443433

@@ -448,6 +438,139 @@ public void wordSplitRenderersWithFloatingElementInsertedInBetween() throws IOEx
448438
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
449439
}
450440

441+
@Test
442+
public void wordSplitRenderersWithFittingFloatingInBetweenInSecondWord() throws IOException, InterruptedException {
443+
String outFileName = destinationFolder + "wordSplitRenderersWithFittingFloatingInBetweenInSecondWord.pdf";
444+
String cmpFileName = sourceFolder + "cmp_wordSplitRenderersWithFittingFloatingInBetweenInSecondWord.pdf";
445+
446+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
447+
Document doc = new Document(pdfDocument);
448+
449+
doc.setFontSize(20);
450+
451+
Text itsAndSpace = new Text("It's ");
452+
Text reg = new Text("reg").setFontColor(ColorConstants.LIGHT_GRAY);
453+
Text ul = new Text("ul").setFontColor(ColorConstants.DARK_GRAY);
454+
Text aaaaaaaaaaaaaaaaaaaaaaati = new Text("aaaaaaaaaaaaaaaaaaaaaaati").setFontColor(ColorConstants.GRAY);
455+
Text ngAndRestOfText = new Text("ng overflow text renderers with floating elements between them")
456+
.setFontColor(ColorConstants.RED);
457+
458+
459+
Div floatDiv = new Div().setWidth(20).setHeight(60)
460+
.setBackgroundColor(ColorConstants.LIGHT_GRAY)
461+
.setBorder(new SolidBorder(2));
462+
floatDiv.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
463+
464+
Paragraph p = new Paragraph()
465+
.add(itsAndSpace)
466+
.add(reg)
467+
.add(ul)
468+
.add(floatDiv)
469+
.add(aaaaaaaaaaaaaaaaaaaaaaati)
470+
.add(ngAndRestOfText)
471+
.setBackgroundColor(ColorConstants.CYAN)
472+
.setWidth(150)
473+
.setBorder(new SolidBorder(1));
474+
475+
p.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
476+
doc.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
477+
478+
doc.add(p);
479+
480+
doc.close();
481+
482+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
483+
}
484+
485+
@Test
486+
public void wordSplitRenderersWithOverflowedFloatingElementInBetween() throws IOException, InterruptedException {
487+
String outFileName = destinationFolder + "wordSplitRenderersWithOverflowedFloatingElementInBetween.pdf";
488+
String cmpFileName = sourceFolder + "cmp_wordSplitRenderersWithOverflowedFloatingElementInBetween.pdf";
489+
490+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
491+
Document doc = new Document(pdfDocument);
492+
493+
doc.setFontSize(20);
494+
495+
Text reg = new Text("reg").setFontColor(ColorConstants.LIGHT_GRAY);
496+
Text ul = new Text("ul").setFontColor(ColorConstants.DARK_GRAY);
497+
Text aaaaaaaaaaaaaaaaaaaaaaati = new Text("aaaaaaaaaaaaaaaaaaaaaaati").setFontColor(ColorConstants.GRAY);
498+
Text ngAndRestOfText = new Text("ng overflow text renderers with floating elements between them")
499+
.setFontColor(ColorConstants.RED);
500+
501+
502+
Div floatDiv = new Div().setWidth(20).setHeight(60)
503+
.setBackgroundColor(ColorConstants.LIGHT_GRAY)
504+
.setBorder(new SolidBorder(2));
505+
floatDiv.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
506+
507+
Paragraph p = new Paragraph()
508+
.add(reg)
509+
.add(ul)
510+
.add(aaaaaaaaaaaaaaaaaaaaaaati)
511+
.add(floatDiv)
512+
.add(ngAndRestOfText)
513+
.setBackgroundColor(ColorConstants.CYAN)
514+
.setWidth(150)
515+
.setBorder(new SolidBorder(1));
516+
517+
p.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
518+
doc.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
519+
520+
doc.add(p);
521+
doc.close();
522+
523+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
524+
}
525+
526+
@Test
527+
public void wordSplitAcrossMutipleTextRenderersWithinFloatingContainer() throws IOException, InterruptedException {
528+
String outFileName = destinationFolder + "wordSplitAcrossMutipleTextRenderersWithinFloatingContainer.pdf";
529+
String cmpFileName = sourceFolder + "cmp_wordSplitAcrossMutipleTextRenderersWithinFloatingContainer.pdf";
530+
531+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
532+
Document doc = new Document(pdfDocument);
533+
534+
doc.setFontSize(20);
535+
536+
Text oooooooooover = new Text("oooooooooover")
537+
.setFontColor(ColorConstants.LIGHT_GRAY);
538+
Text flooooo = new Text("flooooo")
539+
.setFontColor(ColorConstants.GRAY);
540+
Text ooooowNextWords = new Text("ooooow next words")
541+
.setFontColor(ColorConstants.DARK_GRAY);
542+
543+
Paragraph floatingParagraph = new Paragraph()
544+
.add(oooooooooover)
545+
.add(flooooo)
546+
.add(ooooowNextWords)
547+
.setBackgroundColor(ColorConstants.CYAN)
548+
.setWidth(150)
549+
.setBorder(new SolidBorder(1));
550+
floatingParagraph.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
551+
floatingParagraph.setProperty(Property.FLOAT, FloatPropertyValue.RIGHT);
552+
553+
Text regularText = new Text("regular words regular words regular words regular words regular words regular " +
554+
"words regular words regular words regular words");
555+
Paragraph regularParagraph = new Paragraph(regularText)
556+
.setBackgroundColor(ColorConstants.MAGENTA);
557+
558+
Div div = new Div()
559+
.add(floatingParagraph)
560+
.add(regularParagraph)
561+
.setMaxWidth(300)
562+
.setHeight(300)
563+
.setBackgroundColor(ColorConstants.YELLOW);
564+
565+
div.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
566+
567+
doc.add(div);
568+
569+
doc.close();
570+
571+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
572+
}
573+
451574
@Test
452575
public void wordSplitAcrossRenderersWithPrecedingImageRenderer() throws IOException, InterruptedException {
453576
String outFileName = destinationFolder + "wordSplitAcrossRenderersWithPrecedingImageRenderer.pdf";

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

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public void oneThaiWordSplitAcrossMultipleRenderersGetIndexAndLayoutResult() thr
332332

333333
LineRenderer.LastFittingChildRendererData lastFittingChildRendererData = lineRenderer
334334
.getIndexAndLayoutResultOfTheLastTextRendererContainingSpecialScripts(THAI_WORD.length() + 1,
335-
specialScriptLayoutResults, false, new ArrayList<IRenderer>(), true);
335+
specialScriptLayoutResults, false, true);
336336

337337
Assert.assertEquals(5, lastFittingChildRendererData.childIndex);
338338
Assert.assertEquals(LayoutResult.NOTHING, lastFittingChildRendererData.childLayoutResult.getStatus());
@@ -367,7 +367,7 @@ public void multipleFloatsFollowedByUnfittingThaiRenderer() throws IOException {
367367

368368
LineRenderer.LastFittingChildRendererData lastFittingChildRendererData = lineRenderer
369369
.getIndexAndLayoutResultOfTheLastTextRendererContainingSpecialScripts(indexOfThaiRenderer,
370-
specialScriptLayoutResults, false, new ArrayList<IRenderer>(), true);
370+
specialScriptLayoutResults, false, true);
371371

372372
Assert.assertEquals(indexOfThaiRenderer, lastFittingChildRendererData.childIndex);
373373
Assert.assertEquals(LayoutResult.NOTHING, lastFittingChildRendererData.childLayoutResult.getStatus());
@@ -407,7 +407,7 @@ public void trailingRightSideSpacesGetIndexAndLayoutResult() throws IOException
407407

408408
LineRenderer.LastFittingChildRendererData lastFittingChildRendererData = lineRenderer
409409
.getIndexAndLayoutResultOfTheLastTextRendererContainingSpecialScripts(THAI_WORD.length() - 1,
410-
specialScriptLayoutResults, false, new ArrayList<IRenderer>(), true);
410+
specialScriptLayoutResults, false, true);
411411

412412
Assert.assertEquals(THAI_WORD.length() - 1, lastFittingChildRendererData.childIndex);
413413
Assert.assertEquals(specialScriptLayoutResults.get(THAI_WORD.length() - 1), lastFittingChildRendererData.childLayoutResult);
@@ -784,35 +784,6 @@ public void curWidthLayoutResultPartial() {
784784
Assert.assertEquals(widthOfNewPartialResult + simpleWidth, decrement, 0.00001);
785785
}
786786

787-
@Test
788-
public void updateFloatsOverflowedToNextLine() {
789-
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
790-
Document document = new Document(pdfDocument);
791-
792-
LineRenderer lineRenderer = new LineRenderer();
793-
lineRenderer.setParent(document.getRenderer());
794-
795-
List<IRenderer> floatsOverflowedToNextLineIRenderers = new ArrayList<IRenderer>();
796-
Set<Integer> indicesOfFloats = new HashSet<Integer>();
797-
798-
IRenderer onlyFloatToRemain;
799-
800-
for (int i = 0; i < 6; i++) {
801-
TextRenderer textRenderer = new TextRenderer(new Text("text"));
802-
if (i % 2 == 0) {
803-
floatsOverflowedToNextLineIRenderers.add(textRenderer);
804-
indicesOfFloats.add(i);
805-
}
806-
lineRenderer.addChild(textRenderer);
807-
}
808-
809-
onlyFloatToRemain = lineRenderer.getChildRenderers().get(0);
810-
811-
lineRenderer.updateFloatsOverflowedToNextLine(floatsOverflowedToNextLineIRenderers, indicesOfFloats,1);
812-
Assert.assertEquals(1, floatsOverflowedToNextLineIRenderers.size());
813-
Assert.assertEquals(onlyFloatToRemain, floatsOverflowedToNextLineIRenderers.get(0));
814-
}
815-
816787
@Test
817788
public void possibleBreakWithinActualText() throws IOException {
818789
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));

0 commit comments

Comments
 (0)