Skip to content

Commit

Permalink
Fix line endings handling by BestMatchFontSelectorStrategy
Browse files Browse the repository at this point in the history
DEVSIX-8688

Autoported commit.
Original commit hash: [d7b728845]
  • Loading branch information
AnhelinaM authored and iText-CI committed Oct 25, 2024
1 parent 2d0c66a commit 4d88123
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 22 deletions.
15 changes: 15 additions & 0 deletions itext.tests/itext.layout.tests/itext/layout/HyphenateLayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,20 @@ public virtual void HyphenSymbolTest01() {
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
, "diff"));
}

[NUnit.Framework.Test]
public virtual void WordsBreakingWordSoftHyphenTest() {
String outFileName = destinationFolder + "wordsBreakingWordSoftHyphenTest.pdf";
String cmpFileName = sourceFolder + "cmp_wordsBreakingWordSoftHyphenTest.pdf";
String SOFT_HYPHEN = "\u00AD";
String text = "Soft hyphen at the mid" + SOFT_HYPHEN + "dle,\nhyphen at the end: abcdef" + SOFT_HYPHEN + "ghijklmnopqrst\n"
+ SOFT_HYPHEN + "hyphen at the beginning.";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName)))) {
document.Add(new Paragraph(text).SetWidth(150).SetBorder(new SolidBorder(1)).SetHyphenation(new HyphenationConfig
(1, 1)));
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public virtual void TwoDiacriticsInRowTest() {
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("L with accent: \u004f\u0301\u0302 abc");
NUnit.Framework.Assert.AreEqual(3, result.Count);
NUnit.Framework.Assert.AreEqual("L with accent: ", result[0].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("\u004f\u0301\u0302", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(" abc", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("\u004f\u0301\u0302 ", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("abc", result[2].GetFirst().ToString());
// Diacritics and symbol were separated, but the font is the same
NUnit.Framework.Assert.AreEqual(result[0].GetSecond(), result[2].GetSecond());
}
Expand All @@ -49,8 +49,8 @@ public virtual void OneDiacriticTest() {
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("L with accent: \u004f\u0302 abc");
NUnit.Framework.Assert.AreEqual(3, result.Count);
NUnit.Framework.Assert.AreEqual("L with accent: ", result[0].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("\u004f\u0302", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(" abc", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("\u004f\u0302 ", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("abc", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreNotEqual(result[0].GetSecond(), result[1].GetSecond());
}

Expand All @@ -75,8 +75,8 @@ public virtual void DiacriticFontDoesnotContainPreviousSymbolTest() {
NUnit.Framework.Assert.AreEqual(6, result.Count);
NUnit.Framework.Assert.AreEqual("Ми", result[0].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("\u0301", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("ръ", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(" (mír", result[3].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("ръ (", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("mír", result[3].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("ə", result[4].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(")", result[5].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(result[0].GetSecond(), result[2].GetSecond());
Expand All @@ -100,8 +100,8 @@ public virtual void SurrogatePairsTest() {
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("text \uD800\uDF10\uD800\uDF00\uD800\uDF11 text"
);
NUnit.Framework.Assert.AreEqual(3, result.Count);
NUnit.Framework.Assert.AreEqual("text", result[0].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(" \uD800\uDF10\uD800\uDF00\uD800\uDF11 ", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("text ", result[0].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("\uD800\uDF10\uD800\uDF00\uD800\uDF11 ", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("text", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(result[0].GetSecond(), result[2].GetSecond());
}
Expand All @@ -122,13 +122,21 @@ public virtual void ThreeFontWithSpacesTest() {
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithLimitedThreeFonts(new BestMatchFontSelectorStrategy.BestMatchFontSelectorStrategyFactory
());
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines(" axadefa ");
NUnit.Framework.Assert.AreEqual(6, result.Count);
NUnit.Framework.Assert.AreEqual(5, result.Count);
NUnit.Framework.Assert.AreEqual(" a", result[0].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("x", result[1].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("a", result[2].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("def", result[3].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("a", result[4].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual(" ", result[5].GetFirst().ToString());
NUnit.Framework.Assert.AreEqual("a ", result[4].GetFirst().ToString());
}

[NUnit.Framework.Test]
public virtual void WindowsLineEndingsTest() {
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithFreeSans(new BestMatchFontSelectorStrategy.BestMatchFontSelectorStrategyFactory
());
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("Hello\r\n World!\r\n ");
NUnit.Framework.Assert.AreEqual(1, result.Count);
NUnit.Framework.Assert.AreEqual("Hello\r\n World!\r\n ", result[0].GetFirst().ToString());
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public virtual IList<Tuple2<GlyphLine, PdfFont>> GetGlyphLines(String text) {
if (codePoint > 0xFFFF) {
i++;
}
if (IsCurrentFontCheckRequired() && (i != indexDiacritic - 1)) {
if (IsCurrentFontCheckRequired() && (i != indexDiacritic - 1) && !iText.IO.Util.TextUtil.IsWhitespaceOrNonPrintable
(codePoint)) {
if (currentFont != MatchFont(codePoint, fontSelector, fontProvider, additionalFonts)) {
breakRequested = true;
}
Expand Down Expand Up @@ -191,8 +192,7 @@ protected internal virtual PdfFont MatchFont(int codePoint, FontSelector fontSel
foreach (FontInfo fontInfo in fontSelector.GetFonts()) {
if (fontInfo.GetFontUnicodeRange().Contains(codePoint)) {
PdfFont temptFont = GetPdfFont(fontInfo, fontProvider, additionalFonts);
Glyph glyph = temptFont.GetGlyph(codePoint);
if (null != glyph && 0 != glyph.GetCode()) {
if (temptFont.ContainsGlyph(codePoint)) {
matchedFont = temptFont;
break;
}
Expand Down
20 changes: 13 additions & 7 deletions itext/itext.layout/itext/layout/renderer/TextRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,26 @@ public override LayoutResult Layout(LayoutContext layoutContext) {
for (int i = hyph.Length() - 1; i >= 0; i--) {
String pre = hyph.GetPreHyphenText(i);
String pos = hyph.GetPostHyphenText(i);
float currentHyphenationChoicePreTextWidth = GetGlyphLineWidth(ConvertToGlyphLine(text.ToUnicodeString(currentTextPos
, wordBounds[0]) + pre + hyphenationConfig.GetHyphenSymbol()), fontSize.GetValue(), hScale, characterSpacing
, wordSpacing);
char hyphen = hyphenationConfig.GetHyphenSymbol();
String glyphLine = text.ToUnicodeString(currentTextPos, wordBounds[0]) + pre;
if (font.ContainsGlyph(hyphen)) {
glyphLine += hyphen;
}
float currentHyphenationChoicePreTextWidth = GetGlyphLineWidth(ConvertToGlyphLine(glyphLine), fontSize.GetValue
(), hScale, characterSpacing, wordSpacing);
if (currentLineWidth + currentHyphenationChoicePreTextWidth + italicSkewAddition + boldSimulationAddition
<= layoutBox.GetWidth()) {
hyphenationApplied = true;
if (line.GetStart() == -1) {
line.SetStart(currentTextPos);
}
line.SetEnd(Math.Max(line.GetEnd(), wordBounds[0] + pre.Length));
GlyphLine lineCopy = line.Copy(line.GetStart(), line.GetEnd());
lineCopy.Add(font.GetGlyph(hyphenationConfig.GetHyphenSymbol()));
lineCopy.SetEnd(lineCopy.GetEnd() + 1);
line = lineCopy;
if (font.ContainsGlyph(hyphen)) {
GlyphLine lineCopy = line.Copy(line.GetStart(), line.GetEnd());
lineCopy.Add(font.GetGlyph(hyphen));
lineCopy.SetEnd(lineCopy.GetEnd() + 1);
line = lineCopy;
}
// TODO DEVSIX-7010 recalculate line properties in case of word hyphenation.
// These values are based on whole word. Recalculate properly based on hyphenated part.
currentLineAscender = Math.Max(currentLineAscender, nonBreakablePartMaxAscender);
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238107cd5532d937fc246b97f60c8f1599786f9d
d7b7288459c53470547f639cf5386cb2636c2eca

0 comments on commit 4d88123

Please sign in to comment.