-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenStyle.cs
99 lines (80 loc) · 3.58 KB
/
TokenStyle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Drawing;
using FastColoredTextBoxNS;
using Merthsoft.Extensions;
namespace Merthsoft.Tokens {
public class ErrorStyle : TokenStyle {
public static new TokenStyle Default { get { return new ErrorStyle((Brush)Brushes.Black.Clone(), null, FontStyle.Regular, Pens.Red); } }
public ErrorStyle(Brush foreBrush, Brush backgroundBrush, FontStyle fontStyle, Pen errorUnderlinePen)
: base(foreBrush, backgroundBrush, fontStyle, errorUnderlinePen, -1) {
}
public ErrorStyle(TokenStyle tokenStyle)
: base(tokenStyle.ForeBrush, tokenStyle.BackgroundBrush, tokenStyle.FontStyle, Pens.Red, -1) {
}
public override void Draw(System.Drawing.Graphics gr, System.Drawing.Point position, Range range) {
base.Draw(gr, position, range);
int width = (range.End.iChar - range.Start.iChar) * range.tb.CharWidth;
int height = range.tb.CharHeight;
int dx = range.tb.CharWidth;
for (int i = 0; i < width; i += dx) {
gr.DrawLine(TokenUnderlinePen, position.X + i, position.Y + height-2, position.X + i + dx / 2, position.Y + height);
gr.DrawLine(TokenUnderlinePen, position.X + i + dx / 2, position.Y + height, position.X + i + dx, position.Y + height-2);
}
}
}
public class TokenStyle : TextStyle {
public int MinTokenLengthToUnderline { get; set; }
public Pen TokenUnderlinePen { get; set; }
public static TokenStyle Default { get { return new TokenStyle((Brush)Brushes.Black.Clone(), null, FontStyle.Regular, null, 2); } }
public TokenStyle(Brush foreBrush, Brush backgroundBrush, FontStyle fontStyle, Pen tokenUnderlinePen, int minTokenLengthToUnderline)
: base(foreBrush, backgroundBrush, fontStyle) {
MinTokenLengthToUnderline = minTokenLengthToUnderline == -1 ? int.MaxValue : minTokenLengthToUnderline;
TokenUnderlinePen = tokenUnderlinePen ?? Pens.DarkGray;
}
public static TokenStyle FromTokenDataStyle(TokenData.Style s) {
Brush foreBrush = BrushFromString(s.Foreground);
Brush backBrush = BrushFromString(s.Background);
Pen underPen = PenFromString(s.TokenUnderlineColor);
int minTok = s.MinTokenLength;
FontStyle fs = FontStyle.Regular;
if (s.Bold) { fs |= FontStyle.Bold; }
if (s.Italic) { fs |= FontStyle.Italic; }
if (s.Underline) { fs |= FontStyle.Underline; }
if (s.Strike) { fs |= FontStyle.Strikeout; }
return new TokenStyle(foreBrush, backBrush, fs, underPen, minTok);
}
private static Brush BrushFromString(string s) {
if (s == null) { return null; }
Brush b;
if (HexHelper.IsHexString(s)) {
b = new SolidBrush(Color.FromArgb(HexHelper.GetInt(s)));
} else {
b = new SolidBrush(Color.FromName(s));
}
return b;
}
private static Pen PenFromString(string s) {
if (s == null) { return null; }
Pen p;
if (HexHelper.IsHexString(s)) {
p = new Pen(Color.FromArgb(HexHelper.GetInt(s)));
} else {
p = new Pen(Color.FromName(s));
}
return p;
}
public override void Draw(System.Drawing.Graphics gr, System.Drawing.Point position, Range range) {
base.Draw(gr, position, range);
if (range.End.iChar - range.Start.iChar < MinTokenLengthToUnderline) {
return;
}
int width = (range.End.iChar - range.Start.iChar) * range.tb.CharWidth;
int height = range.tb.CharHeight;
// Bottom line
gr.DrawLine(TokenUnderlinePen, position.X, position.Y + height, position.X + width - 2, position.Y + height);
// Left line
gr.DrawLine(TokenUnderlinePen, position.X, position.Y + height - 3, position.X, position.Y + height);
// Right line
gr.DrawLine(TokenUnderlinePen, position.X + width - 2, position.Y + height - 3, position.X + width - 2, position.Y + height);
}
}
}