Skip to content

Commit 152028c

Browse files
introfogiText-CI
authored andcommitted
Fix font selection in case diacritic font doesn't contain previous symbol
DEVSIX-8285 Autoported commit. Original commit hash: [523702a35]
1 parent 7b73cad commit 152028c

File tree

9 files changed

+55
-4
lines changed

9 files changed

+55
-4
lines changed

itext.tests/itext.layout.tests/itext/layout/font/selectorstrategy/BestMatchFontSelectorStrategyTest.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual void OneDiacriticTest() {
5656

5757
[NUnit.Framework.Test]
5858
public virtual void OneDiacriticWithUnsupportedFontTest() {
59-
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithTNR(new FirstMatchFontSelectorStrategy.FirstMathFontSelectorStrategyFactory
59+
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithTNR(new BestMatchFontSelectorStrategy.BestMatchFontSelectorStrategyFactory
6060
());
6161
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("L with accent: \u004f\u0302 abc");
6262
NUnit.Framework.Assert.AreEqual(3, result.Count);
@@ -67,9 +67,25 @@ public virtual void OneDiacriticWithUnsupportedFontTest() {
6767
NUnit.Framework.Assert.AreEqual(result[0].GetSecond(), result[1].GetSecond());
6868
}
6969

70+
[NUnit.Framework.Test]
71+
public virtual void DiacriticFontDoesnotContainPreviousSymbolTest() {
72+
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithNotoSans(new BestMatchFontSelectorStrategy.BestMatchFontSelectorStrategyFactory
73+
());
74+
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("Ми\u0301ръ (mírə)");
75+
NUnit.Framework.Assert.AreEqual(6, result.Count);
76+
NUnit.Framework.Assert.AreEqual("Ми", result[0].GetFirst().ToString());
77+
NUnit.Framework.Assert.AreEqual("\u0301", result[1].GetFirst().ToString());
78+
NUnit.Framework.Assert.AreEqual("ръ", result[2].GetFirst().ToString());
79+
NUnit.Framework.Assert.AreEqual(" (mír", result[3].GetFirst().ToString());
80+
NUnit.Framework.Assert.AreEqual("ə", result[4].GetFirst().ToString());
81+
NUnit.Framework.Assert.AreEqual(")", result[5].GetFirst().ToString());
82+
NUnit.Framework.Assert.AreEqual(result[0].GetSecond(), result[2].GetSecond());
83+
NUnit.Framework.Assert.AreEqual(result[2].GetSecond(), result[3].GetSecond());
84+
}
85+
7086
[NUnit.Framework.Test]
7187
public virtual void OneDiacriticWithOneSupportedFontTest() {
72-
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithFreeSans(new FirstMatchFontSelectorStrategy.FirstMathFontSelectorStrategyFactory
88+
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithFreeSans(new BestMatchFontSelectorStrategy.BestMatchFontSelectorStrategyFactory
7389
());
7490
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("L with accent: \u004f\u0302 abc");
7591
NUnit.Framework.Assert.AreEqual(1, result.Count);

itext.tests/itext.layout.tests/itext/layout/font/selectorstrategy/FirstMatchFontSelectorStrategyTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ public virtual void OneDiacriticTest() {
5050
NUnit.Framework.Assert.AreNotEqual(result[0].GetSecond(), result[1].GetSecond());
5151
}
5252

53+
[NUnit.Framework.Test]
54+
public virtual void DiacriticFontDoesnotContainPreviousSymbolTest() {
55+
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithNotoSans(new FirstMatchFontSelectorStrategy.FirstMathFontSelectorStrategyFactory
56+
());
57+
IList<Tuple2<GlyphLine, PdfFont>> result = strategy.GetGlyphLines("Ми\u0301ръ (mírə)");
58+
NUnit.Framework.Assert.AreEqual(6, result.Count);
59+
NUnit.Framework.Assert.AreEqual("Ми", result[0].GetFirst().ToString());
60+
NUnit.Framework.Assert.AreEqual("\u0301", result[1].GetFirst().ToString());
61+
NUnit.Framework.Assert.AreEqual("ръ (", result[2].GetFirst().ToString());
62+
NUnit.Framework.Assert.AreEqual("mír", result[3].GetFirst().ToString());
63+
NUnit.Framework.Assert.AreEqual("ə", result[4].GetFirst().ToString());
64+
NUnit.Framework.Assert.AreEqual(")", result[5].GetFirst().ToString());
65+
NUnit.Framework.Assert.AreEqual(result[0].GetSecond(), result[2].GetSecond());
66+
NUnit.Framework.Assert.AreEqual(result[2].GetSecond(), result[3].GetSecond());
67+
}
68+
5369
[NUnit.Framework.Test]
5470
public virtual void OneDiacriticWithUnsupportedFontTest() {
5571
IFontSelectorStrategy strategy = FontSelectorTestsUtil.CreateStrategyWithTNR(new FirstMatchFontSelectorStrategy.FirstMathFontSelectorStrategyFactory

itext.tests/itext.layout.tests/itext/layout/font/selectorstrategy/FontSelectorTestsUtil.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public static IFontSelectorStrategy CreateStrategyWithFreeSansAndTNR(IFontSelect
4141
return fontProvider.CreateFontSelectorStrategy(fontFamilies, new FontCharacteristics(), null);
4242
}
4343

44+
public static IFontSelectorStrategy CreateStrategyWithNotoSans(IFontSelectorStrategyFactory factory) {
45+
FontSet fs = new FontSet();
46+
fs.AddFont(FONTS_FOLDER + "NotoKufiArabic-Regular.ttf");
47+
fs.AddFont(FONTS_FOLDER + "NotoSansCJKjp-Regular.otf");
48+
fs.AddFont(FONTS_FOLDER + "NotoSansCherokee-Regular.ttf");
49+
FontProvider fontProvider = new FontProvider(fs, StandardFontFamilies.TIMES);
50+
fontProvider.SetFontSelectorStrategyFactory(factory);
51+
IList<String> fontFamilies = new List<String>();
52+
fontFamilies.Add("random");
53+
return fontProvider.CreateFontSelectorStrategy(fontFamilies, new FontCharacteristics(), null);
54+
}
55+
4456
public static IFontSelectorStrategy CreateStrategyWithTNR(IFontSelectorStrategyFactory factory) {
4557
FontSet fs = new FontSet();
4658
fs.AddFont(StandardFonts.TIMES_ROMAN);

itext.tests/itext.layout.tests/resources/itext/layout/fonts/NOTICE.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Please notice that the following fonts are used with the mentioned below license
1111
* NotoSansThai-Regular - SIL Open Font License v1.1
1212
* Puritan2 - SIL Open Font License v1.1
1313
* NotoSansOldItalic-Regular.ttf - SIL Open Font License v1.1. Based on commit 20bc5918912503bc1537a407a694738c33c048aa (07.31.2020) from "https://github.com/googlefonts/noto-fonts"
14+
* NotoSansCJKjp-Regular.otf - SIL Open Font License v1.1
15+
* NotoSansCherokee-Regular.ttf - SIL Open Font License v1.1
16+
* NotoKufiArabic-Regular.ttf - SIL Open Font License v1.1
1417

1518

1619

Binary file not shown.
Binary file not shown.
Binary file not shown.

itext/itext.layout/itext/layout/font/selectorstrategy/AbstractFontSelectorStrategy.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ public virtual IList<Tuple2<GlyphLine, PdfFont>> GetGlyphLines(String text) {
117117
if (i > indexDiacritic) {
118118
if (iText.IO.Util.TextUtil.IsDiacritic(codePoint)) {
119119
PdfFont diacriticFont = MatchFont(codePoint, fontSelector, fontProvider, additionalFonts);
120+
// Diacritic font must contain previous symbol, if not, don't
121+
// enable special logic for diacritic and process it as usual symbol
122+
bool isPreviousMatchFont = i == 0 || diacriticFont == null || diacriticFont.ContainsGlyph(ExtractCodePoint
123+
(text, i - 1));
120124
// If diacritic font equals to the current font or null, don't
121125
// enable special logic for diacritic and process it as usual symbol
122-
if (diacriticFont != null && diacriticFont != currentFont) {
126+
if (diacriticFont != null && diacriticFont != currentFont && isPreviousMatchFont) {
123127
// If it's the first diacritic in a row, we want to break to try to find a better font for
124128
// the previous letter during the next iteration
125129
if (indexDiacritic != i - 1) {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c02f3a1c49366516edd50b61e0cc90f41d109455
1+
523702a3508c09b439be44375f1a7519f731b37e

0 commit comments

Comments
 (0)