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
length = 1; length = 1;
base.color = color; base.color = color;
} }
public override Font Font {
get {
return null;
}
}
public override TextWordType Type { public override TextWordType Type {
get { get {
return TextWordType.Space; return TextWordType.Space;
@ -66,6 +73,11 @@ namespace ICSharpCode.TextEditor.Document
base.color = color; base.color = color;
} }
public override Font Font {
get {
return null;
}
}
public override TextWordType Type { public override TextWordType Type {
get { get {
@ -129,7 +141,7 @@ namespace ICSharpCode.TextEditor.Document
} }
} }
public Font Font { public virtual Font Font {
get { get {
return color.Font; return color.Font;
} }

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

@ -19,6 +19,18 @@ namespace ICSharpCode.TextEditor.Document
List<TextWord> words = null; List<TextWord> words = null;
Stack<Span> highlightSpanStack = 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 { public override int Length {
get { get {
return length - delimiterLength; return length - delimiterLength;

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

@ -630,6 +630,40 @@ namespace ICSharpCode.TextEditor
return (float)charWitdh[ch]; 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) public int GetVisualColumn(int logicalLine, int logicalColumn)
{ {
return GetVisualColumn(Document.GetLineSegment(logicalLine), logicalColumn); return GetVisualColumn(Document.GetLineSegment(logicalLine), logicalColumn);
@ -842,7 +876,12 @@ namespace ICSharpCode.TextEditor
break; break;
default: default:
++column; ++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; break;
} }
} }

Loading…
Cancel
Save