Skip to content

Commit b4fc4fb

Browse files
author
Fedorov Alexandr
committed
Add recursive font search in a directory
DEVSIX-3137
1 parent ea09cbd commit b4fc4fb

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,17 @@ public static int registerFontDirectory(String dir) {
443443
return fontRegisterProvider.registerFontDirectory(dir);
444444
}
445445

446+
447+
/**
448+
* Register all the fonts in a directory recursively.
449+
*
450+
* @param dir the directory
451+
* @return the number of fonts registered
452+
*/
453+
public static int registerFontDirectoryRecursively(String dir) {
454+
return fontRegisterProvider.registerFontDirectory(dir, true);
455+
}
456+
446457
/**
447458
* Register fonts in some probable directories. It usually works in Windows,
448459
* Linux and Solaris.

io/src/test/java/com/itextpdf/io/font/FontProgramTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This file is part of the iText (R) project.
2929
import com.itextpdf.test.ExtendedITextTest;
3030

3131
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.BeforeEach;
3233
import org.junit.jupiter.api.Test;
3334
import org.junit.jupiter.api.Tag;
3435
import java.io.IOException;
@@ -38,6 +39,13 @@ This file is part of the iText (R) project.
3839
public class FontProgramTest extends ExtendedITextTest {
3940
private static final String notExistingFont = "some-font.ttf";
4041

42+
@BeforeEach
43+
public void clearFonts(){
44+
FontProgramFactory.clearRegisteredFonts();
45+
FontProgramFactory.clearRegisteredFontFamilies();
46+
FontCache.clearSavedFonts();
47+
}
48+
4149
@Test
4250
public void exceptionMessageTest() throws IOException {
4351
Exception e = Assertions.assertThrows(java.io.IOException.class,
@@ -72,6 +80,15 @@ public void registerDirectoryType1Test() throws IOException {
7280
FontProgramFactory.registerFontDirectory("./src/test/resources/com/itextpdf/io/font/type1/");
7381
FontProgram computerModern = FontProgramFactory.createRegisteredFont("computer modern");
7482
FontProgram cmr10 = FontProgramFactory.createRegisteredFont("cmr10");
83+
Assertions.assertNull(computerModern);
84+
Assertions.assertNull(cmr10);
85+
}
86+
87+
@Test
88+
public void registerDirectoryType1RecursivelyTest() throws IOException {
89+
FontProgramFactory.registerFontDirectoryRecursively("./src/test/resources/com/itextpdf/io/font/type1/");
90+
FontProgram computerModern = FontProgramFactory.createRegisteredFont("computer modern");
91+
FontProgram cmr10 = FontProgramFactory.createRegisteredFont("cmr10");
7592
Assertions.assertNotNull(computerModern);
7693
Assertions.assertNotNull(cmr10);
7794
}

kernel/src/main/java/com/itextpdf/kernel/font/PdfFontFactory.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ public static PdfType3Font createType3Font(PdfDocument document, String fontName
425425
* @see PdfFontFactory#register(String, String)
426426
* @see PdfFontFactory#registerFamily(String, String, String)
427427
* @see PdfFontFactory#registerDirectory(String)
428+
* @see PdfFontFactory#registerDirectoryRecursively(String)
428429
* @see PdfFontFactory#registerSystemDirectories()
429430
* @see PdfFontFactory#getRegisteredFamilies()
430431
* @see PdfFontFactory#getRegisteredFonts()
@@ -453,6 +454,7 @@ public static PdfFont createRegisteredFont(String fontName, String encoding,
453454
* @see PdfFontFactory#register(String, String)
454455
* @see PdfFontFactory#registerFamily(String, String, String)
455456
* @see PdfFontFactory#registerDirectory(String)
457+
* @see PdfFontFactory#registerDirectoryRecursively(String)
456458
* @see PdfFontFactory#registerSystemDirectories()
457459
* @see PdfFontFactory#getRegisteredFamilies()
458460
* @see PdfFontFactory#getRegisteredFonts()
@@ -479,6 +481,7 @@ public static PdfFont createRegisteredFont(String fontName, String encoding,
479481
* @see PdfFontFactory#register(String, String)
480482
* @see PdfFontFactory#registerFamily(String, String, String)
481483
* @see PdfFontFactory#registerDirectory(String)
484+
* @see PdfFontFactory#registerDirectoryRecursively(String)
482485
* @see PdfFontFactory#registerSystemDirectories()
483486
* @see PdfFontFactory#getRegisteredFamilies()
484487
* @see PdfFontFactory#getRegisteredFonts()
@@ -506,6 +509,7 @@ public static PdfFont createRegisteredFont(String fontName, String encoding,
506509
* @see PdfFontFactory#register(String, String)
507510
* @see PdfFontFactory#registerFamily(String, String, String)
508511
* @see PdfFontFactory#registerDirectory(String)
512+
* @see PdfFontFactory#registerDirectoryRecursively(String)
509513
* @see PdfFontFactory#registerSystemDirectories()
510514
* @see PdfFontFactory#getRegisteredFamilies()
511515
* @see PdfFontFactory#getRegisteredFonts()
@@ -527,6 +531,7 @@ public static PdfFont createRegisteredFont(String fontName, String encoding,
527531
* @see PdfFontFactory#register(String, String)
528532
* @see PdfFontFactory#registerFamily(String, String, String)
529533
* @see PdfFontFactory#registerDirectory(String)
534+
* @see PdfFontFactory#registerDirectoryRecursively(String)
530535
* @see PdfFontFactory#registerSystemDirectories()
531536
* @see PdfFontFactory#getRegisteredFamilies()
532537
* @see PdfFontFactory#getRegisteredFonts()
@@ -546,6 +551,7 @@ public static PdfFont createRegisteredFont(String fontName, String encoding) thr
546551
* @see PdfFontFactory#register(String, String)
547552
* @see PdfFontFactory#registerFamily(String, String, String)
548553
* @see PdfFontFactory#registerDirectory(String)
554+
* @see PdfFontFactory#registerDirectoryRecursively(String)
549555
* @see PdfFontFactory#registerSystemDirectories()
550556
* @see PdfFontFactory#getRegisteredFamilies()
551557
* @see PdfFontFactory#getRegisteredFonts()
@@ -596,6 +602,17 @@ public static int registerDirectory(String dirPath) {
596602
return FontProgramFactory.registerFontDirectory(dirPath);
597603
}
598604

605+
606+
/**
607+
* Registers all the fonts in a directory recursively.
608+
*
609+
* @param dirPath the directory path to be registered as a font directory path
610+
* @return the number of fonts registered
611+
*/
612+
public static int registerDirectoryRecursively(String dirPath) {
613+
return FontProgramFactory.registerFontDirectoryRecursively(dirPath);
614+
}
615+
599616
/**
600617
* Register fonts in some probable directories. It usually works in Windows,
601618
* Linux and Solaris.

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfFontTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,17 @@ public void testFontDirectoryRegister() throws IOException {
19221922
pdfDoc.close();
19231923
}
19241924

1925+
@Test
1926+
public void fontDirectoryRegisterRecursivelyTest() throws IOException {
1927+
PdfFontFactory.registerDirectoryRecursively(sourceFolder);
1928+
for (String name : PdfFontFactory.getRegisteredFonts()) {
1929+
PdfFont pdfFont = PdfFontFactory.createRegisteredFont(name);
1930+
if (pdfFont == null) {
1931+
Assertions.assertTrue(false, "Font {" + name + "} can't be empty");
1932+
}
1933+
}
1934+
}
1935+
19251936
@Test
19261937
public void fontRegisterTest() throws IOException {
19271938
FontProgramFactory.registerFont(fontsFolder + "NotoSerif-Regular_v1.7.ttf", "notoSerifRegular");

0 commit comments

Comments
 (0)