Results 1 to 4 of 4

Thread: [RESOLVED] how to cache Font

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Resolved [RESOLVED] how to cache Font

    The DataGridView source caches pen and brush, but I didn't see they cache Font, what is the considerations? How to do properly?

    Code:
    private Hashtable pens;
    private Hashtable brushes;
    internal SolidBrush GetCachedBrush(Color color)
    	{
    		SolidBrush solidBrush = (SolidBrush)brushes[color];
    		if (solidBrush == null)
    		{
    			solidBrush = new SolidBrush(color);
    			brushes.Add(color, solidBrush);
    		}
    		return solidBrush;
    	}
    
    	internal Pen GetCachedPen(Color color)
    	{
    		Pen pen = (Pen)pens[color];
    		if (pen == null)
    		{
    			pen = new Pen(color);
    			pens.Add(color, pen);
    		}
    		return pen;
    	}

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: how to cache Font

    In that code, each Pen and Brush are identified simply by a Color value. Color is a simple value type so is easy to use as a key in a Hashtable. You likely do not have any single value you can use to distinguish multiple Fonts.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: how to cache Font

    Quote Originally Posted by jmcilhinney View Post
    In that code, each Pen and Brush are identified simply by a Color value. Color is a simple value type so is easy to use as a key in a Hashtable. You likely do not have any single value you can use to distinguish multiple Fonts.
    For Brush cache, it is easier because the key is Color; for Pen cache, it has Color and width, but MS 'smartly' makes thing simply and cache width = 1 with no specific style only.
    For Font cache, I saw someone doing like this:

    private Dictionary<string, Font> _fontCache;

    _fontCache = new Dictionary<string, Font>();

    protected virtual Font GetFont(string fontFamilyName)
    {
    lock (_fontCache)
    {
    if (!_fontCache.ContainsKey(fontFamilyName))
    {
    Font font;

    font = this.GetFont(fontFamilyName, FontStyle.Regular);
    if (font == null)
    font = this.GetFont(fontFamilyName, FontStyle.Bold);
    if (font == null)
    font = this.GetFont(fontFamilyName, FontStyle.Italic);
    if (font == null)
    font = this.GetFont(fontFamilyName, FontStyle.Bold | FontStyle.Italic);
    if (font == null)
    font = (Font)this.Font.Clone();

    _fontCache.Add(fontFamilyName, font);
    }
    }

    return _fontCache[fontFamilyName];
    }

    protected virtual Font GetFont(string fontFamilyName, FontStyle fontStyle)
    {
    Font font;

    try
    {
    font = new Font(fontFamilyName, this.PreviewFontSize, fontStyle);
    }
    catch
    {
    font = null;
    }

    return font;
    }
    protected virtual void ClearFontCache()
    {
    if (_fontCache != null)
    {
    foreach (string key in _fontCache.Keys)
    _fontCache[key].Dispose();
    _fontCache.Clear();
    }
    }

    it means he only cache Font with fontFamilyName, it is not practical for my case.
    Last edited by DaveDavis; Nov 25th, 2022 at 02:46 AM.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: how to cache Font

    btw, how many Font can I cache considering performance/memory balance for a grid control? How many bytes occupied for one Font? Right now the array CacheFonts[] is a dynamic increase, but I set the max is 255 though never reach but who knows. I possible to sort up unused when reach 100.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width