File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed
main/java/com/itextpdf/io/font
test/java/com/itextpdf/io/font Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -54,7 +54,6 @@ This file is part of the iText (R) project.
5454import com .itextpdf .io .font .constants .FontResources ;
5555import com .itextpdf .io .util .IntHashtable ;
5656import com .itextpdf .io .util .ResourceUtil ;
57-
5857import java .io .InputStream ;
5958import java .util .HashMap ;
6059import 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 }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments