-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft2.c
1435 lines (1306 loc) · 41.8 KB
/
ft2.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
/* FreeType 2 and UTF-8 encoding support for
* DarkPlaces
*/
#include "quakedef.h"
#include "ft2.h"
#include "ft2_defs.h"
#include "ft2_fontdefs.h"
static int img_fontmap[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // shift+digit line
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // digits
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // caps
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // caps
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // small
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // small
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // faces
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/*
================================================================================
CVars introduced with the freetype extension
================================================================================
*/
cvar_t r_font_disable_freetype = {CVAR_SAVE, "r_font_disable_freetype", "1", "disable freetype support for fonts entirely"};
cvar_t r_font_use_alpha_textures = {CVAR_SAVE, "r_font_use_alpha_textures", "0", "use alpha-textures for font rendering, this should safe memory"};
cvar_t r_font_size_snapping = {CVAR_SAVE, "r_font_size_snapping", "1", "stick to good looking font sizes whenever possible - bad when the mod doesn't support it!"};
cvar_t r_font_kerning = {CVAR_SAVE, "r_font_kerning", "1", "Use kerning if available"};
cvar_t developer_font = {CVAR_SAVE, "developer_font", "0", "prints debug messages about fonts"};
/*
================================================================================
Function definitions. Taken from the freetype2 headers.
================================================================================
*/
FT_EXPORT( FT_Error )
(*qFT_Init_FreeType)( FT_Library *alibrary );
FT_EXPORT( FT_Error )
(*qFT_Done_FreeType)( FT_Library library );
/*
FT_EXPORT( FT_Error )
(*qFT_New_Face)( FT_Library library,
const char* filepathname,
FT_Long face_index,
FT_Face *aface );
*/
FT_EXPORT( FT_Error )
(*qFT_New_Memory_Face)( FT_Library library,
const FT_Byte* file_base,
FT_Long file_size,
FT_Long face_index,
FT_Face *aface );
FT_EXPORT( FT_Error )
(*qFT_Done_Face)( FT_Face face );
FT_EXPORT( FT_Error )
(*qFT_Select_Size)( FT_Face face,
FT_Int strike_index );
FT_EXPORT( FT_Error )
(*qFT_Request_Size)( FT_Face face,
FT_Size_Request req );
FT_EXPORT( FT_Error )
(*qFT_Set_Char_Size)( FT_Face face,
FT_F26Dot6 char_width,
FT_F26Dot6 char_height,
FT_UInt horz_resolution,
FT_UInt vert_resolution );
FT_EXPORT( FT_Error )
(*qFT_Set_Pixel_Sizes)( FT_Face face,
FT_UInt pixel_width,
FT_UInt pixel_height );
FT_EXPORT( FT_Error )
(*qFT_Load_Glyph)( FT_Face face,
FT_UInt glyph_index,
FT_Int32 load_flags );
FT_EXPORT( FT_Error )
(*qFT_Load_Char)( FT_Face face,
FT_ULong char_code,
FT_Int32 load_flags );
FT_EXPORT( FT_UInt )
(*qFT_Get_Char_Index)( FT_Face face,
FT_ULong charcode );
FT_EXPORT( FT_Error )
(*qFT_Render_Glyph)( FT_GlyphSlot slot,
FT_Render_Mode render_mode );
FT_EXPORT( FT_Error )
(*qFT_Get_Kerning)( FT_Face face,
FT_UInt left_glyph,
FT_UInt right_glyph,
FT_UInt kern_mode,
FT_Vector *akerning );
FT_EXPORT( FT_Error )
(*qFT_Attach_Stream)( FT_Face face,
FT_Open_Args* parameters );
/*
================================================================================
Support for dynamically loading the FreeType2 library
================================================================================
*/
static dllfunction_t ft2funcs[] =
{
{"FT_Init_FreeType", (void **) &qFT_Init_FreeType},
{"FT_Done_FreeType", (void **) &qFT_Done_FreeType},
//{"FT_New_Face", (void **) &qFT_New_Face},
{"FT_New_Memory_Face", (void **) &qFT_New_Memory_Face},
{"FT_Done_Face", (void **) &qFT_Done_Face},
{"FT_Select_Size", (void **) &qFT_Select_Size},
{"FT_Request_Size", (void **) &qFT_Request_Size},
{"FT_Set_Char_Size", (void **) &qFT_Set_Char_Size},
{"FT_Set_Pixel_Sizes", (void **) &qFT_Set_Pixel_Sizes},
{"FT_Load_Glyph", (void **) &qFT_Load_Glyph},
{"FT_Load_Char", (void **) &qFT_Load_Char},
{"FT_Get_Char_Index", (void **) &qFT_Get_Char_Index},
{"FT_Render_Glyph", (void **) &qFT_Render_Glyph},
{"FT_Get_Kerning", (void **) &qFT_Get_Kerning},
{"FT_Attach_Stream", (void **) &qFT_Attach_Stream},
{NULL, NULL}
};
/// Handle for FreeType2 DLL
static dllhandle_t ft2_dll = NULL;
/// Memory pool for fonts
static mempool_t *font_mempool= NULL;
static rtexturepool_t *font_texturepool = NULL;
/// FreeType library handle
static FT_Library font_ft2lib = NULL;
#define POSTPROCESS_MAXRADIUS 8
typedef struct
{
unsigned char *buf, *buf2;
int bufsize, bufwidth, bufheight, bufpitch;
float blur, outline, shadowx, shadowy, shadowz;
int padding_t, padding_b, padding_l, padding_r, blurpadding_lt, blurpadding_rb, outlinepadding_t, outlinepadding_b, outlinepadding_l, outlinepadding_r;
unsigned char circlematrix[2*POSTPROCESS_MAXRADIUS+1][2*POSTPROCESS_MAXRADIUS+1];
unsigned char gausstable[2*POSTPROCESS_MAXRADIUS+1];
}
font_postprocess_t;
static font_postprocess_t pp;
/*
====================
Font_CloseLibrary
Unload the FreeType2 DLL
====================
*/
void Font_CloseLibrary (void)
{
if (font_mempool)
Mem_FreePool(&font_mempool);
if (font_texturepool)
R_FreeTexturePool(&font_texturepool);
if (font_ft2lib && qFT_Done_FreeType)
{
qFT_Done_FreeType(font_ft2lib);
font_ft2lib = NULL;
}
Sys_UnloadLibrary (&ft2_dll);
pp.buf = NULL;
}
/*
====================
Font_OpenLibrary
Try to load the FreeType2 DLL
====================
*/
qboolean Font_OpenLibrary (void)
{
const char* dllnames [] =
{
#if defined(WIN32)
"freetype6.dll",
"libfreetype-6.dll",
#elif defined(MACOSX)
"libfreetype.6.dylib",
"libfreetype.dylib",
#else
"libfreetype.so.6",
"libfreetype.so",
#endif
NULL
};
if (r_font_disable_freetype.integer)
return false;
// Already loaded?
if (ft2_dll)
return true;
// Load the DLL
if (!Sys_LoadLibrary (dllnames, &ft2_dll, ft2funcs))
return false;
return true;
}
/*
====================
Font_Init
Initialize the freetype2 font subsystem
====================
*/
void font_start(void)
{
if (!Font_OpenLibrary())
return;
if (qFT_Init_FreeType(&font_ft2lib))
{
Con_Print("ERROR: Failed to initialize the FreeType2 library!\n");
Font_CloseLibrary();
return;
}
font_mempool = Mem_AllocPool("FONT", 0, NULL);
if (!font_mempool)
{
Con_Print("ERROR: Failed to allocate FONT memory pool!\n");
Font_CloseLibrary();
return;
}
font_texturepool = R_AllocTexturePool();
if (!font_texturepool)
{
Con_Print("ERROR: Failed to allocate FONT texture pool!\n");
Font_CloseLibrary();
return;
}
}
void font_shutdown(void)
{
int i;
for (i = 0; i < MAX_FONTS; ++i)
{
if (dp_fonts[i].ft2)
{
Font_UnloadFont(dp_fonts[i].ft2);
dp_fonts[i].ft2 = NULL;
}
}
Font_CloseLibrary();
}
void font_newmap(void)
{
}
void Font_Init(void)
{
Cvar_RegisterVariable(&r_font_disable_freetype);
Cvar_RegisterVariable(&r_font_use_alpha_textures);
Cvar_RegisterVariable(&r_font_size_snapping);
Cvar_RegisterVariable(&r_font_kerning);
Cvar_RegisterVariable(&developer_font);
// let's open it at startup already
Font_OpenLibrary();
}
/*
================================================================================
Implementation of a more or less lazy font loading and rendering code.
================================================================================
*/
#include "ft2_fontdefs.h"
ft2_font_t *Font_Alloc(void)
{
if (!ft2_dll)
return NULL;
return Mem_Alloc(font_mempool, sizeof(ft2_font_t));
}
qboolean Font_Attach(ft2_font_t *font, ft2_attachment_t *attachment)
{
ft2_attachment_t *na;
font->attachmentcount++;
na = (ft2_attachment_t*)Mem_Alloc(font_mempool, sizeof(font->attachments[0]) * font->attachmentcount);
if (na == NULL)
return false;
if (font->attachments && font->attachmentcount > 1)
{
memcpy(na, font->attachments, sizeof(font->attachments[0]) * (font->attachmentcount - 1));
Mem_Free(font->attachments);
}
memcpy(na + sizeof(font->attachments[0]) * (font->attachmentcount - 1), attachment, sizeof(*attachment));
font->attachments = na;
return true;
}
float Font_VirtualToRealSize(float sz)
{
int vh;
//int vw;
int si;
float sn;
if(sz < 0)
return sz;
//vw = ((vid.width > 0) ? vid.width : vid_width.value);
vh = ((vid.height > 0) ? vid.height : vid_height.value);
// now try to scale to our actual size:
sn = sz * vh / vid_conheight.value;
si = (int)sn;
if ( sn - (float)si >= 0.5 )
++si;
return si;
}
float Font_SnapTo(float val, float snapwidth)
{
return floor(val / snapwidth + 0.5f) * snapwidth;
}
static qboolean Font_LoadFile(const char *name, int _face, ft2_settings_t *settings, ft2_font_t *font);
static qboolean Font_LoadSize(ft2_font_t *font, float size, qboolean check_only);
qboolean Font_LoadFont(const char *name, dp_font_t *dpfnt)
{
int s, count, i;
ft2_font_t *ft2, *fbfont, *fb;
ft2 = Font_Alloc();
if (!ft2)
{
dpfnt->ft2 = NULL;
return false;
}
// check if a fallback font has been specified, if it has been, and the
// font fails to load, use the image font as main font
for (i = 0; i < MAX_FONT_FALLBACKS; ++i)
{
if (dpfnt->fallbacks[i][0])
break;
}
if (!Font_LoadFile(name, dpfnt->req_face, &dpfnt->settings, ft2))
{
if (i >= MAX_FONT_FALLBACKS)
{
dpfnt->ft2 = NULL;
Mem_Free(ft2);
return false;
}
strlcpy(ft2->name, name, sizeof(ft2->name));
ft2->image_font = true;
ft2->has_kerning = false;
}
else
{
ft2->image_font = false;
}
// attempt to load fallback fonts:
fbfont = ft2;
for (i = 0; i < MAX_FONT_FALLBACKS; ++i)
{
if (!dpfnt->fallbacks[i][0])
break;
if (! (fb = Font_Alloc()) )
{
Con_Printf("Failed to allocate font for fallback %i of font %s\n", i, name);
break;
}
if (!Font_LoadFile(dpfnt->fallbacks[i], dpfnt->fallback_faces[i], &dpfnt->settings, fb))
{
Con_Printf("Failed to allocate font for fallback %i of font %s\n", i, name);
Mem_Free(fb);
break;
}
count = 0;
for (s = 0; s < MAX_FONT_SIZES && dpfnt->req_sizes[s] >= 0; ++s)
{
if (Font_LoadSize(fb, Font_VirtualToRealSize(dpfnt->req_sizes[s]), true))
++count;
}
if (!count)
{
Con_Printf("Failed to allocate font for fallback %i of font %s\n", i, name);
Font_UnloadFont(fb);
Mem_Free(fb);
break;
}
// at least one size of the fallback font loaded successfully
// link it:
fbfont->next = fb;
fbfont = fb;
}
if (fbfont == ft2 && ft2->image_font)
{
// no fallbacks were loaded successfully:
dpfnt->ft2 = NULL;
Mem_Free(ft2);
return false;
}
count = 0;
for (s = 0; s < MAX_FONT_SIZES && dpfnt->req_sizes[s] >= 0; ++s)
{
if (Font_LoadSize(ft2, Font_VirtualToRealSize(dpfnt->req_sizes[s]), false))
++count;
}
if (!count)
{
// loading failed for every requested size
Font_UnloadFont(ft2);
Mem_Free(ft2);
dpfnt->ft2 = NULL;
return false;
}
//Con_Printf("%i sizes loaded\n", count);
dpfnt->ft2 = ft2;
return true;
}
static qboolean Font_LoadFile(const char *name, int _face, ft2_settings_t *settings, ft2_font_t *font)
{
size_t namelen;
char filename[MAX_QPATH];
int status;
size_t i;
unsigned char *data;
fs_offset_t datasize;
memset(font, 0, sizeof(*font));
if (!Font_OpenLibrary())
{
if (!r_font_disable_freetype.integer)
{
Con_Printf("WARNING: can't open load font %s\n"
"You need the FreeType2 DLL to load font files\n",
name);
}
return false;
}
font->settings = settings;
namelen = strlen(name);
memcpy(filename, name, namelen);
memcpy(filename + namelen, ".ttf", 5);
data = FS_LoadFile(filename, font_mempool, false, &datasize);
if (!data)
{
memcpy(filename + namelen, ".otf", 5);
data = FS_LoadFile(filename, font_mempool, false, &datasize);
}
if (!data)
{
ft2_attachment_t afm;
memcpy(filename + namelen, ".pfb", 5);
data = FS_LoadFile(filename, font_mempool, false, &datasize);
if (data)
{
memcpy(filename + namelen, ".afm", 5);
afm.data = FS_LoadFile(filename, font_mempool, false, &afm.size);
if (afm.data)
Font_Attach(font, &afm);
}
}
if (!data)
{
// FS_LoadFile being not-quiet should print an error :)
return false;
}
Con_Printf("Loading font %s face %i...\n", filename, _face);
status = qFT_New_Memory_Face(font_ft2lib, (FT_Bytes)data, datasize, _face, (FT_Face*)&font->face);
if (status && _face != 0)
{
Con_Printf("Failed to load face %i of %s. Falling back to face 0\n", _face, name);
_face = 0;
status = qFT_New_Memory_Face(font_ft2lib, (FT_Bytes)data, datasize, 0, (FT_Face*)&font->face);
}
if (status)
{
Con_Printf("ERROR: can't create face for %s\n"
"Error %i\n", // TODO: error strings
name, status);
Font_UnloadFont(font);
return false;
}
// add the attachments
for (i = 0; i < font->attachmentcount; ++i)
{
FT_Open_Args args;
memset(&args, 0, sizeof(args));
args.flags = FT_OPEN_MEMORY;
args.memory_base = (const FT_Byte*)font->attachments[i].data;
args.memory_size = font->attachments[i].size;
if (qFT_Attach_Stream(font->face, &args))
Con_Printf("Failed to add attachment %u to %s\n", (unsigned)i, font->name);
}
memcpy(font->name, name, namelen+1);
font->image_font = false;
font->has_kerning = !!(((FT_Face)(font->face))->face_flags & FT_FACE_FLAG_KERNING);
return true;
}
void Font_Postprocess_Update(ft2_font_t *fnt, int bpp, int w, int h)
{
int needed, x, y;
float gausstable[2*POSTPROCESS_MAXRADIUS+1];
qboolean need_gauss = (!pp.buf || pp.blur != fnt->settings->blur || pp.shadowz != fnt->settings->shadowz);
qboolean need_circle = (!pp.buf || pp.outline != fnt->settings->outline || pp.shadowx != fnt->settings->shadowx || pp.shadowy != fnt->settings->shadowy);
pp.blur = fnt->settings->blur;
pp.outline = fnt->settings->outline;
pp.shadowx = fnt->settings->shadowx;
pp.shadowy = fnt->settings->shadowy;
pp.shadowz = fnt->settings->shadowz;
pp.outlinepadding_l = bound(0, ceil(pp.outline - pp.shadowx), POSTPROCESS_MAXRADIUS);
pp.outlinepadding_r = bound(0, ceil(pp.outline + pp.shadowx), POSTPROCESS_MAXRADIUS);
pp.outlinepadding_t = bound(0, ceil(pp.outline - pp.shadowy), POSTPROCESS_MAXRADIUS);
pp.outlinepadding_b = bound(0, ceil(pp.outline + pp.shadowy), POSTPROCESS_MAXRADIUS);
pp.blurpadding_lt = bound(0, ceil(pp.blur - pp.shadowz), POSTPROCESS_MAXRADIUS);
pp.blurpadding_rb = bound(0, ceil(pp.blur + pp.shadowz), POSTPROCESS_MAXRADIUS);
pp.padding_l = pp.blurpadding_lt + pp.outlinepadding_l;
pp.padding_r = pp.blurpadding_rb + pp.outlinepadding_r;
pp.padding_t = pp.blurpadding_lt + pp.outlinepadding_t;
pp.padding_b = pp.blurpadding_rb + pp.outlinepadding_b;
if(need_gauss)
{
float sum = 0;
for(x = -POSTPROCESS_MAXRADIUS; x <= POSTPROCESS_MAXRADIUS; ++x)
gausstable[POSTPROCESS_MAXRADIUS+x] = (pp.blur > 0 ? exp(-(pow(x + pp.shadowz, 2))/(pp.blur*pp.blur * 2)) : (floor(x + pp.shadowz + 0.5) == 0));
for(x = -pp.blurpadding_rb; x <= pp.blurpadding_lt; ++x)
sum += gausstable[POSTPROCESS_MAXRADIUS+x];
for(x = -POSTPROCESS_MAXRADIUS; x <= POSTPROCESS_MAXRADIUS; ++x)
pp.gausstable[POSTPROCESS_MAXRADIUS+x] = floor(gausstable[POSTPROCESS_MAXRADIUS+x] / sum * 255 + 0.5);
}
if(need_circle)
{
for(y = -POSTPROCESS_MAXRADIUS; y <= POSTPROCESS_MAXRADIUS; ++y)
for(x = -POSTPROCESS_MAXRADIUS; x <= POSTPROCESS_MAXRADIUS; ++x)
{
float d = pp.outline + 1 - sqrt(pow(x + pp.shadowx, 2) + pow(y + pp.shadowy, 2));
pp.circlematrix[POSTPROCESS_MAXRADIUS+y][POSTPROCESS_MAXRADIUS+x] = (d >= 1) ? 255 : (d <= 0) ? 0 : floor(d * 255 + 0.5);
}
}
pp.bufwidth = w + pp.padding_l + pp.padding_r;
pp.bufheight = h + pp.padding_t + pp.padding_b;
pp.bufpitch = pp.bufwidth;
needed = pp.bufwidth * pp.bufheight;
if(!pp.buf || pp.bufsize < needed * 2)
{
if(pp.buf)
Mem_Free(pp.buf);
pp.bufsize = needed * 4;
pp.buf = Mem_Alloc(font_mempool, pp.bufsize);
pp.buf2 = pp.buf + needed;
}
}
void Font_Postprocess(ft2_font_t *fnt, unsigned char *imagedata, int pitch, int bpp, int w, int h, int *pad_l, int *pad_r, int *pad_t, int *pad_b)
{
int x, y;
Font_Postprocess_Update(fnt, bpp, w, h);
if(imagedata)
{
// enlarge buffer
// perform operation, not exceeding the passed padding values,
// but possibly reducing them
*pad_l = min(*pad_l, pp.padding_l);
*pad_r = min(*pad_r, pp.padding_r);
*pad_t = min(*pad_t, pp.padding_t);
*pad_b = min(*pad_b, pp.padding_b);
// calculate gauss table
// outline the font (RGBA only)
if(bpp == 4 && (pp.outline > 0 || pp.blur > 0 || pp.shadowx != 0 || pp.shadowy != 0 || pp.shadowz != 0)) // we can only do this in BGRA
{
// this is like mplayer subtitle rendering
// bbuffer, bitmap buffer: this is our font
// abuffer, alpha buffer: this is pp.buf
// tmp: this is pp.buf2
// create outline buffer
memset(pp.buf, 0, pp.bufwidth * pp.bufheight);
for(y = -*pad_t; y < h + *pad_b; ++y)
for(x = -*pad_l; x < w + *pad_r; ++x)
{
int x1 = max(-x, -pp.outlinepadding_r);
int y1 = max(-y, -pp.outlinepadding_b);
int x2 = min(pp.outlinepadding_l, w-1-x);
int y2 = min(pp.outlinepadding_t, h-1-y);
int mx, my;
int cur = 0;
int highest = 0;
for(my = y1; my <= y2; ++my)
for(mx = x1; mx <= x2; ++mx)
{
cur = pp.circlematrix[POSTPROCESS_MAXRADIUS+my][POSTPROCESS_MAXRADIUS+mx] * (int)imagedata[(x+mx) * bpp + pitch * (y+my) + (bpp - 1)];
if(cur > highest)
highest = cur;
}
pp.buf[((x + pp.padding_l) + pp.bufpitch * (y + pp.padding_t))] = (highest + 128) / 255;
}
// blur the outline buffer
if(pp.blur > 0 || pp.shadowz != 0)
{
// horizontal blur
for(y = 0; y < pp.bufheight; ++y)
for(x = 0; x < pp.bufwidth; ++x)
{
int x1 = max(-x, -pp.blurpadding_rb);
int x2 = min(pp.blurpadding_lt, pp.bufwidth-1-x);
int mx;
int blurred = 0;
for(mx = x1; mx <= x2; ++mx)
blurred += pp.gausstable[POSTPROCESS_MAXRADIUS+mx] * (int)pp.buf[(x+mx) + pp.bufpitch * y];
pp.buf2[x + pp.bufpitch * y] = bound(0, blurred, 65025) / 255;
}
// vertical blur
for(y = 0; y < pp.bufheight; ++y)
for(x = 0; x < pp.bufwidth; ++x)
{
int y1 = max(-y, -pp.blurpadding_rb);
int y2 = min(pp.blurpadding_lt, pp.bufheight-1-y);
int my;
int blurred = 0;
for(my = y1; my <= y2; ++my)
blurred += pp.gausstable[POSTPROCESS_MAXRADIUS+my] * (int)pp.buf2[x + pp.bufpitch * (y+my)];
pp.buf[x + pp.bufpitch * y] = bound(0, blurred, 65025) / 255;
}
}
// paste the outline below the font
for(y = -*pad_t; y < h + *pad_b; ++y)
for(x = -*pad_l; x < w + *pad_r; ++x)
{
unsigned char outlinealpha = pp.buf[(x + pp.padding_l) + pp.bufpitch * (y + pp.padding_t)];
if(outlinealpha > 0)
{
unsigned char oldalpha = imagedata[x * bpp + pitch * y + (bpp - 1)];
// a' = 1 - (1 - a1) (1 - a2)
unsigned char newalpha = 255 - ((255 - (int)outlinealpha) * (255 - (int)oldalpha)) / 255; // this is >= oldalpha
// c' = (a2 c2 - a1 a2 c1 + a1 c1) / a' = (a2 c2 + a1 (1 - a2) c1) / a'
unsigned char oldfactor = (255 * (int)oldalpha) / newalpha;
//unsigned char outlinefactor = ((255 - oldalpha) * (int)outlinealpha) / newalpha;
int i;
for(i = 0; i < bpp-1; ++i)
{
unsigned char c = imagedata[x * bpp + pitch * y + i];
c = (c * (int)oldfactor) / 255 /* + outlinecolor[i] * (int)outlinefactor */;
imagedata[x * bpp + pitch * y + i] = c;
}
imagedata[x * bpp + pitch * y + (bpp - 1)] = newalpha;
}
//imagedata[x * bpp + pitch * y + (bpp - 1)] |= 0x80;
}
}
}
else
{
// just calculate parameters
*pad_l = pp.padding_l;
*pad_r = pp.padding_r;
*pad_t = pp.padding_t;
*pad_b = pp.padding_b;
}
}
static float Font_SearchSize(ft2_font_t *font, FT_Face fontface, float size);
static qboolean Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch, ft2_font_map_t **outmap);
static qboolean Font_LoadSize(ft2_font_t *font, float size, qboolean check_only)
{
int map_index;
ft2_font_map_t *fmap, temp;
int gpad_l, gpad_r, gpad_t, gpad_b;
if (!(size > 0.001f && size < 1000.0f))
size = 0;
if (!size)
size = 16;
if (size < 2) // bogus sizes are not allowed - and they screw up our allocations
return false;
for (map_index = 0; map_index < MAX_FONT_SIZES; ++map_index)
{
if (!font->font_maps[map_index])
break;
// if a similar size has already been loaded, ignore this one
//abs(font->font_maps[map_index]->size - size) < 4
if (font->font_maps[map_index]->size == size)
return true;
}
if (map_index >= MAX_FONT_SIZES)
return false;
if (check_only) {
FT_Face fontface;
if (font->image_font)
fontface = (FT_Face)font->next->face;
else
fontface = (FT_Face)font->face;
return (Font_SearchSize(font, fontface, size) > 0);
}
Font_Postprocess(font, NULL, 0, 4, size*2, size*2, &gpad_l, &gpad_r, &gpad_t, &gpad_b);
memset(&temp, 0, sizeof(temp));
temp.size = size;
temp.glyphSize = CeilPowerOf2(size*2 + max(gpad_l + gpad_r, gpad_t + gpad_b));
temp.sfx = (1.0/64.0)/(double)size;
temp.sfy = (1.0/64.0)/(double)size;
temp.intSize = -1; // negative value: LoadMap must search now :)
if (!Font_LoadMap(font, &temp, 0, &fmap))
{
Con_Printf("ERROR: can't load the first character map for %s\n"
"This is fatal\n",
font->name);
Font_UnloadFont(font);
return false;
}
font->font_maps[map_index] = temp.next;
fmap->sfx = temp.sfx;
fmap->sfy = temp.sfy;
// load the default kerning vector:
if (font->has_kerning)
{
Uchar l, r;
FT_Vector kernvec;
for (l = 0; l < 256; ++l)
{
for (r = 0; r < 256; ++r)
{
FT_ULong ul, ur;
ul = qFT_Get_Char_Index(font->face, l);
ur = qFT_Get_Char_Index(font->face, r);
if (qFT_Get_Kerning(font->face, ul, ur, FT_KERNING_DEFAULT, &kernvec))
{
fmap->kerning.kerning[l][r][0] = 0;
fmap->kerning.kerning[l][r][1] = 0;
}
else
{
fmap->kerning.kerning[l][r][0] = Font_SnapTo((kernvec.x / 64.0) / fmap->size, 1 / fmap->size);
fmap->kerning.kerning[l][r][1] = Font_SnapTo((kernvec.y / 64.0) / fmap->size, 1 / fmap->size);
}
}
}
}
return true;
}
int Font_IndexForSize(ft2_font_t *font, float _fsize, float *outw, float *outh)
{
int match = -1;
int value = 1000000;
int nval;
int matchsize = -10000;
int m;
float fsize_x, fsize_y;
ft2_font_map_t **maps = font->font_maps;
fsize_x = fsize_y = _fsize * vid.height / vid_conheight.value;
if(outw && *outw)
fsize_x = *outw * vid.width / vid_conwidth.value;
if(outh && *outh)
fsize_y = *outh * vid.height / vid_conheight.value;
if (fsize_x < 0)
{
if(fsize_y < 0)
fsize_x = fsize_y = 16;
else
fsize_x = fsize_y;
}
else
{
if(fsize_y < 0)
fsize_y = fsize_x;
}
for (m = 0; m < MAX_FONT_SIZES; ++m)
{
if (!maps[m])
continue;
// "round up" to the bigger size if two equally-valued matches exist
nval = 0.5 * (abs(maps[m]->size - fsize_x) + abs(maps[m]->size - fsize_y));
if (match == -1 || nval < value || (nval == value && matchsize < maps[m]->size))
{
value = nval;
match = m;
matchsize = maps[m]->size;
if (value == 0) // there is no better match
break;
}
}
if (value <= r_font_size_snapping.value)
{
// do NOT keep the aspect for perfect rendering
if (outh) *outh = maps[match]->size * vid_conheight.value / vid.height;
if (outw) *outw = maps[match]->size * vid_conwidth.value / vid.width;
}
return match;
}
ft2_font_map_t *Font_MapForIndex(ft2_font_t *font, int index)
{
if (index < 0 || index >= MAX_FONT_SIZES)
return NULL;
return font->font_maps[index];
}
static qboolean Font_SetSize(ft2_font_t *font, float w, float h)
{
if (font->currenth == h &&
((!w && (!font->currentw || font->currentw == font->currenth)) || // check if w==h when w is not set
font->currentw == w)) // same size has been requested
{
return true;
}
// sorry, but freetype doesn't seem to care about other sizes
w = (int)w;
h = (int)h;
if (font->image_font)
{
if (qFT_Set_Char_Size((FT_Face)font->next->face, (FT_F26Dot6)(w*64), (FT_F26Dot6)(h*64), 72, 72))
return false;
}
else
{
if (qFT_Set_Char_Size((FT_Face)font->face, (FT_F26Dot6)(w*64), (FT_F26Dot6)(h*64), 72, 72))
return false;
}
font->currentw = w;
font->currenth = h;
return true;
}
qboolean Font_GetKerningForMap(ft2_font_t *font, int map_index, float w, float h, Uchar left, Uchar right, float *outx, float *outy)
{
ft2_font_map_t *fmap;
if (!font->has_kerning || !r_font_kerning.integer)
return false;
if (map_index < 0 || map_index >= MAX_FONT_SIZES)
return false;
fmap = font->font_maps[map_index];
if (!fmap)
return false;
if (left < 256 && right < 256)
{
//Con_Printf("%g : %f, %f, %f :: %f\n", (w / (float)fmap->size), w, fmap->size, fmap->intSize, Font_VirtualToRealSize(w));
// quick-kerning, be aware of the size: scale it
if (outx) *outx = fmap->kerning.kerning[left][right][0];// * (w / (float)fmap->size);
if (outy) *outy = fmap->kerning.kerning[left][right][1];// * (h / (float)fmap->size);
return true;
}
else
{
FT_Vector kernvec;
FT_ULong ul, ur;
//if (qFT_Set_Pixel_Sizes((FT_Face)font->face, 0, fmap->size))
#if 0
if (!Font_SetSize(font, w, h))
{
// this deserves an error message
Con_Printf("Failed to get kerning for %s\n", font->name);
return false;
}
ul = qFT_Get_Char_Index(font->face, left);
ur = qFT_Get_Char_Index(font->face, right);
if (qFT_Get_Kerning(font->face, ul, ur, FT_KERNING_DEFAULT, &kernvec))
{
if (outx) *outx = Font_SnapTo(kernvec.x * fmap->sfx, 1 / fmap->size);
if (outy) *outy = Font_SnapTo(kernvec.y * fmap->sfy, 1 / fmap->size);
return true;
}
#endif
if (!Font_SetSize(font, fmap->intSize, fmap->intSize))
{
// this deserves an error message
Con_Printf("Failed to get kerning for %s\n", font->name);
return false;
}
ul = qFT_Get_Char_Index(font->face, left);
ur = qFT_Get_Char_Index(font->face, right);
if (qFT_Get_Kerning(font->face, ul, ur, FT_KERNING_DEFAULT, &kernvec))
{
if (outx) *outx = Font_SnapTo(kernvec.x * fmap->sfx, 1 / fmap->size);// * (w / (float)fmap->size);
if (outy) *outy = Font_SnapTo(kernvec.y * fmap->sfy, 1 / fmap->size);// * (h / (float)fmap->size);
return true;
}
return false;
}
}
qboolean Font_GetKerningForSize(ft2_font_t *font, float w, float h, Uchar left, Uchar right, float *outx, float *outy)
{
return Font_GetKerningForMap(font, Font_IndexForSize(font, h, NULL, NULL), w, h, left, right, outx, outy);
}
static void UnloadMapRec(ft2_font_map_t *map)
{
if (map->texture)
{
R_FreeTexture(map->texture);
map->texture = NULL;
}
if (map->next)
UnloadMapRec(map->next);
Mem_Free(map);
}
void Font_UnloadFont(ft2_font_t *font)
{
int i;
if (font->attachments && font->attachmentcount)
{
Mem_Free(font->attachments);
font->attachmentcount = 0;
font->attachments = NULL;
}
for (i = 0; i < MAX_FONT_SIZES; ++i)
{
if (font->font_maps[i])
{
UnloadMapRec(font->font_maps[i]);
font->font_maps[i] = NULL;
}
}
if (ft2_dll)
{
if (font->face)
{
qFT_Done_Face((FT_Face)font->face);
font->face = NULL;
}
}
}
static float Font_SearchSize(ft2_font_t *font, FT_Face fontface, float size)
{
float intSize = size;
while (1)
{
if (!Font_SetSize(font, intSize, intSize))
{
Con_Printf("ERROR: can't set size for font %s: %f ((%f))\n", font->name, size, intSize);
return -1;
}
if ((fontface->size->metrics.height>>6) <= size)
return intSize;
if (intSize < 2)
{
Con_Printf("ERROR: no appropriate size found for font %s: %f\n", font->name, size);
return -1;
}
--intSize;
}
}
static qboolean Font_LoadMap(ft2_font_t *font, ft2_font_map_t *mapstart, Uchar _ch, ft2_font_map_t **outmap)
{
char map_identifier[MAX_QPATH];
unsigned long mapidx = _ch / FONT_CHARS_PER_MAP;
unsigned char *data;
FT_ULong ch, mapch;