Skip to content

Commit e9aa51d

Browse files
Support overflow-wrap: break-word/anywhere in case of the not fitting single glyph
DEVSIX-1438 Autoported commit. Original commit hash: [2accef71ca]
1 parent 14cb2eb commit e9aa51d

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

itext.tests/itext.layout.tests/itext/layout/renderer/TextRendererIntegrationTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,24 @@ public virtual void MinMaxWidthWordSplitAcrossMultipleTextRenderers() {
406406
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
407407
));
408408
}
409+
410+
[NUnit.Framework.Test]
411+
public virtual void OverflowWrapBreakWordWithOverflowXTest() {
412+
String outFileName = destinationFolder + "overflowWrapBreakWordWithOverflowXTest.pdf";
413+
String cmpFileName = sourceFolder + "cmp_overflowWrapBreakWordWithOverflowXTest.pdf";
414+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
415+
Document doc = new Document(pdfDocument);
416+
doc.SetFontSize(40);
417+
Text text = new Text("wow");
418+
Paragraph paragraph = new Paragraph().Add(text).SetBackgroundColor(ColorConstants.YELLOW).SetWidth(10).SetBorder
419+
(new SolidBorder(1));
420+
paragraph.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
421+
paragraph.SetProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.BREAK_WORD);
422+
paragraph.SetProperty(Property.RENDERING_MODE, RenderingMode.HTML_MODE);
423+
doc.Add(paragraph);
424+
doc.Close();
425+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
426+
));
427+
}
409428
}
410429
}

itext/itext.layout/itext/layout/renderer/LineRenderer.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,10 +1956,12 @@ internal virtual LineRenderer.LastFittingChildRendererData GetIndexAndLayoutResu
19561956
}
19571957
}
19581958
if (lastAnalyzedTextLayoutResult == null) {
1959-
OverflowWrapPropertyValue? overflowWrapPropertyValue = childRenderers[childPos].GetProperty<OverflowWrapPropertyValue?
1959+
OverflowWrapPropertyValue? overflowWrapValue = childRenderers[childPos].GetProperty<OverflowWrapPropertyValue?
19601960
>(Property.OVERFLOW_WRAP);
1961-
if (overflowWrapPropertyValue == OverflowWrapPropertyValue.ANYWHERE || overflowWrapPropertyValue == OverflowWrapPropertyValue
1962-
.BREAK_WORD || isOverflowFit) {
1961+
bool overflowWrapNotNormal = overflowWrapValue == OverflowWrapPropertyValue.ANYWHERE || overflowWrapValue
1962+
== OverflowWrapPropertyValue.BREAK_WORD;
1963+
if (overflowWrapNotNormal && textSequenceLayoutResults.Get(lastAnalyzedTextRenderer).GetStatus() != LayoutResult
1964+
.NOTHING || isOverflowFit) {
19631965
lastAnalyzedTextRenderer = childPos;
19641966
lastAnalyzedTextLayoutResult = textSequenceLayoutResults.Get(lastAnalyzedTextRenderer);
19651967
}
@@ -2038,10 +2040,11 @@ internal virtual LineRenderer.LastFittingChildRendererData GetIndexAndLayoutResu
20382040
// otherwise return null as a flag to move forward across this.childRenderers
20392041
// till the end of the unbreakable word
20402042
if (status == LineRenderer.SpecialScriptsContainingSequenceStatus.FORCED_SPLIT) {
2041-
OverflowWrapPropertyValue? overflowWrapPropertyValue = childRenderers[childPos].GetProperty<OverflowWrapPropertyValue?
2043+
OverflowWrapPropertyValue? overflowWrapValue = childRenderers[childPos].GetProperty<OverflowWrapPropertyValue?
20422044
>(Property.OVERFLOW_WRAP);
2043-
if (overflowWrapPropertyValue == OverflowWrapPropertyValue.ANYWHERE || overflowWrapPropertyValue == OverflowWrapPropertyValue
2044-
.BREAK_WORD || isOverflowFit) {
2045+
bool overflowWrapNotNormal = overflowWrapValue == OverflowWrapPropertyValue.ANYWHERE || overflowWrapValue
2046+
== OverflowWrapPropertyValue.BREAK_WORD;
2047+
if (overflowWrapNotNormal && childPosLayoutResult.GetStatus() != LayoutResult.NOTHING || isOverflowFit) {
20452048
if (childPosLayoutResult.GetStatus() != LayoutResult.NOTHING) {
20462049
returnLayoutResult = childPosLayoutResult;
20472050
}

itext/itext.layout/itext/layout/renderer/TextRenderer.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
165165
OverflowPropertyValue? overflowX = this.parent.GetProperty<OverflowPropertyValue?>(Property.OVERFLOW_X);
166166
OverflowWrapPropertyValue? overflowWrap = this.GetProperty<OverflowWrapPropertyValue?>(Property.OVERFLOW_WRAP
167167
);
168-
if (overflowWrap == OverflowWrapPropertyValue.ANYWHERE || overflowWrap == OverflowWrapPropertyValue.BREAK_WORD
169-
) {
168+
bool overflowWrapNotNormal = overflowWrap == OverflowWrapPropertyValue.ANYWHERE || overflowWrap == OverflowWrapPropertyValue
169+
.BREAK_WORD;
170+
if (overflowWrapNotNormal) {
170171
overflowX = OverflowPropertyValue.FIT;
171172
}
172173
IList<Rectangle> floatRendererAreas = layoutContext.GetFloatRendererAreas();
@@ -336,9 +337,13 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
336337
) > layoutBox.GetWidth() - currentLineWidth && firstCharacterWhichExceedsAllowedWidth == -1 || ind ==
337338
specialScriptFirstNotFittingIndex) {
338339
firstCharacterWhichExceedsAllowedWidth = ind;
339-
if (iText.IO.Util.TextUtil.IsSpaceOrWhitespace(text.Get(ind))) {
340-
containsPossibleBreak = true;
341-
wordBreakGlyphAtLineEnding = currentGlyph;
340+
bool spaceOrWhitespace = iText.IO.Util.TextUtil.IsSpaceOrWhitespace(text.Get(ind));
341+
OverflowPropertyValue? parentOverflowX = parent.GetProperty<OverflowPropertyValue?>(Property.OVERFLOW_X);
342+
if (spaceOrWhitespace || overflowWrapNotNormal && !IsOverflowFit(parentOverflowX)) {
343+
if (spaceOrWhitespace) {
344+
containsPossibleBreak = true;
345+
wordBreakGlyphAtLineEnding = currentGlyph;
346+
}
342347
if (ind == firstPrintPos) {
343348
forcePartialSplitOnFirstChar = true;
344349
firstCharacterWhichExceedsAllowedWidth = ind + 1;
@@ -886,7 +891,7 @@ public override void Draw(DrawContext drawContext) {
886891
if (horizontalScaling != null && horizontalScaling != 1) {
887892
canvas.SetHorizontalScaling((float)horizontalScaling * 100);
888893
}
889-
GlyphLine.IGlyphLineFilter filter = new _IGlyphLineFilter_948();
894+
GlyphLine.IGlyphLineFilter filter = new _IGlyphLineFilter_953();
890895
bool appearanceStreamLayout = true.Equals(GetPropertyAsBoolean(Property.APPEARANCE_STREAM_LAYOUT));
891896
if (GetReversedRanges() != null) {
892897
bool writeReversedChars = !appearanceStreamLayout;
@@ -948,8 +953,8 @@ public override void Draw(DrawContext drawContext) {
948953
}
949954
}
950955

951-
private sealed class _IGlyphLineFilter_948 : GlyphLine.IGlyphLineFilter {
952-
public _IGlyphLineFilter_948() {
956+
private sealed class _IGlyphLineFilter_953 : GlyphLine.IGlyphLineFilter {
957+
public _IGlyphLineFilter_953() {
953958
}
954959

955960
public bool Accept(Glyph glyph) {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7645d539db59517ed06defbbe092703bb8b4a3a0
1+
2accef71ca21aa49f48fe620b8e4fa127459a532

0 commit comments

Comments
 (0)