-
Notifications
You must be signed in to change notification settings - Fork 0
/
textureFont.sj
1148 lines (979 loc) · 33 KB
/
textureFont.sj
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.
*/
--cstruct--
##undef __FTERRORS_H__
##define FT_ERRORDEF( e, v, s ) { e, s },
##define FT_ERROR_START_LIST {
##define FT_ERROR_END_LIST { 0, 0 } };
const struct {
int code;
const char* message;
} FT_Errors[] =
##include FT_ERRORS_H
/**
* A list of possible ways to render a glyph.
*/
typedef enum rendermode_t
{
RENDER_NORMAL,
RENDER_OUTLINE_EDGE,
RENDER_OUTLINE_POSITIVE,
RENDER_OUTLINE_NEGATIVE,
RENDER_SIGNED_DISTANCE_FIELD
} rendermode_t;
/**
* A structure that hold a kerning value relatively to a Unicode
* codepoint.
*
* This structure cannot be used alone since the (necessary) right
* Unicode codepoint is implicitely held by the owner of this structure.
*/
typedef struct kerning_t
{
/**
* Left Unicode codepoint in the kern pair in UTF-32 LE encoding.
*/
uint32_t codepoint;
/**
* Kerning value (in fractional pixels).
*/
float kerning;
} kerning_t;
/*
* Glyph metrics:
* --------------
*
* xmin xmax
* | |
* |<-------- width -------->|
* | |
* | +-------------------------+----------------- ymax
* | | ggggggggg ggggg | ^ ^
* | | g:::::::::ggg::::g | | |
* | | g:::::::::::::::::g | | |
* | | g::::::ggggg::::::gg | | |
* | | g:::::g g:::::g | | |
* offset_x -|-------->| g:::::g g:::::g | offset_y |
* | | g:::::g g:::::g | | |
* | | g::::::g g:::::g | | |
* | | g:::::::ggggg:::::g | | |
* | | g::::::::::::::::g | | height
* | | gg::::::::::::::g | | |
* baseline ---*---------|---- gggggggg::::::g-----*-------- |
* / | | g:::::g | |
* origin | | gggggg g:::::g | |
* | | g:::::gg gg:::::g | |
* | | g::::::ggg:::::::g | |
* | | gg:::::::::::::g | |
* | | ggg::::::ggg | |
* | | gggggg | v
* | +-------------------------+----------------- ymin
* | |
* |------------- advance_x ---------->|
*/
/**
* A structure that describe a glyph.
*/
typedef struct texture_glyph_t
{
/**
* Unicode codepoint this glyph represents in UTF-32 LE encoding.
*/
uint32_t codepoint;
/**
* Glyph's width in pixels.
*/
size_t width;
/**
* Glyph's height in pixels.
*/
size_t height;
/**
* Glyph's left bearing expressed in integer pixels.
*/
int offset_x;
/**
* Glyphs's top bearing expressed in integer pixels.
*
* Remember that this is the distance from the baseline to the top-most
* glyph scanline, upwards y coordinates being positive.
*/
int offset_y;
/**
* For horizontal text layouts, this is the horizontal distance (in
* fractional pixels) used to increment the pen position when the glyph is
* drawn as part of a string of text.
*/
float advance_x;
/**
* For vertical text layouts, this is the vertical distance (in fractional
* pixels) used to increment the pen position when the glyph is drawn as
* part of a string of text.
*/
float advance_y;
/**
* First normalized texture coordinate (x) of top-left corner
*/
float s0;
/**
* Second normalized texture coordinate (y) of top-left corner
*/
float t0;
/**
* First normalized texture coordinate (x) of bottom-right corner
*/
float s1;
/**
* Second normalized texture coordinate (y) of bottom-right corner
*/
float t1;
/**
* A vector of kerning pairs relative to this glyph.
*/
vector_t * kerning;
/**
* Mode this glyph was rendered
*/
rendermode_t rendermode;
/**
* Glyph outline thickness
*/
float outline_thickness;
} texture_glyph_t;
typedef struct texture_atlas_t texture_atlas_td;
/**
* Texture font structure.
*/
typedef struct texture_font_t
{
/**
* Vector of glyphs contained in this font.
*/
vector_t * glyphs;
/**
* Atlas structure to store glyphs data.
*/
texture_atlas_td * atlas;
/**
* font location
*/
enum {
TEXTURE_FONT_FILE = 0,
TEXTURE_FONT_MEMORY,
} location;
union {
/**
* Font filename, for when location == TEXTURE_FONT_FILE
*/
char *filename;
/**
* Font memory address, for when location == TEXTURE_FONT_MEMORY
*/
struct {
const void *base;
size_t size;
} memory;
};
/**
* Font size
*/
float size;
/**
* Whether to use autohint when rendering font
*/
int hinting;
/**
* Mode the font is rendering its next glyph
*/
rendermode_t rendermode;
/**
* Outline thickness
*/
float outline_thickness;
/**
* Whether to use our own lcd filter.
*/
int filtering;
/**
* LCD filter weights
*/
unsigned char lcd_weights[5];
/**
* Whether to use kerning if available
*/
int kerning;
/**
* This field is simply used to compute a default line spacing (i.e., the
* baseline-to-baseline distance) when writing text with this font. Note
* that it usually is larger than the sum of the ascender and descender
* taken as absolute values. There is also no guarantee that no glyphs
* extend above or below subsequent baselines when using this distance.
*/
float height;
/**
* This field is the distance that must be placed between two lines of
* text. The baseline-to-baseline distance should be computed as:
* ascender - descender + linegap
*/
float linegap;
/**
* The ascender is the vertical distance from the horizontal baseline to
* the highest 'character' coordinate in a font face. Unfortunately, font
* formats define the ascender differently. For some, it represents the
* ascent of all capital latin characters (without accents), for others it
* is the ascent of the highest accented character, and finally, other
* formats define it as being equal to bbox.yMax.
*/
float ascender;
/**
* The descender is the vertical distance from the horizontal baseline to
* the lowest 'character' coordinate in a font face. Unfortunately, font
* formats define the descender differently. For some, it represents the
* descent of all capital latin characters (without accents), for others it
* is the ascent of the lowest accented character, and finally, other
* formats define it as being equal to bbox.yMin. This field is negative
* for values below the baseline.
*/
float descender;
/**
* The position of the underline line for this face. It is the center of
* the underlining stem. Only relevant for scalable formats.
*/
float underline_position;
/**
* The thickness of the underline for this face. Only relevant for scalable
* formats.
*/
float underline_thickness;
} texture_font_t;
--cstruct--
--cdefine--
/**
* This function creates a new texture font from given filename and size. The
* texture atlas is used to store glyph on demand. Note the depth of the atlas
* will determine if the font is rendered as alpha channel only (depth = 1) or
* RGB (depth = 3) that correspond to subpixel rendering (if available on your
* freetype implementation).
*
* @param atlas A texture atlas
* @param pt_size Size of font to be created (in points)
* @param filename A font filename
*
* @return A new empty font (no glyph inside yet)
*
*/
texture_font_t *
texture_font_new_from_file( texture_atlas_t * atlas,
const float pt_size,
const char * filename );
/**
* This function creates a new texture font from a memory location and size.
* The texture atlas is used to store glyph on demand. Note the depth of the
* atlas will determine if the font is rendered as alpha channel only
* (depth = 1) or RGB (depth = 3) that correspond to subpixel rendering (if
* available on your freetype implementation).
*
* @param atlas A texture atlas
* @param pt_size Size of font to be created (in points)
* @param memory_base Start of the font file in memory
* @param memory_size Size of the font file memory region, in bytes
*
* @return A new empty font (no glyph inside yet)
*
*/
texture_font_t *
texture_font_new_from_memory( texture_atlas_t *atlas,
float pt_size,
const void *memory_base,
size_t memory_size );
/**
* Delete a texture font. Note that this does not delete the glyph from the
* texture atlas.
*
* @param self a valid texture font
*/
void
texture_font_delete( texture_font_t * self );
/**
* Request a new glyph from the font. If it has not been created yet, it will
* be.
*
* @param self A valid texture font
* @param codepoint Character codepoint to be loaded in UTF-8 encoding.
*
* @return A pointer on the new glyph or 0 if the texture atlas is not big
* enough
*
*/
texture_glyph_t *
texture_font_get_glyph( texture_font_t * self,
const char * codepoint );
/**
* Request an already loaded glyph from the font.
*
* @param self A valid texture font
* @param codepoint Character codepoint to be found in UTF-8 encoding.
*
* @return A pointer on the glyph or 0 if the glyph is not loaded
*/
texture_glyph_t *
texture_font_find_glyph( texture_font_t * self,
const char * codepoint );
/**
* Request the loading of a given glyph.
*
* @param self A valid texture font
* @param codepoints Character codepoint to be loaded in UTF-8 encoding.
*
* @return One if the glyph could be loaded, zero if not.
*/
int
texture_font_load_glyph( texture_font_t * self,
const char * codepoints );
/**
* Request the loading of several glyphs at once.
*
* @param self A valid texture font
* @param codepoints Character codepoints to be loaded in UTF-8 encoding. May
* contain duplicates.
*
* @return Number of missed glyph if the texture is not big enough to hold
* every glyphs.
*/
size_t
texture_font_load_glyphs( texture_font_t * self,
const char * codepoints );
/*
*Increases the size of a fonts texture atlas
*Invalidates all pointers to font->atlas->data
*Changes the UV Coordinates of existing glyphs in the font
*
*@param self A valid texture font
*@param width_new Width of the texture atlas after resizing (must be bigger or equal to current width)
*@param height_new Height of the texture atlas after resizing (must be bigger or equal to current height)
*/
void
texture_font_enlarge_atlas( texture_font_t * self, size_t width_new,
size_t height_new);
/**
* Get the kerning between two horizontal glyphs.
*
* @param self A valid texture glyph
* @param codepoint Character codepoint of the peceding character in UTF-8 encoding.
*
* @return x kerning value
*/
float
texture_glyph_get_kerning( const texture_glyph_t * self,
const char * codepoint );
/**
* Creates a new empty glyph
*
* @return a new empty glyph (not valid)
*/
texture_glyph_t *
texture_glyph_new( void );
--cdefine--
--cfunction--
#include(<ft2build.h>)
#include(FT_FREETYPE_H)
#include(FT_STROKER_H)
#include(FT_LCD_FILTER_H)
#include(<stdint.h>)
#include(<stdlib.h>)
#include(<stdio.h>)
#include(<assert.h>)
#include(<math.h>)
##define HRES 64
##define HRESf 64.f
##define DPI 72
// ------------------------------------------------- texture_font_load_face ---
static int
texture_font_load_face(texture_font_t *self, float size,
FT_Library *library, FT_Face *face)
{
FT_Error error;
FT_Matrix matrix = { //
(int)((1.0/HRES) * 0x10000L),
(int)((0.0) * 0x10000L),
(int)((0.0) * 0x10000L),
(int)((1.0) * 0x10000L)};
assert(library);
assert(size);
/* Initialize library */
error = FT_Init_FreeType(library);
if(error) {
fprintf(stderr, "FT_Error (0x%02x) : %s\n",
FT_Errors[error].code, FT_Errors[error].message);
goto cleanup;
}
/* Load face */
switch (self->location) {
case TEXTURE_FONT_FILE:
error = FT_New_Face(*library, self->filename, 0, face);
break;
case TEXTURE_FONT_MEMORY:
error = FT_New_Memory_Face(*library,
self->memory.base, self->memory.size, 0, face);
break;
}
if(error) {
fprintf(stderr, "FT_Error (line %d, code 0x%02x) : %s\n",
__LINE__, FT_Errors[error].code, FT_Errors[error].message);
goto cleanup_library;
}
/* Select charmap */
error = FT_Select_Charmap(*face, FT_ENCODING_UNICODE);
if(error) {
fprintf(stderr, "FT_Error (line %d, code 0x%02x) : %s\n",
__LINE__, FT_Errors[error].code, FT_Errors[error].message);
goto cleanup_face;
}
/* Set char size */
error = FT_Set_Char_Size(*face, (int)(size * HRES), 0, DPI * HRES, DPI);
if(error) {
fprintf(stderr, "FT_Error (line %d, code 0x%02x) : %s\n",
__LINE__, FT_Errors[error].code, FT_Errors[error].message);
goto cleanup_face;
}
/* Set transform matrix */
FT_Set_Transform(*face, &matrix, NULL);
return 1;
cleanup_face:
FT_Done_Face( *face );
cleanup_library:
FT_Done_FreeType( *library );
cleanup:
return 0;
}
// ------------------------------------------------------ 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) {
fprintf( stderr,
"line %d: No more memory for allocating data\n", __LINE__);
return NULL;
}
self->codepoint = -1;
self->width = 0;
self->height = 0;
self->rendermode = RENDER_NORMAL;
self->outline_thickness = 0.0;
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(kerning_t) );
return self;
}
// --------------------------------------------------- texture_glyph_delete ---
void
texture_glyph_delete( texture_glyph_t *self )
{
assert( self );
vector_delete( self->kerning );
free( self );
}
// ---------------------------------------------- texture_glyph_get_kerning ---
float
texture_glyph_get_kerning( const texture_glyph_t * self,
const char * codepoint )
{
size_t i;
uint32_t ucodepoint = utf8_to_utf32( codepoint );
assert( self );
for( i=0; i<vector_size(self->kerning); ++i )
{
kerning_t * kerning = (kerning_t *) vector_get( self->kerning, i );
if( kerning->codepoint == ucodepoint )
{
return kerning->kerning;
}
}
return 0;
}
// ------------------------------------------ texture_font_generate_kerning ---
void
texture_font_generate_kerning( texture_font_t *self,
FT_Library *library, FT_Face *face )
{
size_t i, j;
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 backgroudn glyph */
for( i=1; i<self->glyphs->size; ++i )
{
glyph = *(texture_glyph_t **) vector_get( self->glyphs, i );
glyph_index = FT_Get_Char_Index( *face, glyph->codepoint );
vector_clear( glyph->kerning );
for( j=1; j<self->glyphs->size; ++j )
{
prev_glyph = *(texture_glyph_t **) vector_get( self->glyphs, j );
prev_index = FT_Get_Char_Index( *face, prev_glyph->codepoint );
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 )
{
kerning_t k = {prev_glyph->codepoint, kerning.x / (float)(HRESf*HRESf)};
vector_push_back( glyph->kerning, &k );
}
}
}
}
// ------------------------------------------------------ texture_font_init ---
static int
texture_font_init(texture_font_t *self)
{
FT_Library library;
FT_Face face;
FT_Size_Metrics metrics;
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->rendermode = RENDER_NORMAL;
self->outline_thickness = 0.0;
self->hinting = 1;
self->kerning = 1;
self->filtering = 1;
// 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 * 100.f, &library, &face))
return -1;
self->underline_position = 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 = 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 = face->size->metrics;
self->ascender = (metrics.ascender >> 6) / 100.0f;
self->descender = (metrics.descender >> 6) / 100.0f;
self->height = (metrics.height >> 6) / 100.0f;
self->linegap = self->height - self->ascender + self->descender;
FT_Done_Face( face );
FT_Done_FreeType( library );
/* NULL is a special glyph */
texture_font_get_glyph( self, NULL );
return 0;
}
// --------------------------------------------- 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) {
fprintf(stderr,
"line %d: No more memory for allocating data\n", __LINE__);
return NULL;
}
self->atlas = atlas;
self->size = pt_size;
self->location = TEXTURE_FONT_FILE;
self->filename = strdup(filename);
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) {
fprintf(stderr,
"line %d: No more memory for allocating data\n", __LINE__);
return NULL;
}
self->atlas = atlas;
self->size = pt_size;
self->location = TEXTURE_FONT_MEMORY;
self->memory.base = memory_base;
self->memory.size = memory_size;
if (texture_font_init(self)) {
texture_font_delete(self);
return NULL;
}
return self;
}
// ---------------------------------------------------- texture_font_delete ---
void
texture_font_delete( texture_font_t *self )
{
size_t i;
texture_glyph_t *glyph;
assert( self );
if(self->location == TEXTURE_FONT_FILE && self->filename)
free( self->filename );
for( i=0; i<vector_size( self->glyphs ); ++i)
{
glyph = *(texture_glyph_t **) vector_get( self->glyphs, i );
texture_glyph_delete( glyph);
}
vector_delete( self->glyphs );
free( self );
}
texture_glyph_t *
texture_font_find_glyph( texture_font_t * self,
const char * codepoint )
{
size_t i;
texture_glyph_t *glyph;
uint32_t ucodepoint = utf8_to_utf32( codepoint );
for( i = 0; i < self->glyphs->size; ++i )
{
glyph = *(texture_glyph_t **) vector_get( self->glyphs, i );
// If codepoint is -1, we don't care about outline type or thickness
if( (glyph->codepoint == ucodepoint) &&
((ucodepoint == -1) ||
((glyph->rendermode == self->rendermode) &&
(glyph->outline_thickness == self->outline_thickness)) ))
{
return glyph;
}
}
return NULL;
}
// ------------------------------------------------ texture_font_load_glyph ---
int
texture_font_load_glyph( texture_font_t * self,
const char * codepoint )
{
size_t i, x, y;
FT_Library library;
FT_Error error;
FT_Face face;
FT_Glyph ft_glyph = { 0 };
FT_GlyphSlot slot;
FT_Bitmap ft_bitmap;
FT_UInt glyph_index;
texture_glyph_t *glyph;
FT_Int32 flags = 0;
int ft_glyph_top = 0;
int ft_glyph_left = 0;
ivec4 region;
if (!texture_font_load_face(self, self->size, &library, &face))
return 0;
/* Check if codepoint has been already loaded */
if (texture_font_find_glyph(self, codepoint)) {
FT_Done_Face(face);
FT_Done_FreeType(library);
return 1;
}
/* codepoint NULL is special : it is used for line drawing (overline,
* underline, strikethrough) and background.
*/
if( !codepoint )
{
ivec4 region = texture_atlas_get_region( self->atlas, 5, 5 );
texture_glyph_t * glyph = texture_glyph_new( );
static unsigned char data[4*4*3] = {-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};
if ( region.x < 0 )
{
fprintf( stderr, "Texture atlas is full (line %d)\n", __LINE__ );
FT_Done_Face( face );
FT_Done_FreeType( library );
return 0;
}
texture_atlas_set_region( self->atlas, region.x, region.y, 4, 4, data, 0 );
glyph->codepoint = -1;
glyph->s0 = (region.x+2)/(float)self->atlas->width;
glyph->t0 = (region.y+2)/(float)self->atlas->height;
glyph->s1 = (region.x+3)/(float)self->atlas->width;
glyph->t1 = (region.y+3)/(float)self->atlas->height;
vector_push_back( self->glyphs, &glyph );
FT_Done_Face(face);
FT_Done_FreeType(library);
return 1;
}
flags = 0;
ft_glyph_top = 0;
ft_glyph_left = 0;
glyph_index = FT_Get_Char_Index( face, (FT_ULong)utf8_to_utf32( codepoint ) );
// 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( library, FT_LCD_FILTER_LIGHT );
flags |= FT_LOAD_TARGET_LCD;
if( self->filtering )
{
FT_Library_SetLcdFilterWeights( library, self->lcd_weights );
}
}
error = FT_Load_Glyph( face, glyph_index, flags );
if( error )
{
fprintf( stderr, "FT_Error (line %d, code 0x%02x) : %s\n",
__LINE__, FT_Errors[error].code, FT_Errors[error].message );
FT_Done_Face( face );
FT_Done_FreeType( library );
return 0;
}
if( self->rendermode == RENDER_NORMAL || self->rendermode == RENDER_SIGNED_DISTANCE_FIELD )
{
slot = 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( library, &stroker );
if( error )
{
fprintf(stderr, "FT_Error (0x%02x) : %s\n",
FT_Errors[error].code, FT_Errors[error].message);
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( face->glyph, &ft_glyph);
if( error )
{
fprintf(stderr, "FT_Error (0x%02x) : %s\n",
FT_Errors[error].code, FT_Errors[error].message);
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 )
{
fprintf(stderr, "FT_Error (0x%02x) : %s\n",
FT_Errors[error].code, FT_Errors[error].message);
goto cleanup_stroker;
}
if( self->atlas->depth == 1 )
error = FT_Glyph_To_Bitmap( &ft_glyph, FT_RENDER_MODE_NORMAL, 0, 1);
else
error = FT_Glyph_To_Bitmap( &ft_glyph, FT_RENDER_MODE_LCD, 0, 1);
if( error )
{
fprintf(stderr, "FT_Error (0x%02x) : %s\n",
FT_Errors[error].code, FT_Errors[error].message);
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 )
{
FT_Done_Face( face );
FT_Done_FreeType( library );
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;
}
size_t src_w = ft_bitmap.width/self->atlas->depth;
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 )
{
fprintf( stderr, "Texture atlas is full (line %d)\n", __LINE__ );