Skip to content

Commit 81dbdce

Browse files
author
Vitali Prudnikovich
committed
Fix text wrap for border cases when next symbol almost fits on the line
DEVSIX-7146
1 parent e73f534 commit 81dbdce

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ public LayoutResult layout(LayoutContext layoutContext) {
378378
xAdvance = scaleXAdvance(xAdvance, fontSize.getValue(), hScale) / TEXT_SPACE_COEFF;
379379
}
380380

381-
float potentialWidth =
381+
final float potentialWidth =
382382
nonBreakablePartFullWidth + glyphWidth + xAdvance + italicSkewAddition + boldSimulationAddition;
383-
if (!noSoftWrap && (potentialWidth > layoutBox.getWidth() - currentLineWidth + EPS)
384-
&& firstCharacterWhichExceedsAllowedWidth == -1
383+
final boolean symbolNotFitOnLine = potentialWidth > layoutBox.getWidth() - currentLineWidth + EPS;
384+
if ((!noSoftWrap && symbolNotFitOnLine && firstCharacterWhichExceedsAllowedWidth == -1)
385385
|| ind == specialScriptFirstNotFittingIndex) {
386386
firstCharacterWhichExceedsAllowedWidth = ind;
387387
boolean spaceOrWhitespace = TextUtil.isSpaceOrWhitespace(text.get(ind));
@@ -423,8 +423,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
423423

424424
previousCharPos = ind;
425425

426-
if (!noSoftWrap
427-
&& nonBreakablePartFullWidth + italicSkewAddition + boldSimulationAddition > layoutBox.getWidth()
426+
if (!noSoftWrap && symbolNotFitOnLine
428427
&& (0 == nonBreakingHyphenRelatedChunkWidth || ind + 1 == text.end || !glyphBelongsToNonBreakingHyphenRelatedChunk(text, ind + 1))) {
429428
if (isOverflowFit(overflowX)) {
430429
// we have extracted all the information we wanted and we do not want to continue.

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.kernel.colors.ColorConstants;
4747
import com.itextpdf.kernel.font.PdfFont;
4848
import com.itextpdf.kernel.font.PdfFontFactory;
49+
import com.itextpdf.kernel.geom.PageSize;
4950
import com.itextpdf.kernel.pdf.PdfDocument;
5051
import com.itextpdf.kernel.pdf.PdfWriter;
5152
import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
@@ -59,6 +60,7 @@ This file is part of the iText (R) project.
5960
import com.itextpdf.layout.properties.Property;
6061
import com.itextpdf.test.ExtendedITextTest;
6162
import com.itextpdf.test.annotations.type.IntegrationTest;
63+
6264
import org.junit.Assert;
6365
import org.junit.BeforeClass;
6466
import org.junit.Test;
@@ -403,4 +405,41 @@ public void leadingAndFloatInTextTest() throws IOException, InterruptedException
403405

404406
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
405407
}
408+
409+
@Test
410+
public void textWrappingEpsilonTest() throws IOException, InterruptedException {
411+
String outFileName = destinationFolder + "textWrappingEpsilon.pdf";
412+
String cmpFileName = sourceFolder + "cmp_textWrappingEpsilon.pdf";
413+
414+
PdfWriter writer = new PdfWriter(outFileName);
415+
PdfDocument pdfDoc = new PdfDocument(writer);
416+
Document document = new Document(pdfDoc);
417+
418+
// Play with margins to make AbstractRenderer.EPS important for wrapping behavior
419+
document.setLeftMargin(250.0F);
420+
document.setRightMargin(238.727F);
421+
pdfDoc.setDefaultPageSize(PageSize.LETTER);
422+
PdfFont font = PdfFontFactory.createFont(sourceFolder + "../fonts/Open_Sans/OpenSans-Regular.ttf");
423+
424+
String text1 = "First line of some text ";
425+
String text2 = "Second line of some text";
426+
427+
Text text = new Text(text1);
428+
text.setFont(font);
429+
text.setFontSize(9);
430+
Paragraph paragraph = new Paragraph();
431+
paragraph.add(text);
432+
433+
text = new Text(text2);
434+
text.setFont(font);
435+
text.setFontSize(9);
436+
paragraph.add(text);
437+
438+
paragraph.setBackgroundColor(ColorConstants.LIGHT_GRAY);
439+
document.add(paragraph);
440+
document.close();
441+
writer.close();
442+
443+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
444+
}
406445
}

0 commit comments

Comments
 (0)