Skip to content

Commit 0f63702

Browse files
Introduce #setFontFamily methods for setting preferred font families on elements
New setFontFamily methods replace the older setFont(String) method (which is deprecated now). The older method took a single String which was later processed by FontFamilySplitter. Some of the symbols in contents of this String argument were treated in a special way (like commas and quotes), also font family names with non-latin letters were previously ignored. New setFontFamily methods accept an array or list of Strings where every string is treated directly as desired font family name (without any preprocessing: all symbols are treated as a part of font-family name). DEVSIX-2525 Autoported commit. Original commit hash: [7c42087a0] Manual files: layout/src/main/java/com/itextpdf/layout/font/FontFamilySplitter.java
1 parent 13ace78 commit 0f63702

File tree

16 files changed

+226
-60
lines changed

16 files changed

+226
-60
lines changed

itext.tests/itext.layout.tests/itext/layout/BlockTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ public virtual void ParagraphVerticalAlignmentTest01() {
901901
doc.SetFontProvider(fontProvider);
902902
String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
903903
doc.Add(new Paragraph(loremIpsum).SetHeight(100).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetBorder(
904-
new SolidBorder(3)).SetFont(StandardFonts.TIMES_ROMAN));
904+
new SolidBorder(3)).SetFontFamily(StandardFonts.TIMES_ROMAN));
905905
doc.Close();
906906
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
907907
, "diff"));

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,13 @@ public virtual void StandardAndType3Fonts() {
104104
Document doc = new Document(pdfDoc);
105105
doc.SetFontProvider(sel);
106106
Paragraph paragraph = new Paragraph("Next paragraph contains a triangle, actually Type 3 Font");
107-
paragraph.SetProperty(Property.FONT, StandardFontFamilies.TIMES);
108-
// TODO DEVSIX-2136 Update of necessary
107+
paragraph.SetProperty(Property.FONT, new String[] { StandardFontFamilies.TIMES });
109108
doc.Add(paragraph);
110109
paragraph = new Paragraph("A");
111-
paragraph.SetFont("CustomFont");
110+
paragraph.SetFontFamily("CustomFont");
112111
doc.Add(paragraph);
113112
paragraph = new Paragraph("Next paragraph");
114-
paragraph.SetProperty(Property.FONT, StandardFonts.COURIER);
113+
paragraph.SetProperty(Property.FONT, new String[] { StandardFonts.COURIER });
115114
doc.Add(paragraph);
116115
doc.Close();
117116
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
@@ -133,8 +132,8 @@ public virtual void CustomFontProvider() {
133132
doc.SetFontProvider(fontProvider);
134133
Paragraph paragraph1 = new Paragraph("Default Helvetica should be selected.");
135134
doc.Add(paragraph1);
136-
Paragraph paragraph2 = new Paragraph("Default Helvetica should be selected.").SetFont(StandardFonts.COURIER
137-
);
135+
Paragraph paragraph2 = new Paragraph("Default Helvetica should be selected.").SetFontFamily(StandardFonts.
136+
COURIER);
138137
doc.Add(paragraph2);
139138
doc.Close();
140139
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
@@ -160,7 +159,7 @@ public virtual void CustomFontProvider2() {
160159
Document doc = new Document(pdfDoc);
161160
doc.SetFontProvider(fontProvider);
162161
Paragraph paragraph = new Paragraph("There is no default font (Helvetica) inside the used FontProvider's instance. So the first font, that has been added, should be selected. Here it's FreeSans."
163-
).SetFont("ABRACADABRA_THERE_IS_NO_SUCH_FONT");
162+
).SetFontFamily("ABRACADABRA_THERE_IS_NO_SUCH_FONT");
164163
doc.Add(paragraph);
165164
doc.Close();
166165
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder

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

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ source product.
4545
using System.IO;
4646
using iText.IO.Font;
4747
using iText.IO.Font.Constants;
48+
using iText.IO.Util;
4849
using iText.Kernel.Colors;
50+
using iText.Kernel.Font;
4951
using iText.Kernel.Pdf;
5052
using iText.Kernel.Utils;
5153
using iText.Layout.Element;
@@ -84,7 +86,7 @@ public virtual void CyrillicAndLatinGroup() {
8486
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
8587
Document doc = new Document(pdfDoc);
8688
doc.SetFontProvider(sel);
87-
doc.SetProperty(Property.FONT, "Puritan42");
89+
doc.SetProperty(Property.FONT, new String[] { "Puritan42" });
8890
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
8991
Paragraph paragraph = new Paragraph(text);
9092
doc.Add(paragraph);
@@ -108,8 +110,7 @@ public virtual void CyrillicAndLatinGroup2() {
108110
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
109111
Document doc = new Document(pdfDoc);
110112
doc.SetFontProvider(sel);
111-
doc.SetFont("'Puritan', \"FreeSans\"");
112-
// TODO DEVSIX-2120 font-family is Puritan 2.0 here, however it doesn't match font-family pattern
113+
doc.SetFontFamily("Puritan 2.0", "FreeSans");
113114
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
114115
Paragraph paragraph = new Paragraph(text);
115116
doc.Add(paragraph);
@@ -130,7 +131,7 @@ public virtual void LatinAndNotdefGroup() {
130131
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
131132
Document doc = new Document(pdfDoc);
132133
doc.SetFontProvider(sel);
133-
doc.SetFont("Puritan");
134+
doc.SetFontFamily("Puritan 2.0");
134135
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
135136
Paragraph paragraph = new Paragraph(text);
136137
doc.Add(paragraph);
@@ -154,7 +155,7 @@ public virtual void CustomFontWeight() {
154155
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
155156
Document doc = new Document(pdfDoc);
156157
doc.SetFontProvider(sel);
157-
Div div = new Div().SetFont(StandardFonts.TIMES_ROMAN);
158+
Div div = new Div().SetFontFamily(StandardFonts.TIMES_ROMAN);
158159
Paragraph paragraph = new Paragraph("Times Roman Bold text");
159160
paragraph.SetProperty(Property.FONT_WEIGHT, "bold");
160161
div.Add(paragraph);
@@ -180,8 +181,7 @@ public virtual void CustomFontWeight2() {
180181
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
181182
Document doc = new Document(pdfDoc);
182183
doc.SetFontProvider(sel);
183-
Div div = new Div().SetFont(StandardFontFamilies.TIMES);
184-
// TODO DEVSIX-2136 Update of necessary
184+
Div div = new Div().SetFontFamily(StandardFontFamilies.TIMES);
185185
Paragraph paragraph = new Paragraph("Times Roman Bold text");
186186
paragraph.SetProperty(Property.FONT_WEIGHT, "bold");
187187
div.Add(paragraph);
@@ -206,8 +206,7 @@ public virtual void CustomFontWeight3() {
206206
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
207207
Document doc = new Document(pdfDoc);
208208
doc.SetFontProvider(sel);
209-
Div div = new Div().SetFont(StandardFontFamilies.TIMES);
210-
// TODO DEVSIX-2136 Update of necessary
209+
Div div = new Div().SetFontFamily(StandardFontFamilies.TIMES);
211210
Paragraph paragraph = new Paragraph("Times Roman Bold text");
212211
paragraph.SetProperty(Property.FONT_WEIGHT, "bold");
213212
div.Add(paragraph);
@@ -230,10 +229,10 @@ public virtual void StandardPdfFonts() {
230229
Document doc = new Document(pdfDoc);
231230
doc.SetFontProvider(sel);
232231
Paragraph paragraph = new Paragraph(s);
233-
paragraph.SetFont("Courier");
232+
paragraph.SetFontFamily("Courier");
234233
doc.Add(paragraph);
235234
paragraph = new Paragraph(s);
236-
paragraph.SetProperty(Property.FONT, "Times");
235+
paragraph.SetProperty(Property.FONT, new String[] { "Times" });
237236
doc.Add(paragraph);
238237
doc.Close();
239238
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
@@ -306,6 +305,63 @@ public virtual void SearchNames2() {
306305
NUnit.Framework.Assert.IsTrue(GetFirst(sel.GetFontSet().Get("puritan42")) == null, "Puritan42 found!");
307306
}
308307

308+
[NUnit.Framework.Test]
309+
public virtual void SearchFontAliasWithUnicodeChars() {
310+
String cyrillicAlias = "\u0444\u043E\u043D\u04421";
311+
// фонт1
312+
String greekAlias = "\u03B3\u03C1\u03B1\u03BC\u03BC\u03B1\u03C4\u03BF\u03C3\u03B5\u03B9\u03C1\u03AC2";
313+
// γραμματοσειρά2
314+
String japaneseAlias = "\u30D5\u30A9\u30F3\u30C83";
315+
// フォント3
316+
IDictionary<String, String> aliasToFontName = new LinkedDictionary<String, String>();
317+
aliasToFontName.Put(cyrillicAlias, "NotoSans-Regular.ttf");
318+
aliasToFontName.Put(greekAlias, "FreeSans.ttf");
319+
aliasToFontName.Put(japaneseAlias, "Puritan2.otf");
320+
FontProvider provider = new FontProvider();
321+
foreach (KeyValuePair<String, String> e in aliasToFontName) {
322+
provider.GetFontSet().AddFont(fontsFolder + e.Value, PdfEncodings.IDENTITY_H, e.Key);
323+
}
324+
ICollection<String> actualAliases = new HashSet<String>();
325+
foreach (FontInfo fontInfo in provider.GetFontSet().GetFonts()) {
326+
actualAliases.Add(fontInfo.GetAlias());
327+
}
328+
ICollection<String> expectedAliases = aliasToFontName.Keys;
329+
NUnit.Framework.Assert.IsTrue(actualAliases.ContainsAll(expectedAliases) && expectedAliases.ContainsAll(actualAliases
330+
));
331+
foreach (String fontAlias in expectedAliases) {
332+
PdfFont pdfFont = provider.GetPdfFont(provider.GetFontSelector(JavaCollectionsUtil.SingletonList(fontAlias
333+
), new FontCharacteristics()).BestMatch());
334+
String fontName = pdfFont.GetFontProgram().GetFontNames().GetFontName();
335+
NUnit.Framework.Assert.IsTrue(aliasToFontName.Get(fontAlias).Contains(fontName));
336+
}
337+
}
338+
339+
/// <exception cref="System.IO.IOException"/>
340+
/// <exception cref="System.Exception"/>
341+
[NUnit.Framework.Test]
342+
public virtual void WriteTextInFontWhichAliasWithUnicodeChars() {
343+
String fileName = "writeTextInFontWhichAliasWithUnicodeChars";
344+
String outFileName = destinationFolder + fileName + ".pdf";
345+
String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
346+
String japaneseAlias = "\u30D5\u30A9\u30F3\u30C83";
347+
// フォント3
348+
FontProvider provider = new FontProvider();
349+
provider.AddFont(fontsFolder + "NotoSans-Regular.ttf");
350+
provider.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H, japaneseAlias);
351+
provider.AddFont(fontsFolder + "FreeSans.ttf");
352+
String s = "Hello world!";
353+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
354+
Document doc = new Document(pdfDoc);
355+
doc.SetFontProvider(provider);
356+
Paragraph paragraph = new Paragraph(new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY));
357+
paragraph.SetFontFamily(japaneseAlias);
358+
doc.Add(paragraph);
359+
doc.Close();
360+
// Text shall be written in Puritan 2.0
361+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
362+
));
363+
}
364+
309365
/// <exception cref="System.Exception"/>
310366
[NUnit.Framework.Test]
311367
public virtual void CyrillicAndLatinWithUnicodeRange() {
@@ -322,7 +378,7 @@ public virtual void CyrillicAndLatinWithUnicodeRange() {
322378
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
323379
Document doc = new Document(pdfDoc);
324380
doc.SetFontProvider(sel);
325-
doc.SetProperty(Property.FONT, "FontAlias");
381+
doc.SetProperty(Property.FONT, new String[] { "FontAlias" });
326382
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
327383
Paragraph paragraph = new Paragraph(text);
328384
doc.Add(paragraph);
@@ -348,7 +404,7 @@ public virtual void DuplicateFontWithUnicodeRange() {
348404
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
349405
Document doc = new Document(pdfDoc);
350406
doc.SetFontProvider(sel);
351-
doc.SetProperty(Property.FONT, "FontAlias");
407+
doc.SetProperty(Property.FONT, new String[] { "FontAlias" });
352408
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
353409
Paragraph paragraph = new Paragraph(text);
354410
doc.Add(paragraph);
@@ -374,7 +430,7 @@ public virtual void SingleFontWithUnicodeRange() {
374430
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
375431
Document doc = new Document(pdfDoc);
376432
doc.SetFontProvider(sel);
377-
doc.SetProperty(Property.FONT, "FontAlias");
433+
doc.SetProperty(Property.FONT, new String[] { "FontAlias" });
378434
Text text = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
379435
Paragraph paragraph = new Paragraph(text);
380436
doc.Add(paragraph);

itext.tests/itext.layout.tests/itext/layout/NonBreakingHyphenTest.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,36 @@ public virtual void NonBreakingHyphenDifferentFonts() {
8585
document.Add(new Paragraph("StandardFonts - non-breaking hyphen \\u2011").SetUnderline().SetTextAlignment(
8686
TextAlignment.CENTER));
8787
document.Add(new Paragraph("for Standard font TIMES_ROMAN: <&#8209;> non-breaking hyphen <\u2011> 2 hyphens<\u2011\u2011>here "
88-
).SetFont(StandardFonts.TIMES_ROMAN));
88+
).SetFontFamily(StandardFonts.TIMES_ROMAN));
8989
document.Add(new Paragraph("for Standard font COURIER: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
90-
).SetFont(StandardFonts.COURIER));
90+
).SetFontFamily(StandardFonts.COURIER));
9191
document.Add(new Paragraph("for Standard font HELVETICA_BOLD: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
9292
).SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)));
9393
document.Add(new Paragraph("for Standard font SYMBOL: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
9494
).SetFont(PdfFontFactory.CreateFont(StandardFonts.SYMBOL)));
9595
document.Add(new Paragraph("Non-Standard fonts - non-breaking hyphen \\u2011").SetUnderline().SetTextAlignment
9696
(TextAlignment.CENTER));
97-
document.Add(new Paragraph("for NotoSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("NotoSans"
98-
));
99-
document.Add(new Paragraph("for Puritan2: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("Puritan2"
100-
));
97+
document.Add(new Paragraph("for NotoSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFontFamily
98+
("NotoSans"));
99+
document.Add(new Paragraph("for Puritan2: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFontFamily
100+
("Puritan2"));
101101
sel.GetFontSet().AddFont(fontsFolder + "FreeSans.ttf", PdfEncodings.IDENTITY_H, "FreeSans");
102102
document.Add(new Paragraph("AFTER adding of FreeSans font with non-breaking hyphen \\u2011 support").SetUnderline
103103
().SetTextAlignment(TextAlignment.CENTER));
104104
document.Add(new Paragraph("for Standard font TIMES_ROMAN: <&#8209;> non-breaking hyphen <\u2011> 2 hyphens<\u2011\u2011>here "
105-
).SetFont(StandardFonts.TIMES_ROMAN));
105+
).SetFontFamily(StandardFonts.TIMES_ROMAN));
106106
document.Add(new Paragraph("for Standard font COURIER: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
107-
).SetFont(StandardFonts.COURIER));
107+
).SetFontFamily(StandardFonts.COURIER));
108108
document.Add(new Paragraph("for Standard font HELVETICA_BOLD: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
109109
).SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)));
110110
document.Add(new Paragraph("for Standard font SYMBOL: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
111111
).SetFont(PdfFontFactory.CreateFont(StandardFonts.SYMBOL)));
112-
document.Add(new Paragraph("for FreeSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("FreeSans"
113-
));
114-
document.Add(new Paragraph("for NotoSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("NotoSans"
115-
));
116-
document.Add(new Paragraph("for Puritan2: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("Puritan2"
117-
));
112+
document.Add(new Paragraph("for FreeSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFontFamily
113+
("FreeSans"));
114+
document.Add(new Paragraph("for NotoSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFontFamily
115+
("NotoSans"));
116+
document.Add(new Paragraph("for Puritan2: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFontFamily
117+
("Puritan2"));
118118
document.Close();
119119
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
120120
, diffPrefix));

itext.tests/itext.layout.tests/itext/layout/renderer/TextRendererTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public virtual void SetTextException() {
8181
FontProvider fp = new FontProvider();
8282
fp.AddFont(fontName);
8383
rend.SetProperty(Property.FONT_PROVIDER, fp);
84-
rend.SetProperty(Property.FONT, fontName);
84+
rend.SetProperty(Property.FONT, new String[] { fontName });
8585
rend.SetText(val);
8686
NUnit.Framework.Assert.AreEqual(val, rend.GetText().ToString());
8787
}
@@ -104,7 +104,7 @@ public virtual void SetFontAsText() {
104104
FontProvider fp = new FontProvider();
105105
fp.AddFont("Helvetica");
106106
txt.SetProperty(Property.FONT_PROVIDER, fp);
107-
txt.SetFont("Helvetica");
107+
txt.SetFontFamily("Helvetica");
108108
doc.Add(new Paragraph().Add(txt));
109109
doc.Close();
110110
}

itext/itext.io/itext/io/LogMessageConstant.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ public const String ENCRYPTED_PAYLOAD_FILE_SPEC_SHALL_HAVE_AFRELATIONSHIP_FILED_
133133

134134
public const String FONT_HAS_INVALID_GLYPH = "Font {0} has invalid glyph: {1}";
135135

136-
public const String FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT = "The Font Property must be a PdfFont object";
136+
public const String FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT = "The \"Property.FONT\" property must be a PdfFont object in this context.";
137+
138+
public const String FONT_PROPERTY_OF_STRING_TYPE_IS_DEPRECATED_USE_STRINGS_ARRAY_INSTEAD = "The \"Property.FONT\" property with values of String type is deprecated, use String[] as property value type instead.";
137139

138140
public const String FONT_SUBSET_ISSUE = "Font subset issue. Full font will be embedded.";
139141

0 commit comments

Comments
 (0)