Skip to content

Commit c2b2e7f

Browse files
Allow font cache to be cleared
DEVSIX-3216
1 parent 3b0d07f commit c2b2e7f

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ This file is part of the iText (R) project.
5454
import com.itextpdf.io.font.constants.FontResources;
5555
import com.itextpdf.io.util.IntHashtable;
5656
import com.itextpdf.io.util.ResourceUtil;
57-
5857
import java.io.InputStream;
5958
import java.util.HashMap;
6059
import java.util.HashSet;
@@ -148,6 +147,16 @@ public static CMapCidByte getCid2Byte(String cmap) {
148147
return parseCmap(cmap, cidByte);
149148
}
150149

150+
/**
151+
* Clears the cache by removing fonts that were added via {@link #saveFont(FontProgram, String)}.
152+
* <p>
153+
* Be aware that in multithreading environment this method call will affect the result of {@link #getFont(String)}.
154+
* This in its turn affects creation of fonts via factories when {@code cached} argument is set to true (which is by default).
155+
*/
156+
public static void clearSavedFonts() {
157+
fontCache.clear();
158+
}
159+
151160
public static FontProgram getFont(String fontName) {
152161
return fontCache.get(FontCacheKey.create(fontName));
153162
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.itextpdf.io.font;
2+
3+
import com.itextpdf.io.font.otf.Glyph;
4+
import com.itextpdf.test.ExtendedITextTest;
5+
import com.itextpdf.test.annotations.type.UnitTest;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.experimental.categories.Category;
9+
10+
@Category(UnitTest.class)
11+
public class FontCacheTest extends ExtendedITextTest {
12+
13+
@Test
14+
public void clearFontCacheTest() {
15+
String fontName = "FreeSans.ttf";
16+
Assert.assertNull(FontCache.getFont(fontName));
17+
18+
FontProgram fontProgram = new FontProgramMock();
19+
FontCache.saveFont(fontProgram, fontName);
20+
Assert.assertEquals(fontProgram, FontCache.getFont(fontName));
21+
22+
FontCache.clearSavedFonts();
23+
Assert.assertNull(FontCache.getFont(fontName));
24+
}
25+
26+
private static class FontProgramMock extends FontProgram {
27+
28+
@Override
29+
public int getPdfFontFlags() {
30+
return 0;
31+
}
32+
33+
@Override
34+
public int getKerning(Glyph first, Glyph second) {
35+
return 0;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)