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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#include "richtext.h"
static int matchIRCColourString(const char *str, int pos, int *result) {
// First, try to match a single digit.
if (str[pos] >= '0' && str[pos] <= '9') {
int first = str[pos] - '0';
if (str[pos + 1] >= '0' && str[pos + 1] <= '9') {
int second = str[pos + 1] - '0';
int attempt = (first * 10) + second;
if (attempt < 0 || attempt > 15) {
// Invalid number, give up here
return pos;
} else {
// Valid number!
*result = attempt;
return pos + 2;
}
} else {
// Second character was not a digit, so
// let's take the colour we *did* parse as is
*result = first;
return pos + 1;
}
} else {
// First character was not a digit, so pass entirely.
return pos;
}
}
static int matchIRCColourPair(const char *str, int pos, int *whichFG, int *whichBG) {
// str[pos] is the first character following the
// \x03 colour code.
*whichFG = -1;
*whichBG = -1;
pos = matchIRCColourString(str, pos, whichFG);
if (*whichFG != -1) {
if (str[pos] == ',') {
pos = matchIRCColourString(str, pos + 1, whichBG);
}
}
return pos;
}
void RichTextBuilder::appendIRC(const char *str) {
// We're up for fun here!
const int colLayer = COL_LEVEL_IRC;
bool b = false, i = false, u = false;
int activeFG = COL_DEFAULT_FG, activeBG = COL_DEFAULT_BG;
int pos = 0;
int len = strlen(str);
while (pos < len) {
char ch = str[pos];
if (ch < 0 || ch == 7 || ch == 10 || ch >= 0x20) {
// Pass this character through unfiltered.
writeU8(ch);
pos++;
continue;
}
// IRC control code, but what?
if (ch == 2) {
b = !b;
if (b)
bold();
else
endBold();
} else if (ch == 0x1D) {
i = !i;
if (i)
italic();
else
endItalic();
} else if (ch == 0x1F) {
u = !u;
if (u)
underline();
else
endUnderline();
} else if (ch == 0xF) {
// Reset all formatting
if (b) {
endBold();
b = false;
}
if (i) {
endItalic();
i = false;
}
if (u) {
endUnderline();
u = false;
}
if (activeFG != COL_DEFAULT_FG) {
endForeground(colLayer);
activeFG = COL_DEFAULT_FG;
}
if (activeBG != COL_DEFAULT_BG) {
endBackground(colLayer);
activeBG = COL_DEFAULT_BG;
}
} else if (ch == 0x16) {
// Reverse
int swap = activeBG;
activeBG = activeFG;
activeFG = swap;
if (activeFG == COL_DEFAULT_FG)
endForeground(colLayer);
else
foreground(colLayer, activeFG);
if (activeBG == COL_DEFAULT_BG)
endBackground(colLayer);
else
background(colLayer, activeBG);
} else if (ch == 3) {
// Colours!
int whichFG = -1, whichBG = -1;
pos = matchIRCColourPair(str, pos + 1, &whichFG, &whichBG);
if (whichFG == -1) {
if (activeFG != COL_DEFAULT_FG) {
activeFG = COL_DEFAULT_FG;
endForeground(colLayer);
}
} else {
activeFG = whichFG;
foreground(colLayer, whichFG);
}
if (whichBG == -1) {
if (whichFG == -1 && activeBG != COL_DEFAULT_BG) {
activeBG = COL_DEFAULT_BG;
endBackground(colLayer);
}
} else {
activeBG = whichBG;
background(colLayer, whichBG);
}
continue;
}
pos++;
}
// Clean up any leftover state.
if (b)
endBold();
if (i)
endItalic();
if (u)
endUnderline();
if (activeFG != COL_DEFAULT_FG)
endForeground(colLayer);
if (activeBG != COL_DEFAULT_BG)
endBackground(colLayer);
}
const char *RichTextBuilder::c_str() {
if (size() == 0 || data()[size() - 1] != 0)
writeU8(0);
return data();
}
|