Skip to content

Commit 2accef7

Browse files
Support overflow-wrap: break-word/anywhere in case of the not fitting single glyph
DEVSIX-1438
1 parent 7645d53 commit 2accef7

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,10 +1871,12 @@ LastFittingChildRendererData getIndexAndLayoutResultOfTheLastTextRendererWithNoS
18711871
}
18721872

18731873
if (lastAnalyzedTextLayoutResult == null) {
1874-
OverflowWrapPropertyValue overflowWrapPropertyValue =
1874+
OverflowWrapPropertyValue overflowWrapValue =
18751875
childRenderers.get(childPos).<OverflowWrapPropertyValue>getProperty(Property.OVERFLOW_WRAP);
1876-
if (overflowWrapPropertyValue == OverflowWrapPropertyValue.ANYWHERE
1877-
|| overflowWrapPropertyValue == OverflowWrapPropertyValue.BREAK_WORD
1876+
boolean overflowWrapNotNormal = overflowWrapValue == OverflowWrapPropertyValue.ANYWHERE
1877+
|| overflowWrapValue == OverflowWrapPropertyValue.BREAK_WORD;
1878+
if (overflowWrapNotNormal
1879+
&& textSequenceLayoutResults.get(lastAnalyzedTextRenderer).getStatus() != LayoutResult.NOTHING
18781880
|| isOverflowFit) {
18791881
lastAnalyzedTextRenderer = childPos;
18801882
lastAnalyzedTextLayoutResult = textSequenceLayoutResults.get(lastAnalyzedTextRenderer);
@@ -1949,10 +1951,11 @@ LastFittingChildRendererData getIndexAndLayoutResultOfTheLastTextRendererWithNoS
19491951
// otherwise return null as a flag to move forward across this.childRenderers
19501952
// till the end of the unbreakable word
19511953
if (status == SpecialScriptsContainingSequenceStatus.FORCED_SPLIT) {
1952-
OverflowWrapPropertyValue overflowWrapPropertyValue =
1954+
OverflowWrapPropertyValue overflowWrapValue =
19531955
childRenderers.get(childPos).<OverflowWrapPropertyValue>getProperty(Property.OVERFLOW_WRAP);
1954-
if (overflowWrapPropertyValue == OverflowWrapPropertyValue.ANYWHERE
1955-
|| overflowWrapPropertyValue == OverflowWrapPropertyValue.BREAK_WORD
1956+
boolean overflowWrapNotNormal = overflowWrapValue == OverflowWrapPropertyValue.ANYWHERE
1957+
|| overflowWrapValue == OverflowWrapPropertyValue.BREAK_WORD;
1958+
if (overflowWrapNotNormal && childPosLayoutResult.getStatus() != LayoutResult.NOTHING
19561959
|| isOverflowFit) {
19571960
if (childPosLayoutResult.getStatus() != LayoutResult.NOTHING) {
19581961
returnLayoutResult = childPosLayoutResult;

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ public LayoutResult layout(LayoutContext layoutContext) {
183183
OverflowPropertyValue overflowX = this.parent.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X);
184184

185185
OverflowWrapPropertyValue overflowWrap = this.<OverflowWrapPropertyValue>getProperty(Property.OVERFLOW_WRAP);
186-
if (overflowWrap == OverflowWrapPropertyValue.ANYWHERE
187-
|| overflowWrap == OverflowWrapPropertyValue.BREAK_WORD) {
186+
boolean overflowWrapNotNormal = overflowWrap == OverflowWrapPropertyValue.ANYWHERE
187+
|| overflowWrap == OverflowWrapPropertyValue.BREAK_WORD;
188+
if (overflowWrapNotNormal) {
188189
overflowX = OverflowPropertyValue.FIT;
189190
}
190191

@@ -378,9 +379,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
378379
&& firstCharacterWhichExceedsAllowedWidth == -1
379380
|| ind == specialScriptFirstNotFittingIndex) {
380381
firstCharacterWhichExceedsAllowedWidth = ind;
381-
if (TextUtil.isSpaceOrWhitespace(text.get(ind))) {
382-
containsPossibleBreak = true;
383-
wordBreakGlyphAtLineEnding = currentGlyph;
382+
boolean spaceOrWhitespace = TextUtil.isSpaceOrWhitespace(text.get(ind));
383+
OverflowPropertyValue parentOverflowX = parent.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X);
384+
if (spaceOrWhitespace || overflowWrapNotNormal && !isOverflowFit(parentOverflowX)) {
385+
if (spaceOrWhitespace) {
386+
containsPossibleBreak = true;
387+
wordBreakGlyphAtLineEnding = currentGlyph;
388+
}
384389
if (ind == firstPrintPos) {
385390
forcePartialSplitOnFirstChar = true;
386391
firstCharacterWhichExceedsAllowedWidth = ind + 1;
@@ -1242,7 +1247,7 @@ public static float[] calculateAscenderDescender(PdfFont font, RenderingMode mod
12421247
}
12431248
return new float[] {ascender, descender};
12441249
}
1245-
1250+
12461251
List<int[]> getReversedRanges() {
12471252
return reversedRanges;
12481253
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
import com.itextpdf.layout.element.Text;
4444
import com.itextpdf.layout.property.FloatPropertyValue;
4545
import com.itextpdf.layout.property.OverflowPropertyValue;
46+
import com.itextpdf.layout.property.OverflowWrapPropertyValue;
4647
import com.itextpdf.layout.property.Property;
4748
import com.itextpdf.layout.property.RenderingMode;
4849
import com.itextpdf.layout.property.TextAlignment;
@@ -657,4 +658,25 @@ public void minMaxWidthWordSplitAcrossMultipleTextRenderers() throws IOException
657658

658659
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
659660
}
661+
662+
@Test
663+
public void overflowWrapBreakWordWithOverflowXTest() throws IOException, InterruptedException {
664+
String outFileName = destinationFolder + "overflowWrapBreakWordWithOverflowXTest.pdf";
665+
String cmpFileName = sourceFolder + "cmp_overflowWrapBreakWordWithOverflowXTest.pdf";
666+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
667+
Document doc = new Document(pdfDocument);
668+
doc.setFontSize(40);
669+
Text text = new Text("wow");
670+
Paragraph paragraph = new Paragraph()
671+
.add(text)
672+
.setBackgroundColor(ColorConstants.YELLOW)
673+
.setWidth(10)
674+
.setBorder(new SolidBorder(1));
675+
paragraph.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
676+
paragraph.setProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.BREAK_WORD);
677+
paragraph.setProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
678+
doc.add(paragraph);
679+
doc.close();
680+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder));
681+
}
660682
}

0 commit comments

Comments
 (0)