-
Notifications
You must be signed in to change notification settings - Fork 2
/
RFont.h
3719 lines (3125 loc) · 118 KB
/
RFont.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
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
/*
* Copyright (c) 2021-24 ColleagueRiley ColleagueRiley@gmail.com
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following r estrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
*
*/
/*
preprocessor args
make sure
** #define RFONT_IMPLEMENTATION ** - include function defines
is in exactly one of your files or arguments
#define RFONT_NO_OPENGL - do not define graphics functions (that use opengl)
#define RFONT_NO_STDIO - do not include stdio.h
#define RFONT_EXTERNAL_STB - load stb_truetype from stb_truetype.h instead of using the internal version
#define RFONT_NO_GRAPHICS - do not include any graphics functions at all
#define RFONT_RENDER_RGL - use RGL functions for rendering
#define RFONT_RENDER_LEGACY - use opengl legacy functions for rendering (if RGL is not chosen)
-- NOTE: By default, opengl 3.3 vbos are used for rendering --
*/
/*
credits :
stb_truetype.h - a dependency for RFont, most of (a slightly motified version of) stb_truetype.h is included directly into RFont.h
http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ - UTF-8 decoding function
fontstash - fontstash was used as a refference for some parts
*/
/*
... = [add code here]
BASIC TEMPLATE :
#define RFONT_IMPLEMENTATION
#include "RFont.h"
...
int main () {
...
RFont_init(window_width, window_height);
RFont_font* font = RFont_font_init("font.ttf");
while (1) {
...
RFont_draw_text(font, "text", 100, 100, 20);
...
}
RFont_font_free(font);
RFont_close();
...
}
*/
#ifndef RFONT_NO_STDIO
#include <stdio.h>
#endif
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#if !defined(u8)
#if defined(_MSC_VER) || defined(__SYMBIAN32__)
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef signed short i16;
typedef unsigned int u32;
typedef signed int i32;
typedef unsigned long u64;
typedef signed long i64;
#else
#include <stdint.h>
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
#endif
#endif
#if !defined(b8)
typedef u8 b8;
#endif
/*
You can define these yourself if
you want to change anything
*/
#ifndef RFONT_MAX_GLYPHS
#define RFONT_MAX_GLYPHS 652
#endif
#ifndef RFONT_ATLAS_WIDTH
#define RFONT_ATLAS_WIDTH 6000
#endif
#ifndef RFONT_ATLAS_HEIGHT
#define RFONT_ATLAS_HEIGHT 400
#endif
#ifndef RFONT_INIT_TEXT_SIZE
#define RFONT_INIT_TEXT_SIZE 500
#endif
#ifndef RFONT_INIT_VERTS
#define RFONT_INIT_VERTS 1024
#endif
#ifndef RFONT_TEXTFORMAT_MAX_SIZE
#define RFONT_TEXTFORMAT_MAX_SIZE 923
#endif
#ifndef RFONT_VSNPRINTF
#define RFONT_VSNPRINTF vsnprintf
#endif
#ifndef RFONT_UNUSED
#define RFONT_UNUSED(x) (void) (x);
#endif
/* make sure RFont declares aren't declared twice */
#ifndef RFONT_H
#define RFONT_H
#ifndef RFont_area
typedef struct { u32 w, h; } RFont_area;
#endif
typedef struct RFont_font RFont_font;
typedef struct {
u32 codepoint; /* the character (for checking) */
size_t size; /* the size of the glyph */
i32 x, x2; /* coords of the character on the texture */
/* source glyph data */
i32 src;
float w, h, x1, y1, advance;
} RFont_glyph;
/**
* @brief Sets the framebuffer size AND runs the graphics init function.
* @param width The framebuffer width.
* @param height The framebuffer height.
*/
inline void RFont_init(size_t width, size_t height);
/**
* @brief Frees data allocated by the RFont for the RFont
*/
inline void RFont_close(void);
/**
* @brief Just updates the framebuffer size.
* @param width The framebuffer width.
* @param height The framebuffer height.
*/
inline void RFont_update_framebuffer(size_t width, size_t height);
#ifndef RFONT_NO_STDIO
/**
* @brief Init font stucture with a TTF file path.
* @param font_name The TTF file path.
* @return The `RFont_font` created using the TTF file data.
*/
inline RFont_font* RFont_font_init(const char* font_name);
#endif
/**
* @brief Init font stucture with raw TTF data.
* @param font_data The raw TTF data.
* @param auto_free If the memory should be automatically freed by `RFont_font_free`.
* @return The `RFont_font` created from the data.
*/
inline RFont_font* RFont_font_init_data(u8* font_data, b8 auto_free);
/**
* @brief Free data from the font stucture, including the stucture itself
* @param font The font stucture to free
*/
inline void RFont_font_free(RFont_font* font);
/**
* @brief Add a character to the font's atlas.
* @param font The font to use.
* @param ch The character to add to the atlas.
* @param size The size of the character.
* @return The `RFont_glyph` created from the data and added to the atlas.
*/
inline RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size);
#ifndef RFONT_NO_FMT
/**
* @brief Formats a string.
* @param string The source string
* @param ... format data
* @return The formatted string
*/
inline const char* RFont_fmt(const char* string, ...);
#endif
/**
* @brief Add a string to the font's atlas.
* @param font The font to use.
* @param ch The character to add to the atlas.
* @param sizes The supported sizes of the character.
* @param sizeLen length of the size array
*/
inline void RFont_font_add_string(RFont_font* font, const char* string, size_t* sizes, size_t sizeLen);
/**
* @brief Add a string to the font's atlas based on a given string length.
* @param font The font to use.
* @param ch The character to add to the atlas.
* @param strLen length of the string
* @param sizes The supported sizes of the character.
* @param sizeLen length of the size array
*/
inline void RFont_font_add_string_len(RFont_font* font, const char* string, size_t strLen, size_t* sizes, size_t sizeLen);
/**
* @brief Get the area of the text based on the size using the font.
* @param font The font stucture to use for drawing
* @param text The string to draw
* @param size The size of the text
* @return The area of the text based on the size
*/
inline RFont_area RFont_text_area(RFont_font* font, const char* text, u32 size);
/**
* @brief Get the area of the text based on the size using the font, using a given length.
* @param font The font stucture to use for drawing
* @param text The string to draw
* @param size The size of the text
* @param spacing The spacing of the text
* @return The area of the text based on the size
*/
inline RFont_area RFont_text_area_spacing(RFont_font* font, const char* text, float spacing, u32 size);
/**
* @brief Get the area of the text based on the size using the font, using a given length.
* @param font The font stucture to use for drawing
* @param text The string to draw
* @param len The length of the string
* @param size The size of the text
* @param stopNL the number of \n s until it stops (0 = don't stop until the end)
* @param spacing The spacing of the text
* @return The area of the text based on the size
*/
inline RFont_area RFont_text_area_len(RFont_font* font, const char* text, size_t len, u32 size, size_t stopNL, float spacing);
/**
* @brief Draw a text string using the font.
* @param font The font stucture to use for drawing
* @param text The string to draw
* @param x The x position of the text
* @param y The y position of the text
* @param size The size of the text
* @return The area of the text based on the size
*/
inline RFont_area RFont_draw_text(RFont_font* font, const char* text, float x, float y, u32 size);
/**
* @brief Draw a text string using the font and a given spacing.
* @param font The font stucture to use for drawing
* @param text The string to draw
* @param x The x position of the text
* @param y The y position of the text
* @param size The size of the text
* @param spacing The spacing of the text
* @return The area of the text based on the size
*/
inline RFont_area RFont_draw_text_spacing(RFont_font* font, const char* text, float x, float y, u32 size, float spacing);
/**
* @brief Draw a text string using the font using a given length and a given spacing.
* @param font The font stucture to use for drawing
* @param text The string to draw
* @param len The length of the string
* @param x The x position of the text
* @param y The y position of the text
* @param size The size of the text
* @param spacing The spacing of the text
* @return The area of the text based on the size
*/
inline RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, float x, float y, u32 size, float spacing);
#define RFont_set_color RFont_render_set_color
#ifndef RFONT_NO_GRAPHICS
/*
if you do not want to use opengl (or want to create your own implemntation of these functions),
you'll have to define these yourself
and add `#define RFONT_NO_OPENGL`
*/
inline void RFont_render_set_color(float r, float g, float b, float a); /* set the current rendering color */
inline void RFont_render_init(void); /* any initalizations the renderer needs to do */
inline u32 RFont_create_atlas(u32 atlasWidth, u32 atlasHeight); /* create a bitmap texture based on the given size */
inline void RFont_bitmap_to_atlas(u32 atlas, u8* bitmap, float x, float y, float w, float h); /* add the given bitmap to the texture based on the given coords and size data */
inline void RFont_render_text(u32 atlas, float* verts, float* tcoords, size_t nverts); /* render the text, using the vertices, atlas texture, and texture coords given. */
inline void RFont_render_free(u32 atlas); /* free any memory the renderer might need to free */
/* (if modern opengl is being used) switch to rendering using opengl legacy or not */
inline void RFont_render_legacy(u8 legacy);
#endif
#endif /* RFONT_H */
#ifdef RFONT_IMPLEMENTATION
#ifdef RFONT_EXTERNAL_STB
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#endif
#define RFONT_GET_TEXPOSX(x) (float)((float)(x) / (float)(RFONT_ATLAS_WIDTH))
#define RFONT_GET_TEXPOSY(y) (float)((float)(y) / (float)(RFONT_ATLAS_HEIGHT))
#define RFONT_GET_WORLD_X(x, w) (float)((x) / (((w) / 2.0f)) - 1.0f)
#define RFONT_GET_WORLD_Y(y, h) (float)(1.0f - ((y) / ((h) / 2.0f)))
/*
stb defines required by RFont
you probably don't care about this part if you're reading just the RFont code
*/
#ifndef RFONT_EXTERNAL_STB
// private structure
typedef struct
{
unsigned char *data;
int cursor;
int size;
} stbtt__buf;
typedef struct stbtt_fontinfo stbtt_fontinfo;
struct stbtt_fontinfo
{
void * userdata;
unsigned char * data; // pointer to .ttf file
int fontstart; // offset of start of font
int numGlyphs; // number of glyphs, needed for range checking
int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf
int index_map; // a cmap mapping for our chosen character encoding
int indexToLocFormat; // format needed to map from glyph index to glyph
stbtt__buf cff; // cff font data
stbtt__buf charstrings; // the charstring index
stbtt__buf gsubrs; // global charstring subroutines index
stbtt__buf subrs; // private charstring subroutines index
stbtt__buf fontdicts; // array of font dicts
stbtt__buf fdselect; // map from glyph to fontdict
};
#ifdef STBTT_STATIC
#define STBTT_DEF static
#else
#define STBTT_DEF extern inline
#endif
STBTT_DEF i16 ttSHORT(u8 *p);
STBTT_DEF u16 ttUSHORT(u8 *p);
STBTT_DEF u32 ttULONG(u8 *p);
STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);
STBTT_DEF unsigned char* stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff);
STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);
STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);
STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
#endif /* RFONT_EXTERNAL_STB */
/*
END of stb defines required by RFont
you probably care about this part
*/
#ifndef RFONT_NO_FMT
#include <stdarg.h>
const char* RFont_fmt(const char* string, ...) {
static char output[RFONT_TEXTFORMAT_MAX_SIZE];
va_list args;
va_start(args, string);
RFONT_VSNPRINTF(output, RFONT_TEXTFORMAT_MAX_SIZE, string, args);
va_end(args);
return output;
}
#endif
struct RFont_font {
stbtt_fontinfo info; /* source stb font */
b8 free_font_memory;
float fheight; /* font height from stb */
float descent; /* font descent */
float numOfLongHorMetrics;
float space_adv;
RFont_glyph glyphs[RFONT_MAX_GLYPHS]; /* glyphs */
size_t glyph_len;
u32 atlas; /* atlas texture */
float atlasX; /* the current x position inside the atlas */
};
size_t RFont_width = 0, RFont_height = 0;
float* RFont_verts;
float* RFont_tcoords;
RFont_font* font2;
void RFont_update_framebuffer(size_t width, size_t height) {
/* set size of the framebuffer (for rendering later on) */
RFont_width = width;
RFont_height = height;
}
void RFont_init(size_t width, size_t height) {
RFont_update_framebuffer(width, height);
#ifndef RFONT_NO_GRAPHICS
/* init any rendering stuff that needs to be initalized (eg. vbo objects) */
RFont_render_init();
#endif
RFont_verts = malloc(sizeof(float) * RFONT_INIT_VERTS * 600);
RFont_tcoords = malloc(sizeof(float) * RFONT_INIT_VERTS * 600);
}
#ifndef RFONT_NO_STDIO
RFont_font* RFont_font_init(const char* font_name) {
FILE* ttf_file = fopen(font_name, "rb");
fseek(ttf_file, 0U, SEEK_END);
size_t size = ftell(ttf_file);
char* ttf_buffer = (char*)malloc(sizeof(char) * size);
fseek(ttf_file, 0U, SEEK_SET);
size_t out = fread(ttf_buffer, 1, size, ttf_file);
RFONT_UNUSED(out)
return RFont_font_init_data((u8*)ttf_buffer, 1);
}
#endif
RFont_font* RFont_font_init_data(u8* font_data, b8 auto_free) {
RFont_font* font = (RFont_font*)malloc(sizeof(RFont_font));
stbtt_InitFont(&font->info, font_data, 0);
font->fheight = ttSHORT(font->info.data + font->info.hhea + 4) - ttSHORT(font->info.data + font->info.hhea + 6);
font->descent = ttSHORT(font->info.data + font->info.hhea + 6);
font->numOfLongHorMetrics = ttUSHORT(font->info.data + font->info.hhea + 34);
font->space_adv = ttSHORT(font->info.data + font->info.hmtx + 4 * (u32)(font->numOfLongHorMetrics - 1));
#ifndef RFONT_NO_GRAPHICS
font->atlas = RFont_create_atlas(RFONT_ATLAS_WIDTH, RFONT_ATLAS_HEIGHT);
#endif
font->atlasX = 0;
font->glyph_len = 0;
font->free_font_memory = auto_free;
return font;
}
void RFont_font_free(RFont_font* font) {
#ifndef RFONT_NO_GRAPHICS
RFont_render_free(font->atlas);
#endif
if (font->free_font_memory)
free(font->info.data);
free(font);
}
void RFont_close(void) {
free(RFont_verts);
free(RFont_tcoords);
}
/*
decode utf8 character to codepoint
*/
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
#define RFONT_UTF8_ACCEPT 0
#define RFont_UTF8_REJECT 12
inline static u32 RFont_decode_utf8(u32* state, u32* codep, u32 byte);
static u32 RFont_decode_utf8(u32* state, u32* codep, u32 byte) {
static const uint8_t utf8d[] = {
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, // 00..1f
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, // 20..3f
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, // 40..5f
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, // 60..7f
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
};
uint32_t type = utf8d[byte];
*codep = (*state != RFONT_UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
*state = utf8d[256 + *state * 16 + type];
return *state;
}
void RFont_font_add_string(RFont_font* font, const char* string, size_t* sizes, size_t sizeLen) {
RFont_font_add_string_len(font, string, 0, sizes, sizeLen);
}
void RFont_font_add_string_len(RFont_font* font, const char* string, size_t strLen, size_t* sizes, size_t sizeLen) {
u32 i;
char* str;
for (str = (char*)string; (!strLen || (size_t)(str - string) < strLen) && *str; str++)
for (i = 0; i < sizeLen; i++)
RFont_font_add_char(font, *str, sizes[i]);
}
RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size) {
static u32 utf8state = 0, codepoint = 0;
if (RFont_decode_utf8(&utf8state, &codepoint, (u8)ch) != RFONT_UTF8_ACCEPT)
return (RFont_glyph){0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
u32 i;
for (i = 0; i < font->glyph_len; i++)
if (font->glyphs[i].codepoint == codepoint && font->glyphs[i].size == size)
return font->glyphs[i];
RFont_glyph* glyph = &font->glyphs[i];
glyph->src = stbtt_FindGlyphIndex(&font->info, codepoint);
if (glyph->src == 0 && font2 != NULL && font2->info.data != font->info.data) {
stbtt_fontinfo saveInfo = font->info;
RFont_font* fakeFont = font;
fakeFont->info = font2->info;
RFont_glyph g = RFont_font_add_char(fakeFont, 't', size);
fakeFont->info = saveInfo;
return g;
}
font->glyph_len++;
i32 x0, y0, x1, y1, w = 0, h = 0;
if (stbtt_GetGlyphBox(&font->info, glyph->src, &x0, &y0, &x1, &y1) == 0)
return (RFont_glyph){0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
float scale = ((float)size) / font->fheight;
u8* bitmap = stbtt_GetGlyphBitmapSubpixel(&font->info, 0, scale, 0.0f, 0.0f, glyph->src, &w, &h, 0, 0);
glyph->w = (float)w;
glyph->h = (float)h;
glyph->codepoint = codepoint;
glyph->size = size;
glyph->x = font->atlasX;
glyph->x2 = font->atlasX + glyph->w;
glyph->x1 = floorf(x0 * scale);
glyph->y1 = floor(-y1 * scale);
#ifndef RFONT_NO_GRAPHICS
RFont_bitmap_to_atlas(font->atlas, bitmap, font->atlasX, 0, glyph->w, glyph->h);
#endif
font->atlasX += glyph->w;
free(bitmap);
i32 advanceX;
if (glyph->src < font->numOfLongHorMetrics)
advanceX = ttSHORT(font->info.data + font->info.hmtx + 4 * glyph->src);
else
advanceX = ttSHORT(font->info.data + font->info.hmtx + 4 * (u32)(font->numOfLongHorMetrics - 1));
glyph->advance = advanceX * scale;
return *glyph;
}
RFont_area RFont_text_area(RFont_font* font, const char* text, u32 size) {
return RFont_text_area_len(font, text, 0, size, 0, 0.0f);
}
RFont_area RFont_text_area_spacing(RFont_font* font, const char* text, float spacing, u32 size) {
return RFont_text_area_len(font, text, 0, size, 0, spacing);
}
RFont_area RFont_text_area_len(RFont_font* font, const char* text, size_t len, u32 size, size_t stopNL, float spacing) {
float x = 0;
size_t y = 1;
char* str;
float scale = (((float)size) / font->fheight);
float space_adv = (scale * font->space_adv) / 2;
for (str = (char*)text; (len == 0 || (size_t)(str - text) < len) && *str; str++) {
if (*str == '\n') {
if (y == stopNL)
return (RFont_area){(u32)x, y * size};
y++;
x = 0;
continue;
}
if (*str == ' ' || *str == '\t') {
x += space_adv + spacing;
continue;
}
RFont_glyph glyph = RFont_font_add_char(font, *str, size);
if (glyph.codepoint == 0 && glyph.size == 0)
continue;
x += (float)glyph.advance + spacing;
}
return (RFont_area){(u32)x, y * size};
}
RFont_area RFont_draw_text(RFont_font* font, const char* text, float x, float y, u32 size) {
return RFont_draw_text_len(font, text, 0, x, y, size, 0.0f);
}
RFont_area RFont_draw_text_spacing(RFont_font* font, const char* text, float x, float y, u32 size, float spacing) {
return RFont_draw_text_len(font, text, 0, x, y, size, spacing);
}
RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, float x, float y, u32 size, float spacing) {
float* verts = RFont_verts;
float* tcoords = RFont_tcoords;
float startX = x;
float startY = y;
y += size;
u32 i = 0;
u32 tIndex = 0;
char* str;
float scale = (((float)size) / font->fheight);
float space_adv = (scale * font->space_adv) / 2;
y -= (-font->descent * scale);
for (str = (char*)text; (len == 0 || (size_t)(str - text) < len) && *str; str++) {
if (*str == '\n') {
x = startX;
y += size;
continue;
}
if (*str == ' ' || *str == '\t') {
x += space_adv + spacing;
continue;
}
RFont_glyph glyph = RFont_font_add_char(font, *str, size);
if (glyph.codepoint == 0 && glyph.size == 0)
continue;
float realX = x + glyph.x1;
float realY = y + glyph.y1;
verts[i] = RFONT_GET_WORLD_X((i32)realX, RFont_width);
verts[i + 1] = RFONT_GET_WORLD_Y(realY, RFont_height);
verts[i + 2] = 0;
/* */
verts[i + 3] = RFONT_GET_WORLD_X((i32)realX, RFont_width);
verts[i + 4] = RFONT_GET_WORLD_Y(realY + glyph.h , RFont_height);
verts[i + 5] = 0;
/* */
verts[i + 6] = RFONT_GET_WORLD_X((i32)(realX + glyph.w), RFont_width);
verts[i + 7] = RFONT_GET_WORLD_Y(realY + glyph.h , RFont_height);
verts[i + 8] = 0;
/* */
/* */
verts[i + 9] = RFONT_GET_WORLD_X((i32)(realX + glyph.w), RFont_width);
verts[i + 10] = RFONT_GET_WORLD_Y(realY, RFont_height);
verts[i + 11] = 0;
/* */
verts[i + 12] = RFONT_GET_WORLD_X((i32)realX, RFont_width);
verts[i + 13] = RFONT_GET_WORLD_Y(realY, RFont_height);
verts[i + 14] = 0;
/* */
verts[i + 15] = RFONT_GET_WORLD_X((i32)(realX + glyph.w), RFont_width);
verts[i + 16] = RFONT_GET_WORLD_Y(realY + glyph.h , RFont_height);
verts[i + 17] = 0;
/* texture coords */
//#if defined(RFONT_RENDER_LEGACY) || defined(RFONT_RENDER_RGL)
tcoords[tIndex] = RFONT_GET_TEXPOSX(glyph.x);
tcoords[tIndex + 1] = 0;
//#endif
/* */
tcoords[tIndex + 2] = RFONT_GET_TEXPOSX(glyph.x);
tcoords[tIndex + 3] = RFONT_GET_TEXPOSY(glyph.h);
/* */
tcoords[tIndex + 4] = RFONT_GET_TEXPOSX(glyph.x2);
tcoords[tIndex + 5] = RFONT_GET_TEXPOSY(glyph.h);
/* */
/* */
tcoords[tIndex + 6] = RFONT_GET_TEXPOSX(glyph.x2);
tcoords[tIndex + 7] = 0;
/* */
tcoords[tIndex + 8] = RFONT_GET_TEXPOSX(glyph.x);
tcoords[tIndex + 9] = 0;
/* */
tcoords[tIndex + 10] = RFONT_GET_TEXPOSX(glyph.x2);
tcoords[tIndex + 11] = RFONT_GET_TEXPOSY(glyph.h);
i += 18;
tIndex += 12;
x += glyph.advance + spacing;
}
#ifndef RFONT_NO_GRAPHICS
RFont_render_text(font->atlas, verts, tcoords, i / 3);
#endif
return (RFont_area){(u32)(x - startX), (u32)(y - startY) + (-font->descent * scale)};
}
#ifndef __APPLE__
#include <GL/gl.h>
#else
#include <OpenGL/gl.h>
#endif
#if !defined(RFONT_NO_OPENGL) && !defined(RFONT_NO_GRAPHICS)
#if !defined(RFONT_RENDER_LEGACY) && !defined(RFONT_RENDER_RGL)
#define GL_GLEXT_PROTOTYPES
#endif
#ifndef GL_PERSPECTIVE_CORRECTION_HINT
#define GL_PERSPECTIVE_CORRECTION_HINT 0x0C50
#endif
#ifndef GL_TEXTURE_SWIZZLE_RGBA
#define GL_TEXTURE_SWIZZLE_RGBA 0x8E46
#endif
#ifndef GL_TEXTURE0
#define GL_TEXTURE0 0x84C0
#endif
#ifndef GL_CLAMP_TO_EDGE
#define GL_CLAMP_TO_EDGE 0x812F
#endif
#ifdef RFONT_DEBUG
#ifndef GL_DEBUG_TYPE_ERROR
#define GL_DEBUG_TYPE_ERROR 0x824C
#define GL_DEBUG_OUTPUT 0x92E0
#define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242
#define GL_COMPILE_STATUS 0x8B81
#define GL_LINK_STATUS 0x8B82
#define GL_INFO_LOG_LENGTH 0x8B84
#endif
void RFont_debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char* message, const void* userParam) {
RFONT_UNUSED(source) RFONT_UNUSED(id) RFONT_UNUSED(severity) RFONT_UNUSED(length) RFONT_UNUSED(userParam)
if (type != GL_DEBUG_TYPE_ERROR)
return;
printf("OpenGL Debug Message: %s\n", message);
}
void RFont_opengl_getError(void) {
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
switch (err) {
case GL_INVALID_ENUM:
printf("OpenGL error: GL_INVALID_ENUM\n");
break;
case GL_INVALID_VALUE:
printf("OpenGL error: GL_INVALID_VALUE\n");
break;
case GL_INVALID_OPERATION:
printf("OpenGL error: GL_INVALID_OPERATION\n");
break;
case GL_STACK_OVERFLOW:
printf("OpenGL error: GL_STACK_OVERFLOW\n");
break;
case GL_STACK_UNDERFLOW:
printf("OpenGL error: GL_STACK_UNDERFLOW\n");
break;
default:
printf("OpenGL error: Unknown error code 0x%x\n", err);
break;
}
exit(1);
}
}
#endif
u32 RFont_create_atlas(u32 atlasWidth, u32 atlasHeight) {
#if defined(RFONT_DEBUG) && !defined(RFONT_RENDER_LEGACY)
glEnable(GL_DEBUG_OUTPUT);
#endif
u32 id = 0;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
u8* data = (u8*)calloc(atlasWidth * atlasHeight * 4, sizeof(u8));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, atlasWidth, atlasHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
free(data);
glBindTexture(GL_TEXTURE_2D, id);
static GLint swizzleRgbaParams[4] = {GL_ONE, GL_ONE, GL_ONE, GL_RED};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleRgbaParams);
glBindTexture(GL_TEXTURE_2D, 0);
return id;
}
#ifndef GL_UNPACK_ROW_LENGTH
#define GL_UNPACK_ROW_LENGTH 0x0CF2
#define GL_UNPACK_SKIP_PIXELS 0x0CF4
#define GL_UNPACK_SKIP_ROWS 0x0CF3
#endif
void RFont_push_pixel_values(GLint alignment, GLint rowLength, GLint skipPixels, GLint skipRows);
void RFont_push_pixel_values(GLint alignment, GLint rowLength, GLint skipPixels, GLint skipRows) {
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, skipPixels);
glPixelStorei(GL_UNPACK_SKIP_ROWS, skipRows);
}
void RFont_bitmap_to_atlas(u32 atlas, u8* bitmap, float x, float y, float w, float h) {
glEnable(GL_TEXTURE_2D);
GLint alignment, rowLength, skipPixels, skipRows;
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength);
glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skipPixels);
glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skipRows);
#if !defined(RFONT_RENDER_LEGACY)
glActiveTexture(GL_TEXTURE0 + atlas - 1);
#endif
glBindTexture(GL_TEXTURE_2D, atlas);
RFont_push_pixel_values(1, w, 0, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RED, GL_UNSIGNED_BYTE, bitmap);
RFont_push_pixel_values(alignment, rowLength, skipPixels, skipRows);
glBindTexture(GL_TEXTURE_2D, 0);
}
#if defined(RFONT_RENDER_RGL) && !defined(RFONT_CUSTOM_GL)
void RFont_render_set_color(float r, float g, float b, float a) {
rglColor4f(r, g, b, a);
}
void RFont_render_text(u32 atlas, float* verts, float* tcoords, size_t nverts) {
glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
rglMatrixMode(RGL_MODELVIEW);
rglLoadIdentity();
rglPushMatrix();
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
rglSetTexture(atlas);
rglBegin(RGL_TRIANGLES_2D);
size_t i;
size_t tIndex = 0;
for (i = 0; i < (nverts * 3); i += 3) {
rglTexCoord2f(tcoords[tIndex], tcoords[tIndex + 1]);
tIndex += 2;
rglVertex2f(verts[i], verts[i + 1]);
}
rglEnd();
rglPopMatrix();
rglSetTexture(0);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_DEPTH_TEST);
}
void RFont_render_free(u32 atlas) { glDeleteTextures(1, &atlas); }
void RFont_render_legacy(u8 legacy) { rglLegacy(legacy); }
void RFont_render_init() {}