diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/TextWord.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/TextWord.cs index dc708a04a9..3dd2974e6a 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/TextWord.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/TextWord.cs @@ -42,6 +42,13 @@ namespace ICSharpCode.TextEditor.Document length = 1; base.color = color; } + + public override Font Font { + get { + return null; + } + } + public override TextWordType Type { get { return TextWordType.Space; @@ -66,6 +73,11 @@ namespace ICSharpCode.TextEditor.Document base.color = color; } + public override Font Font { + get { + return null; + } + } public override TextWordType Type { get { @@ -129,7 +141,7 @@ namespace ICSharpCode.TextEditor.Document } } - public Font Font { + public virtual Font Font { get { return color.Font; } diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/LineManager/LineSegment.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/LineManager/LineSegment.cs index 65e09e0d8d..84f733c7a6 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/LineManager/LineSegment.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/LineManager/LineSegment.cs @@ -19,6 +19,18 @@ namespace ICSharpCode.TextEditor.Document List words = null; Stack highlightSpanStack = null; + public TextWord GetWord(int column) + { + int curColumn = 0; + foreach (TextWord word in words) { + if (column < curColumn + word.Length) { + return word; + } + curColumn += word.Length; + } + return null; + } + public override int Length { get { return length - delimiterLength; diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs index ee2b705bc3..677a0e1e22 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs @@ -630,6 +630,40 @@ namespace ICSharpCode.TextEditor return (float)charWitdh[ch]; } + Dictionary> fontBoundCharWidth = new Dictionary>(); + public float GetWidth(char ch, Font font) + { + if (!fontBoundCharWidth.ContainsKey(font)) { + fontBoundCharWidth.Add(font, new Dictionary()); + } + if (!fontBoundCharWidth[font].ContainsKey(ch)) { + using (Graphics g = textArea.CreateGraphics()) { + return GetWidth(g, ch, font); + } + } + return (float)fontBoundCharWidth[font][ch]; + } + + public float GetWidth(string text, Font font) + { + float width = 0; + for (int i = 0; i < text.Length; ++i) { + width += GetWidth(text[i], font); + } + return width; + } + + public float GetWidth(Graphics g, char ch, Font font) + { + if (!fontBoundCharWidth.ContainsKey(font)) { + fontBoundCharWidth.Add(font, new Dictionary()); + } + if (!fontBoundCharWidth[font].ContainsKey(ch)) { + fontBoundCharWidth[font].Add(ch, g.MeasureString(ch.ToString(), font, 2000, measureStringFormat).Width); + } + return (float)fontBoundCharWidth[font][ch]; + } + public int GetVisualColumn(int logicalLine, int logicalColumn) { return GetVisualColumn(Document.GetLineSegment(logicalLine), logicalColumn); @@ -842,7 +876,12 @@ namespace ICSharpCode.TextEditor break; default: ++column; - drawingPos += GetWidth(ch); + TextWord word = line.GetWord(j); + if (word == null || word.Font == null) { + drawingPos += GetWidth(ch); + } else { + drawingPos += GetWidth(ch, word.Font); + } break; } }