From 3435cdef0a7c766250fd904dda66b485a925c630 Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Mon, 4 Nov 2019 13:23:15 +0100 Subject: [PATCH] Fix GetCharSize consuming too much rendering power --- .gitignore | 2 + FastColoredTextBox/CharSizeCache.cs | 46 ++++++++++++++++++++ FastColoredTextBox/FastColoredTextBox.cs | 5 +-- FastColoredTextBox/FastColoredTextBox.csproj | 1 + FastColoredTextBox/FastColoredTextBox.nuspec | 12 +++++ 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 FastColoredTextBox/CharSizeCache.cs create mode 100644 FastColoredTextBox/FastColoredTextBox.nuspec diff --git a/.gitignore b/.gitignore index bdc3535f..b25ac44d 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,5 @@ Generated_Code #added for RIA/Silverlight projects _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML + +.vs diff --git a/FastColoredTextBox/CharSizeCache.cs b/FastColoredTextBox/CharSizeCache.cs new file mode 100644 index 00000000..51dad64f --- /dev/null +++ b/FastColoredTextBox/CharSizeCache.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Text; +using System.Windows.Forms; + +namespace FastColoredTextBoxNS +{ + /// + /// Calling MeasureText on each char is extremly slow + /// Thankfully the size of the char does not change for the same font + /// However we would still need to indentify the font somehow + /// + static class CharSizeCache + { + static readonly Dictionary cache = new Dictionary(); + internal static SizeF GetCharSize(Font font, char c) + { + var key = GetKey(font, c); + if (!cache.ContainsKey(key)) + { + Size sz2 = TextRenderer.MeasureText("<" + c.ToString() + ">", font); + Size sz3 = TextRenderer.MeasureText("<>", font); + cache[key] = new SizeF(sz2.Width - sz3.Width + 1, /*sz2.Height*/font.Height); + } + return cache[key]; + } + + /// + /// Font is disposable, so we need to indentify it without keeping manged resources + /// + /// + /// + /// + private static string GetKey(Font font, char c) + { + return font.FontFamily.Name + + ":" + font.Size + + ":" + font.Style + + ":" + font.Unit + + ":" + font.GdiCharSet + + ":" + font.GdiVerticalFont + + ":" + c; + } + } +} diff --git a/FastColoredTextBox/FastColoredTextBox.cs b/FastColoredTextBox/FastColoredTextBox.cs index d7e2a1e9..5fcd8994 100644 --- a/FastColoredTextBox/FastColoredTextBox.cs +++ b/FastColoredTextBox/FastColoredTextBox.cs @@ -2901,10 +2901,7 @@ internal int GetOrSetStyleLayerIndex(Style style) public static SizeF GetCharSize(Font font, char c) { - Size sz2 = TextRenderer.MeasureText("<" + c.ToString() + ">", font); - Size sz3 = TextRenderer.MeasureText("<>", font); - - return new SizeF(sz2.Width - sz3.Width + 1, /*sz2.Height*/font.Height); + return CharSizeCache.GetCharSize(font, c); } [DllImport("Imm32.dll")] diff --git a/FastColoredTextBox/FastColoredTextBox.csproj b/FastColoredTextBox/FastColoredTextBox.csproj index 0707e4e2..78ca92db 100644 --- a/FastColoredTextBox/FastColoredTextBox.csproj +++ b/FastColoredTextBox/FastColoredTextBox.csproj @@ -53,6 +53,7 @@ + Component diff --git a/FastColoredTextBox/FastColoredTextBox.nuspec b/FastColoredTextBox/FastColoredTextBox.nuspec new file mode 100644 index 00000000..f6a75ef7 --- /dev/null +++ b/FastColoredTextBox/FastColoredTextBox.nuspec @@ -0,0 +1,12 @@ + + + + Technica.FCTB + $version$ + $title$ + $author$ + $author$ + $description$ + $copyright$ + + \ No newline at end of file