forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzahnrad.h
2261 lines (2028 loc) · 82.2 KB
/
zahnrad.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) 2016 Micha Mettke
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 restrictions:
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.
*/
#ifndef ZR_H_
#define ZR_H_
#ifdef __cplusplus
extern "C" {
#endif
/*
* ==============================================================
*
* CONSTANTS
*
* ===============================================================
*/
#define ZR_UTF_INVALID 0xFFFD
#define ZR_UTF_SIZE 4
/* describes the number of bytes a glyph consists of*/
#define ZR_INPUT_MAX 16
/* defines the max number of bytes to be added as text input in one frame */
#define ZR_MAX_COLOR_STACK 32
/* Number of temporary configuration color changes that can be stored */
#define ZR_MAX_ATTRIB_STACK 32
/* Number of temporary configuration attribute changes that can be stored */
#define ZR_MAX_FONT_STACK 32
/* Number of temporary configuration user font changes that can be stored */
#define ZR_MAX_FONT_HEIGHT_STACK 32
/* Number of temporary configuration font height changes that can be stored */
#define ZR_MAX_NUMBER_BUFFER 64
/* Buffer size for the conversion buffer between float and string */
#define ZR_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
/* Initial buffer size for buffer with default allocator */
/*
* ==============================================================
*
* COMPILING FLAGS
*
* ===============================================================
*/
#define ZR_COMPILE_WITH_FIXED_TYPES 1
/* setting this define to 1 adds header <stdint.h> for fixed sized types
* if 0 each type has to be set to the correct size */
#define ZR_COMPILE_WITH_ASSERT 1
/* setting this define to 1 adds header <assert.h> for the assert macro
IMPORTANT: it also adds the standard library assert so only use it if wanted */
#define ZR_COMPILE_WITH_DEFAULT_ALLOCATOR 1
/* setting this to 1 create a default allocator to be used for memory management
* everywhere inside this library. if 0 you either have to provide a fixed size
* memory block or a custom allocator.
* IMPORTANT: this adds <stdlib.h> with malloc and free so set 0 if you don't want
* to link to the standard library!*/
#define ZR_COMPILE_WITH_STANDARD_FILE_IO 1
/* setting this to 1 create a default allocator to be used for memory management
* IMPORTANT: this adds <stdio.h> with fopen,fclose,... so set 0 if you don't want
* to link to the standard library!*/
#define ZR_COMPILE_WITH_VERTEX_BUFFER 1
/* setting this to 1 adds a vertex draw command list backend to this
library, which allows you to convert queue commands into vertex draw commands.
If you do not want or need a default backend you can set this flag to zero
and the module of the library will not be compiled */
#define ZR_COMPILE_WITH_FONT 1
/* setting this to 1 adds the `stb_truetype` and `stb_rect_pack` header
to this library and provides a default font for font loading and rendering.
If you already have font handling or do not want to use this font handler
you can just set this define to zero and the font module will not be compiled
and the two headers will not be needed. */
#define ZR_DISABLE_STB_RECT_PACK_IMPLEMENTATION 0
/* If you already provide the implementation for stb_rect_pack.h in one of your
files you have to define this as 1 to prevent another implementation and the
resulting symbol collision. */
#define ZR_DISABLE_STB_TRUETYPE_IMPLEMENTATION 0
/* If you already provide the implementation for stb_truetype.h in one of your
files you have to define this as 1 to prevent another implementation and the
resulting symbol collision. */
#define ZR_COMPILE_WITH_DEFAULT_FONT 1
/* setting this to 1 adds a default font into this library which can be loaded
* into a font atlas and allows using this library without having a truetype font
* IMPORTANT: enableing this requires ~12kb global stack memory. */
#define ZR_COMPILE_WITH_COMMAND_USERDATA 0
/* Activating this adds a userdata pointer into each command. Can be usefull if
* you want to provide custom shader depending on the used widget. Can be combined
* with the style structures. */
/*
* ===============================================================
*
* BASIC
*
* ===============================================================
*/
#if ZR_COMPILE_WITH_FIXED_TYPES
#include <stdint.h>
typedef uint32_t zr_uint;
typedef uint32_t zr_hash;
typedef uintptr_t zr_size;
typedef uintptr_t zr_ptr;
typedef uint32_t zr_flags;
typedef uint32_t zr_rune;
typedef uint8_t zr_byte;
#else
typedef unsigned int zr_uint;
typedef unsigned int zr_hash;
typedef unsigned long zr_size;
typedef zr_size zr_ptr;
typedef unsigned int zr_flags;
typedef unsigned int zr_rune;
typedef unsigned char zr_byte;
#endif
#if ZR_COMPILE_WITH_ASSERT
#ifndef ZR_ASSERT
#include <assert.h>
#define ZR_ASSERT(expr) assert(expr)
#endif
#else
#define ZR_ASSERT(expr)
#endif
struct zr_user_font;
struct zr_edit_box;
struct zr_user_font_glyph;
/* ===============================================================
* UTILITY
* ===============================================================*/
#define ZR_UNDEFINED (-1.0f)
#define ZR_FLAG(x) (1 << (x))
typedef zr_size zr_element;
enum {zr_false, zr_true};
struct zr_color {zr_byte r,g,b,a;};
struct zr_vec2 {float x,y;};
struct zr_vec2i {short x, y;};
struct zr_rect {float x,y,w,h;};
struct zr_recti {short x,y,w,h;};
typedef char zr_glyph[ZR_UTF_SIZE];
typedef union {void *ptr; int id;} zr_handle;
struct zr_image {zr_handle handle;unsigned short w,h;unsigned short region[4];};
struct zr_scroll {unsigned short x, y;};
enum zr_heading {ZR_UP, ZR_RIGHT, ZR_DOWN, ZR_LEFT};
/* math */
zr_hash zr_murmur_hash(const void *key, int len, zr_hash seed);
void zr_triangle_from_direction(struct zr_vec2 *result, struct zr_rect r,
float pad_x, float pad_y, enum zr_heading);
struct zr_vec2 zr_vec2(float x, float y);
struct zr_vec2 zr_vec2i(int x, int y);
struct zr_vec2 zr_vec2v(const float *xy);
struct zr_vec2 zr_vec2iv(const int *xy);
struct zr_rect zr_get_null_rect(void);
struct zr_rect zr_rect(float x, float y, float w, float h);
struct zr_rect zr_recti(int x, int y, int w, int h);
struct zr_rect zr_recta(struct zr_vec2 pos, struct zr_vec2 size);
struct zr_rect zr_rectv(const float *xywh);
struct zr_rect zr_rectiv(const int *xywh);
/* UTF-8 */
zr_size zr_utf_decode(const char*, zr_rune*, zr_size);
zr_size zr_utf_encode(zr_rune, char*, zr_size);
zr_size zr_utf_len(const char*, zr_size byte_len);
/* color (conversion user --> zahnrad) */
struct zr_color zr_rgb(int r, int g, int b);
struct zr_color zr_rgb_iv(const int *rgb);
struct zr_color zr_rgb_bv(const zr_byte* rgb);
struct zr_color zr_rgb_f(float r, float g, float b);
struct zr_color zr_rgb_fv(const float *rgb);
struct zr_color zr_rgb_hex(const char *rgb);
struct zr_color zr_rgba(int r, int g, int b, int a);
struct zr_color zr_rgba_u32(zr_uint);
struct zr_color zr_rgba_iv(const int *rgba);
struct zr_color zr_rgba_bv(const zr_byte *rgba);
struct zr_color zr_rgba_f(float r, float g, float b, float a);
struct zr_color zr_rgba_fv(const float *rgba);
struct zr_color zr_rgba_hex(const char *rgb);
struct zr_color zr_hsv(int h, int s, int v);
struct zr_color zr_hsv_iv(const int *hsv);
struct zr_color zr_hsv_bv(const zr_byte *hsv);
struct zr_color zr_hsv_f(float h, float s, float v);
struct zr_color zr_hsv_fv(const float *hsv);
struct zr_color zr_hsva(int h, int s, int v, int a);
struct zr_color zr_hsva_iv(const int *hsva);
struct zr_color zr_hsva_bv(const zr_byte *hsva);
struct zr_color zr_hsva_f(float h, float s, float v, float a);
struct zr_color zr_hsva_fv(const float *hsva);
/* color (conversion zahnrad --> user) */
void zr_color_f(float *r, float *g, float *b, float *a, struct zr_color);
void zr_color_fv(float *rgba_out, struct zr_color);
zr_uint zr_color_u32(struct zr_color);
void zr_color_hex_rgba(char *output, struct zr_color);
void zr_color_hex_rgb(char *output, struct zr_color);
void zr_color_hsv_i(int *out_h, int *out_s, int *out_v, struct zr_color);
void zr_color_hsv_b(zr_byte *out_h, zr_byte *out_s, zr_byte *out_v, struct zr_color);
void zr_color_hsv_iv(int *hsv_out, struct zr_color);
void zr_color_hsv_bv(zr_byte *hsv_out, struct zr_color);
void zr_color_hsv_f(float *out_h, float *out_s, float *out_v, struct zr_color);
void zr_color_hsv_fv(float *hsv_out, struct zr_color);
void zr_color_hsva_i(int *h, int *s, int *v, int *a, struct zr_color);
void zr_color_hsva_b(zr_byte *h, zr_byte *s, zr_byte *v, zr_byte *a, struct zr_color);
void zr_color_hsva_iv(int *hsva_out, struct zr_color);
void zr_color_hsva_bv(zr_byte *hsva_out, struct zr_color);
void zr_color_hsva_f(float *out_h, float *out_s, float *out_v,
float *out_a, struct zr_color);
void zr_color_hsva_fv(float *hsva_out, struct zr_color);
/* image */
zr_handle zr_handle_ptr(void*);
zr_handle zr_handle_id(int);
struct zr_image zr_image_ptr(void*);
struct zr_image zr_image_id(int);
int zr_image_is_subimage(const struct zr_image* img);
struct zr_image zr_subimage_ptr(void*, unsigned short w, unsigned short h,
struct zr_rect sub_region);
struct zr_image zr_subimage_id(int, unsigned short w, unsigned short h,
struct zr_rect sub_region);
/* ==============================================================
*
* MEMORY BUFFER
*
* ===============================================================*/
/* A basic (double)-buffer with linear allocation and resetting as only
freeing policy. The buffers main purpose is to control all memory management
inside the GUI toolkit and still leave memory control as much as possible in
the hand of the user. The memory is provided in three different ways.
The first way is to use a fixed size block of memory to be filled up.
Biggest advantage is a simple memory model. Downside is that if the buffer
is full there is no way to accesses more memory, which fits target
application with a GUI with roughly known memory consumptions.
The second way to manage memory is by extending the fixed size block by
querying information from the buffer about the used size and needed size and
allocate new memory if the buffer is full. While this approach is still
better than just using a fixed size memory block the reallocation still has
one invalid frame as consquence since the used memory information is only
available at the end of the frame which leads to the last way of handling
memory.
The last and most complicated way of handling memory is by allocator
callbacks. The user hereby registers callbacks to be called to allocate and
free memory if needed. While this solves most allocation problems it causes
some loss of flow control on the user side.
USAGE
----------------------------
To instantiate the buffer you either have to call the fixed size or
allocator initialization function and provide a memory block in the first
case and an allocator in the second case.
To allocate memory from the buffer you would call zr_buffer_alloc with a
request memory block size as well as an alignment for the block.
Finally to reset the memory at the end of the frame and when the memory
buffer inside the buffer is no longer needed you would call zr_buffer_reset.
To free all memory that has been allocated by an allocator if the buffer is
no longer being used you have to call zr_buffer_clear.
*/
struct zr_memory_status {
void *memory;
/* pointer to the currently used memory block inside the referenced buffer*/
unsigned int type;
/* type of the buffer which is either fixed size or dynamic */
zr_size size;
/* total size of the memory block */
zr_size allocated;
/* allocated amount of memory */
zr_size needed;
/* memory size that would have been allocated if enough memory was present*/
zr_size calls;
/* number of allocation calls referencing this buffer */
};
struct zr_allocator {
zr_handle userdata;
void*(*alloc)(zr_handle, zr_size);
void(*free)(zr_handle, void*);
};
enum zr_allocation_type {
ZR_BUFFER_FIXED,
ZR_BUFFER_DYNAMIC
};
enum zr_buffer_allocation_type {
ZR_BUFFER_FRONT,
ZR_BUFFER_BACK,
ZR_BUFFER_MAX
};
struct zr_buffer_marker {
int active;
zr_size offset;
};
struct zr_memory {void *ptr;zr_size size;};
struct zr_buffer {
struct zr_buffer_marker marker[ZR_BUFFER_MAX];
/* buffer marker to free a buffer to a certain offset */
struct zr_allocator pool;
/* allocator callback for dynamic buffers */
enum zr_allocation_type type;
/* memory management type */
struct zr_memory memory;
/* memory and size of the current memory block */
float grow_factor;
/* growing factor for dynamic memory management */
zr_size allocated;
/* total amount of memory allocated */
zr_size needed;
/* totally consumed memory given that enough memory is present */
zr_size calls;
/* number of allocation calls */
zr_size size;
/* current size of the buffer */
};
#if ZR_COMPILE_WITH_DEFAULT_ALLOCATOR
void zr_buffer_init_default(struct zr_buffer*);
#endif
void zr_buffer_init(struct zr_buffer*, const struct zr_allocator*, zr_size size);
void zr_buffer_init_fixed(struct zr_buffer*, void *memory, zr_size size);
void zr_buffer_info(struct zr_memory_status*, struct zr_buffer*);
void zr_buffer_free(struct zr_buffer*);
void *zr_buffer_memory(struct zr_buffer*);
const void *zr_buffer_memory_const(const struct zr_buffer*);
zr_size zr_buffer_total(struct zr_buffer*);
/* ===============================================================
*
* FONT
*
* ===============================================================*/
/* Font handling in this library can be achived in three different ways.
The first and simplest ways is by just using your font handling mechanism
and provide a simple callback for text string width calculation with
`zr_user_font`. This requires the default drawing output
and is not possible for the optional vertex buffer output.
The second way of font handling is by using the same `zr_user_font` struct
to referencing a font as before but providing a second callback for
`zr_user_font_glyph` querying which is used for text drawing in the optional
vertex buffer output. In addition to the callback it is also required to
provide a texture atlas from the font to draw.
The final and most complex way is to use the optional font baker
and font handling function, which requires two additional headers for
TTF font baking. While the previous two methods did no need any functions
outside callbacks and are therefore rather simple to handle, the final
font handling method is quite complex and you need to handle the complex
font baking API. The reason why it is complex is because there are multible
ways of using the API. For example it must be possible to use the font
for default command output as well as vertex buffer output. So for example
texture coordinates can either be UV for vertex buffer output or absolute
pixel for drawing function based on pixels. Furthermore it is possible to
incoperate custom user data into the resulting baked image (for example a
white pixel for the vertex buffer output).
In addition and probably the most complex aspect of the baking API was to
incoperate baking of multible fonts into one image.
In general the font baking API can be understood as having a number of
loaded in memory TTF-fonts, font baking configuration and optional custom
render data as input, while the output is made of font specific data, a big
glyph array of all baked glyphs and the baked image. The API
was designed that way to have a typical file format and not
a perfectly ready in memory library instance of a font. The reason is more
control and seperates the font baking code from the in library used font
format.
*/
typedef zr_size(*zr_text_width_f)(zr_handle, float h, const char*, zr_size len);
typedef void(*zr_query_font_glyph_f)(zr_handle handle, float font_height,
struct zr_user_font_glyph *glyph,
zr_rune codepoint, zr_rune next_codepoint);
#if ZR_COMPILE_WITH_VERTEX_BUFFER
struct zr_user_font_glyph {
struct zr_vec2 uv[2];
/* texture coordinates */
struct zr_vec2 offset;
/* offset between top left and glyph */
float width, height;
/* size of the glyph */
float xadvance;
/* offset to the next glyph */
};
#endif
struct zr_user_font {
zr_handle userdata;
/* user provided font handle */
float height;
/* max height of the font */
zr_text_width_f width;
/* font string width in pixel callback */
#if ZR_COMPILE_WITH_VERTEX_BUFFER
zr_query_font_glyph_f query;
/* font glyph callback to query drawing info */
zr_handle texture;
/* texture handle to the used font atlas or texture */
#endif
};
#ifdef ZR_COMPILE_WITH_FONT
enum zr_font_coord_type {
ZR_COORD_UV,
/* texture coordinates inside font glyphs are clamped between 0-1 */
ZR_COORD_PIXEL
/* texture coordinates inside font glyphs are in absolute pixel */
};
struct zr_baked_font {
float height;
/* height of the font */
float ascent, descent;
/* font glyphs ascent and descent */
zr_rune glyph_offset;
/* glyph array offset inside the font glyph baking output array */
zr_rune glyph_count;
/* number of glyphs of this font inside the glyph baking array output */
const zr_rune *ranges;
/* font codepoint ranges as pairs of (from/to) and 0 as last element */
};
struct zr_font_config {
void *ttf_blob;
/* pointer to loaded TTF file memory block.
* NOTE: not needed for zr_font_atlas_add_from_memory and zr_font_atlas_add_from_file. */
zr_size ttf_size;
/* size of the loaded TTF file memory block
* NOTE: not needed for zr_font_atlas_add_from_memory and zr_font_atlas_add_from_file. */
int ttf_data_owned_by_atlas;
/* used inside font atlas: default to: 0*/
float size;
/* baked pixel height of the font */
unsigned int oversample_h, oversample_v;
/* rasterize at hight quality for sub-pixel position */
int pixel_snap;
/* align very character to pixel boundry (if true set oversample (1,1)) */
enum zr_font_coord_type coord_type;
/* texture coordinate format with either pixel or UV coordinates */
struct zr_vec2 spacing;
/* extra pixel spacing between glyphs */
const zr_rune *range;
/* list of unicode ranges (2 values per range, zero terminated) */
struct zr_baked_font *font;
/* font to setup in the baking process: NOTE: not needed for font atlas */
zr_rune fallback_glyph;
/* fallback glyph to use if a given rune is not found */
};
struct zr_font_glyph {
zr_rune codepoint;
float xadvance;
float x0, y0, x1, y1, w, h;
float u0, v0, u1, v1;
};
struct zr_font {
struct zr_user_font handle;
float size;
float scale;
float ascent, descent;
struct zr_font_glyph *glyphs;
const struct zr_font_glyph *fallback;
zr_rune fallback_codepoint;
zr_rune glyph_count;
const zr_rune *ranges;
zr_handle texture;
int config;
};
enum zr_font_atlas_format {
ZR_FONT_ATLAS_ALPHA8,
ZR_FONT_ATLAS_RGBA32
};
struct zr_draw_null_texture {
zr_handle texture;
/* texture handle to a texture with a white pixel */
struct zr_vec2 uv;
/* coordinates to a white pixel in the texture */
};
struct zr_font_atlas {
void *pixel;
int tex_width;
int tex_height;
struct zr_allocator alloc;
struct zr_recti custom;
int glyph_count;
struct zr_font_glyph *glyphes;
struct zr_font **fonts;
struct zr_font_config *config;
int font_num, font_cap;
};
/* some language glyph codepoint ranges */
const zr_rune *zr_font_default_glyph_ranges(void);
const zr_rune *zr_font_chinese_glyph_ranges(void);
const zr_rune *zr_font_cyrillic_glyph_ranges(void);
const zr_rune *zr_font_korean_glyph_ranges(void);
/* Font baking (needs to be called sequentially top to bottom)
* --------------------------------------------------------------------
* This is a low level API to bake font glyphs into an image and is more
* complex than the atlas API but provides more control over the baking
* process with custom bake data and memory management. */
void zr_font_bake_memory(zr_size *temporary_memory, int *glyph_count,
struct zr_font_config*, int count);
int zr_font_bake_pack(zr_size *img_memory, int *img_width, int *img_height,
struct zr_recti *custom_space,
void *temporary_memory, zr_size temporary_size,
const struct zr_font_config*, int font_count);
void zr_font_bake(void *image_memory, int image_width, int image_height,
void *temporary_memory, zr_size temporary_memory_size,
struct zr_font_glyph*, int glyphs_count,
const struct zr_font_config*, int font_count);
void zr_font_bake_custom_data(void *img_memory, int img_width, int img_height,
struct zr_recti img_dst, const char *image_data_mask,
int tex_width, int tex_height,char white,char black);
void zr_font_bake_convert(void *out_memory, int image_width, int image_height,
const void *in_memory);
/* Font
* -----------------------------------------------------------------
* The font structure is just a simple container to hold the output of a baking
* process in the low level API and as output in the atlas font API.
* The actual font handle used inside the library only references the font
* which allows using custom user font handlers and can be generated by calling
* `zr_font_ref`. */
void zr_font_init(struct zr_font*, float pixel_height, zr_rune fallback_codepoint,
struct zr_font_glyph*, const struct zr_baked_font*,
zr_handle atlas);
const struct zr_font_glyph* zr_font_find_glyph(struct zr_font*, zr_rune unicode);
/* Font Atlas
* ---------------------------------------------------------------
* This is the high level font baking and handling API to generate an image
* out of font glyphes used to draw text onto the screen. This API takes away
* some control over the baking process like fine grained memory control and
* custom baking data but provides additional functionality and easier to
* use and manage datastructures and functions. */
#if ZR_COMPILE_WITH_DEFAULT_ALLOCATOR
void zr_font_atlas_init_default(struct zr_font_atlas*);
#endif
void zr_font_atlas_init(struct zr_font_atlas*, struct zr_allocator*);
void zr_font_atlas_begin(struct zr_font_atlas*);
struct zr_font_config zr_font_config(float pixel_height);
struct zr_font *zr_font_atlas_add(struct zr_font_atlas*, const struct zr_font_config*);
#if ZR_COMPILE_WITH_DEFAULT_FONT
struct zr_font* zr_font_atlas_add_default(struct zr_font_atlas*, float height,
const struct zr_font_config*);
#endif
struct zr_font* zr_font_atlas_add_from_memory(struct zr_font_atlas *atlas, void *memory,
zr_size size, float height,
const struct zr_font_config *config);
#if ZR_COMPILE_WITH_STANDARD_FILE_IO
struct zr_font* zr_font_atlas_add_from_file(struct zr_font_atlas *atlas,
const char *file_path, float height,
const struct zr_font_config*);
#endif
struct zr_font *zr_font_atlas_add_compressed(struct zr_font_atlas*,
void *memory, zr_size size, float height,
const struct zr_font_config*);
struct zr_font* zr_font_atlas_add_compressed_base85(struct zr_font_atlas *atlas,
const char *data, float height,
const struct zr_font_config *config);
const void* zr_font_atlas_bake(struct zr_font_atlas*, int *width, int *height,
enum zr_font_atlas_format);
void zr_font_atlas_end(struct zr_font_atlas*, zr_handle tex, struct zr_draw_null_texture*);
void zr_font_atlas_clear(struct zr_font_atlas*);
#endif
/*===============================================================
*
* EDIT BOX
*
* ===============================================================*/
typedef int(*zr_filter)(const struct zr_edit_box*, zr_rune unicode);
typedef void(*zr_paste_f)(zr_handle, struct zr_edit_box*);
typedef void(*zr_copy_f)(zr_handle, const char*, zr_size size);
enum zr_edit_remove_operation {
ZR_DELETE = 0,
ZR_REMOVE
};
enum zr_edit_flags {
ZR_EDIT_READ_ONLY = ZR_FLAG(0),
/* text inside the edit widget cannot be modified */
ZR_EDIT_CURSOR = ZR_FLAG(1),
/* edit widget will have a movable cursor */
ZR_EDIT_SELECTABLE = ZR_FLAG(2),
/* edit widget allows text selection */
ZR_EDIT_CLIPBOARD = ZR_FLAG(3),
/* edit widget tries to use the clipbard callback for copy & paste */
ZR_EDIT_SIGCOMIT = ZR_FLAG(4)
/* edit widget generateds ZR_EDIT_COMMITED event on enter */
};
enum zr_edit_types {
ZR_EDIT_SIMPLE = 0,
ZR_EDIT_FIELD = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|ZR_EDIT_CLIPBOARD),
ZR_EDIT_BOX = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE| ZR_EDIT_CLIPBOARD)
};
enum zr_edit_events {
ZR_EDIT_ACTIVE = ZR_FLAG(0),
/* edit widget is currently being modified */
ZR_EDIT_INACTIVE = ZR_FLAG(1),
/* edit widget is not active and is not being modified */
ZR_EDIT_ACTIVATED = ZR_FLAG(2),
/* edit widget went from state inactive to state active */
ZR_EDIT_DEACTIVATED = ZR_FLAG(3),
/* edit widget went from state active to state inactive */
ZR_EDIT_COMMITED = ZR_FLAG(4)
/* edit widget has received an enter and lost focus */
};
struct zr_text_selection {
int active;
zr_size begin;
zr_size end;
};
struct zr_clipboard {
zr_handle userdata;
zr_paste_f paste;
zr_copy_f copy;
};
struct zr_edit_box {
struct zr_buffer buffer;
int active;
zr_size cursor;
zr_size glyphs;
struct zr_clipboard clip;
zr_filter filter;
struct zr_text_selection sel;
float scrollbar;
int text_inserted;
};
/* filter function */
struct zr_edit_box;
int zr_filter_default(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_ascii(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_float(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_decimal(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_hex(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_oct(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_binary(const struct zr_edit_box*, zr_rune unicode);
/* editbox */
void zr_edit_box_clear(struct zr_edit_box*);
void zr_edit_box_add(struct zr_edit_box*, const char*, zr_size);
void zr_edit_box_remove(struct zr_edit_box*, enum zr_edit_remove_operation);
char *zr_edit_box_get(struct zr_edit_box*);
const char *zr_edit_box_get_const(const struct zr_edit_box*);
void zr_edit_box_at(struct zr_edit_box*, zr_size pos, zr_glyph, zr_size*);
void zr_edit_box_at_cursor(struct zr_edit_box*, zr_glyph, zr_size*);
char zr_edit_box_at_char(struct zr_edit_box*, zr_size pos);
void zr_edit_box_set_cursor(struct zr_edit_box*, zr_size pos);
zr_size zr_edit_box_get_cursor(struct zr_edit_box *eb);
zr_size zr_edit_box_len_char(struct zr_edit_box*);
zr_size zr_edit_box_len(struct zr_edit_box*);
int zr_edit_box_has_selection(const struct zr_edit_box*);
const char *zr_edit_box_get_selection(zr_size *len, struct zr_edit_box*);
/* ===============================================================
*
* DRAWING
*
* ===============================================================*/
/* This library was designed to be render backend agnostic so it does
not draw anything to the screen. Instead all drawn shapes, widgets
are made of, are buffered into memory and make up a command queue.
Each frame therefore fills the command buffer with draw commands
that then need to be executed by the user and his own render backend.
After that the command buffer needs to be cleared and a new frame can be
started.
The reason for buffering simple primitives as draw commands instead of
directly buffering a hardware accessible format with vertex and element
buffer was to support native render backends like X11 and Win32.
That being said it is possible to convert the command buffer into a
hardware accessible format to support hardware based rendering as well.
*/
enum zr_command_type {
ZR_COMMAND_NOP,
ZR_COMMAND_SCISSOR,
ZR_COMMAND_LINE,
ZR_COMMAND_CURVE,
ZR_COMMAND_RECT,
ZR_COMMAND_RECT_FILLED,
ZR_COMMAND_RECT_MULTI_COLOR,
ZR_COMMAND_CIRCLE,
ZR_COMMAND_CIRCLE_FILLED,
ZR_COMMAND_ARC,
ZR_COMMAND_ARC_FILLED,
ZR_COMMAND_TRIANGLE,
ZR_COMMAND_TRIANGLE_FILLED,
ZR_COMMAND_POLYGON,
ZR_COMMAND_POLYGON_FILLED,
ZR_COMMAND_POLYLINE,
ZR_COMMAND_TEXT,
ZR_COMMAND_IMAGE
};
/* command base and header of every comand inside the buffer */
struct zr_command {
enum zr_command_type type;
zr_size next;
#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_handle userdata;
#endif
};
struct zr_command_scissor {
struct zr_command header;
short x, y;
unsigned short w, h;
};
struct zr_command_line {
struct zr_command header;
unsigned short line_thickness;
struct zr_vec2i begin;
struct zr_vec2i end;
struct zr_color color;
};
struct zr_command_curve {
struct zr_command header;
unsigned short line_thickness;
struct zr_vec2i begin;
struct zr_vec2i end;
struct zr_vec2i ctrl[2];
struct zr_color color;
};
struct zr_command_rect {
struct zr_command header;
unsigned short rounding;
unsigned short line_thickness;
short x, y;
unsigned short w, h;
struct zr_color color;
};
struct zr_command_rect_filled {
struct zr_command header;
unsigned short rounding;
short x, y;
unsigned short w, h;
struct zr_color color;
};
struct zr_command_rect_multi_color {
struct zr_command header;
short x, y;
unsigned short w, h;
struct zr_color left;
struct zr_color top;
struct zr_color bottom;
struct zr_color right;
};
struct zr_command_triangle {
struct zr_command header;
unsigned short line_thickness;
struct zr_vec2i a;
struct zr_vec2i b;
struct zr_vec2i c;
struct zr_color color;
};
struct zr_command_triangle_filled {
struct zr_command header;
struct zr_vec2i a;
struct zr_vec2i b;
struct zr_vec2i c;
struct zr_color color;
};
struct zr_command_circle {
struct zr_command header;
short x, y;
unsigned short line_thickness;
unsigned short w, h;
struct zr_color color;
};
struct zr_command_circle_filled {
struct zr_command header;
short x, y;
unsigned short w, h;
struct zr_color color;
};
struct zr_command_arc {
struct zr_command header;
short cx, cy;
unsigned short r;
unsigned short line_thickness;
float a[2];
struct zr_color color;
};
struct zr_command_arc_filled {
struct zr_command header;
short cx, cy;
unsigned short r;
float a[2];
struct zr_color color;
};
struct zr_command_polygon {
struct zr_command header;
struct zr_color color;
unsigned short line_thickness;
unsigned short point_count;
struct zr_vec2i points[1];
};
struct zr_command_polygon_filled {
struct zr_command header;
struct zr_color color;
unsigned short point_count;
struct zr_vec2i points[1];
};
struct zr_command_polyline {
struct zr_command header;
struct zr_color color;
unsigned short line_thickness;
unsigned short point_count;
struct zr_vec2i points[1];
};
struct zr_command_image {
struct zr_command header;
short x, y;
unsigned short w, h;
struct zr_image img;
};
struct zr_command_text {
struct zr_command header;
const struct zr_user_font *font;
struct zr_color background;
struct zr_color foreground;
short x, y;
unsigned short w, h;
float height;
zr_size length;
char string[1];
};
enum zr_command_clipping {
ZR_CLIPPING_OFF = zr_false,
ZR_CLIPPING_ON = zr_true
};
struct zr_command_buffer {
struct zr_buffer *base;
struct zr_rect clip;
int use_clipping;
zr_handle userdata;
zr_size begin, end, last;
};
#if ZR_COMPILE_WITH_VERTEX_BUFFER
typedef unsigned short zr_draw_index;
typedef zr_uint zr_draw_vertex_color;
enum zr_anti_aliasing {
ZR_ANTI_ALIASING_OFF = zr_false,
ZR_ANTI_ALIASING_ON
};
struct zr_draw_vertex {
struct zr_vec2 position;
struct zr_vec2 uv;
zr_draw_vertex_color col;
};
#endif
struct zr_draw_command {
unsigned int elem_count;
/* number of elements in the current draw batch */
struct zr_rect clip_rect;
/* current screen clipping rectangle */
zr_handle texture;
/* current texture to set */
#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_handle userdata;
#endif
};
/* shape outlines */
void zr_push_scissor(struct zr_command_buffer*, struct zr_rect);
void zr_stroke_line(struct zr_command_buffer *b, float x0, float y0,
float x1, float y1, float line_thickness, struct zr_color);
void zr_stroke_curve(struct zr_command_buffer*, float, float, float, float,
float, float, float, float, float line_thickness, struct zr_color);
void zr_stroke_rect(struct zr_command_buffer*, struct zr_rect, float rounding,
float line_thickness, struct zr_color);
void zr_stroke_circle(struct zr_command_buffer*, struct zr_rect, float line_thickness,
struct zr_color);
void zr_stroke_arc(struct zr_command_buffer*, float cx, float cy, float radius,
float a_min, float a_max, float line_thickness, struct zr_color);
void zr_stroke_triangle(struct zr_command_buffer*, float, float, float, float,
float, float, float line_thichness, struct zr_color);
void zr_stroke_polyline(struct zr_command_buffer*, float *points, int point_count,
struct zr_color col);
void zr_stroke_polygon(struct zr_command_buffer*, float*, int point_count,
float line_thickness, struct zr_color);
/* filled shades */
void zr_fill_rect(struct zr_command_buffer*, struct zr_rect, float rounding,
struct zr_color);
void zr_fill_rect_multi_color(struct zr_command_buffer*, struct zr_rect,
struct zr_color left, struct zr_color top,
struct zr_color right, struct zr_color bottom);
void zr_fill_circle(struct zr_command_buffer*, struct zr_rect, struct zr_color);
void zr_fill_arc(struct zr_command_buffer*, float cx, float cy, float radius,
float a_min, float a_max, struct zr_color);
void zr_fill_triangle(struct zr_command_buffer*, float x0, float y0,
float x1, float y1, float x2, float y2, struct zr_color);
void zr_fill_polygon(struct zr_command_buffer*, float*, int point_count, struct zr_color);
void zr_draw_image(struct zr_command_buffer*, struct zr_rect, const struct zr_image*);
void zr_draw_text(struct zr_command_buffer*, struct zr_rect,
const char *text, zr_size len, const struct zr_user_font*,
struct zr_color, struct zr_color);
/* ===============================================================
*
* GUI
*
* ===============================================================*/
enum zr_keys {
ZR_KEY_SHIFT,
ZR_KEY_DEL,
ZR_KEY_ENTER,
ZR_KEY_TAB,
ZR_KEY_BACKSPACE,
ZR_KEY_COPY,
ZR_KEY_CUT,
ZR_KEY_PASTE,
ZR_KEY_UP,
ZR_KEY_DOWN,
ZR_KEY_LEFT,
ZR_KEY_RIGHT,
ZR_KEY_MAX
};
enum zr_buttons {
ZR_BUTTON_LEFT,
ZR_BUTTON_MIDDLE,
ZR_BUTTON_RIGHT,
ZR_BUTTON_MAX
};
struct zr_mouse_button {
int down;
unsigned int clicked;
struct zr_vec2 clicked_pos;
};
struct zr_mouse {
struct zr_mouse_button buttons[ZR_BUTTON_MAX];
struct zr_vec2 pos;
struct zr_vec2 prev;
struct zr_vec2 delta;
float scroll_delta;
};
struct zr_key {