Browse Source

Fixed text editor caret positioning bug. (occured with fonts that had different font withs for bold and plain chars)

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@292 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Mike Krüger 21 years ago
parent
commit
2344eff173
  1. 14
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/TextWord.cs
  2. 12
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/LineManager/LineSegment.cs
  3. 41
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs

14
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/TextWord.cs

@ -42,6 +42,13 @@ namespace ICSharpCode.TextEditor.Document @@ -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 @@ -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 @@ -129,7 +141,7 @@ namespace ICSharpCode.TextEditor.Document
}
}
public Font Font {
public virtual Font Font {
get {
return color.Font;
}

12
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/LineManager/LineSegment.cs

@ -19,6 +19,18 @@ namespace ICSharpCode.TextEditor.Document @@ -19,6 +19,18 @@ namespace ICSharpCode.TextEditor.Document
List<TextWord> words = null;
Stack<Span> 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;

41
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs

@ -630,6 +630,40 @@ namespace ICSharpCode.TextEditor @@ -630,6 +630,40 @@ namespace ICSharpCode.TextEditor
return (float)charWitdh[ch];
}
Dictionary<Font, Dictionary<char, float>> fontBoundCharWidth = new Dictionary<Font, Dictionary<char, float>>();
public float GetWidth(char ch, Font font)
{
if (!fontBoundCharWidth.ContainsKey(font)) {
fontBoundCharWidth.Add(font, new Dictionary<char, float>());
}
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<char, float>());
}
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 @@ -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;
}
}

Loading…
Cancel
Save