Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML

.vs
46 changes: 46 additions & 0 deletions FastColoredTextBox/CharSizeCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FastColoredTextBoxNS
{
/// <summary>
/// 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
/// </summary>
static class CharSizeCache
{
static readonly Dictionary<string, SizeF> cache = new Dictionary<string, SizeF>();
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];
}

/// <summary>
/// Font is disposable, so we need to indentify it without keeping manged resources
/// </summary>
/// <param name="font"></param>
/// <param name="c"></param>
/// <returns></returns>
private static string GetKey(Font font, char c)
{
return font.FontFamily.Name
+ ":" + font.Size
+ ":" + font.Style
+ ":" + font.Unit
+ ":" + font.GdiCharSet
+ ":" + font.GdiVerticalFont
+ ":" + c;
}
}
}
5 changes: 1 addition & 4 deletions FastColoredTextBox/FastColoredTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
1 change: 1 addition & 0 deletions FastColoredTextBox/FastColoredTextBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
</Compile>
<Compile Include="Bookmarks.cs" />
<Compile Include="Char.cs" />
<Compile Include="CharSizeCache.cs" />
<Compile Include="DocumentMap.cs">
<SubType>Component</SubType>
</Compile>
Expand Down
12 changes: 12 additions & 0 deletions FastColoredTextBox/FastColoredTextBox.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Technica.FCTB</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<description>$description$</description>
<copyright>$copyright$</copyright>
</metadata>
</package>