-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathGUIFont.cpp
More file actions
417 lines (349 loc) · 9.72 KB
/
GUIFont.cpp
File metadata and controls
417 lines (349 loc) · 9.72 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "GUI.h"
#include <cassert>
using namespace RTE;
GUIFont::GUIFont(const std::string& Name) {
m_Screen = nullptr;
m_Font = nullptr;
m_FontHeight = 0;
m_Name = Name;
m_Kerning = 0;
m_Leading = 0;
m_ColorCache.clear();
m_MainColor = 15; // Color index of the main font color
m_CurrentColor = m_MainColor;
m_CurrentBitmap = nullptr;
m_CharIndexCap = 256;
}
bool GUIFont::Load(GUIScreen* Screen, const std::string& Filename) {
assert(Screen);
m_Screen = Screen;
// Load the font image
m_Font = m_Screen->CreateBitmap(Filename);
if (!m_Font) {
return false;
}
m_CurrentBitmap = m_Font;
// Clear the cache
m_ColorCache.clear();
// Convert the MainColor
m_MainColor = Screen->ConvertColor(m_MainColor, m_CurrentBitmap->GetColorDepth());
m_CurrentColor = m_MainColor;
// Set the color key to be the same color as the Top-Right hand corner pixel
unsigned long BackG = m_Font->GetPixel(m_Font->GetWidth() - 1, 0);
m_Font->SetColorKey(BackG);
// The red separator MUST be on the Top-Left hand corner
unsigned long Red = m_Font->GetPixel(0, 0);
// Find the separating gap of the font lines
int y;
m_FontHeight = 0;
for (y = 1; y < m_Font->GetHeight(); y++) {
if (m_Font->GetPixel(0, y) == Red) {
m_FontHeight = y;
break;
}
}
// Pre-calculate the character details
memset(m_Characters, 0, sizeof(Character) * 256);
int x = 1;
y = 0;
int charOnLine = 0;
for (int chr = 32; chr < 256; chr++) {
m_Characters[chr].m_Offset = x;
// Find the next red pixel
int w = 0;
for (int n = x; n < m_Font->GetWidth(); n++, w++) {
if (m_Font->GetPixel(n, y) == Red) {
break;
}
}
// Calculate the maximum height of the character
int Height = 0;
for (int j = y; j < y + m_FontHeight; j++) {
for (int i = x; i < x + w; i++) {
unsigned long Pixel = m_Font->GetPixel(i, j);
if (Pixel != Red && Pixel != BackG) {
Height = std::max(Height, j - y);
}
}
}
m_Characters[chr].m_Height = Height;
m_Characters[chr].m_Width = w - 1;
x += w + 1;
m_CharIndexCap = chr + 1;
charOnLine++;
if (charOnLine >= 16) {
charOnLine = 0;
x = 1;
y += m_FontHeight;
// Stop if we run out of bitmap
if ((y + m_FontHeight) > m_Font->GetHeight()) {
break;
}
}
}
return true;
}
void GUIFont::Draw(GUIBitmap* Bitmap, int X, int Y, const std::string& Text, unsigned long Shadow) {
unsigned char c;
GUIRect Rect;
GUIBitmap* Surf = m_CurrentBitmap;
int initX = X;
assert(Surf);
// Make the shadow color
FontColor* FSC = nullptr;
if (Shadow) {
FSC = GetFontColor(Shadow);
if (!FSC) {
CacheColor(Shadow);
FSC = GetFontColor(Shadow);
}
}
// Go through every character
for (int i = 0; i < Text.length(); i++) {
c = Text.at(i);
if (c == '\n') {
Y += m_FontHeight;
X = initX;
}
if (c == '\t') {
X += m_Characters[' '].m_Width * 4;
}
if (c < 32 || c >= m_CharIndexCap) {
continue;
}
int CharWidth = m_Characters[c].m_Width;
int offX = m_Characters[c].m_Offset;
int offY = ((c - 32) / 16) * m_FontHeight;
SetRect(&Rect, offX, offY, offX + CharWidth, offY + m_FontHeight);
// Draw the shadow
if (Shadow && FSC) {
FSC->m_Bitmap->DrawTrans(Bitmap, X + 1, Y + 1, &Rect);
}
// Draw the main color
Surf->DrawTrans(Bitmap, X, Y, &Rect);
// Find the starting position
X += CharWidth + m_Kerning;
}
}
void GUIFont::DrawAligned(GUIBitmap* Bitmap, int X, int Y, const std::string& Text, int HAlign, int VAlign, int MaxWidth, unsigned long Shadow) {
std::string TextLine = Text;
int lineStartPos = 0;
int lineEndPos = 0;
int lineWidth = 0;
int yLine = Y;
int lastSpacePos = 0;
// Adjust the starting of the Y based on vertical alignment
if (VAlign == Middle) {
yLine -= (CalculateHeight(TextLine, MaxWidth) / 2);
} else if (VAlign == Bottom) {
yLine -= CalculateHeight(TextLine, MaxWidth);
}
while (lineStartPos < Text.size()) {
// Find the next newline, if any
lineEndPos = Text.find('\n', lineStartPos);
// Grab the whole line
TextLine = Text.substr(lineStartPos, (lineEndPos == std::string::npos ? Text.size() : lineEndPos) - lineStartPos);
// Figure its width, in pixels
lineWidth = CalculateWidth(TextLine);
// See if it's too wide to fit within the maxWidth
if (MaxWidth > 0 && lineWidth > MaxWidth) {
// Find the last space that makes the line within the max width
do {
// Find last space
lastSpacePos = Text.rfind(' ', lineEndPos - 1);
// if there was no space encountered before hitting beginning, then just leave the too long line as is and let it exceed the max width
if (lastSpacePos == std::string::npos || lastSpacePos <= lineStartPos) {
lineEndPos = Text.size();
break;
}
// Update the new end position
lineEndPos = lastSpacePos;
// Get the new, shorter line
TextLine = Text.substr(lineStartPos, lineEndPos - lineStartPos);
// Figure the new line width, in pixels
lineWidth = CalculateWidth(TextLine);
} while (lineWidth > MaxWidth);
// Update the new start position for next line
lineStartPos = lineEndPos + 1;
// Make sure it's not starting on a space
while (lineStartPos < Text.size() && Text.at(lineStartPos) == ' ') {
lineStartPos++;
}
} else {
// Advance the line start to the next line, or to the end of the text if there are no more lines
lineStartPos = lineEndPos == std::string::npos ? Text.size() : (lineEndPos + 1);
}
// If the line is scrolled above the bitmap top, then don't try to draw anything
if ((yLine + m_FontHeight) >= 0) {
switch (HAlign) {
// Left HAlignment: Where X is the starting point of the text
case Left:
Draw(Bitmap, X, yLine, TextLine, Shadow);
break;
// Center HAlignment: Where X is the center point of the text
case Centre:
Draw(Bitmap, X - lineWidth / 2, yLine, TextLine, Shadow);
break;
// Right HAlignment: Where X is the end point of the text
case Right:
Draw(Bitmap, X - lineWidth, yLine, TextLine, Shadow);
break;
default:
break;
}
}
// Add the height
yLine += m_FontHeight;
}
}
void GUIFont::SetColor(unsigned long Color) {
// Only check the change if the color is different
if (Color != m_CurrentColor) {
// Find the cached color
FontColor* FC = GetFontColor(Color);
// Use the cached color, otherwise just draw the default bitmap
if (FC) {
m_CurrentBitmap = FC->m_Bitmap;
m_CurrentColor = Color;
}
}
}
int GUIFont::CalculateWidth(const std::string& Text) {
unsigned char c;
int Width = 0;
int WidestLine = 0;
// Go through every character
for (int i = 0; i < Text.length(); i++) {
c = Text.at(i);
// Reset line counting if newline encountered
if (c == '\n') {
if (Width > WidestLine) {
WidestLine = Width;
}
Width = 0;
continue;
}
if (c < 32 || c >= m_CharIndexCap) {
continue;
}
Width += m_Characters[c].m_Width;
// Add kerning
Width += m_Kerning;
}
if (Width > WidestLine) {
WidestLine = Width;
}
return WidestLine;
}
int GUIFont::CalculateWidth(const char Character) {
if (Character >= 32 && Character < m_CharIndexCap) {
return m_Characters[Character].m_Width + m_Kerning;
}
return 0;
}
int GUIFont::CalculateHeight(const std::string& Text, int MaxWidth) {
if (Text.empty()) {
return 0;
}
unsigned char c;
int Width = 0;
int Height = m_FontHeight;
int lastSpacePos = 0;
// Go through every character
for (int i = 0; i < Text.length(); i++) {
c = Text.at(i);
// Add the new line's height if newline encountered
if (c == '\n') {
Width = 0;
Height += m_FontHeight;
continue;
}
if (c < 32 || c >= m_CharIndexCap) {
continue;
}
if (c == ' ') {
lastSpacePos = i;
}
Width += m_Characters[c].m_Width + m_Kerning;
if (MaxWidth > 0 && Width > MaxWidth) {
// Rewind to the last space, and do line break, but only if we've passed a space since last wrap
if (lastSpacePos > 0) {
i = lastSpacePos;
lastSpacePos = 0;
Width = 0;
Height += m_FontHeight;
}
continue;
}
}
return Height;
}
void GUIFont::CacheColor(unsigned long Color) {
// Make sure we haven't already cached this color and it isn't a 0 color
if (GetFontColor(Color) != nullptr || !Color) {
return;
}
// Add a new color to the cache
FontColor FC;
FC.m_Color = Color;
FC.m_Bitmap = m_Screen->CreateBitmap(m_Font->GetWidth(), m_Font->GetHeight());
// Created the bitmap?
if (!FC.m_Bitmap) {
return;
}
// Copy the bitmap
m_Font->Draw(FC.m_Bitmap, 0, 0, nullptr);
// Set the color key to be the same color as the Top-Right hand corner pixel
unsigned long BackG = FC.m_Bitmap->GetPixel(FC.m_Bitmap->GetWidth() - 1, 0);
FC.m_Bitmap->SetColorKey(BackG);
// Go through the bitmap and change the pixels
for (int y = 0; y < FC.m_Bitmap->GetHeight(); y++) {
for (int x = 0; x < FC.m_Bitmap->GetWidth(); x++) {
if (FC.m_Bitmap->GetPixel(x, y) == m_MainColor) {
FC.m_Bitmap->SetPixel(x, y, Color);
}
}
}
// Add the color to the list
m_ColorCache.push_back(FC);
}
GUIFont::FontColor* GUIFont::GetFontColor(unsigned long Color) {
std::vector<FontColor>::iterator it;
FontColor* F = nullptr;
for (it = m_ColorCache.begin(); it != m_ColorCache.end(); it++) {
F = &(*it);
// Colors match?
if (F->m_Color == Color) {
return F;
}
}
// Not found
return nullptr;
}
int GUIFont::GetFontHeight() const {
return m_FontHeight;
}
std::string GUIFont::GetName() const {
return m_Name;
}
int GUIFont::GetKerning() const {
return m_Kerning;
}
void GUIFont::Destroy() {
if (m_Font) {
m_Font->Destroy();
delete m_Font;
m_Font = nullptr;
}
// Go through the color cache and destroy the bitmaps
std::vector<FontColor>::iterator it;
FontColor* FC = 0;
for (it = m_ColorCache.begin(); it != m_ColorCache.end(); it++) {
FC = &(*it);
if (FC && FC->m_Bitmap) {
FC->m_Bitmap->Destroy();
delete FC->m_Bitmap;
}
}
m_ColorCache.clear();
}