Skip to content

Commit 98dae1f

Browse files
Add tests for setFontProvider+setFontFamily pair and for deprecated Property.FONT as String
DEVSIX-2525
1 parent 7c42087 commit 98dae1f

File tree

7 files changed

+88
-4
lines changed

7 files changed

+88
-4
lines changed

kernel/src/main/java/com/itextpdf/kernel/PdfException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public class PdfException extends RuntimeException {
165165
public static final String FlushedPageCannotBeAddedOrInserted = "Flushed page cannot be added or inserted.";
166166
public static final String FontAndSizeMustBeSetBeforeWritingAnyText = "Font and size must be set before writing any text.";
167167
public static final String FontEmbeddingIssue = "Font embedding issue.";
168+
public static final String FontProviderNotSetFontFamilyNotResolved = "FontProvider and FontSet are empty. Cannot resolve font family name (see ElementPropertyContainer#setFontFamily) without initialized FontProvider (see RootElement#setFontProvider).";
168169
@Deprecated
169170
public static final String FontSizeIsTooSmall = "Font size is too small.";
170171
public static final String FormXObjectMustHaveBbox = "Form XObject must have BBox.";

layout/src/main/java/com/itextpdf/layout/renderer/AbstractRenderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.io.LogMessageConstant;
4747
import com.itextpdf.io.util.MessageFormatUtil;
4848
import com.itextpdf.io.util.NumberUtil;
49+
import com.itextpdf.kernel.PdfException;
4950
import com.itextpdf.kernel.colors.Color;
5051
import com.itextpdf.kernel.font.PdfFont;
5152
import com.itextpdf.kernel.geom.AffineTransform;
@@ -2163,7 +2164,7 @@ PdfFont resolveFirstPdfFont() {
21632164
}
21642165
FontProvider provider = this.<FontProvider>getProperty(Property.FONT_PROVIDER);
21652166
if (provider == null) {
2166-
throw new IllegalStateException("Invalid font type. FontProvider expected. Cannot resolve font with string value");
2167+
throw new IllegalStateException(PdfException.FontProviderNotSetFontFamilyNotResolved);
21672168
}
21682169
FontCharacteristics fc = createFontCharacteristics();
21692170
return resolveFirstPdfFont((String[]) font, provider, fc);

layout/src/main/java/com/itextpdf/layout/renderer/TextRenderer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.io.util.EnumUtil;
5353
import com.itextpdf.io.util.MessageFormatUtil;
5454
import com.itextpdf.io.util.TextUtil;
55+
import com.itextpdf.kernel.PdfException;
5556
import com.itextpdf.kernel.colors.Color;
5657
import com.itextpdf.kernel.font.PdfFont;
5758
import com.itextpdf.kernel.font.PdfType0Font;
@@ -1218,7 +1219,7 @@ protected boolean resolveFonts(List<IRenderer> addTo) {
12181219
FontProvider provider = this.<FontProvider>getProperty(Property.FONT_PROVIDER);
12191220
FontSet fontSet = this.<FontSet>getProperty(Property.FONT_SET);
12201221
if (provider.getFontSet().isEmpty() && (fontSet == null || fontSet.isEmpty())) {
1221-
throw new IllegalStateException("Invalid font type. FontProvider and FontSet are empty. Cannot resolve font with string value.");
1222+
throw new IllegalStateException(PdfException.FontProviderNotSetFontFamilyNotResolved);
12221223
}
12231224
FontCharacteristics fc = createFontCharacteristics();
12241225
FontSelectorStrategy strategy = provider.getStrategy(strToBeConverted, Arrays.asList((String[])font), fc, fontSet);
@@ -1235,7 +1236,7 @@ protected boolean resolveFonts(List<IRenderer> addTo) {
12351236
}
12361237
return true;
12371238
} else {
1238-
throw new IllegalStateException("Invalid font type.");
1239+
throw new IllegalStateException("Invalid FONT property value type.");
12391240
}
12401241
}
12411242

layout/src/test/java/com/itextpdf/layout/FontProviderTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.io.font.PdfEncodings;
4646
import com.itextpdf.io.font.constants.StandardFontFamilies;
4747
import com.itextpdf.io.font.constants.StandardFonts;
48+
import com.itextpdf.kernel.PdfException;
4849
import com.itextpdf.kernel.font.PdfFont;
4950
import com.itextpdf.kernel.font.PdfFontFactory;
5051
import com.itextpdf.kernel.font.PdfType3Font;
@@ -63,8 +64,10 @@ This file is part of the iText (R) project.
6364
import com.itextpdf.test.annotations.type.IntegrationTest;
6465
import org.junit.Assert;
6566
import org.junit.BeforeClass;
67+
import org.junit.Rule;
6668
import org.junit.Test;
6769
import org.junit.experimental.categories.Category;
70+
import org.junit.rules.ExpectedException;
6871

6972
import java.io.FileInputStream;
7073
import java.io.FileOutputStream;
@@ -75,6 +78,9 @@ This file is part of the iText (R) project.
7578
@Category(IntegrationTest.class)
7679
public class FontProviderTest extends ExtendedITextTest {
7780

81+
@Rule
82+
public ExpectedException junitExpectedException = ExpectedException.none();
83+
7884
private static class PdfFontProvider extends FontProvider {
7985

8086
private List<FontInfo> pdfFontInfos = new ArrayList<>();
@@ -188,4 +194,22 @@ public void customFontProvider2() throws Exception {
188194
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff" + fileName));
189195
}
190196

197+
@Test
198+
public void fontProviderNotSetExceptionTest() throws Exception {
199+
junitExpectedException.expect(IllegalStateException.class);
200+
junitExpectedException.expectMessage(PdfException.FontProviderNotSetFontFamilyNotResolved);
201+
202+
String fileName = "fontProviderNotSetExceptionTest.pdf";
203+
String outFileName = destinationFolder + fileName + ".pdf";
204+
205+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
206+
Document doc = new Document(pdfDoc);
207+
208+
Paragraph paragraph = new Paragraph("Hello world!")
209+
.setFontFamily("ABRACADABRA_NO_FONT_PROVIDER_ANYWAY");
210+
doc.add(paragraph);
211+
212+
doc.close();
213+
}
214+
191215
}

layout/src/test/java/com/itextpdf/layout/FontSelectorTest.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.layout;
4444

45+
import com.itextpdf.io.LogMessageConstant;
4546
import com.itextpdf.io.font.PdfEncodings;
4647
import com.itextpdf.io.font.constants.StandardFontFamilies;
4748
import com.itextpdf.io.font.constants.StandardFonts;
@@ -62,6 +63,8 @@ This file is part of the iText (R) project.
6263
import com.itextpdf.layout.font.RangeBuilder;
6364
import com.itextpdf.layout.property.Property;
6465
import com.itextpdf.test.ExtendedITextTest;
66+
import com.itextpdf.test.annotations.LogMessage;
67+
import com.itextpdf.test.annotations.LogMessages;
6568
import com.itextpdf.test.annotations.type.IntegrationTest;
6669
import org.junit.Assert;
6770
import org.junit.BeforeClass;
@@ -121,7 +124,6 @@ public void cyrillicAndLatinGroup() throws Exception {
121124
}
122125

123126
@Test
124-
// TODO DEVSIX-2120 The font-family name of Puritan2.otf is 'Puritan 2.0' but this name doesn't match font-family name pattern
125127
public void cyrillicAndLatinGroup2() throws Exception {
126128
String fileName = "cyrillicAndLatinGroup2";
127129
String outFileName = destinationFolder + fileName + ".pdf";
@@ -147,6 +149,61 @@ public void cyrillicAndLatinGroup2() throws Exception {
147149
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff" + fileName));
148150
}
149151

152+
@Test
153+
public void cyrillicAndLatinGroup3() throws Exception {
154+
String fileName = "cyrillicAndLatinGroup3";
155+
String outFileName = destinationFolder + fileName + ".pdf";
156+
String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
157+
158+
FontProvider sel = new FontProvider();
159+
160+
Assert.assertTrue(sel.addFont(fontsFolder + "FreeSans.ttf"));
161+
Assert.assertTrue(sel.addFont(fontsFolder + "NotoSans-Regular.ttf"));
162+
Assert.assertTrue(sel.addFont(fontsFolder + "Puritan2.otf"));
163+
164+
165+
String s = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
166+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
167+
Document doc = new Document(pdfDoc);
168+
169+
doc.setFontProvider(sel);
170+
doc.setFontFamily(Arrays.asList("Puritan 2.0", "Noto Sans"));
171+
Text text = new Text(s).setBackgroundColor(ColorConstants.LIGHT_GRAY);
172+
Paragraph paragraph = new Paragraph(text);
173+
doc.add(paragraph);
174+
doc.close();
175+
176+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff" + fileName));
177+
}
178+
179+
@Test
180+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FONT_PROPERTY_OF_STRING_TYPE_IS_DEPRECATED_USE_STRINGS_ARRAY_INSTEAD))
181+
public void cyrillicAndLatinGroupDeprecatedFontAsStringValue() throws Exception {
182+
String fileName = "cyrillicAndLatinGroupDeprecatedFontAsStringValue";
183+
String outFileName = destinationFolder + fileName + ".pdf";
184+
String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
185+
186+
FontProvider sel = new FontProvider();
187+
188+
Assert.assertTrue(sel.addFont(fontsFolder + "FreeSans.ttf"));
189+
Assert.assertTrue(sel.addFont(fontsFolder + "NotoSans-Regular.ttf"));
190+
Assert.assertTrue(sel.addFont(fontsFolder + "Puritan2.otf"));
191+
192+
193+
String s = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
194+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)));
195+
Document doc = new Document(pdfDoc);
196+
197+
doc.setFontProvider(sel);
198+
doc.setProperty(Property.FONT, "'Puritan', \"FreeSans\"");
199+
Text text = new Text(s).setBackgroundColor(ColorConstants.LIGHT_GRAY);
200+
Paragraph paragraph = new Paragraph(text);
201+
doc.add(paragraph);
202+
doc.close();
203+
204+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff" + fileName));
205+
}
206+
150207
@Test
151208
public void latinAndNotdefGroup() throws Exception {
152209
String fileName = "latinAndNotdefGroup";

0 commit comments

Comments
 (0)