-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathr_font.cpp
More file actions
475 lines (432 loc) · 12.3 KB
/
r_font.cpp
File metadata and controls
475 lines (432 loc) · 12.3 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// SimpleGraphic Engine
// (c) David Gowor, 2014
//
// Module: Render Font
//
#include "r_local.h"
#include <algorithm>
#include <fmt/format.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
// =======
// Classes
// =======
// Glyph parameters
struct f_glyph_s {
float tcLeft = 0.0;
float tcRight = 0.0;
float tcTop = 0.0;
float tcBottom = 0.0;
int width = 0;
int spLeft = 0;
int spRight = 0;
};
// Font height info
struct f_fontHeight_s {
r_tex_c* tex;
int height;
int numGlyph;
f_glyph_s glyphs[128];
f_glyph_s defGlyph{0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0};
f_glyph_s const& Glyph(char ch) const {
if ((unsigned char)ch >= numGlyph) {
return defGlyph;
}
return glyphs[(unsigned char)ch];
}
};
// ===========
// Font Loader
// ===========
r_font_c::r_font_c(r_renderer_c* renderer, const char* fontName)
: renderer(renderer)
{
numFontHeight = 0;
fontHeightMap = NULL;
std::string fileNameBase = fmt::format(CFG_DATAPATH "Fonts/{}", fontName);
// Open info file
std::string tgfName = fileNameBase + ".tgf";
std::ifstream tgf(tgfName);
if (!tgf) {
renderer->sys->con->Warning("font \"%s\" not found", fontName);
return;
}
maxHeight = 0;
f_fontHeight_s* fh = NULL;
// Parse info file
std::string sub;
while (std::getline(tgf, sub)) {
int h, x, y, w, sl, sr;
if (sscanf(sub.c_str(), "HEIGHT %u;", &h) == 1) {
// New height
fh = new f_fontHeight_s;
fontHeights[numFontHeight++] = fh;
std::string tgaName = fmt::format("{}.{}.tga", fileNameBase, h);
fh->tex = new r_tex_c(renderer->texMan, tgaName.c_str(), TF_NOMIPMAP);
fh->height = h;
if (h > maxHeight) {
maxHeight = h;
}
fh->numGlyph = 0;
}
else if (fh && sscanf(sub.c_str(), "GLYPH %u %u %u %d %d;", &x, &y, &w, &sl, &sr) == 5) {
// Add glyph
if (fh->numGlyph >= 128) continue;
f_glyph_s* glyph = &fh->glyphs[fh->numGlyph++];
glyph->tcLeft = (float)x / fh->tex->fileWidth;
glyph->tcRight = (float)(x + w) / fh->tex->fileWidth;
glyph->tcTop = (float)y / fh->tex->fileHeight;
glyph->tcBottom = (float)(y + fh->height) / fh->tex->fileHeight;
glyph->width = w;
glyph->spLeft = sl;
glyph->spRight = sr;
}
}
// Generate mapping of text height to font height
fontHeightMap = new int[maxHeight + 1];
memset(fontHeightMap, 0, sizeof(int) * (maxHeight + 1));
for (int i = 0; i < numFontHeight; i++) {
int gh = fontHeights[i]->height;
for (int h = gh; h <= maxHeight; h++) {
fontHeightMap[h] = i;
}
if (i > 0) {
int belowH = fontHeights[i - 1]->height;
int lim = (gh - belowH - 1) / 2;
for (int b = 0; b < lim; b++) {
fontHeightMap[gh - b - 1] = i;
}
}
}
}
r_font_c::~r_font_c()
{
// Delete textures
for (int i = 0; i < numFontHeight; i++) {
delete fontHeights[i]->tex;
delete fontHeights[i];
}
delete fontHeightMap;
}
// =============
// Font Renderer
// =============
std::u32string BuildTofuString(char32_t cp) {
// Format unhandled Unicode codepoints like U+0123, higher planes have wider numbers.
fmt::memory_buffer buf;
fmt::format_to(fmt::appender(buf), "[U+{:04X}]", (uint32_t)cp);
std::u32string ret;
ret.reserve(buf.size());
std::copy(buf.begin(), buf.end(), std::back_inserter(ret));
return ret;
}
int const tofuSizeReduction = 3;
int r_font_c::StringWidthInternal(f_fontHeight_s* fh, std::u32string_view str, int height, float scale)
{
int heightIdx = (int)(std::find(fontHeights, fontHeights + numFontHeight, fh) - fontHeights);
auto tofuFont = FindSmallerFontHeight(height, heightIdx, tofuSizeReduction);
auto measureCodepoint = [](f_fontHeight_s* glyphFh, char32_t cp) {
auto& glyph = glyphFh->Glyph((char)(unsigned char)cp);
return glyph.width + glyph.spLeft + glyph.spRight;
};
float width = 0.0f;
for (size_t idx = 0; idx < str.size();) {
auto ch = str[idx];
int escLen = IsColorEscape(str.substr(idx));
if (escLen) {
idx += escLen;
}
else if (ch >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(ch);
for (auto cp : tofu) {
width += measureCodepoint(tofuFont.fh, cp);
width = std::ceil(width);
}
++idx;
}
else if (ch == U'\t') {
auto& glyph = fh->Glyph(' ');
int spWidth = glyph.width + glyph.spLeft + glyph.spRight;
width += spWidth * 4 * scale;
width = std::ceil(width);
++idx;
}
else {
width += measureCodepoint(fh, ch) * scale;
width = std::ceil(width);
++idx;
}
}
return static_cast<int>(width);
}
int r_font_c::StringWidth(int height, std::u32string_view str)
{
auto mainFont = FindFontHeight(height);
f_fontHeight_s* fh = mainFont.fh;
int max = 0;
float const scale = (float)height / fh->height;
for (auto I = str.begin(); I != str.end(); ++I) {
auto lineEnd = std::find(I, str.end(), U'\n');
if (I != lineEnd) {
std::u32string_view line(&*I, std::distance(I, lineEnd));
int lw = StringWidthInternal(fh, line, height, scale);
max = (std::max)(max, lw);
}
if (lineEnd == str.end()) {
break;
}
I = lineEnd;
}
return max;
}
size_t r_font_c::StringCursorInternal(f_fontHeight_s* fh, std::u32string_view str, int height, float scale, int curX)
{
int heightIdx = (int)(std::find(fontHeights, fontHeights + numFontHeight, fh) - fontHeights);
auto tofuFont = FindSmallerFontHeight(height, heightIdx, tofuSizeReduction);
auto measureCodepoint = [](f_fontHeight_s* glyphFh, char32_t cp) {
auto& glyph = glyphFh->Glyph((char)(unsigned char)cp);
return glyph.width + glyph.spLeft + glyph.spRight;
};
float x = 0.0f;
auto I = str.begin();
auto lineEnd = std::find(I, str.end(), U'\n');
while (I != lineEnd) {
auto tail = str.substr(std::distance(str.begin(), I));
int escLen = IsColorEscape(tail);
if (escLen) {
I += escLen;
}
else if (*I >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(*I);
for (auto cp : tofu) {
x += measureCodepoint(tofuFont.fh, cp);
x = std::ceil(x);
if (curX <= x) {
return std::distance(str.begin(), I);
}
}
++I;
}
else if (*I == U'\t') {
auto& glyph = fh->Glyph(' ');
float fullWidth = (glyph.width + glyph.spLeft + glyph.spRight) * 4.0f * scale;
float halfWidth = std::ceil(fullWidth / 2.0f);
x += halfWidth;
x = std::ceil(x);
if (curX <= x) {
break;
}
x += fullWidth - halfWidth;
x = std::ceil(x);
if (curX <= x) {
break;
}
++I;
}
else {
x += measureCodepoint(fh, *I) * scale;
x = std::ceil(x);
if (curX <= x) {
break;
}
++I;
}
}
return std::distance(str.begin(), I);
}
int r_font_c::StringCursorIndex(int height, std::u32string_view str, int curX, int curY)
{
auto mainFont = FindFontHeight(height);
f_fontHeight_s* fh = mainFont.fh;
int lastIndex = 0;
int lineY = height;
float scale = (float)height / fh->height;
auto I = str.begin();
while (I != str.end()) {
auto lineEnd = std::find(I, str.end(), U'\n');
auto line = str.substr(std::distance(str.begin(), I), std::distance(I, lineEnd));
lastIndex = (int)(StringCursorInternal(fh, line, height, scale, curX));
if (curY <= lineY) {
break;
}
if (lineEnd == str.end()) {
break;
}
I = lineEnd + 1;
lineY += height;
}
return (int)std::distance(str.begin(), I) + lastIndex;
}
r_font_c::EmbeddedFontSpec r_font_c::FindSmallerFontHeight(int height, int heightIdx, int sizeReduction) {
EmbeddedFontSpec ret{};
ret.fh = fontHeights[heightIdx];
ret.yPad = 0;
for (int tofuIdx = heightIdx - 1; tofuIdx >= 0; --tofuIdx) {
auto candFh = fontHeights[tofuIdx];
int heightDiff = height - candFh->height;
if (heightDiff >= sizeReduction) {
ret.fh = candFh;
ret.yPad = (int)std::ceil(heightDiff / 2.0f);
break;
}
}
return ret;
}
r_font_c::FontHeightEntry r_font_c::FindFontHeight(int height) {
FontHeightEntry ret{};
if (height > maxHeight) {
// Too large heights get the largest font size.
ret.heightIdx = numFontHeight - 1;
}
else if (height < 0) {
// Negative heights get the smallest font size.
ret.heightIdx = 0;
}
else {
ret.heightIdx = fontHeightMap[height];
}
ret.fh = fontHeights[ret.heightIdx];
return ret;
}
void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, std::u32string_view str)
{
// Check if the line is visible
if (pos[Y] >= renderer->sys->video->vid.size[1] || pos[Y] <= -height) {
// Just process the colour codes
while (!str.empty()) {
// Check for escape character
int escLen = IsColorEscape(str);
if (escLen) {
str = ReadColorEscape(str, col);
col[3] = 1.0f;
renderer->curLayer->Color(col);
continue;
}
str = str.substr(1);
}
return;
}
// Find best height to use
auto mainFont = FindFontHeight(height);
f_fontHeight_s* fh = mainFont.fh;
float scale = (float)height / fh->height;
auto tofuFont = FindSmallerFontHeight(height, mainFont.heightIdx, tofuSizeReduction);
// Calculate the string position
float x = pos[X];
float y = std::floor(pos[Y]);
if (align != F_LEFT) {
// Calculate the real width of the string
float width = StringWidthInternal(fh, str, height, scale);
switch (align) {
case F_CENTRE:
x = floor((renderer->VirtualScreenWidth() - width) / 2.0f + pos[X]);
break;
case F_RIGHT:
x = floor(renderer->VirtualScreenWidth() - width - pos[X]);
break;
case F_CENTRE_X:
x = floor(pos[X] - width / 2.0f);
break;
case F_RIGHT_X:
x = floor(pos[X] - width);
break;
}
}
// Snap the starting x position to the pixel grid so the leading glyph isn't blurred.
x = std::round(x);
r_tex_c* curTex{};
auto drawCodepoint = [this, &curTex, &x, y](f_fontHeight_s* fh, int height, float scale, int yShift, char32_t cp) {
float cpY = y + yShift;
if (curTex != fh->tex) {
curTex = fh->tex;
renderer->curLayer->Bind(fh->tex);
}
auto& glyph = fh->Glyph((char)(unsigned char)cp);
x += glyph.spLeft * scale;
if (glyph.width) {
float w = glyph.width * scale;
if (x + w >= 0 && x < renderer->VirtualScreenWidth()) {
renderer->curLayer->Quad(
glyph.tcLeft, glyph.tcTop, x, cpY,
glyph.tcRight, glyph.tcTop, x + w, cpY,
glyph.tcRight, glyph.tcBottom, x + w, cpY + height,
glyph.tcLeft, glyph.tcBottom, x, cpY + height
);
}
x += w;
}
x += glyph.spRight * scale;
x = std::ceil(x);
};
// Render the string
for (auto tail = str; !tail.empty();) {
// Draw unprintable characters as tofu placeholders
auto ch = tail[0];
if (ch >= (unsigned)fh->numGlyph) {
auto tofu = BuildTofuString(ch);
for (auto ch : tofu) {
drawCodepoint(tofuFont.fh, tofuFont.fh->height, 1.0f, tofuFont.yPad, ch);
}
tail = tail.substr(1);
continue;
}
// Check for escape character
int escLen = IsColorEscape(tail);
if (escLen) {
tail = ReadColorEscape(tail, col);
col[3] = 1.0f;
renderer->curLayer->Color(col);
continue;
}
// Handle tabs
if (ch == U'\t') {
auto& glyph = fh->Glyph(' ');
int spWidth = glyph.width + glyph.spLeft + glyph.spRight;
x+= (spWidth << 2) * scale;
tail = tail.substr(1);
continue;
}
// Draw glyph
drawCodepoint(fh, height, scale, 0, ch);
tail = tail.substr(1);
}
}
void r_font_c::Draw(scp_t pos, int align, int height, col4_t col, std::u32string_view str)
{
if (str.empty()) {
pos[Y]+= height;
return;
}
// Prepare for rendering
renderer->curLayer->Color(col);
// Separate into lines and render them
for (auto I = str.begin(); I != str.end(); ++I) {
auto lineEnd = std::find(I, str.end(), U'\n');
if (I != lineEnd) {
std::u32string_view line(&*I, std::distance(I, lineEnd));
DrawTextLine(pos, align, height, col, line);
}
pos[Y] += height;
if (lineEnd == str.end()) {
break;
}
I = lineEnd;
}
}
void r_font_c::FDraw(scp_t pos, int align, int height, col4_t col, const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
VDraw(pos, align, height, col, fmt, va);
va_end(va);
}
void r_font_c::VDraw(scp_t pos, int align, int height, col4_t col, const char* fmt, va_list va)
{
char str[65536];
vsnprintf(str, 65535, fmt, va);
str[65535] = 0;
auto idxStr = IndexUTF8ToUTF32(str);
Draw(pos, align, height, col, idxStr.text);
}