Skip to content

Commit 7b7beaf

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Allow font cache to be cleared
DEVSIX-3216 Autoported commit. Original commit hash: [c2b2e7f65]
1 parent 5bbb74e commit 7b7beaf

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using iText.IO.Font.Otf;
3+
using iText.Test;
4+
5+
namespace iText.IO.Font {
6+
public class FontCacheTest : ExtendedITextTest {
7+
[NUnit.Framework.Test]
8+
public virtual void ClearFontCacheTest() {
9+
String fontName = "FreeSans.ttf";
10+
NUnit.Framework.Assert.IsNull(FontCache.GetFont(fontName));
11+
FontProgram fontProgram = new FontCacheTest.FontProgramMock();
12+
FontCache.SaveFont(fontProgram, fontName);
13+
NUnit.Framework.Assert.AreEqual(fontProgram, FontCache.GetFont(fontName));
14+
FontCache.ClearSavedFonts();
15+
NUnit.Framework.Assert.IsNull(FontCache.GetFont(fontName));
16+
}
17+
18+
private class FontProgramMock : FontProgram {
19+
public override int GetPdfFontFlags() {
20+
return 0;
21+
}
22+
23+
public override int GetKerning(Glyph first, Glyph second) {
24+
return 0;
25+
}
26+
}
27+
}
28+
}

itext/itext.io/itext/io/font/FontCache.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ public static CMapCidByte GetCid2Byte(String cmap) {
150150
return ParseCmap(cmap, cidByte);
151151
}
152152

153+
/// <summary>
154+
/// Clears the cache by removing fonts that were added via
155+
/// <see cref="SaveFont(FontProgram, System.String)"/>
156+
/// .
157+
/// <para />
158+
/// Be aware that in multithreading environment this method call will affect the result of
159+
/// <see cref="GetFont(System.String)"/>
160+
/// .
161+
/// This in its turn affects creation of fonts via factories when
162+
/// <c>cached</c>
163+
/// argument is set to true (which is by default).
164+
/// </summary>
165+
public static void ClearSavedFonts() {
166+
fontCache.Clear();
167+
}
168+
153169
public static FontProgram GetFont(String fontName) {
154170
return fontCache.Get(FontCacheKey.Create(fontName));
155171
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3b0d07f7b864be476b654b2344f46cf01fd20131
1+
c2b2e7f65cb8c55d184420181403551267812a16

0 commit comments

Comments
 (0)