forked from rougier/freetype-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture-font.c
1169 lines (994 loc) · 34.1 KB
/
texture-font.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 GL - A C OpenGL Freetype engine
*
* Distributed under the OSI-approved BSD 2-Clause License. See accompanying
* file `LICENSE` for more details.
*/
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_SIZES_H
#include FT_STROKER_H
// #include FT_ADVANCES_H
#include FT_LCD_FILTER_H
#include FT_TRUETYPE_TABLES_H
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#ifdef __APPLE__
# include <machine/endian.h>
# define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
# define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
# define __BYTE_ORDER __BYTE_ORDER__
#elif defined(_WIN32) || defined(_WIN64)
# define __LITTLE_ENDIAN 1234
# define __BIG_ENDIAN 4321
# define __BYTE_ORDER __LITTLE_ENDIAN
#else
# include <endian.h>
#endif
#include "distance-field.h"
#include "texture-font.h"
#include "platform.h"
#include "utf8-utils.h"
#include "ftgl-utils.h"
#define HRES 64
#define HRESf 64.f
#define DPI 72
static float convert_F26Dot6_to_float(FT_F26Dot6 value)
{
return ((float)value) / 64.0;
}
static FT_F26Dot6 convert_float_to_F26Dot6(float value)
{
return (FT_F26Dot6) (value * 64.0);
}
// per-thread library
__THREAD texture_font_library_t * freetype_gl_library = NULL;
__THREAD font_mode_t mode_default=MODE_FREE_CLOSE;
// rol8 ror8
#if (defined(_WIN32) || defined(_WIN64)) && !defined(__GNUC__)
# define inline
static inline __builtin_bswap32(uint32_t in)
{
return ((in >> 24) & 0xFF) | ((in >> 8) & 0xFF00) | ((in & 0xFF00) << 8) | ((in & 0xFF) << 24);
}
#endif
static inline uint32_t rol(uint32_t in, uint32_t x)
{
return (in >> (32-x)) | (in << x);
}
// ------------------------------------------------------ texture_glyph_clone ---
texture_glyph_t*
texture_glyph_clone(texture_glyph_t* self)
{
int i;
texture_glyph_t* new_glyph;
float* source;
float** target;
assert(self);
new_glyph = (texture_glyph_t *) malloc( sizeof(texture_glyph_t) );
if(new_glyph == NULL) {
freetype_gl_error( Out_Of_Memory );
return NULL;
}
memcpy(new_glyph, self, sizeof(texture_glyph_t));
new_glyph->kerning = vector_new(sizeof(float**));
vector_resize(new_glyph->kerning, self->kerning->size);
for (i = 0; i < self->kerning->size; i++) {
source = *(float**)vector_get(self->kerning, i);
target = (float**)vector_get(new_glyph->kerning, i);
*target = calloc(0x100, sizeof(float));
memcpy(*target, source, 0x100);
}
return new_glyph;
}
// ------------------------------------------------------ texture_glyph_new ---
texture_glyph_t *
texture_glyph_new(void)
{
texture_glyph_t *self = (texture_glyph_t *) malloc( sizeof(texture_glyph_t) );
if(self == NULL) {
freetype_gl_error( Out_Of_Memory );
return NULL;
}
self->codepoint = -1;
self->width = 0;
self->height = 0;
/* Attributes that can have different images for the same codepoint */
self->rendermode = RENDER_NORMAL;
self->outline_thickness = 0.0;
self->glyphmode = GLYPH_END;
/* End of attribute part */
self->offset_x = 0;
self->offset_y = 0;
self->advance_x = 0.0;
self->advance_y = 0.0;
self->s0 = 0.0;
self->t0 = 0.0;
self->s1 = 0.0;
self->t1 = 0.0;
self->kerning = vector_new( sizeof(float**) );
return self;
}
// ---------------------------------------------- texture_font_default_mode ---
void
texture_font_default_mode(font_mode_t mode)
{
mode_default=mode;
}
// --------------------------------------------------- texture_glyph_delete ---
void
texture_glyph_delete( texture_glyph_t *self )
{
int i;
assert( self );
for(i=0; i < self->kerning->size; i++)
free( *(float **) vector_get( self->kerning, i ) );
vector_delete( self->kerning );
free( self );
}
// ---------------------------------------------- texture_glyph_get_kerning ---
float
texture_glyph_get_kerning( const texture_glyph_t * self,
const char * codepoint )
{
uint32_t ucodepoint = utf8_to_utf32( codepoint );
uint32_t i = ucodepoint >> 8;
uint32_t j = ucodepoint & 0xFF;
float *kern_index;
assert( self );
if(ucodepoint == -1)
return 0;
if(self->kerning->size <= i)
return 0;
kern_index = *(float **) vector_get( self->kerning, i );
if(!kern_index)
return 0;
else
return kern_index[j];
}
// ---------------------------------------------- texture_font_index_kerning ---
void texture_font_index_kerning( texture_glyph_t * self,
uint32_t codepoint,
float kerning)
{
uint32_t i = codepoint >> 8;
uint32_t j = codepoint & 0xFF;
float ** kerning_index;
if(self->kerning->size <= i) {
vector_resize( self->kerning, i+1);
}
kerning_index = (float **) vector_get( self->kerning, i );
if(!*kerning_index) {
*kerning_index = calloc( 0x100, sizeof(float) );
}
(*kerning_index)[j] = kerning;
}
// ------------------------------------------ texture_font_generate_kerning ---
void
texture_font_generate_kerning( texture_font_t *self,
FT_Library *library, FT_Face *face )
{
size_t i, j, k;
FT_UInt glyph_index, prev_index;
texture_glyph_t *glyph, *prev_glyph;
FT_Vector kerning;
assert( self );
/* For each glyph couple combination, check if kerning is necessary */
/* Starts at index 1 since 0 is for the special background glyph */
GLYPHS_ITERATOR(i, glyph, self->glyphs ) {
glyph_index = FT_Get_Char_Index( *face, glyph->codepoint );
// fprintf(stderr, "Retrieving glyph %p from index %i\n", __glyphs, __i);
// fprintf(stderr, "Glpyh %p: Indexing %d, kerning %p\n", glyph, glyph_index, glyph->kerning);
for(k=0; k < glyph->kerning->size; k++)
free( *(float **) vector_get( glyph->kerning, k ) );
vector_clear( glyph->kerning );
GLYPHS_ITERATOR(j, prev_glyph, self->glyphs ) {
prev_index = FT_Get_Char_Index( *face, prev_glyph->codepoint );
// FT_KERNING_UNFITTED returns FT_F26Dot6 values.
FT_Get_Kerning( *face, prev_index, glyph_index, FT_KERNING_UNFITTED, &kerning );
// printf("%c(%d)-%c(%d): %ld\n",
// prev_glyph->codepoint, prev_glyph->codepoint,
// glyph_index, glyph_index, kerning.x);
if( kerning.x ) {
texture_font_index_kerning( glyph,
prev_glyph->codepoint,
convert_F26Dot6_to_float(kerning.x) / HRESf );
}
// also insert kerning with the current added element
FT_Get_Kerning( *face, glyph_index, prev_index, FT_KERNING_UNFITTED, &kerning );
if( kerning.x ) {
texture_font_index_kerning( prev_glyph,
glyph->codepoint,
kerning.x / (float)(HRESf*HRESf) );
}
}
GLYPHS_ITERATOR_END
}
GLYPHS_ITERATOR_END
}
// -------------------------------------------------- texture_is_color_font ---
int
texture_is_color_font( texture_font_t *self) {
static const uint32_t tag = FT_MAKE_TAG('C', 'B', 'D', 'T');
unsigned long length = 0;
FT_Load_Sfnt_Table(self->face, tag, 0, NULL, &length);
return length != 0;
}
// -------------------------------------------------- texture_font_set_size ---
int
texture_font_set_size ( texture_font_t *self, float size )
{
FT_Error error=0;
FT_Matrix matrix = {
(int)((1.0/HRES) * 0x10000L),
(int)((0.0) * 0x10000L),
(int)((0.0) * 0x10000L),
(int)((1.0) * 0x10000L)};
if( FT_HAS_FIXED_SIZES( self->face ) ) {
/* Select best size */
if (self->face->num_fixed_sizes == 0) {
freetype_gl_error( No_Fixed_Size_In_Color_Font );
return 0;
}
int best_match = 0;
float diff = 1e20;
int i;
for (i = 0; i < self->face->num_fixed_sizes; ++i) {
float new_size = convert_F26Dot6_to_float(self->face->available_sizes[i].size);
float ndiff = size > new_size ? size / new_size : new_size / size;
if(freetype_gl_warnings)
log_error("candiate: size[%i]=%f %d*%d\n", i, new_size,
self->face->available_sizes[i].width,
self->face->available_sizes[i].height);
if (ndiff < diff) {
best_match = i;
diff = ndiff;
}
}
if(freetype_gl_warnings)
log_error("selected: size[%i] for %f\n", best_match, size);
error = FT_Select_Size(self->face, best_match);
if(error) {
freetype_error( error );
return 0;
}
self->scale = self->size / convert_F26Dot6_to_float(self->face->available_sizes[best_match].size);
} else {
/* Set char size */
error = FT_Set_Char_Size(self->face, convert_float_to_F26Dot6(size), 0, DPI * HRES, DPI);
if(error) {
freetype_error( error );
return 0;
}
}
/* Set transform matrix */
FT_Set_Transform(self->face, &matrix, NULL);
return 1;
}
// --------------------------------------------------
void
texture_font_init_size( texture_font_t * self)
{
FT_Size_Metrics metrics;
self->underline_position = self->face->underline_position / (float)(HRESf*HRESf) * self->size;
self->underline_position = roundf( self->underline_position );
if( self->underline_position > -2 )
{
self->underline_position = -2.0;
}
self->underline_thickness = self->face->underline_thickness / (float)(HRESf*HRESf) * self->size;
self->underline_thickness = roundf( self->underline_thickness );
if( self->underline_thickness < 1 )
{
self->underline_thickness = 1.0;
}
metrics = self->face->size->metrics;
self->ascender = metrics.ascender >> 6;
self->descender = metrics.descender >> 6;
self->height = metrics.height >> 6;
self->linegap = self->height - self->ascender + self->descender;
}
// ------------------------------------------------------ texture_font_init ---
static int
texture_font_init(texture_font_t *self)
{
assert(self->atlas);
assert(self->size > 0);
assert((self->location == TEXTURE_FONT_FILE && self->filename)
|| (self->location == TEXTURE_FONT_MEMORY
&& self->memory.base && self->memory.size));
self->glyphs = vector_new(sizeof(texture_glyph_t *));
self->height = 0;
self->ascender = 0;
self->descender = 0;
self->linegap = 0;
self->rendermode = RENDER_NORMAL;
self->outline_thickness = 0.0;
self->hinting = 1;
self->kerning = 1;
self->filtering = 1;
self->scaletex = 1;
self->scale = 1.0;
// FT_LCD_FILTER_LIGHT is (0x00, 0x55, 0x56, 0x55, 0x00)
// FT_LCD_FILTER_DEFAULT is (0x10, 0x40, 0x70, 0x40, 0x10)
self->lcd_weights[0] = 0x10;
self->lcd_weights[1] = 0x40;
self->lcd_weights[2] = 0x70;
self->lcd_weights[3] = 0x40;
self->lcd_weights[4] = 0x10;
if (!texture_font_load_face(self, self->size))
return -1;
texture_font_init_size( self );
if (!texture_font_set_size(self, self->size))
return -1;
/* NULL is a special glyph */
texture_font_get_glyph( self, NULL );
return 0;
}
// ---------------------------------------------------- texture_library_new ---
texture_font_library_t *
texture_library_new(void)
{
texture_font_library_t *self = calloc(1, sizeof(*self));
self->mode = MODE_ALWAYS_OPEN;
return self;
}
// --------------------------------------------- texture_font_new_from_file ---
texture_font_t *
texture_font_new_from_file(texture_atlas_t *atlas, const float pt_size,
const char *filename)
{
texture_font_t *self;
assert(filename);
self = calloc(1, sizeof(*self));
if (!self) {
freetype_gl_error( Out_Of_Memory );
return NULL;
}
self->atlas = atlas;
self->size = pt_size;
self->location = TEXTURE_FONT_FILE;
self->filename = strdup(filename);
self->mode = mode_default;
if (texture_font_init(self)) {
texture_font_delete(self);
return NULL;
}
return self;
}
// ------------------------------------------- texture_font_new_from_memory ---
texture_font_t *
texture_font_new_from_memory(texture_atlas_t *atlas, float pt_size,
const void *memory_base, size_t memory_size)
{
texture_font_t *self;
assert(memory_base);
assert(memory_size);
self = calloc(1, sizeof(*self));
if (!self) {
freetype_gl_error( Out_Of_Memory );
return NULL;
}
self->atlas = atlas;
self->size = pt_size;
self->location = TEXTURE_FONT_MEMORY;
self->memory.base = memory_base;
self->memory.size = memory_size;
self->mode = mode_default;
if (texture_font_init(self)) {
texture_font_delete(self);
return NULL;
}
return self;
}
// ----------------------------------------------------- texture_font_clone ---
texture_font_t *
texture_font_clone( texture_font_t *old, float pt_size)
{
texture_font_t *self;
FT_Error error = 0;
float native_size = old->size / old->scale; // unscale fonts
self = calloc(1, sizeof(*self));
if (!self) {
freetype_gl_error( Out_Of_Memory );
return NULL;
}
memcpy(self, old, sizeof(*self));
self->size = pt_size;
error = FT_New_Size( self->face, &self->ft_size );
if(error) {
freetype_error( error );
return NULL;
}
error = FT_Activate_Size( self->ft_size );
if(error) {
freetype_error( error );
return NULL;
}
if(!texture_font_set_size ( self, pt_size ))
return NULL;
texture_font_init_size( self );
if(self->size / self->scale != native_size)
self->glyphs = vector_new(sizeof(texture_glyph_t *));
return self;
}
// ----------------------------------------------------- texture_font_close ---
void
texture_font_close( texture_font_t *self, font_mode_t face_mode, font_mode_t library_mode )
{
if( self->face && self->mode <= face_mode ) {
FT_Done_Face( self->face );
self->face = NULL;
} else {
return; // never close the library when the face stays open
}
if( self->library && self->library->library && self->library->mode <= library_mode ) {
FT_Done_FreeType( self->library->library );
self->library->library = NULL;
}
}
// ------------------------------------------------- texture_font_load_face ---
int
texture_font_load_face( texture_font_t *self, float size )
{
FT_Error error;
if ( !self->library ) {
if ( !freetype_gl_library ) {
freetype_gl_library = texture_library_new();
}
self->library = freetype_gl_library;
}
if( !self->library->library ) {
error = FT_Init_FreeType( &self->library->library );
if(error) {
freetype_error( error );
goto cleanup;
}
}
if( !self->face ) {
switch (self->location) {
case TEXTURE_FONT_FILE:
error = FT_New_Face(self->library->library, self->filename, 0, &self->face);
if(error) {
freetype_error( error );
goto cleanup_library;
}
break;
case TEXTURE_FONT_MEMORY:
error = FT_New_Memory_Face(self->library->library,
self->memory.base, self->memory.size, 0, &self->face);
if(error) {
freetype_error( error );
goto cleanup_library;
}
break;
}
/* Select charmap */
error = FT_Select_Charmap(self->face, FT_ENCODING_UNICODE);
if(error) {
freetype_error( error );
goto cleanup_face;
}
error = FT_New_Size( self->face, &self->ft_size );
if(error) {
freetype_error( error );
goto cleanup_face;
}
error = FT_Activate_Size( self->ft_size );
if(error) {
freetype_error( error );
goto cleanup_face;
}
if(!texture_font_set_size ( self, size ))
goto cleanup_face;
}
return 1;
cleanup_face:
texture_font_close( self, MODE_ALWAYS_OPEN, MODE_FREE_CLOSE );
return 0;
cleanup_library:
texture_font_close( self, MODE_ALWAYS_OPEN, MODE_ALWAYS_OPEN );
cleanup:
return 0;
}
// ---------------------------------------------------- texture_font_delete ---
void
texture_font_delete( texture_font_t *self )
{
size_t i;
texture_glyph_t *glyph;
FT_Error error=0;
assert( self );
error = FT_Done_Size( self->ft_size );
if(error) {
freetype_error( error );
}
texture_font_close( self, MODE_ALWAYS_OPEN, MODE_FREE_CLOSE );
if(self->location == TEXTURE_FONT_FILE && self->filename)
free( self->filename );
GLYPHS_ITERATOR(i, glyph, self->glyphs) {
texture_glyph_delete( glyph );
} GLYPHS_ITERATOR_END1
free( __glyphs );
GLYPHS_ITERATOR_END2;
vector_delete( self->glyphs );
free( self );
}
// ------------------------------------------------ texture_font_find_glyph ---
texture_glyph_t *
texture_font_find_glyph( texture_font_t * self,
const char * codepoint )
{
if(!codepoint)
return (texture_glyph_t *)self->atlas->special;
return texture_font_find_glyph_gi(self, utf8_to_utf32( codepoint ));
}
// ---------------------------------------------- texture_font_find_glyph_gi ---
texture_glyph_t *
texture_font_find_glyph_gi( texture_font_t * self,
uint32_t codepoint )
{
uint32_t i = codepoint >> 8;
uint32_t j = codepoint & 0xFF;
texture_glyph_t **glyph_index1, *glyph;
if(self->glyphs->size <= i)
return NULL;
glyph_index1 = *(texture_glyph_t ***) vector_get( self->glyphs, i );
if(!glyph_index1)
return NULL;
else
glyph = glyph_index1[j];
while( glyph && // if no glyph is there, we are done here
(glyph->rendermode != self->rendermode ||
glyph->outline_thickness != self->outline_thickness) ) {
if( glyph->glyphmode != GLYPH_CONT)
return NULL;
glyph++;
}
return glyph;
}
int
texture_font_index_glyph( texture_font_t * self,
texture_glyph_t *glyph,
uint32_t codepoint)
{
uint32_t i = codepoint >> 8;
uint32_t j = codepoint & 0xFF;
texture_glyph_t ***glyph_index1, *glyph_insert;
if(self->glyphs->size <= i) {
vector_resize( self->glyphs, i+1);
}
glyph_index1 = (texture_glyph_t ***) vector_get( self->glyphs, i );
if(!*glyph_index1) {
*glyph_index1 = calloc( 0x100, sizeof(texture_glyph_t*) );
}
if(( glyph_insert = (*glyph_index1)[j] )) {
int i = 0;
// fprintf(stderr, "glyph already there\n");
while (glyph_insert[i].glyphmode != GLYPH_END)
i++;
// fprintf(stderr, "Insert a glyph after position %d\n", i);
glyph_insert[i].glyphmode = GLYPH_CONT;
(*glyph_index1)[j] = glyph_insert = realloc( glyph_insert, sizeof(texture_glyph_t)*(i+2) );
memcpy( glyph_insert+(i+1), glyph, sizeof(texture_glyph_t) );
return 1;
} else {
(*glyph_index1)[j] = glyph;
return 0;
}
}
// ------------------------------------------------ texture_font_load_glyph ---
int
texture_font_load_glyph( texture_font_t * self,
const char * codepoint )
{
/* codepoint NULL is special : it is used for line drawing (overline,
* underline, strikethrough) and background.
*/
if( !codepoint ) {
return 1;
}
uint32_t ucodepoint = utf8_to_utf32(codepoint);
return texture_font_load_glyph_gi( self,
FT_Get_Char_Index( self->face, ucodepoint),
ucodepoint);
}
// ------------------------------------------------ texture_font_load_glyph ---
int
texture_font_load_glyph_gi( texture_font_t * self,
uint32_t glyph_index,
uint32_t ucodepoint )
{
size_t i, x, y;
FT_Error error;
FT_Face face;
FT_Glyph ft_glyph = NULL;
FT_GlyphSlot slot;
FT_Bitmap ft_bitmap;
texture_glyph_t *glyph;
FT_Int32 flags = 0;
int ft_glyph_top = 0;
int ft_glyph_left = 0;
ivec4 region;
size_t missed = 0;
/* Check if codepoint has been already loaded */
if (texture_font_find_glyph_gi(self, ucodepoint)) {
return 1;
}
if (!texture_font_load_face(self, self->size))
return 0;
flags = 0;
ft_glyph_top = 0;
ft_glyph_left = 0;
if(!glyph_index) {
texture_glyph_t * glyph;
if ((glyph = texture_font_find_glyph(self, "\0"))) {
texture_font_index_glyph( self, glyph, ucodepoint );
texture_font_close( self, MODE_AUTO_CLOSE, MODE_AUTO_CLOSE );
return 1;
}
}
// WARNING: We use texture-atlas depth to guess if user wants
// LCD subpixel rendering
if( self->rendermode != RENDER_NORMAL && self->rendermode != RENDER_SIGNED_DISTANCE_FIELD )
{
flags |= FT_LOAD_NO_BITMAP;
}
else
{
flags |= FT_LOAD_RENDER;
}
if( !self->hinting )
{
flags |= FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT;
}
else
{
flags |= FT_LOAD_FORCE_AUTOHINT;
}
if( self->atlas->depth == 3 )
{
FT_Library_SetLcdFilter( self->library->library, FT_LCD_FILTER_LIGHT );
flags |= FT_LOAD_TARGET_LCD;
if( self->filtering )
{
FT_Library_SetLcdFilterWeights( self->library->library, self->lcd_weights );
}
}
else if (HRES == 1)
{
/* “FT_LOAD_TARGET_LIGHT
* A lighter hinting algorithm for gray-level modes. Many generated
* glyphs are fuzzier but better resemble their original shape.
* This is achieved by snapping glyphs to the pixel grid
* only vertically (Y-axis), as is done by FreeType's new CFF engine
* or Microsoft's ClearType font renderer.”
* https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_target_xxx
*/
flags |= FT_LOAD_TARGET_LIGHT;
}
if( self->atlas->depth == 4 )
{
#ifdef FT_LOAD_COLOR
flags |= FT_LOAD_COLOR;
#else
freetype_gl_error( Load_Color_Not_Available );
#endif
}
error = FT_Activate_Size( self->ft_size );
if(error) {
freetype_error( error );
return 0;
}
error = FT_Load_Glyph( self->face, glyph_index, flags );
if( error )
{
freetype_error( error );
texture_font_close( self, MODE_AUTO_CLOSE, MODE_AUTO_CLOSE );
return 0;
}
if( self->rendermode == RENDER_NORMAL || self->rendermode == RENDER_SIGNED_DISTANCE_FIELD )
{
slot = self->face->glyph;
ft_bitmap = slot->bitmap;
ft_glyph_top = slot->bitmap_top;
ft_glyph_left = slot->bitmap_left;
}
else
{
FT_Stroker stroker;
FT_BitmapGlyph ft_bitmap_glyph;
error = FT_Stroker_New( self->library->library, &stroker );
if( error )
{
freetype_error( error );
goto cleanup_stroker;
}
FT_Stroker_Set(stroker,
(int)(self->outline_thickness * HRES),
FT_STROKER_LINECAP_ROUND,
FT_STROKER_LINEJOIN_ROUND,
0);
error = FT_Get_Glyph( self->face->glyph, &ft_glyph);
if( error )
{
freetype_error( error );
goto cleanup_stroker;
}
if( self->rendermode == RENDER_OUTLINE_EDGE )
error = FT_Glyph_Stroke( &ft_glyph, stroker, 1 );
else if ( self->rendermode == RENDER_OUTLINE_POSITIVE )
error = FT_Glyph_StrokeBorder( &ft_glyph, stroker, 0, 1 );
else if ( self->rendermode == RENDER_OUTLINE_NEGATIVE )
error = FT_Glyph_StrokeBorder( &ft_glyph, stroker, 1, 1 );
if( error )
{
freetype_error( error );
goto cleanup_stroker;
}
switch( self->atlas->depth ) {
case 1:
error = FT_Glyph_To_Bitmap( &ft_glyph, FT_RENDER_MODE_NORMAL, 0, 1);
break;
case 3:
error = FT_Glyph_To_Bitmap( &ft_glyph, FT_RENDER_MODE_LCD, 0, 1);
break;
case 4:
error = FT_Glyph_To_Bitmap( &ft_glyph, FT_RENDER_MODE_NORMAL, 0, 1);
break;
}
if( error )
{
freetype_error( error );
goto cleanup_stroker;
}
ft_bitmap_glyph = (FT_BitmapGlyph) ft_glyph;
ft_bitmap = ft_bitmap_glyph->bitmap;
ft_glyph_top = ft_bitmap_glyph->top;
ft_glyph_left = ft_bitmap_glyph->left;
cleanup_stroker:
FT_Stroker_Done( stroker );
if( error )
{
texture_font_close( self, MODE_AUTO_CLOSE, MODE_AUTO_CLOSE );
return 0;
}
}
struct {
int left;
int top;
int right;
int bottom;
} padding = { 0, 0, 1, 1 };
if( self->rendermode == RENDER_SIGNED_DISTANCE_FIELD )
{
padding.top = 1;
padding.left = 1;
}
if( self->padding != 0 )
{
padding.top += self->padding;
padding.left += self->padding;
padding.right += self->padding;
padding.bottom += self->padding;
}
size_t src_w = self->atlas->depth == 3 ? ft_bitmap.width/3 : ft_bitmap.width;
size_t src_h = ft_bitmap.rows;
size_t tgt_w = src_w + padding.left + padding.right;
size_t tgt_h = src_h + padding.top + padding.bottom;
region = texture_atlas_get_region( self->atlas, tgt_w, tgt_h );
if ( region.x < 0 )
{
freetype_gl_warning( Texture_Atlas_Full );
texture_font_close( self, MODE_AUTO_CLOSE, MODE_AUTO_CLOSE );
return 0;
}
x = region.x;
y = region.y;
// Copy pixel data over
unsigned char *buffer = calloc( tgt_w * tgt_h * self->atlas->depth, sizeof(unsigned char) );
unsigned char *dst_ptr = buffer + (padding.top * tgt_w + padding.left) * self->atlas->depth;
unsigned char *src_ptr = ft_bitmap.buffer;
if( ft_bitmap.pixel_mode == FT_PIXEL_MODE_BGRA && self->atlas->depth == 4 )
{
// BGRA in, RGBA out
for( i = 0; i < src_h; i++ ) {
int j;
for( j = 0; j < ft_bitmap.width; j++ ) {
uint32_t bgra, rgba;
bgra = ((uint32_t*)src_ptr)[j];
#if __BYTE_ORDER == __BIG_ENDIAN
rgba = rol(__builtin_bswap32(bgra), 8);
#else
rgba = rol(__builtin_bswap32(bgra), 24);
#endif
((uint32_t*)dst_ptr)[j] = rgba;
}
dst_ptr += tgt_w * self->atlas->depth;
src_ptr += ft_bitmap.pitch;
}
}
else if( ft_bitmap.pixel_mode == FT_PIXEL_MODE_BGRA && self->atlas->depth == 1 )
{
// BGRA in, grey out: Use weighted sum for luminosity, and multiply by alpha
struct src_pixel_t { uint8_t b; uint8_t g; uint8_t r; uint8_t a; } * src = (struct src_pixel_t *)ft_bitmap.buffer;
for( int row = 0; row < src_h; row++, dst_ptr += tgt_w * self->atlas->depth ) {
for( int col = 0; col < src_w; col++, src++ ) {
dst_ptr[col] = (0.3*src->r + 0.59*src->g + 0.11*src->b) * (src->a/255.0);
}
}
}
else if( ft_bitmap.pixel_mode == FT_PIXEL_MODE_GRAY && self->atlas->depth == 4 ) {
// Grey in, RGBA out: Use grey level for alpha channel, with white color
struct dst_pixel_t { uint8_t r; uint8_t g; uint8_t b; uint8_t a; } * dst = (struct dst_pixel_t *)dst_ptr;
for( int row = 0; row < src_h; row++, dst += tgt_w ) {
for( int col = 0; col < src_w; col++, src_ptr++ ) {
dst[col] = (struct dst_pixel_t){ 255, 255, 255, *src_ptr };
}
}
}
else
{
// Straight copy, per row
for( i = 0; i < src_h; i++ ) {
//difference between width and pitch: https://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html#FT_Bitmap
memcpy( dst_ptr, src_ptr, ft_bitmap.width);
dst_ptr += tgt_w * self->atlas->depth;
src_ptr += ft_bitmap.pitch;
}
}
if( self->rendermode == RENDER_SIGNED_DISTANCE_FIELD )
{
unsigned char *sdf = make_distance_mapb( buffer, tgt_w, tgt_h );
free( buffer );
buffer = sdf;
}
texture_atlas_set_region( self->atlas, x, y, tgt_w, tgt_h, buffer, tgt_w * self->atlas->depth);
free( buffer );
glyph = texture_glyph_new( );