Browse Source

Merge pull request #668 from donandren/issues/658

SetForegroundBrush in skia uses the smallest Length first. fixes #658
pull/671/head
danwalmsley 10 years ago
committed by GitHub
parent
commit
06bacaa7ee
  1. 54
      src/Skia/Avalonia.Skia/FormattedTextImpl.cs

54
src/Skia/Avalonia.Skia/FormattedTextImpl.cs

@ -165,14 +165,16 @@ namespace Avalonia.Skia
public void SetForegroundBrush(IBrush brush, int startIndex, int length) public void SetForegroundBrush(IBrush brush, int startIndex, int length)
{ {
var key = new FBrushRange(startIndex, length); var key = new FBrushRange(startIndex, length);
if (brush == null) int index = _foregroundBrushes.FindIndex(v => v.Key.Equals(key));
if (index > -1)
{ {
if (_foregroundBrushes.ContainsKey(key)) _foregroundBrushes.RemoveAt(index);
_foregroundBrushes.Remove(key);
} }
else
if (brush != null)
{ {
_foregroundBrushes[key] = brush; _foregroundBrushes.Insert(0, new KeyValuePair<FBrushRange, IBrush>(key, brush));
} }
} }
@ -275,8 +277,8 @@ namespace Avalonia.Skia
private const float MAX_LINE_WIDTH = 10000; private const float MAX_LINE_WIDTH = 10000;
private readonly Dictionary<FBrushRange, IBrush> _foregroundBrushes = private readonly List<KeyValuePair<FBrushRange, IBrush>> _foregroundBrushes =
new Dictionary<FBrushRange, IBrush>(); new List<KeyValuePair<FBrushRange, IBrush>>();
private readonly List<FormattedTextLine> _lines = new List<FormattedTextLine>(); private readonly List<FormattedTextLine> _lines = new List<FormattedTextLine>();
private readonly SKPaint _paint; private readonly SKPaint _paint;
private readonly List<Rect> _rects = new List<Rect>(); private readonly List<Rect> _rects = new List<Rect>();
@ -449,27 +451,36 @@ namespace Avalonia.Skia
if (_foregroundBrushes.Any()) if (_foregroundBrushes.Any())
{ {
var cbi = _foregroundBrushes.FirstOrDefault(b => b.Key.Intersects(index, len)); var bi = _foregroundBrushes.FindIndex(b =>
b.Key.StartIndex <= index &&
b.Key.EndIndex > index
);
if (cbi.Value != null) if (bi > -1)
{ {
var r = cbi.Key; var match = _foregroundBrushes[bi];
if (r.StartIndex > index) len = match.Key.EndIndex - index + 1;
{ result = match.Value;
len = r.StartIndex - index;
}
else
{
len = r.EndIndex - index + 1;
result = cbi.Value;
}
if (len > 0 && len < length) if (len > 0 && len < length)
{ {
length = len; length = len;
} }
} }
int endIndex = index + length;
int max = bi == -1 ? _foregroundBrushes.Count : bi;
var next = _foregroundBrushes.Take(max)
.Where(b => b.Key.StartIndex < endIndex &&
b.Key.StartIndex > index)
.OrderBy(b => b.Key.StartIndex)
.FirstOrDefault();
if (next.Value != null)
{
length = next.Key.StartIndex - index;
}
} }
return result; return result;
@ -632,6 +643,11 @@ namespace Avalonia.Skia
public bool Intersects(int index, int len) => public bool Intersects(int index, int len) =>
(index + len) > StartIndex && (index + len) > StartIndex &&
(StartIndex + Length) > index; (StartIndex + Length) > index;
public override string ToString()
{
return $"{StartIndex}-{EndIndex}";
}
} }
} }
} }
Loading…
Cancel
Save