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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
#include "Drawer2D.h"
#include "String.h"
#include "Graphics.h"
#include "Funcs.h"
#include "Platform.h"
#include "ExtMath.h"
#include "Logger.h"
#include "Game.h"
#include "Event.h"
#include "Chat.h"
#include "Stream.h"
#include "Utils.h"
#include "Errors.h"
#include "Window.h"
#include "Options.h"
#include "TexturePack.h"
#include "SystemFonts.h"
struct _Drawer2DData Drawer2D;
#define Font_IsBitmap(font) (!(font)->handle)
void DrawTextArgs_Make(struct DrawTextArgs* args, STRING_REF const cc_string* text, struct FontDesc* font, cc_bool useShadow) {
args->text = *text;
args->font = font;
args->useShadow = useShadow;
}
void DrawTextArgs_MakeEmpty(struct DrawTextArgs* args, struct FontDesc* font, cc_bool useShadow) {
args->text = String_Empty;
args->font = font;
args->useShadow = useShadow;
}
/*########################################################################################################################*
*-----------------------------------------------------Font functions------------------------------------------------------*
*#########################################################################################################################*/
int Drawer2D_AdjHeight(int point) { return Math_CeilDiv(point * 3, 2); }
void Font_MakeBitmapped(struct FontDesc* desc, int size, int flags) {
/* TODO: Scale X and Y independently */
size = Display_ScaleY(size);
desc->handle = NULL;
desc->size = size;
desc->flags = flags;
desc->height = Drawer2D_AdjHeight(size);
}
void Font_Make(struct FontDesc* desc, int size, int flags) {
if (Drawer2D.BitmappedText) {
Font_MakeBitmapped(desc, size, flags);
} else {
SysFont_MakeDefault(desc, size, flags);
}
}
void Font_Free(struct FontDesc* desc) {
desc->size = 0;
if (Font_IsBitmap(desc)) return;
SysFont_Free(desc);
desc->handle = NULL;
}
static struct Bitmap fontBitmap;
static int tileSize = 8; /* avoid divide by 0 if default.png missing */
/* So really 16 characters per row */
#define LOG2_CHARS_PER_ROW 4
static int tileWidths[256];
/* Finds the right-most non-transparent pixel in each tile in default.png */
static void CalculateTextWidths(void) {
int width = fontBitmap.width, height = fontBitmap.height;
BitmapCol* row;
int i, x, y, xx, tileY;
for (y = 0; y < height; y++) {
tileY = y / tileSize;
row = Bitmap_GetRow(&fontBitmap, y);
i = 0 | (tileY << LOG2_CHARS_PER_ROW);
/* Iterate through each tile on current scanline */
for (x = 0; x < width; x += tileSize, i++) {
/* Iterate through each pixel of the given character, on the current scanline */
for (xx = tileSize - 1; xx >= 0; xx--) {
if (!BitmapCol_A(row[x + xx])) continue;
/* Check if this is the pixel furthest to the right, for the current character */
tileWidths[i] = max(tileWidths[i], xx + 1);
break;
}
}
}
tileWidths[' '] = tileSize / 4;
}
static void FreeFontBitmap(void) {
int i;
for (i = 0; i < Array_Elems(tileWidths); i++) tileWidths[i] = 0;
Mem_Free(fontBitmap.scan0);
}
cc_bool Font_SetBitmapAtlas(struct Bitmap* bmp) {
/* If not all of these cases are accounted for, end up overwriting memory after tileWidths */
if (bmp->width != bmp->height) {
static const cc_string msg = String_FromConst("&cWidth of default.png must equal its height");
Logger_WarnFunc(&msg);
return false;
} else if (bmp->width < 16) {
static const cc_string msg = String_FromConst("&cdefault.png must be at least 16 pixels wide");
Logger_WarnFunc(&msg);
return false;
} else if (!Math_IsPowOf2(bmp->width)) {
static const cc_string msg = String_FromConst("&cWidth of default.png must be a power of two");
Logger_WarnFunc(&msg);
return false;
}
/* TODO: Use shift instead of mul/div */
FreeFontBitmap();
fontBitmap = *bmp;
tileSize = bmp->width >> LOG2_CHARS_PER_ROW;
CalculateTextWidths();
return true;
}
void Font_SetPadding(struct FontDesc* desc, int amount) {
if (!Font_IsBitmap(desc)) return;
desc->height = desc->size + Display_ScaleY(amount) * 2;
}
/*########################################################################################################################*
*---------------------------------------------------Drawing functions-----------------------------------------------------*
*#########################################################################################################################*/
cc_bool Drawer2D_Clamp(struct Context2D* ctx, int* x, int* y, int* width, int* height) {
if (*x >= ctx->width || *y >= ctx->height) return false;
/* origin is negative, move inside */
if (*x < 0) { *width += *x; *x = 0; }
if (*y < 0) { *height += *y; *y = 0; }
*width = min(*x + *width, ctx->width) - *x;
*height = min(*y + *height, ctx->height) - *y;
return *width > 0 && *height > 0;
}
#define Drawer2D_ClampPixel(p) p = (p < 0 ? 0 : (p > 255 ? 255 : p))
void Context2D_Alloc(struct Context2D* ctx, int width, int height) {
ctx->width = width;
ctx->height = height;
ctx->meta = NULL;
if (!Gfx.SupportsNonPowTwoTextures) {
/* Allocate power-of-2 sized bitmap equal to or greater than the given size */
width = Math_NextPowOf2(width);
height = Math_NextPowOf2(height);
}
if (Gfx.MinTexWidth) { width = max(width, Gfx.MinTexWidth); }
if (Gfx.MinTexHeight) { height = max(height, Gfx.MinTexHeight); }
ctx->bmp.width = width;
ctx->bmp.height = height;
ctx->bmp.scan0 = (BitmapCol*)Mem_AllocCleared(width * height, 4, "bitmap data");
}
void Context2D_Wrap(struct Context2D* ctx, struct Bitmap* bmp) {
ctx->bmp = *bmp;
ctx->width = bmp->width;
ctx->height = bmp->height;
ctx->meta = NULL;
}
void Context2D_Free(struct Context2D* ctx) {
Mem_Free(ctx->bmp.scan0);
}
#define BitmapColor_Raw(r, g, b) (BitmapColor_R_Bits(r) | BitmapColor_G_Bits(g) | BitmapColor_B_Bits(b))
void Gradient_Noise(struct Context2D* ctx, BitmapCol color, int variation,
int x, int y, int width, int height) {
struct Bitmap* bmp = (struct Bitmap*)ctx;
BitmapCol* dst;
int R, G, B, xx, yy, n;
int noise, delta;
cc_uint32 alpha;
if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return;
alpha = color & BITMAPCOLOR_A_MASK;
for (yy = 0; yy < height; yy++) {
dst = Bitmap_GetRow(bmp, y + yy) + x;
for (xx = 0; xx < width; xx++, dst++) {
n = (x + xx) + (y + yy) * 57;
n = (n << 13) ^ n;
/*
float noise = 1.0f - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
int delta = (int)(noise * variation);
*/
/* Fixed point equivalent to the above expression */
noise = ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff);
delta = (((1024 - noise / 0x100000)) * variation) >> 10;
R = BitmapCol_R(color) + delta; Drawer2D_ClampPixel(R);
G = BitmapCol_G(color) + delta; Drawer2D_ClampPixel(G);
B = BitmapCol_B(color) + delta; Drawer2D_ClampPixel(B);
*dst = BitmapColor_Raw(R, G, B) | alpha;
}
}
}
void Gradient_Vertical(struct Context2D* ctx, BitmapCol a, BitmapCol b,
int x, int y, int width, int height) {
struct Bitmap* bmp = (struct Bitmap*)ctx;
BitmapCol* row, color;
int xx, yy;
float t;
if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return;
for (yy = 0; yy < height; yy++) {
row = Bitmap_GetRow(bmp, y + yy) + x;
t = (float)yy / (height - 1); /* so last row has color of b */
color = BitmapCol_Make(
Math_Lerp(BitmapCol_R(a), BitmapCol_R(b), t),
Math_Lerp(BitmapCol_G(a), BitmapCol_G(b), t),
Math_Lerp(BitmapCol_B(a), BitmapCol_B(b), t),
255);
for (xx = 0; xx < width; xx++) { row[xx] = color; }
}
}
void Gradient_Blend(struct Context2D* ctx, BitmapCol color, int blend,
int x, int y, int width, int height) {
struct Bitmap* bmp = (struct Bitmap*)ctx;
BitmapCol* dst;
int R, G, B, xx, yy;
if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return;
/* Pre compute the alpha blended source color */
/* TODO: Avoid shift when multiplying */
color = BitmapCol_Make(
BitmapCol_R(color) * blend / 255,
BitmapCol_G(color) * blend / 255,
BitmapCol_B(color) * blend / 255,
0);
blend = 255 - blend; /* inverse for existing pixels */
for (yy = 0; yy < height; yy++) {
dst = Bitmap_GetRow(bmp, y + yy) + x;
for (xx = 0; xx < width; xx++, dst++) {
/* TODO: Not shift when multiplying */
R = BitmapCol_R(color) + (BitmapCol_R(*dst) * blend) / 255;
G = BitmapCol_G(color) + (BitmapCol_G(*dst) * blend) / 255;
B = BitmapCol_B(color) + (BitmapCol_B(*dst) * blend) / 255;
*dst = BitmapColor_RGB(R, G, B);
}
}
}
void Context2D_DrawPixels(struct Context2D* ctx, int x, int y, struct Bitmap* src) {
struct Bitmap* dst = (struct Bitmap*)ctx;
int width = src->width, height = src->height;
BitmapCol* dstRow;
BitmapCol* srcRow;
int xx, yy;
if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return;
for (yy = 0; yy < height; yy++) {
srcRow = Bitmap_GetRow(src, yy);
dstRow = Bitmap_GetRow(dst, y + yy) + x;
for (xx = 0; xx < width; xx++) { dstRow[xx] = srcRow[xx]; }
}
}
void Context2D_Clear(struct Context2D* ctx, BitmapCol color,
int x, int y, int width, int height) {
struct Bitmap* bmp = (struct Bitmap*)ctx;
BitmapCol* row;
int xx, yy;
if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return;
for (yy = 0; yy < height; yy++) {
row = Bitmap_GetRow(bmp, y + yy) + x;
for (xx = 0; xx < width; xx++) { row[xx] = color; }
}
}
void Drawer2D_MakeTextTexture(struct Texture* tex, struct DrawTextArgs* args) {
static struct Texture empty = { 0, Tex_Rect(0,0, 0,0), Tex_UV(0,0, 1,1) };
struct Context2D ctx;
int width, height;
/* pointless to draw anything when context is lost */
if (Gfx.LostContext) { *tex = empty; return; }
width = Drawer2D_TextWidth(args);
if (!width) { *tex = empty; return; }
height = Drawer2D_TextHeight(args);
Context2D_Alloc(&ctx, width, height);
{
Context2D_DrawText(&ctx, args, 0, 0);
Context2D_MakeTexture(tex, &ctx);
}
Context2D_Free(&ctx);
}
void Context2D_MakeTexture(struct Texture* tex, struct Context2D* ctx) {
int flags = TEXTURE_FLAG_NONPOW2 | TEXTURE_FLAG_LOWRES;
Gfx_RecreateTexture(&tex->ID, &ctx->bmp, flags, false);
tex->width = ctx->width;
tex->height = ctx->height;
tex->uv.u1 = 0.0f; tex->uv.v1 = 0.0f;
tex->uv.u2 = (float)ctx->width / (float)ctx->bmp.width;
tex->uv.v2 = (float)ctx->height / (float)ctx->bmp.height;
}
cc_bool Drawer2D_ValidColorCodeAt(const cc_string* text, int i) {
if (i >= text->length) return false;
return BitmapCol_A(Drawer2D_GetColor(text->buffer[i])) != 0;
}
cc_bool Drawer2D_UNSAFE_NextPart(cc_string* left, cc_string* part, char* colorCode) {
BitmapCol color;
char cur;
int i;
/* check if current part starts with a colour code */
if (left->length >= 2 && left->buffer[0] == '&') {
cur = left->buffer[1];
color = Drawer2D_GetColor(cur);
if (BitmapCol_A(color)) {
*colorCode = cur;
left->buffer += 2;
left->length -= 2;
}
}
for (i = 0; i < left->length; i++)
{
if (left->buffer[i] == '&' && Drawer2D_ValidColorCodeAt(left, i + 1)) break;
}
/* advance string starts and lengths */
part->buffer = left->buffer;
part->length = i;
left->buffer += i;
left->length -= i;
return part->length > 0 || left->length > 0;
}
cc_bool Drawer2D_IsEmptyText(const cc_string* text) {
cc_string left = *text, part;
char colorCode;
while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode))
{
if (part.length) return false;
}
return true;
}
void Drawer2D_WithoutColors(cc_string* str, const cc_string* src) {
cc_string left = *src, part;
char colorCode;
while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode))
{
String_AppendString(str, &part);
}
}
char Drawer2D_LastColor(const cc_string* text, int start) {
int i;
if (start >= text->length) start = text->length - 1;
for (i = start; i >= 0; i--) {
if (text->buffer[i] != '&') continue;
if (Drawer2D_ValidColorCodeAt(text, i + 1)) {
return text->buffer[i + 1];
}
}
return '\0';
}
cc_bool Drawer2D_IsWhiteColor(char c) { return c == '\0' || c == 'f' || c == 'F'; }
/* TODO: Needs to account for DPI */
#define Drawer2D_ShadowOffset(point) (point / 8)
#define Drawer2D_XPadding(point) (Math_CeilDiv(point, 8))
static int Drawer2D_Width(int point, char c) {
return Math_CeilDiv(tileWidths[(cc_uint8)c] * point, tileSize);
}
void Drawer2D_ReducePadding_Tex(struct Texture* tex, int point, int scale) {
int padding;
float vAdj;
if (!Drawer2D.BitmappedText) return;
padding = (tex->height - point) / scale;
vAdj = (float)padding / Math_NextPowOf2(tex->height);
tex->uv.v1 += vAdj; tex->uv.v2 -= vAdj;
tex->height -= (cc_uint16)(padding * 2);
}
void Drawer2D_ReducePadding_Height(int* height, int point, int scale) {
int padding;
if (!Drawer2D.BitmappedText) return;
padding = (*height - point) / scale;
*height -= padding * 2;
}
void Drawer2D_Fill(struct Bitmap* bmp, int x, int y, int width, int height, BitmapCol color) {
BitmapCol* row;
int xx, yy;
for (yy = y; yy < y + height; yy++) {
if (yy >= bmp->height) return;
row = Bitmap_GetRow(bmp, yy);
for (xx = x; xx < x + width; xx++) {
if (xx >= bmp->width) break;
row[xx] = color;
}
}
}
static void DrawBitmappedTextCore(struct Bitmap* bmp, struct DrawTextArgs* args, int x, int y, cc_bool shadow) {
BitmapCol color;
cc_string text = args->text;
int i, point = args->font->size, count = 0;
int xPadding;
int srcX, srcY, dstX, dstY;
int fontX, fontY;
int srcWidth, dstWidth;
int dstHeight, begX, xx, yy;
int cellY, underlineY, underlineHeight;
BitmapCol* srcRow, src;
BitmapCol* dstRow;
cc_uint8 coords[DRAWER2D_MAX_TEXT_LENGTH];
BitmapCol colors[DRAWER2D_MAX_TEXT_LENGTH];
cc_uint16 dstWidths[DRAWER2D_MAX_TEXT_LENGTH];
color = Drawer2D.Colors['f'];
if (shadow) color = GetShadowColor(color);
for (i = 0; i < text.length; i++) {
char c = text.buffer[i];
if (c == '&' && Drawer2D_ValidColorCodeAt(&text, i + 1)) {
color = Drawer2D_GetColor(text.buffer[i + 1]);
if (shadow) color = GetShadowColor(color);
i++; continue; /* skip over the color code */
}
coords[count] = c;
colors[count] = color;
dstWidths[count] = Drawer2D_Width(point, c);
count++;
}
dstHeight = point; begX = x;
/* adjust coords to make drawn text match GDI fonts */
y += (args->font->height - dstHeight) / 2;
xPadding = Drawer2D_XPadding(point);
for (yy = 0; yy < dstHeight; yy++) {
dstY = y + yy;
if ((unsigned)dstY >= (unsigned)bmp->height) continue;
fontY = 0 + yy * tileSize / dstHeight;
dstRow = Bitmap_GetRow(bmp, dstY);
for (i = 0; i < count; i++) {
srcX = (coords[i] & 0x0F) * tileSize;
srcY = (coords[i] >> 4) * tileSize;
srcRow = Bitmap_GetRow(&fontBitmap, fontY + srcY);
srcWidth = tileWidths[coords[i]];
dstWidth = dstWidths[i];
color = colors[i];
for (xx = 0; xx < dstWidth; xx++) {
fontX = srcX + xx * srcWidth / dstWidth;
src = srcRow[fontX];
if (!BitmapCol_A(src)) continue;
dstX = x + xx;
if ((unsigned)dstX >= (unsigned)bmp->width) continue;
/* TODO: Transparent text by multiplying by col.A */
/* TODO: Not shift when multiplying */
/* TODO: avoid BitmapCol_A shift */
dstRow[dstX] = BitmapCol_Make(
BitmapCol_R(src) * BitmapCol_R(color) / 255,
BitmapCol_G(src) * BitmapCol_G(color) / 255,
BitmapCol_B(src) * BitmapCol_B(color) / 255,
BitmapCol_A(src));
}
x += dstWidth + xPadding;
}
x = begX;
}
if (!(args->font->flags & FONT_FLAGS_UNDERLINE)) return;
/* scale up bottom row of a cell to drawn text font */
cellY = (8 - 1) * dstHeight / 8;
underlineY = y + cellY;
underlineHeight = dstHeight - cellY;
for (i = 0; i < count; ) {
dstWidth = 0;
color = colors[i];
for (; i < count && color == colors[i]; i++) {
dstWidth += dstWidths[i] + xPadding;
}
Drawer2D_Fill(bmp, x, underlineY, dstWidth, underlineHeight, color);
x += dstWidth;
}
}
static void DrawBitmappedText(struct Bitmap* bmp, struct DrawTextArgs* args, int x, int y) {
int offset = Drawer2D_ShadowOffset(args->font->size);
if (!fontBitmap.scan0) {
if (args->useShadow) FallbackFont_DrawText(args, bmp, x, y, true);
FallbackFont_DrawText(args, bmp, x, y, false);
return;
}
if (args->useShadow) {
DrawBitmappedTextCore(bmp, args, x + offset, y + offset, true);
}
DrawBitmappedTextCore(bmp, args, x, y, false);
}
static int MeasureBitmappedWidth(const struct DrawTextArgs* args) {
int i, point = args->font->size;
int xPadding, width;
cc_string text;
if (!fontBitmap.scan0) return FallbackFont_TextWidth(args);
/* adjust coords to make drawn text match GDI fonts */
xPadding = Drawer2D_XPadding(point);
width = 0;
text = args->text;
for (i = 0; i < text.length; i++) {
char c = text.buffer[i];
if (c == '&' && Drawer2D_ValidColorCodeAt(&text, i + 1)) {
i++; continue; /* skip over the color code */
}
width += Drawer2D_Width(point, c) + xPadding;
}
if (!width) return 0;
/* Remove padding at end */
if (!(args->font->flags & FONT_FLAGS_PADDING)) width -= xPadding;
if (args->useShadow) { width += Drawer2D_ShadowOffset(point); }
return width;
}
void Context2D_DrawText(struct Context2D* ctx, struct DrawTextArgs* args, int x, int y) {
struct Bitmap* bmp = (struct Bitmap*)ctx;
if (Drawer2D_IsEmptyText(&args->text)) return;
if (Font_IsBitmap(args->font)) { DrawBitmappedText(bmp, args, x, y); return; }
if (args->useShadow) { SysFont_DrawText(args, bmp, x, y, true); }
SysFont_DrawText(args, bmp, x, y, false);
}
int Drawer2D_TextWidth(struct DrawTextArgs* args) {
if (Font_IsBitmap(args->font)) return MeasureBitmappedWidth(args);
return SysFont_TextWidth(args);
}
int Drawer2D_TextHeight(struct DrawTextArgs* args) {
return Font_CalcHeight(args->font, args->useShadow);
}
int Font_CalcHeight(const struct FontDesc* font, cc_bool useShadow) {
int height = font->height;
if (Font_IsBitmap(font)) {
if (useShadow) { height += Drawer2D_ShadowOffset(font->size); }
} else {
if (useShadow) height += 2;
}
return height;
}
void Drawer2D_DrawClippedText(struct Context2D* ctx, struct DrawTextArgs* args,
int x, int y, int maxWidth) {
char strBuffer[512];
struct DrawTextArgs part;
int i, width;
width = Drawer2D_TextWidth(args);
/* No clipping needed */
if (width <= maxWidth) { Context2D_DrawText(ctx, args, x, y); return; }
part = *args;
String_InitArray(part.text, strBuffer);
String_Copy(&part.text, &args->text);
String_Append(&part.text, '.');
for (i = part.text.length - 2; i > 0; i--) {
part.text.buffer[i] = '.';
/* skip over trailing spaces */
if (part.text.buffer[i - 1] == ' ') continue;
part.text.length = i + 2;
width = Drawer2D_TextWidth(&part);
if (width <= maxWidth) { Context2D_DrawText(ctx, &part, x, y); return; }
/* If down to <= 2 chars, try omitting the .. */
if (i > 2) continue;
part.text.length = i;
width = Drawer2D_TextWidth(&part);
if (width <= maxWidth) { Context2D_DrawText(ctx, &part, x, y); return; }
}
}
/*########################################################################################################################*
*---------------------------------------------------Drawer2D component----------------------------------------------------*
*#########################################################################################################################*/
static void DefaultPngProcess(struct Stream* stream, const cc_string* name) {
struct Bitmap bmp;
cc_result res;
if ((res = Png_Decode(&bmp, stream))) {
Logger_SysWarn2(res, "decoding", name);
Mem_Free(bmp.scan0);
} else if (Font_SetBitmapAtlas(&bmp)) {
Event_RaiseVoid(&ChatEvents.FontChanged);
} else {
Mem_Free(bmp.scan0);
}
}
static struct TextureEntry default_entry = { "default.png", DefaultPngProcess };
/* The default 16 colours are the CGA 16 color palette (without special brown colour) */
/* See https://en.wikipedia.org/wiki/Color_Graphics_Adapter#With_an_RGBI_monitor for reference */
/* The 16 hex colours below were produced from the following formula: */
/* R = 191 * ((hex >> 2) & 1) + 64 * (hex >> 3) */
/* G = 191 * ((hex >> 1) & 1) + 64 * (hex >> 3) */
/* B = 191 * ((hex >> 0) & 1) + 64 * (hex >> 3) */
static const BitmapCol defaults_0_9[] = {
BitmapColor_RGB( 0, 0, 0), /* 0 */
BitmapColor_RGB( 0, 0, 191), /* 1 */
BitmapColor_RGB( 0, 191, 0), /* 2 */
BitmapColor_RGB( 0, 191, 191), /* 3 */
BitmapColor_RGB(191, 0, 0), /* 4 */
BitmapColor_RGB(191, 0, 191), /* 5 */
BitmapColor_RGB(191, 191, 0), /* 6 */
BitmapColor_RGB(191, 191, 191), /* 7 */
BitmapColor_RGB( 64, 64, 64), /* 8 */
BitmapColor_RGB( 64, 64, 255) /* 9 */
};
static const BitmapCol defaults_a_f[] = {
BitmapColor_RGB( 64, 255, 64), /* A */
BitmapColor_RGB( 64, 255, 255), /* B */
BitmapColor_RGB(255, 64, 64), /* C */
BitmapColor_RGB(255, 64, 255), /* D */
BitmapColor_RGB(255, 255, 64), /* E */
BitmapColor_RGB(255, 255, 255), /* F */
};
static void OnReset(void) {
Mem_Set(Drawer2D.Colors, 0, sizeof(Drawer2D.Colors));
Mem_Copy(&Drawer2D.Colors['0'], defaults_0_9, sizeof(defaults_0_9));
Mem_Copy(&Drawer2D.Colors['a'], defaults_a_f, sizeof(defaults_a_f));
Mem_Copy(&Drawer2D.Colors['A'], defaults_a_f, sizeof(defaults_a_f));
}
static void OnInit(void) {
OnReset();
TextureEntry_Register(&default_entry);
Drawer2D.BitmappedText = Game_ClassicMode || !Options_GetBool(OPT_USE_CHAT_FONT, false);
Drawer2D.BlackTextShadows = Options_GetBool(OPT_BLACK_TEXT, false);
}
static void OnFree(void) {
FreeFontBitmap();
fontBitmap.scan0 = NULL;
}
struct IGameComponent Drawer2D_Component = {
OnInit, /* Init */
OnFree, /* Free */
OnReset, /* Reset */
};
|