Skip to content

Commit dee84be

Browse files
author
Alexandr Pliushchou
committed
Add test for true type collection fonts, improve documentation
DEVSIX-8824
1 parent 1c25237 commit dee84be

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

io/src/main/java/com/itextpdf/io/font/FontProgramFactory.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ public static FontProgram createFont(String fontProgram, String cmap, boolean ca
120120
}
121121

122122
/**
123-
* Creates a new font program. This font program can be one of the 14 built in fonts,
124-
* a Type1 font referred to by an AFM or PFM file, a TrueType font or
125-
* a CJK font from the Adobe Asian Font Pack.
126-
* Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
127-
* This would get the second font (indexes start at 0), in this case "MS PGothic".
128-
* <p>
123+
* Creates a new font program.
129124
* The fonts are cached and if they already exist they are extracted from the cache,
130125
* not parsed again.
131126
* <p>
@@ -139,12 +134,7 @@ public static FontProgram createFont(byte[] fontProgram) throws java.io.IOExcept
139134
}
140135

141136
/**
142-
* Creates a new font program. This font program can be one of the 14 built in fonts,
143-
* a Type 1 font referred to by an AFM or PFM file, a TrueType font or
144-
* a CJK font from the Adobe Asian Font Pack.
145-
* Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1".
146-
* This would get the second font (indexes start at 0), in this case "MS PGothic".
147-
* <p>
137+
* Creates a new font program.
148138
* The fonts are cached and if they already exist they are extracted from the cache,
149139
* not parsed again.
150140
* <p>

layout/src/main/java/com/itextpdf/layout/font/FontProvider.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ public boolean addFont(FontProgram fontProgram) {
157157
/**
158158
* Add font to {@link FontSet} cache.
159159
*
160-
* @param fontPath path to the font file to add. Can be a path to file or font name,
161-
* see {@link FontProgramFactory#createFont(String)}.
160+
* @param fontPath path to the font file to add. Can be a path to file or font name.
161+
* As a font name one of 14 built in fonts can be used, see {@link StandardFonts}.
162+
* If file is a true type collection, fonts in it should be addressed by index
163+
* such as "msgothic.ttc,1", starting with index 0.
164+
* See {@link FontProgramFactory#createFont(String)} for more information.
162165
* @param encoding font encoding to create {@link com.itextpdf.kernel.font.PdfFont}. Possible values for this
163166
* argument are the same as for {@link PdfFontFactory#createFont()} family of methods.
164167
* @param unicodeRange sets the specific range of characters to be used from the font.
@@ -172,8 +175,11 @@ public boolean addFont(String fontPath, String encoding, Range unicodeRange) {
172175
/**
173176
* Add font to {@link FontSet} cache.
174177
*
175-
* @param fontPath path to the font file to add. Can be a path to file or font name,
176-
* see {@link FontProgramFactory#createFont(String)}.
178+
* @param fontPath path to the font file to add. Can be a path to file or font name.
179+
* As a font name one of 14 built in fonts can be used, see {@link StandardFonts}.
180+
* If file is a true type collection, fonts in it should be addressed by index
181+
* such as "msgothic.ttc,1", starting with index 0.
182+
* See {@link FontProgramFactory#createFont(String)} for more information.
177183
* @param encoding font encoding to create {@link com.itextpdf.kernel.font.PdfFont}. Possible values for this
178184
* argument are the same as for {@link PdfFontFactory#createFont()} family of methods.
179185
*
@@ -186,9 +192,11 @@ public boolean addFont(String fontPath, String encoding) {
186192
/**
187193
* Add font to {@link FontSet} cache.
188194
*
189-
* @param fontPath path to the font file to add. Can be a path to file or font name,
190-
* see {@link FontProgramFactory#createFont(String)}.
191-
*
195+
* @param fontPath path to the font file to add. Can be a path to file or font name.
196+
* As a font name one of 14 built in fonts can be used, see {@link StandardFonts}.
197+
* If file is a true type collection, fonts in it should be addressed by index
198+
* such as "msgothic.ttc,1", starting with index 0.
199+
* See {@link FontProgramFactory#createFont(String)} for more information.
192200
* @return true, if font was successfully added, otherwise false.
193201
*/
194202
public boolean addFont(String fontPath) {

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ This file is part of the iText (R) project.
4747
import org.junit.jupiter.api.Test;
4848
import org.junit.jupiter.api.Tag;
4949

50-
import java.io.FileInputStream;
51-
import java.io.FileOutputStream;
50+
import java.io.IOException;
5251
import java.util.ArrayList;
5352
import java.util.Collection;
5453
import java.util.List;
@@ -180,7 +179,6 @@ public void customFontProvider2() throws Exception {
180179
public void fontProviderNotSetExceptionTest() throws Exception {
181180
String fileName = "fontProviderNotSetExceptionTest.pdf";
182181
String outFileName = destinationFolder + fileName + ".pdf";
183-
184182
try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(FileUtil.getFileOutputStream(outFileName)))) {
185183
Document doc = new Document(pdfDoc);
186184

@@ -191,4 +189,28 @@ public void fontProviderNotSetExceptionTest() throws Exception {
191189
Assertions.assertEquals(LayoutExceptionMessageConstant.FONT_PROVIDER_NOT_SET_FONT_FAMILY_NOT_RESOLVED, e.getMessage());
192190
}
193191
}
192+
193+
@Test
194+
public void trueTypeCollectionTest() throws IOException, InterruptedException {
195+
String fileName = "trueTypeCollectionFonts.pdf";
196+
String outFileName = destinationFolder + fileName;
197+
String cmpFileName = sourceFolder + "cmp_" + fileName;
198+
PdfDocument pdfDoc = new PdfDocument(CompareTool.createTestPdfWriter(outFileName));
199+
Document doc = new Document(pdfDoc);
200+
201+
FontProvider fontProvider = new FontProvider();
202+
fontProvider.addFont(fontsFolder + "/NotoSansAndSpaceMono.ttc,0");
203+
fontProvider.addFont(fontsFolder + "/NotoSansAndSpaceMono.ttc,1");
204+
doc.setFontProvider(fontProvider);
205+
206+
Paragraph paragraph1 = new Paragraph("some test text here").setFontFamily("Noto Sans");
207+
doc.add(paragraph1);
208+
209+
Paragraph paragraph2 = new Paragraph("and here should be different font").setFontFamily("Space Mono");
210+
doc.add(paragraph2);
211+
212+
doc.close();
213+
214+
Assertions.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff" + fileName));
215+
}
194216
}

layout/src/test/resources/com/itextpdf/layout/fonts/NOTICE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Please notice that the following fonts are used with the mentioned below license
1616
* NotoKufiArabic-Regular.ttf - SIL Open Font License v1.1
1717
* SpaceMono-Regular - SIL Open Font License v1.1 - OFL.txt (29.04.2024)
1818
* Roboto-Regular - Apache License V 2.0
19+
* NotoSansAndSpaceMono - SIL Open Font License v1.1
1920

2021

2122
All the used bold Google Noto fonts have been taken from the https://github.com/googlefonts.
Binary file not shown.

0 commit comments

Comments
 (0)