forked from ColleagueRiley/RFont
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFont.h
More file actions
3239 lines (2768 loc) · 108 KB
/
RFont.h
File metadata and controls
3239 lines (2768 loc) · 108 KB
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-25 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_INT_DEFINED - int types are already defined
#define RFONT_C89 - uses cints instead of stdint.h and __inline instead of inline
#define RFONT_INLINE x - set your own inline
#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_EXTERNAL_STB_IMPLEMENTATION - the same as RFONT_EXTERNAL_STB but doesn't define STB_TRUETYPE_IMPLEMENTATION
-- 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_renderer renderer = ...;
RFont_renderer_init(&renderer);
RFont_font* font = RFont_font_init(&renderer, "font.ttf", 20, 500, 500);
i32 w = ...;
i32 h = ...;
while (1) {
...
RFont_renderer_set_framebuffer(&renderer, (u32)w, (u32)h);
RFont_renderer_set_color(&renderer, 0.0f, 1.0f, 0, 1.0f);
RFont_draw_text(&renderer, font, "text", 100, 100, 20);
...
}
RFont_font_free(&renderer, font);
RFont_renderer_free(&renderer);
...
}
*/
#ifndef RFONT_INLINE
#ifdef RFONT_C89
#define RFONT_INLINE
#else
#define RFONT_INLINE inline
#endif
#endif
#ifndef RFONT_API
#ifdef RFONT_STATIC
#define RFONT_API static
#else
#define RFONT_API extern RFONT_INLINE
#endif
#endif
#ifndef RFONT_NO_STDIO
#include <stdio.h>
#endif
#ifndef RFONT_MALLOC
#include <stdlib.h>
#define RFONT_MALLOC malloc
#define RFONT_FREE free
#endif
#if !defined(RFONT_MEMCPY) || !defined(RFONT_MEMSET)
#include <string.h>
#endif
#ifndef RFONT_MEMSET
#define RFONT_MEMSET(ptr, value, num) memset(ptr, value, num)
#endif
#ifndef RFONT_MEMCPY
#define RFONT_MEMCPY(dist, src, len) memcpy(dist, src, len)
#endif
#ifndef RFONT_ASSERT
#include <assert.h>
#define RFONT_ASSERT(x) assert(x)
#endif
#if !defined(RFONT_FLOOR) || !defined(RFONT_CEIL) || !defined(RFONT_FABS) || !defined(RFONT_SQRT)
#include <math.h>
#endif
#ifndef RFONT_FLOOR
#define RFONT_FLOOR(x) floor(x)
#endif
#ifndef RFONT_CEIL
#define RFONT_CEIL(x) ceil(x)
#endif
#ifndef RFONT_FABS
#define RFONT_FABS(x) fabs(x)
#endif
#ifndef RFONT_SQRT
#define RFONT_SQRT(x) sqrt(x)
#endif
#if defined(__STDC__) && !defined(__STDC_VERSION__)
#define RFONT_C89
#endif
#if !defined(RFONT_INT_DEFINED)
#define RFONT_INT_DEFINED
#if defined(_MSC_VER) || defined(__SYMBIAN32__) || defined(RFONT_C89)
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_texture
typedef size_t RFont_texture;
#endif
#ifndef RFont_surface
typedef void* RFont_surface;
#endif
#ifndef RFONT_MAX_GLYPHS
#define RFONT_MAX_GLYPHS 256
#endif
#ifndef RFONT_INIT_VERTS
#define RFONT_INIT_VERTS 20 * RFONT_MAX_GLYPHS
#endif
#ifndef RFONT_UNUSED
#define RFONT_UNUSED(x) (void) (x);
#endif
#ifndef RFONT_RENDERER_H
#define RFONT_RENDERER_H
typedef struct RFont_render_data {
float* verts;
float* tcoords;
u16* elements;
RFont_texture atlas;
size_t nverts;
size_t nelements;
} RFont_render_data;
typedef struct RFont_renderer_proc {
size_t (*size)(void); /*!< get the size of the renderer context */
void (*initPtr)(void* ctx); /* any initalizations the renderer needs to do */
RFont_texture (*create_atlas)(void* ctx, u32 atlasWidth, u32 atlasHeight); /* create a bitmap texture based on the given size */
void (*free_atlas)(void* ctx, RFont_texture atlas);
void (*bitmap_to_atlas)(void* ctx, RFont_texture atlas, u32 atlasWidth, u32 atlasHeight, u32 maxHeight, u8* bitmap, float w, float h, float* x, float* y); /* add the given bitmap to the texture based on the given coords and size data */
void (*render)(void* ctx, const RFont_render_data* data); /* render the text, using the vertices, atlas texture, and texture coords given. */
void (*set_framebuffer)(void* ctx, u32 weight, u32 height); /*!< set the frame buffer size (for ortho, for example) */
void (*set_color)(void* ctx, float r, float g, float b, float a); /*!< set the current rendering color */
void (*set_surface)(void* ctx, RFont_surface surface);
void (*freePtr)(void* ctx); /* free any memory the renderer might need to free */
} RFont_renderer_proc;
typedef struct RFont_renderer {
void* ctx; /*!< source renderer data */
RFont_renderer_proc proc;
} RFont_renderer;
typedef struct RFont_font RFont_font;
#endif /* RFONT_RENDERER_H */
#ifndef RFONT_H
#define RFONT_H
RFONT_API size_t RFont_renderer_size(RFont_renderer* renderer);
RFONT_API RFont_renderer* RFont_renderer_init(RFont_renderer_proc proc);
RFONT_API void RFont_renderer_initPtr(RFont_renderer_proc proc, void* ptr, RFont_renderer* renderer);
RFONT_API void RFont_renderer_set_framebuffer(RFont_renderer* renderer, u32 w, u32 h);
RFONT_API void RFont_renderer_set_surface(RFont_renderer* renderer, RFont_surface surface);
RFONT_API void RFont_renderer_set_color(RFont_renderer* renderer, float r, float g, float b, float a);
RFONT_API void RFont_renderer_free(RFont_renderer* renderer);
RFONT_API void RFont_renderer_freePtr(RFont_renderer* renderer);
#define RFONT_GET_FONT_WIDTH(fontHeight) RFONT_MAX_GLYPHS * fontHeight
typedef struct {
u32 codepoint; /* the character (for checking) */
size_t size; /* the size of the glyph */
i32 x, x2, y, y2; /* coords of the character on the texture */
RFont_font* font; /* the font that the glyph belongs to */
/* source glyph data */
i32 src;
float w, h, x1, y1, advance;
} RFont_glyph;
typedef struct RFont_src RFont_src;
struct RFont_font {
RFont_src* src; /* source stb font info */
float fheight; /* source font height */
float descent; /* font descent */
float numOfLongHorMetrics;
float space_adv;
u32 maxHeight;
RFont_glyph glyphs[RFONT_MAX_GLYPHS]; /* glyphs */
size_t glyph_len;
RFont_texture atlas; /* atlas texture */
size_t atlasWidth, atlasHeight;
float atlasX, atlasY; /* the current position inside the atlas */
float verts[RFONT_INIT_VERTS * 3];
float tcoords[RFONT_INIT_VERTS * 2];
u16 elements[RFONT_INIT_VERTS * 6];
};
/**
* @brief Converts a codepoint to a utf8 string.
* @param codepoint The codepoint to convert to utf8.
* @return The utf8 string.
*/
RFONT_API char* RFont_codepoint_to_utf8(u32 codepoint);
#ifndef RFONT_NO_STDIO
/**
* @brief Init font stucture with a TTF file path.
* @param font_name The TTF file path.
* @param atlasWidth The width of the atlas texture.
* @param atlasHeight The height of the atlas texture. (This should == the max text size)
* @return The `RFont_font` created using the TTF file data.
*/
RFONT_API RFont_font* RFont_font_init(RFont_renderer* renderer, const char* font_name, u32 maxHeight, size_t atlasWidth, size_t atlasHeight);
/**
* @brief Init a given font stucture with a TTF file path.
* @param font_name The TTF file path.
* @param atlasWidth The width of the atlas texture.
* @param atlasHeight The height of the atlas texture. (This should == the max text size)
* @pram ptr Pointer to the given font structure
* @return returns the same pointer or NULL if the font failed to load
*/
RFONT_API RFont_font* RFont_font_init_ptr(RFont_renderer* renderer, const char* font_name, u32 maxHeight, size_t atlasWidth, size_t atlasHeight, RFont_font* font);
#endif
/**
* @brief Init font stucture with raw TTF data.
* @param font_data The raw TTF data.
* @param atlasWidth The width of the atlas texture.
* @param atlasHeight The height of the atlas texture. (This should == the max text size)
* @return The `RFont_font` created from the data.
*/
RFONT_API RFont_font* RFont_font_init_data(RFont_renderer* renderer, u8* font_data, u32 maxHeight, size_t atlasWidth, size_t atlasHeight);
/**
* @brief Init a given font stucture with raw TTF data.
* @param font_data The raw TTF data.
* @param atlasWidth The width of the atlas texture.
* @param atlasHeight The height of the atlas texture. (This should == the max text size)
* @return The `RFont_font` created from the data.
* @return returns the same pointer or NULL if the font failed to load
*/
RFONT_API RFont_font* RFont_font_init_data_ptr(RFont_renderer* renderer, u8* font_data, u32 maxHeight, size_t atlasWidth, size_t atlasHeight, RFont_font* ptr);
/**
* @brief Free data from the font stucture, including the stucture itself
* @param font The font stucture to free
*/
RFONT_API void RFont_font_free(RFont_renderer* renderer, RFont_font* font);
/**
* @brief Free data from the font stucture only (not including the stucture)
* @param font The strucutre with the font data to free
*/
RFONT_API void RFont_font_free_ptr(RFont_renderer* renderer, RFont_font* font);
typedef RFont_glyph (*RFont_glyph_fallback_callback)(RFont_renderer* renderer, RFont_font* font, u32 codepoint, size_t size);
RFont_glyph_fallback_callback RFont_set_glyph_fallback_callback(RFont_glyph_fallback_callback callback);
/**
* @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.
*/
RFONT_API RFont_glyph RFont_font_add_char(RFont_renderer* renderer,RFont_font* font, char ch, size_t size);
/**
* @brief Add a codepoint to the font's atlas.
* @param font The font to use.
* @param codepoint The codepoint 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.
*/
RFONT_API RFont_glyph RFont_font_add_codepoint(RFont_renderer* renderer, RFont_font* font, u32 codepoint, size_t size);
/**
* @brief Add a codepoint to the font's atlas.
* @param font The font to use.
* @param codepoint The codepoint to add to the atlas.
* @param size The size of the character.
* @param fallback If the fallback function should not be called.
* @return The `RFont_glyph` created from the data and added to the atlas.
*/
RFONT_API RFont_glyph RFont_font_add_codepoint_ex(RFont_renderer* renderer, RFont_font* font, u32 codepoint, size_t size, b8 fallback);
/**
* @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
*/
RFONT_API void RFont_font_add_string(RFont_renderer* renderer, 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
*/
RFONT_API void RFont_font_add_string_len(RFont_renderer* renderer, 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
* @param [OUTPUT] the output width
* @param [OUTPUT] the output height
*/
RFONT_API void RFont_text_area(RFont_renderer* renderer, RFont_font* font, const char* text, u32 size, u32* w, u32* h);
/**
* @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
* @param [OUTPUT] the output width
* @param [OUTPUT] the output height
*/
RFONT_API void RFont_text_area_spacing(RFont_renderer* renderer, RFont_font* font, const char* text, float spacing, u32 size, u32* w, u32* h);
/**
* @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
* @param [OUTPUT] the output width
* @param [OUTPUT] the output height
*/
RFONT_API void RFont_text_area_len(RFont_renderer* renderer, RFont_font* font, const char* text, size_t len, u32 size, size_t stopNL, float spacing, u32* w, u32* h);
/**
* @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 number of verts rendered
*/
RFONT_API size_t RFont_draw_text(RFont_renderer* renderer, 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 number of verts rendered
*/
RFONT_API size_t RFont_draw_text_spacing(RFont_renderer* renderer, 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 number of verts rendered
*/
RFONT_API size_t RFont_draw_text_len(RFont_renderer* renderer, RFont_font* font, const char* text, size_t len, float x, float y, u32 size, float spacing);
#endif /* RFONT_H */
#ifdef RFONT_IMPLEMENTATION
#ifndef RFONT_GET_TEXPOSX
#define RFONT_GET_TEXPOSX(x, w) (float)((float)(x) / (float)(w))
#define RFONT_GET_TEXPOSY(y, h) (float)((float)(y) / (float)(h))
#endif
size_t RFont_renderer_size(RFont_renderer* renderer) {
size_t size = 0;
if (renderer->proc.size)
size = renderer->proc.size();
return size;
}
RFont_renderer* RFont_renderer_init(RFont_renderer_proc proc) {
void* ptr = NULL;
size_t size;
RFont_renderer* renderer = (RFont_renderer*)RFONT_MALLOC(sizeof(RFont_renderer));
renderer->proc = proc;
size = RFont_renderer_size(renderer);
if (size) ptr = RFONT_MALLOC(size);
RFont_renderer_initPtr(proc, ptr, renderer);
return renderer;
}
void RFont_renderer_initPtr(RFont_renderer_proc proc, void* ptr, RFont_renderer* renderer) {
renderer->ctx = ptr;
renderer->proc = proc;
if (renderer->proc.initPtr)
renderer->proc.initPtr(renderer->ctx);
}
void RFont_renderer_set_framebuffer(RFont_renderer* renderer, u32 w, u32 h) {
if (renderer->proc.set_framebuffer)
renderer->proc.set_framebuffer(renderer->ctx, w, h);
}
void RFont_renderer_set_surface(RFont_renderer* renderer, RFont_surface surface) {
if (renderer->proc.set_surface)
renderer->proc.set_surface(renderer, surface);
}
void RFont_renderer_set_color(RFont_renderer* renderer, float r, float g, float b, float a) {
if (renderer->proc.set_color)
renderer->proc.set_color(renderer->ctx, r, g, b, a);
}
void RFont_renderer_free(RFont_renderer* renderer) {
RFont_renderer_freePtr(renderer);
if (renderer->ctx) RFONT_FREE(renderer->ctx);
RFONT_FREE(renderer);
}
void RFont_renderer_freePtr(RFont_renderer* renderer) {
if (renderer->proc.freePtr)
renderer->proc.freePtr(renderer->ctx);
}
#define RFONT_CHAR(p, index) (((char*)p)[index])
#define RFONT_BYTE(p, index) (((u8*)p)[index])
#define RFONT_SHORT(arr, index) (i16)((i16)((u8*)arr)[(size_t)(index)]*256 + (i16)(((u8*)arr)[(size_t)(index) + 1]))
#define RFONT_USHORT(arr, index) (u16)((u16)((u8*)arr)[(size_t)(index)]*256 + (u16)(((u8*)arr)[(size_t)(index) + 1]))
#define RFONT_ULONG(arr, index) (u32)((u32)(((u8*)arr)[(size_t)(index)]<<24) + (u32)(((u8*)arr)[(size_t)(index) + 1]<<16) + (u32)(((u8*)arr)[(size_t)(index) + 2]<<8) + (u32)(((u8*)arr))[(size_t)(index) + 3])
/*
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
typedef struct {
unsigned char *data;
int cursor;
int size;
} rstbtt__buf;
typedef struct rstbtt_fontinfo rstbtt_fontinfo;
struct rstbtt_fontinfo {
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 */
rstbtt__buf cff; /* cff font data */
rstbtt__buf charstrings; /* the charstring index */
rstbtt__buf gsubrs; /* global charstring subroutines index */
rstbtt__buf subrs; /* private charstring subroutines index */
rstbtt__buf fontdicts; /* array of font dicts */
rstbtt__buf fdselect; /* map from glyph to fontdict */
};
RFONT_API int rstbtt_InitFont(rstbtt_fontinfo *info, const unsigned char *data, int offset);
RFONT_API unsigned char* rstbtt_GetGlyphBitmapSubpixel(const rstbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff);
RFONT_API int rstbtt_FindGlyphIndex(const rstbtt_fontinfo *info, int unicode_codepoint);
RFONT_API int rstbtt_GetGlyphKernAdvance(const rstbtt_fontinfo *info, int glyph1, int glyph2);
RFONT_API int rstbtt_GetGlyphBox(const rstbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
#else
#ifdef RFONT_EXTERNAL_STB_IMPLEMENTATION
#define RFONT_EXTERNAL_STB
#endif
#ifndef RFONT_EXTERNAL_STB_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#endif
#include "stb_truetype.h"
typedef struct stbtt_fontinfo rstbtt_fontinfo;
#define rstbtt_InitFont stbtt_InitFont
#define rstbtt_GetGlyphBitmapSubpixel stbtt_GetGlyphBitmapSubpixel
#define rstbtt_FindGlyphIndex stbtt_FindGlyphIndex
#define rstbtt_GetGlyphKernAdvance stbtt_GetGlyphKernAdvance
#define rstbtt_GetGlyphBox stbtt_GetGlyphBox
#endif /* RFONT_EXTERNAL_STB */
struct RFont_src {
rstbtt_fontinfo info;
};
/*
END of stb defines required by RFont
you probably care about this part
*/
#ifndef RFONT_NO_STDIO
char* RFont_read_file(const char* font_name) {
size_t size, out;
char* ttf_buffer;
FILE* ttf_file = fopen(font_name, "rb");
if (ttf_file == NULL) return NULL;
fseek(ttf_file, 0U, SEEK_END);
size = (size_t)ftell(ttf_file);
if (size <= 0) return NULL;
ttf_buffer = (char*)RFONT_MALLOC(sizeof(char) * (size_t)size);
fseek(ttf_file, 0U, SEEK_SET);
out = fread(ttf_buffer, 1, (size_t)size, ttf_file);
RFONT_UNUSED(out);
return ttf_buffer;
}
RFont_font* RFont_font_init(RFont_renderer* renderer, const char* font_name, u32 maxHeight, size_t atlasWidth, size_t atlasHeight) {
char* ttf_buffer = RFont_read_file(font_name);
RFont_font* font = RFont_font_init_data(renderer, (u8*)ttf_buffer, maxHeight, atlasWidth, atlasHeight);
return font;
}
RFont_font* RFont_font_init_ptr(RFont_renderer* renderer, const char* font_name, u32 maxHeight, size_t atlasWidth, size_t atlasHeight, RFont_font* ptr) {
char* ttf_buffer = RFont_read_file(font_name);
RFont_font* font = RFont_font_init_data_ptr(renderer, (u8*)ttf_buffer, maxHeight, atlasWidth, atlasHeight, ptr);
return font;
}
#endif
RFont_font* RFont_font_init_data(RFont_renderer* renderer, u8* font_data, u32 maxHeight, size_t atlasWidth, size_t atlasHeight) {
RFont_font* font = (RFont_font*)RFONT_MALLOC(sizeof(RFont_font));
return RFont_font_init_data_ptr(renderer, font_data, maxHeight, atlasWidth, atlasHeight, font);
}
RFont_font* RFont_font_init_data_ptr(RFont_renderer* renderer, u8* font_data, u32 maxHeight, size_t atlasWidth, size_t atlasHeight, RFont_font* font) {
u16 index = 0;
u16 vert_index = 0;
i32 space_codepoint = 0;
font->src = (RFont_src*)RFONT_MALLOC(sizeof(RFont_src));
font->atlasWidth = atlasWidth;
font->atlasHeight = atlasHeight;
font->maxHeight = maxHeight;
rstbtt_InitFont(&font->src->info, font_data, 0);
font->fheight = RFONT_SHORT(font->src->info.data, font->src->info.hhea + 4) - RFONT_SHORT(font->src->info.data, font->src->info.hhea + 6);
font->descent = RFONT_SHORT(font->src->info.data, font->src->info.hhea + 6);
font->numOfLongHorMetrics = RFONT_USHORT(font->src->info.data, font->src->info.hhea + 34);
space_codepoint = rstbtt_FindGlyphIndex(&font->src->info, (int)' ');
if (' ' < font->numOfLongHorMetrics)
font->space_adv = RFONT_SHORT(font->src->info.data, font->src->info.hmtx + 4 * space_codepoint);
else
font->space_adv = RFONT_SHORT(font->src->info.data, font->src->info.hmtx + 4 * (i32)(font->numOfLongHorMetrics - 1));
if (renderer->proc.create_atlas)
font->atlas = renderer->proc.create_atlas(renderer->ctx, (u32)atlasWidth, (u32)atlasHeight);
font->atlasX = 0;
font->atlasY = 0;
font->glyph_len = 0;
for (index = 0; index < RFONT_INIT_VERTS; index += 6) {
font->elements[index + 0] = vert_index + 0;
font->elements[index + 1] = vert_index + 1;
font->elements[index + 2] = vert_index + 2;
font->elements[index + 3] = vert_index + 3;
font->elements[index + 4] = vert_index + 0;
font->elements[index + 5] = vert_index + 2;
vert_index += 4;
}
return font;
}
void RFont_font_free_ptr(RFont_renderer* renderer, RFont_font* font) {
if (renderer->proc.free_atlas)
renderer->proc.free_atlas(renderer->ctx, font->atlas);
RFONT_FREE(font->src);
}
void RFont_font_free(RFont_renderer* renderer, RFont_font* font) {
RFONT_FREE(font->src->info.data);
RFont_font_free_ptr(renderer, font);
RFONT_FREE(font);
}
/*
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
RFONT_API u32 RFont_decode_utf8(u32* state, u32* codep, u32 byte);
u32 RFont_decode_utf8(u32* state, u32* codep, u32 byte) {
static const u8 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 */
};
u32 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_renderer* renderer, RFont_font* font, const char* string, size_t* sizes, size_t sizeLen) {
RFont_font_add_string_len(renderer, font, string, 0, sizes, sizeLen);
}
void RFont_font_add_string_len(RFont_renderer* renderer, 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(renderer, font, *str, sizes[i]);
}
RFont_glyph_fallback_callback RFont_glyph_fallback = NULL;
RFont_glyph_fallback_callback RFont_set_glyph_fallback_callback(RFont_glyph_fallback_callback callback) {
RFont_glyph_fallback_callback old = RFont_glyph_fallback;
RFont_glyph_fallback = callback;
return old;
}
RFont_glyph RFont_font_add_char(RFont_renderer* renderer, 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) {
RFont_glyph g;
RFONT_MEMSET(&g, 0, sizeof(RFont_glyph));
return g;
}
return RFont_font_add_codepoint(renderer, font, codepoint, size);
}
RFont_glyph RFont_font_add_codepoint(RFont_renderer* renderer, RFont_font* font, u32 codepoint, size_t size) {
return RFont_font_add_codepoint_ex(renderer, font, codepoint, size, 1);
}
RFont_glyph RFont_font_add_codepoint_ex(RFont_renderer* renderer, RFont_font* font, u32 codepoint, size_t size, b8 fallback) {
RFont_glyph* glyph;
RFont_glyph glyphNull;
u8* bitmap;
float scale;
i32 x0, y0, x1, y1, w = 0, h = 0, advanceX = 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_MEMSET(&glyphNull, 0, sizeof(glyphNull));
if (i < sizeof(font->glyphs)) {
glyph = &font->glyphs[i];
} else {
return glyphNull;
}
glyph->src = rstbtt_FindGlyphIndex(&font->src->info, (int)codepoint);
if ((glyph->src == 0 && codepoint) && fallback && RFont_glyph_fallback) {
RFont_glyph fallbackGlyph = RFont_glyph_fallback(renderer, font, codepoint, size);
if (fallbackGlyph.codepoint != 0 && fallbackGlyph.size != 0) {
return fallbackGlyph;
}
}
if (glyph->src == 0 && codepoint) return RFont_font_add_codepoint_ex(renderer, font, 0, size, fallback);
font->glyph_len++;
if (codepoint && rstbtt_GetGlyphBox(&font->src->info, glyph->src, &x0, &y0, &x1, &y1) == 0) {
return glyphNull;
}
scale = ((float)size) / font->fheight;
bitmap = rstbtt_GetGlyphBitmapSubpixel(&font->src->info, 0, scale, 0.0f, 0.0f, glyph->src, &w, &h, 0, 0);
glyph->w = (float)w;
glyph->h = (float)h;
if (codepoint) {
glyph->x1 = (float)floor((float)x0 * scale);
glyph->y1 = (float)floor((float)-y1 * scale);
} else glyph->y1 = (float)-((float)h * 0.75f);
glyph->codepoint = codepoint;
glyph->size = size;
glyph->font = font;
if (renderer->proc.bitmap_to_atlas)
renderer->proc.bitmap_to_atlas(renderer->ctx, font->atlas, (u32)font->atlasWidth, (u32)font->atlasHeight, font->maxHeight, bitmap, glyph->w, glyph->h, &font->atlasX, &font->atlasY);
RFONT_FREE(bitmap);
glyph->x = (i32)(font->atlasX - glyph->w);
glyph->x2 = (i32)(font->atlasX);
glyph->y = (i32)(font->atlasY);
glyph->y2 = (i32)((font->atlasY) + glyph->h);
if (glyph->src < font->numOfLongHorMetrics)
advanceX = RFONT_SHORT(font->src->info.data, font->src->info.hmtx + 4 * glyph->src);
else
advanceX = RFONT_SHORT(font->src->info.data, font->src->info.hmtx + 4 * (i32)(font->numOfLongHorMetrics - 1));
glyph->advance = (u32)((float)advanceX * scale);
return *glyph;
}
void RFont_text_area(RFont_renderer* renderer, RFont_font* font, const char* text, u32 size, u32* w, u32* h) {
RFont_text_area_len(renderer, font, text, 0, size, 0, 0.0f, w, h);
}
void RFont_text_area_spacing(RFont_renderer* renderer, RFont_font* font, const char* text, float spacing, u32 size, u32* w, u32* h) {
RFont_text_area_len(renderer, font, text, 0, size, 0, spacing, w, h);
}
void RFont_text_area_len(RFont_renderer* renderer, RFont_font* font, const char* text, size_t len, u32 size, size_t stopNL, float spacing, u32* w, u32* h) {
float x = 0;
size_t y = 1;
char* str;
float scale = (((float)size) / font->fheight);
float space_adv = (scale * font->space_adv);
RFont_glyph glyph;
for (str = (char*)text; (len == 0 || (size_t)(str - text) < len) && *str; str++) {
if (*str == '\n') {
if (y == stopNL) {
if (w) *w = (u32)x;
if (h) *h = (u32)y * size;
return;
}
y++;
x = 0;
continue;
}
if (*str == ' ' || *str == '\t') {
x += space_adv + spacing;
continue;
}
glyph = RFont_font_add_char(renderer, font, *str, size);
if (glyph.codepoint == 0 && glyph.size == 0)
continue;
x += (float)glyph.advance + spacing;
}
if (w) *w = (u32)x;
if(h) *h = (u32)(y * size);
}
size_t RFont_draw_text(RFont_renderer* renderer, RFont_font* font, const char* text, float x, float y, u32 size) {
return RFont_draw_text_len(renderer, font, text, 0, x, y, size, 0.0f);
}
size_t RFont_draw_text_spacing(RFont_renderer* renderer, RFont_font* font, const char* text, float x, float y, u32 size, float spacing) {
return RFont_draw_text_len(renderer, font, text, 0, x, y, size, spacing);
}
char* RFont_codepoint_to_utf8(u32 codepoint) {
static char utf8[5];
if (codepoint <= 0x7F) {
utf8[0] = (char)codepoint;
utf8[1] = 0;
} else if (codepoint <= 0x7FF) {
utf8[0] = (char)(0xC0 | (codepoint >> 6));
utf8[1] = (char)(0x80 | (codepoint & 0x3F));
utf8[2] = 0;
} else if (codepoint <= 0xFFFF) {
utf8[0] = (char)(0xE0 | (codepoint >> 12));
utf8[1] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
utf8[2] = (char)(0x80 | (codepoint & 0x3F));
utf8[3] = 0;
} else if (codepoint <= 0x10FFFF) {
utf8[0] = (char)(0xF0 | (codepoint >> 18));
utf8[1] = (char)(0x80 | ((codepoint >> 12) & 0x3F));
utf8[2] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
utf8[3] = (char)(0x80 | (codepoint & 0x3F));
utf8[4] = 0;
} else {
utf8[0] = 0;
}
return utf8;
}
size_t RFont_draw_text_len(RFont_renderer* renderer, RFont_font* font, const char* text, size_t len, float x, float y, u32 size, float spacing) {
RFont_render_data data;
float startX = x;
float startY = y;
size_t i = 0;
size_t tIndex = 0;
char* str;
RFont_glyph glyph;
float realX, realY;
float scale = (((float)size) / font->fheight);
float space_adv = (scale * font->space_adv);
float descent_offset = (-font->descent * scale);
data.verts = font->verts;
data.tcoords = font->tcoords;
data.elements = font->elements;
data.atlas = font->atlas;
data.nverts = 0;
data.nelements = 0;
RFONT_UNUSED(startY);
y = (y + (float)size - descent_offset);
for (str = (char*)text; (len == 0 || (size_t)(str - text) < len) && *str; str++) {
if (*str == '\n') {
x = startX;
y += (float)size;
continue;
}
if (*str == ' ' || *str == '\t') {
x += space_adv + spacing;
continue;
}
glyph = RFont_font_add_char(renderer, font, *str, size);
if (glyph.codepoint == 0 && glyph.size == 0)
continue;
if (glyph.font != font) {
RFont_draw_text_len(renderer, glyph.font, RFont_codepoint_to_utf8(glyph.codepoint), 4, x, y - (float)size + descent_offset, size, spacing);
x += glyph.advance + spacing;
continue;
}
realX = x + glyph.x1;
realY = y + glyph.y1;
i = data.nverts * 3;
tIndex = data.nverts * 2;
data.verts[i] = (i32)realX;
data.verts[i + 1] = realY;
data.verts[i + 2] = 0;
/* */
data.verts[i + 3] = (i32)realX;
data.verts[i + 4] = realY + glyph.h;
data.verts[i + 5] = 0;
/* */
data.verts[i + 6] = (i32)(realX + glyph.w);
data.verts[i + 7] = realY + glyph.h;
data.verts[i + 8] = 0;
/* */