Skip to content

Commit 5667e27

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Add tests for setFontProvider+setFontFamily pair and for deprecated Property.FONT as String
DEVSIX-2525 Autoported commit. Original commit hash: [98dae1fa1]
1 parent 0f63702 commit 5667e27

File tree

8 files changed

+77
-10
lines changed

8 files changed

+77
-10
lines changed

itext.tests/itext.layout.tests/itext/layout/FontProviderTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ source product.
4545
using System.IO;
4646
using iText.IO.Font;
4747
using iText.IO.Font.Constants;
48+
using iText.Kernel;
4849
using iText.Kernel.Font;
4950
using iText.Kernel.Pdf;
5051
using iText.Kernel.Utils;
@@ -165,5 +166,21 @@ public virtual void CustomFontProvider2() {
165166
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
166167
, "diff" + fileName));
167168
}
169+
170+
/// <exception cref="System.Exception"/>
171+
[NUnit.Framework.Test]
172+
public virtual void FontProviderNotSetExceptionTest() {
173+
NUnit.Framework.Assert.That(() => {
174+
String fileName = "fontProviderNotSetExceptionTest.pdf";
175+
String outFileName = destinationFolder + fileName + ".pdf";
176+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
177+
Document doc = new Document(pdfDoc);
178+
Paragraph paragraph = new Paragraph("Hello world!").SetFontFamily("ABRACADABRA_NO_FONT_PROVIDER_ANYWAY");
179+
doc.Add(paragraph);
180+
doc.Close();
181+
}
182+
, NUnit.Framework.Throws.InstanceOf<InvalidOperationException>().With.Message.EqualTo(PdfException.FontProviderNotSetFontFamilyNotResolved))
183+
;
184+
}
168185
}
169186
}

itext.tests/itext.layout.tests/itext/layout/FontSelectorTest.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ source product.
5454
using iText.Layout.Font;
5555
using iText.Layout.Properties;
5656
using iText.Test;
57+
using iText.Test.Attributes;
5758

5859
namespace iText.Layout {
5960
public class FontSelectorTest : ExtendedITextTest {
@@ -98,7 +99,6 @@ public virtual void CyrillicAndLatinGroup() {
9899
/// <exception cref="System.Exception"/>
99100
[NUnit.Framework.Test]
100101
public virtual void CyrillicAndLatinGroup2() {
101-
// TODO DEVSIX-2120 The font-family name of Puritan2.otf is 'Puritan 2.0' but this name doesn't match font-family name pattern
102102
String fileName = "cyrillicAndLatinGroup2";
103103
String outFileName = destinationFolder + fileName + ".pdf";
104104
String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
@@ -119,6 +119,54 @@ public virtual void CyrillicAndLatinGroup2() {
119119
, "diff" + fileName));
120120
}
121121

122+
/// <exception cref="System.Exception"/>
123+
[NUnit.Framework.Test]
124+
public virtual void CyrillicAndLatinGroup3() {
125+
String fileName = "cyrillicAndLatinGroup3";
126+
String outFileName = destinationFolder + fileName + ".pdf";
127+
String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
128+
FontProvider sel = new FontProvider();
129+
NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "FreeSans.ttf"));
130+
NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "NotoSans-Regular.ttf"));
131+
NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "Puritan2.otf"));
132+
String s = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
133+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
134+
Document doc = new Document(pdfDoc);
135+
doc.SetFontProvider(sel);
136+
doc.SetFontFamily(JavaUtil.ArraysAsList("Puritan 2.0", "Noto Sans"));
137+
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
138+
Paragraph paragraph = new Paragraph(text);
139+
doc.Add(paragraph);
140+
doc.Close();
141+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
142+
, "diff" + fileName));
143+
}
144+
145+
/// <exception cref="System.Exception"/>
146+
[NUnit.Framework.Test]
147+
[LogMessage(iText.IO.LogMessageConstant.FONT_PROPERTY_OF_STRING_TYPE_IS_DEPRECATED_USE_STRINGS_ARRAY_INSTEAD
148+
)]
149+
public virtual void CyrillicAndLatinGroupDeprecatedFontAsStringValue() {
150+
String fileName = "cyrillicAndLatinGroupDeprecatedFontAsStringValue";
151+
String outFileName = destinationFolder + fileName + ".pdf";
152+
String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
153+
FontProvider sel = new FontProvider();
154+
NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "FreeSans.ttf"));
155+
NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "NotoSans-Regular.ttf"));
156+
NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "Puritan2.otf"));
157+
String s = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
158+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
159+
Document doc = new Document(pdfDoc);
160+
doc.SetFontProvider(sel);
161+
doc.SetProperty(Property.FONT, "'Puritan', \"FreeSans\"");
162+
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
163+
Paragraph paragraph = new Paragraph(text);
164+
doc.Add(paragraph);
165+
doc.Close();
166+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
167+
, "diff" + fileName));
168+
}
169+
122170
/// <exception cref="System.Exception"/>
123171
[NUnit.Framework.Test]
124172
public virtual void LatinAndNotdefGroup() {

itext/itext.kernel/itext/kernel/PdfException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ public class PdfException : Exception {
261261

262262
public const String FontEmbeddingIssue = "Font embedding issue.";
263263

264+
public const String FontProviderNotSetFontFamilyNotResolved = "FontProvider and FontSet are empty. Cannot resolve font family name (see ElementPropertyContainer#setFontFamily) without initialized FontProvider (see RootElement#setFontProvider).";
265+
264266
[Obsolete]
265267
public const String FontSizeIsTooSmall = "Font size is too small.";
266268

itext/itext.layout/itext/layout/renderer/AbstractRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ source product.
4646
using System.Text;
4747
using Common.Logging;
4848
using iText.IO.Util;
49+
using iText.Kernel;
4950
using iText.Kernel.Colors;
5051
using iText.Kernel.Font;
5152
using iText.Kernel.Geom;
@@ -2200,8 +2201,7 @@ internal virtual PdfFont ResolveFirstPdfFont() {
22002201
}
22012202
FontProvider provider = this.GetProperty<FontProvider>(Property.FONT_PROVIDER);
22022203
if (provider == null) {
2203-
throw new InvalidOperationException("Invalid font type. FontProvider expected. Cannot resolve font with string value"
2204-
);
2204+
throw new InvalidOperationException(PdfException.FontProviderNotSetFontFamilyNotResolved);
22052205
}
22062206
FontCharacteristics fc = CreateFontCharacteristics();
22072207
return ResolveFirstPdfFont((String[])font, provider, fc);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ source product.
4848
using iText.IO.Font;
4949
using iText.IO.Font.Otf;
5050
using iText.IO.Util;
51+
using iText.Kernel;
5152
using iText.Kernel.Colors;
5253
using iText.Kernel.Font;
5354
using iText.Kernel.Geom;
@@ -742,7 +743,7 @@ public override void Draw(DrawContext drawContext) {
742743
if (horizontalScaling != null && horizontalScaling != 1) {
743744
canvas.SetHorizontalScaling((float)horizontalScaling * 100);
744745
}
745-
GlyphLine.IGlyphLineFilter filter = new _IGlyphLineFilter_779();
746+
GlyphLine.IGlyphLineFilter filter = new _IGlyphLineFilter_780();
746747
bool appearanceStreamLayout = true.Equals(GetPropertyAsBoolean(Property.APPEARANCE_STREAM_LAYOUT));
747748
if (GetReversedRanges() != null) {
748749
bool writeReversedChars = !appearanceStreamLayout;
@@ -807,8 +808,8 @@ public override void Draw(DrawContext drawContext) {
807808
}
808809
}
809810

810-
private sealed class _IGlyphLineFilter_779 : GlyphLine.IGlyphLineFilter {
811-
public _IGlyphLineFilter_779() {
811+
private sealed class _IGlyphLineFilter_780 : GlyphLine.IGlyphLineFilter {
812+
public _IGlyphLineFilter_780() {
812813
}
813814

814815
public bool Accept(Glyph glyph) {
@@ -1184,8 +1185,7 @@ protected internal virtual bool ResolveFonts(IList<IRenderer> addTo) {
11841185
FontProvider provider = this.GetProperty<FontProvider>(Property.FONT_PROVIDER);
11851186
FontSet fontSet = this.GetProperty<FontSet>(Property.FONT_SET);
11861187
if (provider.GetFontSet().IsEmpty() && (fontSet == null || fontSet.IsEmpty())) {
1187-
throw new InvalidOperationException("Invalid font type. FontProvider and FontSet are empty. Cannot resolve font with string value."
1188-
);
1188+
throw new InvalidOperationException(PdfException.FontProviderNotSetFontFamilyNotResolved);
11891189
}
11901190
FontCharacteristics fc = CreateFontCharacteristics();
11911191
FontSelectorStrategy strategy = provider.GetStrategy(strToBeConverted, JavaUtil.ArraysAsList((String[])font
@@ -1206,7 +1206,7 @@ protected internal virtual bool ResolveFonts(IList<IRenderer> addTo) {
12061206
return true;
12071207
}
12081208
else {
1209-
throw new InvalidOperationException("Invalid font type.");
1209+
throw new InvalidOperationException("Invalid FONT property value type.");
12101210
}
12111211
}
12121212
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7c42087a0e89171aa3226cad1eb31e6a1a037937
1+
98dae1fa142770319f13920146038d43e3724c49

0 commit comments

Comments
 (0)