public static Gdi32Font GetPoolingFont(string faceName, FontSizeUnit fontSizeUnit, float size, Gdi32FontStyleInfo fontStyle, byte charSet, Gdi32FontQuality fontQuality) { lock (FontCache) { var key = Gdi32Font.ToFontCacheKey(faceName, fontSizeUnit, size, fontStyle, charSet, fontQuality); var font = new Gdi32Font(key); for (var node = FontCache.First; node != null; node = node.Next) { if (node.Value.Key == key) { if (FontCache.First != node) { FontCache.Remove(node); FontCache.AddFirst(node); } return(font); } } FontCache.AddFirst(new FontCacheEntry(key, font)); while (FontCache.Count > 20) { var lastNode = FontCache.Last; lastNode.Value.Font.Dispose(); FontCache.Remove(lastNode); } return(font); } }
public FontCacheEntry(Gdi32Font.FontCacheKey key, Gdi32Font font) { Key = key ?? throw new ArgumentNullException(nameof(key)); Font = font ?? throw new ArgumentNullException(nameof(font)); }