-
Notifications
You must be signed in to change notification settings - Fork 1
/
fontcache.c
2925 lines (2342 loc) · 73.3 KB
/
fontcache.c
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SDL_FontCache: A font cache for SDL and SDL_ttf
by Jonathan Dearborn
See fontcache.h for license info.
*/
#include "fontcache.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Visual C does not support static inline
#ifndef static_inline
#ifdef _MSC_VER
#define static_inline static
#else
#define static_inline static inline
#endif
#endif
#if SDL_VERSION_ATLEAST(2,0,0)
#define FC_GET_ALPHA(sdl_color) ((sdl_color).a)
#else
#define FC_GET_ALPHA(sdl_color) ((sdl_color).unused)
#endif
// Need SDL_RenderIsClipEnabled() for proper clipping support
#if SDL_VERSION_ATLEAST(2,0,4)
#define ENABLE_SDL_CLIPPING
#endif
#define FC_MIN(a,b) ((a) < (b)? (a) : (b))
#define FC_MAX(a,b) ((a) > (b)? (a) : (b))
// vsnprintf replacement from Valentin Milea:
// http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf c99_snprintf
#define vsnprintf c99_vsnprintf
__inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
{
int count = -1;
if (size != 0)
count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);
return count;
}
__inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...)
{
int count;
va_list ap;
va_start(ap, format);
count = c99_vsnprintf(outBuf, size, format, ap);
va_end(ap);
return count;
}
#endif
#define FC_EXTRACT_VARARGS(buffer, start_args) \
{ \
va_list lst; \
va_start(lst, start_args); \
vsnprintf(buffer, fc_buffer_size, start_args, lst); \
va_end(lst); \
}
// Extra pixels of padding around each glyph to avoid linear filtering artifacts
#define FC_CACHE_PADDING 1
static Uint8 has_clip(FC_Target* dest)
{
#ifdef FC_USE_SDL_GPU
return dest->use_clip_rect;
#elif defined(ENABLE_SDL_CLIPPING)
return SDL_RenderIsClipEnabled(dest);
#else
return 0;
#endif
}
static FC_Rect get_clip(FC_Target* dest)
{
#ifdef FC_USE_SDL_GPU
return dest->clip_rect;
#elif defined(ENABLE_SDL_CLIPPING)
SDL_Rect r;
SDL_RenderGetClipRect(dest, &r);
return r;
#else
SDL_Rect r = {0, 0, 0, 0};
return r;
#endif
}
static void set_clip(FC_Target* dest, FC_Rect* rect)
{
#ifdef FC_USE_SDL_GPU
if(rect != NULL)
GPU_SetClipRect(dest, *rect);
else
GPU_UnsetClip(dest);
#elif defined(ENABLE_SDL_CLIPPING)
SDL_RenderSetClipRect(dest, rect);
#endif
}
static void set_color(FC_Image* src, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
#ifdef FC_USE_SDL_GPU
GPU_SetRGBA(src, r, g, b, a);
#else
SDL_SetTextureColorMod(src, r, g, b);
SDL_SetTextureAlphaMod(src, a);
#endif
}
static char* new_concat(const char* a, const char* b)
{
// Create new buffer
unsigned int size = strlen(a) + strlen(b);
char* new_string = (char*)malloc(size+1);
// Concatenate strings in the new buffer
strcpy(new_string, a);
strcat(new_string, b);
return new_string;
}
static char* replace_concat(char** a, const char* b)
{
char* new_string = new_concat(*a, b);
free(*a);
*a = new_string;
return *a;
}
// Width of a tab in units of the space width (sorry, no tab alignment!)
static unsigned int fc_tab_width = 4;
// Shared buffer for variadic text
static char* fc_buffer = NULL;
static unsigned int fc_buffer_size = 1024;
static Uint8 fc_has_render_target_support = 0;
// The number of fonts that has been created but not freed
static int NUM_EXISTING_FONTS = 0;
// Globals for GetString functions
static char* ASCII_STRING = NULL;
static char* LATIN_1_STRING = NULL;
static char* ASCII_LATIN_1_STRING = NULL;
char* FC_GetStringASCII(void)
{
if(ASCII_STRING == NULL)
{
int i;
char c;
ASCII_STRING = (char*)malloc(512);
memset(ASCII_STRING, 0, 512);
i = 0;
c = 32;
while(1)
{
ASCII_STRING[i] = c;
if(c == 126)
break;
++i;
++c;
}
}
return U8_strdup(ASCII_STRING);
}
char* FC_GetStringLatin1(void)
{
if(LATIN_1_STRING == NULL)
{
int i;
unsigned char c;
LATIN_1_STRING = (char*)malloc(512);
memset(LATIN_1_STRING, 0, 512);
i = 0;
c = 0xA0;
while(1)
{
LATIN_1_STRING[i] = '\xc2';
LATIN_1_STRING[i+1] = c;
if(c == 0xBF)
break;
i += 2;
++c;
}
i += 2;
c = 0x80;
while(1)
{
LATIN_1_STRING[i] = '\xc3';
LATIN_1_STRING[i+1] = c;
if(c == 0xBF)
break;
i += 2;
++c;
}
}
return U8_strdup(LATIN_1_STRING);
}
char* FC_GetStringASCII_Latin1(void)
{
if(ASCII_LATIN_1_STRING == NULL)
ASCII_LATIN_1_STRING = new_concat(FC_GetStringASCII(), FC_GetStringLatin1());
return U8_strdup(ASCII_LATIN_1_STRING);
}
FC_Rect FC_MakeRect(float x, float y, float w, float h)
{
FC_Rect r = {x, y, w, h};
return r;
}
FC_Scale FC_MakeScale(float x, float y)
{
FC_Scale s = {x, y};
return s;
}
SDL_Color FC_MakeColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_Color c = {r, g, b, a};
return c;
}
FC_Effect FC_MakeEffect(FC_AlignEnum alignment, FC_Scale scale, SDL_Color color)
{
FC_Effect e;
e.alignment = alignment;
e.scale = scale;
e.color = color;
return e;
}
FC_GlyphData FC_MakeGlyphData(int cache_level, Sint16 x, Sint16 y, Uint16 w, Uint16 h)
{
FC_GlyphData gd;
gd.rect.x = x;
gd.rect.y = y;
gd.rect.w = w;
gd.rect.h = h;
gd.cache_level = cache_level;
return gd;
}
// Enough to hold all of the ascii characters and some.
#define FC_DEFAULT_NUM_BUCKETS 300
typedef struct FC_MapNode
{
Uint32 key;
FC_GlyphData value;
struct FC_MapNode* next;
} FC_MapNode;
typedef struct FC_Map
{
int num_buckets;
FC_MapNode** buckets;
} FC_Map;
static FC_Map* FC_MapCreate(int num_buckets)
{
int i;
FC_Map* map = (FC_Map*)malloc(sizeof(FC_Map));
map->num_buckets = num_buckets;
map->buckets = (FC_MapNode**)malloc(num_buckets * sizeof(FC_MapNode*));
for(i = 0; i < num_buckets; ++i)
{
map->buckets[i] = NULL;
}
return map;
}
/*static void FC_MapClear(FC_Map* map)
{
int i;
if(map == NULL)
return;
// Go through each bucket
for(i = 0; i < map->num_buckets; ++i)
{
// Delete the nodes in order
FC_MapNode* node = map->buckets[i];
while(node != NULL)
{
FC_MapNode* last = node;
node = node->next;
free(last);
}
// Set the bucket to empty
map->buckets[i] = NULL;
}
}*/
static void FC_MapFree(FC_Map* map)
{
int i;
if(map == NULL)
return;
// Go through each bucket
for(i = 0; i < map->num_buckets; ++i)
{
// Delete the nodes in order
FC_MapNode* node = map->buckets[i];
while(node != NULL)
{
FC_MapNode* last = node;
node = node->next;
free(last);
}
}
free(map->buckets);
free(map);
}
// Note: Does not handle duplicates in any special way.
static FC_GlyphData* FC_MapInsert(FC_Map* map, Uint32 codepoint, FC_GlyphData glyph)
{
Uint32 index;
FC_MapNode* node;
if(map == NULL)
return NULL;
// Get index for bucket
index = codepoint % map->num_buckets;
// If this bucket is empty, create a node and return its value
if(map->buckets[index] == NULL)
{
node = map->buckets[index] = (FC_MapNode*)malloc(sizeof(FC_MapNode));
node->key = codepoint;
node->value = glyph;
node->next = NULL;
return &node->value;
}
for(node = map->buckets[index]; node != NULL; node = node->next)
{
// Find empty node and add a new one on.
if(node->next == NULL)
{
node->next = (FC_MapNode*)malloc(sizeof(FC_MapNode));
node = node->next;
node->key = codepoint;
node->value = glyph;
node->next = NULL;
return &node->value;
}
}
return NULL;
}
static FC_GlyphData* FC_MapFind(FC_Map* map, Uint32 codepoint)
{
Uint32 index;
FC_MapNode* node;
if(map == NULL)
return NULL;
// Get index for bucket
index = codepoint % map->num_buckets;
// Go through list until we find a match
for(node = map->buckets[index]; node != NULL; node = node->next)
{
if(node->key == codepoint)
return &node->value;
}
return NULL;
}
struct FC_Font
{
#ifndef FC_USE_SDL_GPU
SDL_Renderer* renderer;
#endif
TTF_Font* ttf_source; // TTF_Font source of characters
Uint8 owns_ttf_source; // Can we delete the TTF_Font ourselves?
FC_FilterEnum filter;
SDL_Color default_color;
Uint16 height;
Uint16 maxWidth;
Uint16 baseline;
int ascent;
int descent;
int lineSpacing;
int letterSpacing;
// Uses 32-bit (4-byte) Unicode codepoints to refer to each glyph
// Codepoints are little endian (reversed from UTF-8) so that something like 0x00000005 is ASCII 5 and the map can be indexed by ASCII values
FC_Map* glyphs;
FC_GlyphData last_glyph; // Texture packing cursor
int glyph_cache_size;
int glyph_cache_count;
FC_Image** glyph_cache;
char* loading_string;
};
// Private
static FC_GlyphData* FC_PackGlyphData(FC_Font* font, Uint32 codepoint, Uint16 width, Uint16 maxWidth, Uint16 maxHeight);
static FC_Rect FC_RenderLeft(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text);
static FC_Rect FC_RenderCenter(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text);
static FC_Rect FC_RenderRight(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* text);
static_inline SDL_Surface* FC_CreateSurface32(Uint32 width, Uint32 height)
{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
return SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
#else
return SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
#endif
}
char* U8_alloc(unsigned int size)
{
char* result;
if(size == 0)
return NULL;
result = (char*)malloc(size);
result[0] = '\0';
return result;
}
void U8_free(char* string)
{
free(string);
}
char* U8_strdup(const char* string)
{
char* result;
if(string == NULL)
return NULL;
result = (char*)malloc(strlen(string)+1);
strcpy(result, string);
return result;
}
int U8_strlen(const char* string)
{
int length = 0;
if(string == NULL)
return 0;
while(*string != '\0')
{
string = U8_next(string);
++length;
}
return length;
}
int U8_charsize(const char* character)
{
if(character == NULL)
return 0;
if((unsigned char)*character <= 0x7F)
return 1;
else if((unsigned char)*character < 0xE0)
return 2;
else if((unsigned char)*character < 0xF0)
return 3;
else
return 4;
return 1;
}
int U8_charcpy(char* buffer, const char* source, int buffer_size)
{
int charsize;
if(buffer == NULL || source == NULL || buffer_size < 1)
return 0;
charsize = U8_charsize(source);
if(charsize > buffer_size)
return 0;
memcpy(buffer, source, charsize);
return charsize;
}
const char* U8_next(const char* string)
{
return string + U8_charsize(string);
}
int U8_strinsert(char* string, int position, const char* source, int max_bytes)
{
int pos_u8char;
int len;
int add_len;
int ulen;
const char* string_start = string;
if(string == NULL || source == NULL)
return 0;
len = strlen(string);
add_len = strlen(source);
ulen = U8_strlen(string);
if(position == -1)
position = ulen;
if(position < 0 || position > ulen || len + add_len + 1 > max_bytes)
return 0;
// Move string pointer to the proper position
pos_u8char = 0;
while(*string != '\0' && pos_u8char < position)
{
string = (char*)U8_next(string);
++pos_u8char;
}
// Move the rest of the string out of the way
memmove(string + add_len, string, len - (string - string_start) + 1);
// Copy in the new characters
memcpy(string, source, add_len);
return 1;
}
void U8_strdel(char* string, int position)
{
if(string == NULL || position < 0)
return;
while(*string != '\0')
{
if(position == 0)
{
int chars_to_erase = U8_charsize(string);
int remaining_bytes = strlen(string) + 1;
memmove(string, string + chars_to_erase, remaining_bytes);
break;
}
string = (char*)U8_next(string);
--position;
}
}
static_inline FC_Rect FC_RectUnion(FC_Rect A, FC_Rect B)
{
float x,x2,y,y2;
x = FC_MIN(A.x, B.x);
y = FC_MIN(A.y, B.y);
x2 = FC_MAX(A.x+A.w, B.x+B.w);
y2 = FC_MAX(A.y+A.h, B.y+B.h);
{
FC_Rect result = {x, y, FC_MAX(0, x2 - x), FC_MAX(0, y2 - y)};
return result;
}
}
// Adapted from SDL_IntersectRect
static_inline FC_Rect FC_RectIntersect(FC_Rect A, FC_Rect B)
{
FC_Rect result;
float Amin, Amax, Bmin, Bmax;
// Horizontal intersection
Amin = A.x;
Amax = Amin + A.w;
Bmin = B.x;
Bmax = Bmin + B.w;
if(Bmin > Amin)
Amin = Bmin;
result.x = Amin;
if(Bmax < Amax)
Amax = Bmax;
result.w = Amax - Amin > 0 ? Amax - Amin : 0;
// Vertical intersection
Amin = A.y;
Amax = Amin + A.h;
Bmin = B.y;
Bmax = Bmin + B.h;
if(Bmin > Amin)
Amin = Bmin;
result.y = Amin;
if(Bmax < Amax)
Amax = Bmax;
result.h = Amax - Amin > 0 ? Amax - Amin : 0;
return result;
}
FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale)
{
float w = srcrect->w * xscale;
float h = srcrect->h * yscale;
FC_Rect result;
// FIXME: Why does the scaled offset look so wrong?
#ifdef FC_USE_SDL_GPU
{
GPU_Rect r = *srcrect;
GPU_BlitScale(src, &r, dest, x + xscale*r.w/2.0f, y + r.h/2.0f, xscale, yscale);
}
#else
{
SDL_RendererFlip flip = SDL_FLIP_NONE;
if(xscale < 0)
{
xscale = -xscale;
flip = (SDL_RendererFlip) ((int)flip | (int)SDL_FLIP_HORIZONTAL);
}
if(yscale < 0)
{
yscale = -yscale;
flip = (SDL_RendererFlip) ((int)flip | (int)SDL_FLIP_VERTICAL);
}
SDL_Rect r = *srcrect;
SDL_Rect dr = {(int)x, (int)y, (int)(xscale*r.w), (int)(yscale*r.h)};
SDL_RenderCopyEx(dest, src, &r, &dr, 0, NULL, flip);
}
#endif
result.x = x;
result.y = y;
result.w = w;
result.h = h;
return result;
}
static FC_Rect (*fc_render_callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale) = &FC_DefaultRenderCallback;
void FC_SetRenderCallback(FC_Rect (*callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale))
{
if(callback == NULL)
fc_render_callback = &FC_DefaultRenderCallback;
else
fc_render_callback = callback;
}
void FC_GetUTF8FromCodepoint(char* result, Uint32 codepoint)
{
char a, b, c, d;
if(result == NULL)
return;
a = (codepoint >> 24) & 0xFF;
b = (codepoint >> 16) & 0xFF;
c = (codepoint >> 8) & 0xFF;
d = codepoint & 0xFF;
if(a == 0)
{
if(b == 0)
{
if(c == 0)
{
result[0] = d;
result[1] = '\0';
}
else
{
result[0] = c;
result[1] = d;
result[2] = '\0';
}
}
else
{
result[0] = b;
result[1] = c;
result[2] = d;
result[3] = '\0';
}
}
else
{
result[0] = a;
result[1] = b;
result[2] = c;
result[3] = d;
result[4] = '\0';
}
}
Uint32 FC_GetCodepointFromUTF8(const char** c, Uint8 advance_pointer)
{
Uint32 result = 0;
const char* str;
if(c == NULL || *c == NULL)
return 0;
str = *c;
if((unsigned char)*str <= 0x7F)
result = *str;
else if((unsigned char)*str < 0xE0)
{
result |= (unsigned char)(*str) << 8;
result |= (unsigned char)(*(str+1));
if(advance_pointer)
*c += 1;
}
else if((unsigned char)*str < 0xF0)
{
result |= (unsigned char)(*str) << 16;
result |= (unsigned char)(*(str+1)) << 8;
result |= (unsigned char)(*(str+2));
if(advance_pointer)
*c += 2;
}
else
{
result |= (unsigned char)(*str) << 24;
result |= (unsigned char)(*(str+1)) << 16;
result |= (unsigned char)(*(str+2)) << 8;
result |= (unsigned char)(*(str+3));
if(advance_pointer)
*c += 3;
}
return result;
}
void FC_SetLoadingString(FC_Font* font, const char* string)
{
if(font == NULL)
return;
free(font->loading_string);
font->loading_string = U8_strdup(string);
}
unsigned int FC_GetBufferSize(void)
{
return fc_buffer_size;
}
void FC_SetBufferSize(unsigned int size)
{
free(fc_buffer);
if(size > 0)
{
fc_buffer_size = size;
fc_buffer = (char*)malloc(fc_buffer_size);
}
else
fc_buffer = (char*)malloc(fc_buffer_size);
}
unsigned int FC_GetTabWidth(void)
{
return fc_tab_width;
}
void FC_SetTabWidth(unsigned int width_in_spaces)
{
fc_tab_width = width_in_spaces;
}
// Constructors
static void FC_Init(FC_Font* font)
{
if(font == NULL)
return;
#ifndef FC_USE_SDL_GPU
font->renderer = NULL;
#endif
font->ttf_source = NULL;
font->owns_ttf_source = 0;
font->filter = FC_FILTER_NEAREST;
font->default_color.r = 0;
font->default_color.g = 0;
font->default_color.b = 0;
FC_GET_ALPHA(font->default_color) = 255;
font->height = 0; // ascent+descent
font->maxWidth = 0;
font->baseline = 0;
font->ascent = 0;
font->descent = 0;
font->lineSpacing = 0;
font->letterSpacing = 0;
// Give a little offset for when filtering/mipmaps are used. Depending on mipmap level, this will still not be enough.
font->last_glyph.rect.x = FC_CACHE_PADDING;
font->last_glyph.rect.y = FC_CACHE_PADDING;
font->last_glyph.rect.w = 0;
font->last_glyph.rect.h = 0;
font->last_glyph.cache_level = 0;
if(font->glyphs != NULL)
FC_MapFree(font->glyphs);
font->glyphs = FC_MapCreate(FC_DEFAULT_NUM_BUCKETS);
font->glyph_cache_size = 3;
font->glyph_cache_count = 0;
font->glyph_cache = (FC_Image**)malloc(font->glyph_cache_size * sizeof(FC_Image*));
if (font->loading_string == NULL)
font->loading_string = FC_GetStringASCII();
if(fc_buffer == NULL)
fc_buffer = (char*)malloc(fc_buffer_size);
}
static Uint8 FC_GrowGlyphCache(FC_Font* font)
{
if(font == NULL)
return 0;
#ifdef FC_USE_SDL_GPU
GPU_Image* new_level = GPU_CreateImage(font->height * 12, font->height * 12, GPU_FORMAT_RGBA);
GPU_SetAnchor(new_level, 0.5f, 0.5f); // Just in case the default is different
#else
SDL_Texture* new_level = SDL_CreateTexture(font->renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, font->height * 12, font->height * 12);
#endif
if(new_level == NULL || !FC_SetGlyphCacheLevel(font, font->glyph_cache_count, new_level))
{
FC_Log("Error: SDL_FontCache ran out of packing space and could not add another cache level.\n");
#ifdef FC_USE_SDL_GPU
GPU_FreeImage(new_level);
#else
SDL_DestroyTexture(new_level);
#endif
return 0;
}
// bug: we do not have the correct color here, this might be the wrong color!
// , most functions use set_color_for_all_caches()
// - for evading this bug, you must use FC_SetDefaultColor(), before using any draw functions
set_color(new_level, font->default_color.r, font->default_color.g, font->default_color.b, FC_GET_ALPHA(font->default_color));
#ifndef FC_USE_SDL_GPU
{
Uint8 r, g, b, a;
SDL_Texture* prev_target = SDL_GetRenderTarget(font->renderer);
SDL_Rect prev_clip, prev_viewport;
int prev_logicalw, prev_logicalh;
Uint8 prev_clip_enabled;
float prev_scalex, prev_scaley;
// only backup if previous target existed (SDL will preserve them for the default target)
if (prev_target) {
prev_clip_enabled = has_clip(font->renderer);
if (prev_clip_enabled)
prev_clip = get_clip(font->renderer);
SDL_RenderGetViewport(font->renderer, &prev_viewport);
SDL_RenderGetScale(font->renderer, &prev_scalex, &prev_scaley);
SDL_RenderGetLogicalSize(font->renderer, &prev_logicalw, &prev_logicalh);
}
SDL_SetTextureBlendMode(new_level, SDL_BLENDMODE_BLEND);
SDL_SetRenderTarget(font->renderer, new_level);
SDL_GetRenderDrawColor(font->renderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(font->renderer, 0, 0, 0, 0);
SDL_RenderClear(font->renderer);
SDL_SetRenderDrawColor(font->renderer, r, g, b, a);
SDL_SetRenderTarget(font->renderer, prev_target);
if (prev_target) {
if (prev_clip_enabled)
set_clip(font->renderer, &prev_clip);
if (prev_logicalw && prev_logicalh)
SDL_RenderSetLogicalSize(font->renderer, prev_logicalw, prev_logicalh);
else {
SDL_RenderSetViewport(font->renderer, &prev_viewport);
SDL_RenderSetScale(font->renderer, prev_scalex, prev_scaley);
}
}
}
#endif
return 1;
}
Uint8 FC_UploadGlyphCache(FC_Font* font, int cache_level, SDL_Surface* data_surface)
{
if(font == NULL || data_surface == NULL)
return 0;
#ifdef FC_USE_SDL_GPU
GPU_Image* new_level = GPU_CopyImageFromSurface(data_surface);
GPU_SetAnchor(new_level, 0.5f, 0.5f); // Just in case the default is different
if(FC_GetFilterMode(font) == FC_FILTER_LINEAR)
GPU_SetImageFilter(new_level, GPU_FILTER_LINEAR);
else
GPU_SetImageFilter(new_level, GPU_FILTER_NEAREST);
#else
SDL_Texture* new_level;
if(!fc_has_render_target_support)
new_level = SDL_CreateTextureFromSurface(font->renderer, data_surface);
else
{
// Must upload with render target enabled so we can put more glyphs on later
SDL_Renderer* renderer = font->renderer;
// Set filter mode for new texture
char old_filter_mode[16]; // Save it so we can change the hint value in the meantime
const char* old_filter_hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if(!old_filter_hint)
old_filter_hint = "nearest";
snprintf(old_filter_mode, 16, "%s", old_filter_hint);
if(FC_GetFilterMode(font) == FC_FILTER_LINEAR)
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
else
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");