-
Notifications
You must be signed in to change notification settings - Fork 266
/
texture-font.h
760 lines (664 loc) · 19.3 KB
/
texture-font.h
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
/* Freetype GL - A C OpenGL Freetype engine
*
* Distributed under the OSI-approved BSD 2-Clause License. See accompanying
* file `LICENSE` for more details.
*/
#ifndef __TEXTURE_FONT_H__
#define __TEXTURE_FONT_H__
#include <stdlib.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "vector.h"
#include "texture-atlas.h"
#ifndef __THREAD
#if defined(__GNUC__) || defined(__clang__)
#define __THREAD __thread
#elif defined(_MSC_VER)
#define __THREAD __declspec( thread )
#else
#define __THREAD
#endif
#endif
#ifdef __cplusplus
namespace ftgl {
#endif
/**
* @file texture-font.h
* @author Nicolas Rougier ([email protected])
*
* @defgroup texture-font Texture font
*
* Texture font.
*
* Example Usage:
* @code
* #include "texture-font.h"
*
* int main( int arrgc, char *argv[] )
* {
* return 0;
* }
* @endcode
*
* @{
*/
/**
* 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;
/**
* Glyph array end mark type
*/
typedef enum glyphmode_t
{
GLYPH_END=0,
GLYPH_CONT=1
} glyphmode_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;
/**
* Glyph scan end mark
*/
glyphmode_t glyphmode;
} texture_glyph_t;
/**
* Enum type for texture location
*/
typedef enum font_location_t {
TEXTURE_FONT_FILE = 0,
TEXTURE_FONT_MEMORY
} font_location_t;
/**
* Enum type for automatic open/close
*/
typedef enum font_mode_t {
MODE_AUTO_CLOSE = 0,
MODE_GLYPHS_CLOSE,
MODE_FREE_CLOSE,
MODE_MANUAL_CLOSE,
MODE_ALWAYS_OPEN
} font_mode_t;
/**
* default mode for fonts
*/
extern __THREAD font_mode_t mode_default;
/** set defualt mode for fonts
*
* @param mode The mode for automatic open/close new fonts get
*/
void
texture_font_default_mode(font_mode_t mode);
/* If there is no Freetype included, just define that as incomplete pointer */
#if !defined(FT2BUILD_H_) && !defined(__FT2BUILD_H__) && !defined(FREETYPE_H_)
typedef struct FT_FaceRec_* FT_Face;
typedef struct FT_LibraryRec_* FT_Library;
typedef struct FT_SizeRec_* FT_Size;
typedef signed long FT_Fixed;
#endif
/* same for harfbuzz */
#ifndef HB_BUFFER_H
typedef struct hb_font_t hb_font_t;
#endif
/**
* Texture font library structure.
*/
typedef struct texture_font_library_t
{
/**
* Flag for mode
*/
font_mode_t mode;
/**
* Freetype library pointer
*/
FT_Library library;
} texture_font_library_t;
/**
* Texture font structure.
*/
typedef struct texture_font_t
{
/**
* Vector of glyphs contained in this font.
* This is actually a two-stage table, indexing into 256 glyphs each
*/
vector_t * glyphs;
/**
* Atlas structure to store glyphs data.
*/
texture_atlas_t * atlas;
/**
* font location
*/
font_location_t 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;
};
/**
* Texture font library
*/
texture_font_library_t * library;
/**
* Font size
*/
float size;
/**
* Mode the font is rendering its next glyph
*/
rendermode_t rendermode;
/**
* Outline thickness
*/
float outline_thickness;
/**
* Whether to use our own lcd filter.
*/
unsigned char filtering;
/**
* Whether to use kerning if available
*/
unsigned char kerning;
/**
* Whether to use autohint when rendering font
*/
unsigned char hinting;
/**
* Whether to scale texture coordinates
*/
unsigned char scaletex;
/**
* LCD filter weights
*/
unsigned char lcd_weights[5];
/**
* 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;
/**
* The padding to be add to the glyph's texture that are loaded by this font.
* Usefull when adding effects with shaders.
*/
int padding;
/**
* Flag for mode
*/
font_mode_t mode;
/**
* Freetype face pointer
*/
FT_Face face;
/**
* Freetype size pointer
*/
FT_Size ft_size;
/**
* Harfbuzz font pointer
*/
hb_font_t* hb_font;
/**
* factor to scale font coordinates
*/
float scale;
} texture_font_t;
/**
* This function creates a new font library
*
* @return a new library (no font loaded yet)
*/
texture_font_library_t *
texture_library_new(void);
/**
* This variable holds the per-thread library
*/
extern __THREAD texture_font_library_t * freetype_gl_library;
/**
* 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), or RGBA (depth = 4) for color fonts.
*
* @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 );
/**
* Clone the freetype-gl font and set a different size
*
* @param self a valid texture font
* @param size the new size of the font
*/
texture_font_t *
texture_font_clone( texture_font_t *self,
float size);
/**
* Close the freetype structures from a font and the associated library
*
* @param self a valid texture font
* @param face_mode if the mode of the face is less or equal, be done with it
* @param library_mode if the mode of the library is less or equal, be done with it
*/
void
texture_font_close( texture_font_t *self, font_mode_t face_mode, font_mode_t library_mode );
/**
* 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 );
/**
* Load a texture font.
*
* @param self a valid texture font
* @param size the size of the font
*
* @return 1 on success, 0 on error
*/
int
texture_font_load_face( texture_font_t * self, float size );
/**
* Texture font contains multiple masters and is capable of interpolating between them.
*
* @param self a valid texture font
*
* @return 1 if true, 0 if false
*
* @see texture_font_get_weight, texture_font_set_weight
*/
int
texture_font_is_variable( texture_font_t * self );
/**
* Get font weights (variable fonts only)
*
* @param self a valid texture font
* @param def default weight
* @param min minimum weight
* @param max maximum weight
*
* @return 1 on success, 0 on error
*/
int
texture_font_get_weight( texture_font_t * self, FT_Fixed * def,
FT_Fixed * min, FT_Fixed * max );
/**
* Set font weight (variable fonts only)
*
* @param self a valid texture font
* @param wght new font weight
*
* @return 1 on success, 0 on error
*/
int
texture_font_set_weight( texture_font_t * self, FT_Fixed wght );
/**
* 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 );
/**
* Index a glyph in a font
*
* @param self A valid texture font
* @param glyph The glyph to index in the font
* @param codepoint The codepoint to insert into
*
* @return 1 if glyph was copied, 0 if it was inserted
*/
int
texture_font_index_glyph( texture_font_t * self,
texture_glyph_t * glyph,
uint32_t codepoint );
/**
* Request the loading of a given glyph.
*
* @param self A valid texture font
* @param codepoint 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 * codepoint );
/**
* Request a new glyph from the font. If it has not been created yet, it will
* be.
*
* @param self A valid texture font
* @param glyph_index Font's character glyph index to be obtained
*
* @return A pointer on the new glyph or 0 if the texture atlas is not big
* enough
*
*/
texture_glyph_t *
texture_font_get_glyph_gi( texture_font_t * self,
uint32_t glyph_index );
/**
* Request an already loaded glyph from the font.
*
* @param self A valid texture font
* @param glyph_index Font's character codepoint to be found
*
* @return A pointer on the glyph or 0 if the glyph is not loaded
*/
texture_glyph_t *
texture_font_find_glyph_gi( texture_font_t * self,
uint32_t glyph_index );
/**
* Request the loading of a given glyph.
*
* @param self A valid texture font
* @param glyph_index Character codepoint to be loaded in font's codepoint
* @param ucodepoint Character codepoint for inserting into lookup table
*
* @return One if the glyph could be loaded, zero if not.
*/
int
texture_font_load_glyph_gi( texture_font_t * self,
uint32_t glyph_index,
uint32_t ucodepoint);
/**
* 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 );
/**
* Changes the UV Coordinates of existing glyphs in the font
*
* @param self A valid texture font
* @param mulw scale factor for width
* @param mulh scale factor for height
*/
void
texture_font_enlarge_glyphs( texture_font_t * self, float mulw, float mulh );
/**
* Increases the size of a fonts texture atlas
*
* @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_texture( 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 );
/**
* Delete a glyph
*
* @param self A valid texture glyph
*/
void
texture_glyph_delete( texture_glyph_t * self );
/**
* Clone a glyph
*
* @param self A valid texture glyph
*/
texture_glyph_t*
texture_glyph_clone( texture_glyph_t* self );
/** @} */
#define GLYPHS_ITERATOR1(index, name, glyph) \
for( index = 0; index < vector_size ( glyph ); index++ ) { \
texture_glyph_t ** __glyphs;
#define GLYPHS_ITERATOR2(index, name, glyph) \
if(( __glyphs = *(texture_glyph_t *** ) vector_get ( glyph, index ) )) { \
int __i; \
for( __i = 0; __i < 0x100; __i++ ) { \
if(( name = __glyphs[__i] ))
#define GLYPHS_ITERATOR(index, name, glyph) \
GLYPHS_ITERATOR1(index, name, glyph) \
GLYPHS_ITERATOR2(index, name, glyph)
#define GLYPHS_ITERATOR_END1 }
#define GLYPHS_ITERATOR_END2 } }
#define GLYPHS_ITERATOR_END } } }
#ifdef __cplusplus
}
}
#endif
#endif /* __TEXTURE_FONT_H__ */