/
gtxt_layout.c
887 lines (781 loc) · 19.3 KB
/
gtxt_layout.c
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
#include "gtxt_layout.h"
#include "gtxt_glyph.h"
#include "gtxt_label.h"
#include "gtxt_richtext.h"
#include <ds_array.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define INIT_ROW_CAP 4
#define INIT_GLYPH_CAP 16
#define OMIT_UNICODE 46
#define OMIT_COUNT 3
#define CONNECT_UNICODE 45
#define MAX_ROW_CONDENSE 0.10f
static bool ENABLE_HORI_OFFSET = true;
struct glyph {
int unicode;
float x, y;
float w, h;
float out_width;
struct glyph* next;
};
struct row {
float width;
float height;
float ymax, ymin;
struct glyph *head, *tail;
int glyph_count;
float offset;
struct row* next;
};
enum CONNECTED_GLYPH_TYPE
{
CGT_NULL = 0,
CGT_NUMBER,
CGT_LETTER,
};
struct layout {
const struct gtxt_label_style* style;
struct row* head;
int row_count;
struct glyph* glyph_freelist;
size_t glyph_cap;
struct row* row_freelist;
size_t row_cap;
float prev_tot_h;
float offset_vert;
struct glyph* prev_single_glyph;
enum CONNECTED_GLYPH_TYPE connected_glyph_type;
struct glyph *prev_glyph, *prev_prev_glyph;
struct row* curr_row;
};
static struct layout L;
void
gtxt_layout_release() {
free(L.glyph_freelist);
free(L.row_freelist);
memset(&L, 0, sizeof(L));
}
static inline void
_prepare_glyph_freelist(int cap) {
if ((size_t)cap <= L.glyph_cap || cap <= 0) {
return;
}
int new_num = cap - L.glyph_cap;
int sz = sizeof(struct glyph) * new_num;
struct glyph* new_list = (struct glyph*)malloc(sz);
if (!new_list) {
printf("malloc fail, gtxt_layout _prepare_glyph_freelist \n");
exit(1);
}
memset(new_list, 0, sz);
for (int i = 0; i < new_num - 1; ++i) {
new_list[i].next = &new_list[i+1];
}
new_list[new_num - 1].next = NULL;
if (L.glyph_freelist) {
struct glyph* end = L.glyph_freelist;
while (end->next) {
end = end->next;
}
end->next = new_list;
} else {
L.glyph_freelist = new_list;
}
L.glyph_cap = cap;
}
static inline void
_prepare_row_freelist(int cap) {
if ((size_t)cap <= L.row_cap || cap <= 0) {
return;
}
int new_num = cap - L.row_cap;
int sz = sizeof(struct row) * new_num;
struct row* new_list = (struct row*)malloc(sz);
if (!new_list) {
printf("malloc fail, gtxt_layout _prepare_row_freelist \n");
exit(1);
}
memset(new_list, 0, sz);
for (int i = 0; i < new_num - 1; ++i) {
new_list[i].next = &new_list[i+1];
}
new_list[new_num - 1].next = NULL;
if (L.row_freelist) {
struct row* end = L.row_freelist;
while (end->next) {
end = end->next;
}
end->next = new_list;
} else {
L.row_freelist = new_list;
}
L.row_cap = cap;
}
// #define PREPARE_FREELIST(type, cap) do { \
// if ((cap) <= L.##type##_cap || (cap) <= 0) { \
// break; \
// } \
// \
// int new_num = (cap) - L.##type##_cap; \
// int sz = sizeof(struct type) * new_num; \
// struct type * new_list = (struct type *)malloc(sz); \
// if (!new_list) { \
// printf("malloc fail, gtxt_layout _prepare_##type##_freelist \n"); \
// exit(1); \
// } \
// memset(new_list, 0, sz); \
// for (int i = 0; i < new_num - 1; ++i) { \
// new_list[i].next = &new_list[i+1]; \
// } \
// new_list[new_num - 1].next = NULL; \
// \
// if (L.##type##_freelist) { \
// struct type * end = L.##type##_freelist; \
// while (end->next) { \
// end = end->next; \
// } \
// end->next = new_list; \
// } else { \
// L.##type##_freelist = new_list; \
// } \
// \
// L.##type##_cap = (cap); \
// } while (0)
static inline void
_prepare_freelist(int row_cap, int glyph_cap) {
_prepare_row_freelist(row_cap);
_prepare_glyph_freelist(glyph_cap);
}
static inline struct row*
_new_row() {
if (!L.row_freelist) {
assert(L.row_cap > 0);
_prepare_row_freelist(L.row_cap * 2);
return _new_row();
}
struct row* r = L.row_freelist;
assert(r);
L.row_freelist = r->next;
r->next = NULL;
return r;
}
void
gtxt_layout_begin(const struct gtxt_label_style* style) {
_prepare_freelist(INIT_ROW_CAP, INIT_GLYPH_CAP);
L.style = style;
L.prev_tot_h = 0;
L.offset_vert = 0;
L.prev_single_glyph = NULL;
L.connected_glyph_type = CGT_NULL;
L.prev_glyph = NULL;
L.prev_prev_glyph = NULL;
L.curr_row = _new_row();
L.head = L.curr_row;
L.row_count = 1;
}
void
gtxt_layout_end() {
struct row* last_row = L.head;
while (last_row->next) {
last_row = last_row->next;
}
last_row->next = L.row_freelist;
L.row_freelist = L.head;
struct glyph* freelist = L.glyph_freelist;
L.glyph_freelist = L.head->head;
struct glyph* last_tail = NULL;
struct row* r = L.head;
while (r) {
if (last_tail && r->head) {
last_tail->next = r->head;
}
if (r->tail) {
last_tail = r->tail;
}
r->width = r->height = 0;
r->ymax = r->ymin = 0;
r->head = NULL;
r->tail = NULL;
r->glyph_count = 0;
r->offset = 0;
r = r->next;
}
if (last_tail) {
last_tail->next = freelist;
}
if (!L.glyph_freelist) {
L.glyph_freelist = freelist;
}
}
static inline struct glyph*
_new_glyph() {
if (!L.glyph_freelist) {
assert(L.glyph_cap > 0);
_prepare_glyph_freelist(L.glyph_cap * 2);
return _new_glyph();
}
struct glyph* g = L.glyph_freelist;
assert(g);
L.glyph_freelist = g->next;
g->next = NULL;
return g;
}
static inline bool
_new_line() {
L.prev_single_glyph = NULL;
L.connected_glyph_type = CGT_NULL;
L.prev_glyph = NULL;
L.prev_prev_glyph = NULL;
float h = L.curr_row->height * L.style->space_v;
L.prev_tot_h += h;
// over label height
float tot_h = L.prev_tot_h + L.curr_row->height;
if (tot_h > L.style->height) {
switch (L.style->over_label)
{
case OL_OVERFLOW:
break;
case OL_CUT_OFF:
L.prev_tot_h -= h;
return false;
break;
case OL_CONDENSE:
L.offset_vert = tot_h - L.style->height;
break;
}
}
struct row* prev = L.curr_row;
L.curr_row = _new_row();
// no free row
if (!L.curr_row) {
return false;
}
++L.row_count;
prev->next = L.curr_row;
L.curr_row->next = NULL;
return true;
}
static inline void
_add_glyph(struct glyph* g) {
if (!L.curr_row->head) {
assert(!L.curr_row->tail);
L.curr_row->head = L.curr_row->tail = g;
} else {
L.curr_row->tail->next = g;
L.curr_row->tail = g;
}
++L.curr_row->glyph_count;
}
static inline bool
_is_letter(int unicode) {
return
(unicode >= 97 && unicode <= 122) || // a-z
(unicode >= 65 && unicode <= 90); // A-Z
}
static inline bool
_is_number(int unicode) {
return
(unicode >= 48 && unicode <= 57) || // 0-9
unicode == 46 || // .
unicode == 37 || // %
unicode == 45 || // -
unicode == 43; // +
}
static inline enum CONNECTED_GLYPH_TYPE
_get_connected_glyph_type(int unicode) {
if (_is_number(unicode)) {
return CGT_NUMBER;
} else if (_is_letter(unicode)) {
return CGT_LETTER;
} else {
return CGT_NULL;
}
}
static inline bool
_is_punctuation(int unicode) {
return
unicode == 65292 || // ,
unicode == 12290 || // 。
unicode == 65311 || // ?
unicode == 8220 || // “
unicode == 8221 || // ”
unicode == 65281 || // !
unicode == 65307 || // ;
unicode == 65306 || // :
unicode == 8216 || //‘
unicode == 8217 || // ’
unicode == 12289 || // 、
unicode == 65289 || // )
unicode == 12305 || // 】
unicode == 12303 || // 』
unicode == 12299 || // 》
unicode == 44 || // ,
unicode == 46 || // .
unicode == 63 || // ?
unicode == 39 || // '
unicode == 34 || // "
unicode == 33 || // !
unicode == 59 || // ;
unicode == 58 || // :
unicode == 39 || // '
unicode == 41 || // )
unicode == 93 || // ]
unicode == 125 ; // }
}
static int
_remove_glyph_to_end(struct glyph* g) {
int count = 0;
struct glyph* curr = g;
while (curr) {
++count;
struct glyph* next = curr->next;
curr->next = L.glyph_freelist;
L.glyph_freelist = curr;
curr = next;
}
return count;
}
static enum GLO_STATUS
_handle_new_line_force(struct gtxt_glyph_layout* g_layout) {
if (L.curr_row->height == 0) {
L.curr_row->height = g_layout->metrics_height;
}
if (!_new_line()) {
return GLOS_FULL;
} else {
return GLOS_NEWLINE;
}
}
static enum GLO_STATUS
_new_line_for_common(float w, float max_condense) {
float offset = L.style->width - L.curr_row->width - w;
float room = L.style->width - L.curr_row->width;
if (room > w * 0.5f && -offset / L.style->width < max_condense) {
L.curr_row->offset = offset;
} else {
L.curr_row->offset = L.style->width - L.curr_row->width;
if (!_new_line()) {
return GLOS_FULL;
}
}
return GLOS_NORMAL;
}
static enum GLO_STATUS
_new_line_directly(float w, float max_condense) {
if (!_new_line()) {
return GLOS_FULL;
}
return GLOS_NORMAL;
}
static inline struct glyph*
_row_backtracking(struct row* row, float remain) {
float w = 0;
struct glyph* curr = row->head;
struct glyph* prev = NULL;
while (prev != row->tail) {
w += curr->out_width;
if (w > remain) {
return prev;
}
prev = curr;
curr = curr->next;
}
return prev;
}
static enum GLO_STATUS
_add_connected_sym(struct gtxt_richtext_style* style, const struct gtxt_glyph_style* gs) {
struct gtxt_glyph_layout* layout = gtxt_glyph_get_layout(CONNECT_UNICODE, 0, gs);
float conn_w = layout->advance * L.style->space_h;
if (conn_w > L.style->width) {
return GLOS_FULL;
}
struct glyph* prev_glyph = _row_backtracking(L.curr_row, L.style->width - conn_w);
if (!prev_glyph) {
return GLOS_FULL;
}
struct glyph* next_glyph = prev_glyph->next;
struct row* prev_row = L.curr_row;
struct glyph* g = prev_glyph->next;
while (g) {
prev_row->width -= g->out_width;
--prev_row->glyph_count;
g = g->next;
}
prev_row->offset = 0;
prev_row->tail = prev_glyph;
prev_glyph->next = NULL;
gtxt_layout_single(CONNECT_UNICODE, 0, NULL);
prev_row->offset = L.style->width - prev_row->width;
if (!_new_line()) {
_remove_glyph_to_end(next_glyph);
return GLOS_FULL;
} else {
struct glyph* g = next_glyph;
while (g) {
gtxt_layout_single(g->unicode, 0, style);
g = g->next;
}
_remove_glyph_to_end(next_glyph);
return GLOS_CONNECTION;
}
}
static enum GLO_STATUS
_move_chars2nextline(float line_x, struct gtxt_richtext_style* style, struct glyph* prev_glyph) {
struct row* prev_row = L.curr_row;
struct glyph* g = prev_glyph->next;
while (g) {
prev_row->width -= g->out_width;
--prev_row->glyph_count;
g = g->next;
}
prev_row->offset = L.style->width - prev_row->width;
prev_row->tail = prev_glyph;
if (!_new_line()) {
_remove_glyph_to_end(prev_glyph->next);
prev_glyph->next = NULL;
return GLOS_FULL;
} else {
struct glyph* g = prev_glyph->next;
while (g) {
gtxt_layout_single(g->unicode, line_x, style);
g = g->next;
}
_remove_glyph_to_end(prev_glyph->next);
prev_glyph->next = NULL;
return GLOS_NORMAL;
}
}
static enum GLO_STATUS
_new_line_for_connected(float line_x, struct gtxt_richtext_style* style, const struct gtxt_glyph_style* gs, float w) {
L.curr_row->offset = L.style->width - L.curr_row->width - w;
if (-L.curr_row->offset / L.style->width <= MAX_ROW_CONDENSE) {
return GLOS_NORMAL;
}
if (!L.prev_single_glyph) {
return _add_connected_sym(style, gs);
} else {
return _move_chars2nextline(line_x, style, L.prev_single_glyph);
}
}
static enum GLO_STATUS
_new_line_for_punctuation(float line_x, struct gtxt_richtext_style* style, const struct gtxt_glyph_style* gs, float w) {
L.curr_row->offset = L.style->width - L.curr_row->width - w;
if (-L.curr_row->offset / L.style->width <= MAX_ROW_CONDENSE) {
return GLOS_NORMAL;
}
if (!L.prev_prev_glyph) {
L.curr_row->offset = L.style->width - L.curr_row->width;
if (!_new_line()) {
return GLOS_FULL;
} else {
return GLOS_NORMAL;
}
} else {
return _move_chars2nextline(line_x, style, L.prev_prev_glyph);
}
}
static enum GLO_STATUS
_handle_new_line_too_long(int unicode, float line_x, struct gtxt_richtext_style* style, const struct gtxt_glyph_style* gs, struct gtxt_glyph_layout* g_layout, float w) {
if (L.curr_row->height == 0) {
L.curr_row->height = g_layout->metrics_height;
}
if (_is_punctuation(unicode)) {
return _new_line_for_punctuation(line_x, style, gs, w);
} else if (_get_connected_glyph_type(unicode) == L.connected_glyph_type &&
L.connected_glyph_type != CGT_NULL) {
return _new_line_for_connected(line_x, style, gs, w);
} else {
return _new_line_for_common(w, MAX_ROW_CONDENSE);
}
return GLOS_NORMAL;
}
static enum GLO_STATUS
_handle_new_line(int unicode, float line_x, struct gtxt_richtext_style* style, const struct gtxt_glyph_style* gs, struct gtxt_glyph_layout* g_layout, float w) {
if (unicode == '\n') {
return _handle_new_line_force(g_layout);
} else if (L.curr_row->width + w + L.curr_row->offset > L.style->width) {
return _handle_new_line_too_long(unicode, line_x, style, gs, g_layout, w);
} else {
return GLOS_NORMAL;
}
}
enum GLO_STATUS
gtxt_layout_single(int unicode, float line_x, struct gtxt_richtext_style* style) {
const struct gtxt_glyph_style* gs;
if (style) {
gs = &style->gs;
} else {
gs = &L.style->gs;
}
struct gtxt_glyph_layout* g_layout = gtxt_glyph_get_layout(unicode, line_x, gs);
if (!g_layout) {
return GLOS_NORMAL;
}
float w = g_layout->advance * L.style->space_h;
enum GLO_STATUS status = _handle_new_line(unicode, line_x, style, gs, g_layout, w);
if (status == GLOS_NEWLINE || status == GLOS_FULL) {
return status;
}
struct glyph* g = _new_glyph();
assert(g);
g->unicode = unicode;
g->x = g_layout->bearing_x;
g->y = g_layout->bearing_y;
g->w = g_layout->sizer.width;
g->h = g_layout->sizer.height;
g->out_width = w;
if (g_layout->metrics_height > L.curr_row->height) {
L.curr_row->height = g_layout->metrics_height;
}
if (g_layout->bearing_y > L.curr_row->ymax) {
L.curr_row->ymax = g_layout->bearing_y;
}
if (g_layout->bearing_y - g_layout->sizer.height < L.curr_row->ymin) {
L.curr_row->ymin = g_layout->bearing_y - g_layout->sizer.height;
}
L.curr_row->width += w;
_add_glyph(g);
L.connected_glyph_type = _get_connected_glyph_type(unicode);
if (L.connected_glyph_type == CGT_NULL) {
L.prev_single_glyph = g;
}
L.prev_prev_glyph = L.prev_glyph;
L.prev_glyph = g;
return status;
}
void
gtxt_layout_multi(struct ds_array* unicodes) {
int glyph_sz = ds_array_size(unicodes);
_prepare_glyph_freelist(glyph_sz * 2);
for (int i = 0; i < glyph_sz; ++i) {
int unicode = *(int*)ds_array_fetch(unicodes, i);
enum GLO_STATUS status = gtxt_layout_single(unicode, 0, NULL);
if (status == GLOS_FULL) {
gtxt_layout_add_omit_sym(&L.style->gs);
break;
}
}
}
static inline int
_row_make_room(struct row* row, float remain) {
int count = 0;
float w = 0;
struct glyph* curr = row->head;
struct glyph* prev = NULL;
while (prev != row->tail) {
w += curr->out_width;
if (w > remain) {
row->width = w - curr->out_width;
if (prev) {
count = _remove_glyph_to_end(curr);
prev->next = NULL;
row->tail = prev;
}
break;
}
prev = curr;
curr = curr->next;
}
return count;
}
int
gtxt_layout_add_omit_sym(const struct gtxt_glyph_style* gs) {
struct gtxt_glyph_layout* layout = gtxt_glyph_get_layout(OMIT_UNICODE, 0, gs);
float omit_w = layout->advance * L.style->space_h * OMIT_COUNT;
if (omit_w > L.style->width) {
return 0;
}
float max_w = L.style->width - omit_w;
struct row* row = L.curr_row;
int rm_count = _row_make_room(row, max_w);
for (int i = 0; i < OMIT_COUNT; ++i) {
gtxt_layout_single(OMIT_UNICODE, 0, NULL);
}
row->width += omit_w;
row->glyph_count += OMIT_COUNT - rm_count;
return OMIT_COUNT - rm_count;
}
enum GLO_STATUS
gtxt_layout_ext_sym(int width, int height) {
if (L.curr_row->width + width > L.style->width) {
if (!_new_line()) {
return GLOS_FULL;
}
}
if (height > L.curr_row->height) {
L.curr_row->height = (float)height;
}
if (height > L.curr_row->ymax) {
L.curr_row->ymax = (float)height;
}
L.curr_row->width += width;
struct glyph* g = _new_glyph();
assert(g);
g->unicode = -1;
g->x = 0;
g->y = (float)height;
g->w = (float)width;
g->h = (float)height;
g->out_width = (float)width;
_add_glyph(g);
L.connected_glyph_type = CGT_NULL;
L.prev_single_glyph = g;
L.prev_prev_glyph = L.prev_glyph;
L.prev_glyph = g;
return GLOS_NORMAL;
}
static float
_get_start_x(float line_width) {
float x = 0;
switch (L.style->align_h) {
case HA_LEFT: case HA_AUTO: case HA_TILE:
x = -L.style->width * 0.5f;
break;
case HA_RIGHT:
x = L.style->width * 0.5f - line_width;
break;
case HA_CENTER:
x = -line_width * 0.5f;
break;
}
return x;
}
static float
_get_tot_height() {
float h = 0;
if (L.head->next) {
h = L.prev_tot_h + L.curr_row->height;
} else {
assert(L.curr_row == L.head);
h = L.head->ymax - L.head->ymin;
}
return h;
}
static float
_get_start_y() {
float y;
float tot_h = _get_tot_height();
if (L.head->next) {
switch (L.style->align_v) {
case VA_TOP: case VA_AUTO:
y = L.style->height * 0.5f - L.head->height * L.style->space_v;
break;
case VA_BOTTOM:
y = -L.style->height * 0.5f + tot_h - L.head->height * L.style->space_v;
break;
case VA_CENTER:
y = tot_h * 0.5f - L.head->height * L.style->space_v;
break;
case VA_TILE:
y = L.style->height * 0.5f;
break;
default:
assert(0);
}
} else {
struct row* r = L.head;
switch (L.style->align_v) {
case VA_TOP: case VA_AUTO: case VA_TILE:
y = L.style->height * 0.5f - r->ymax;
break;
case VA_BOTTOM:
y = -L.style->height * 0.5f - r->ymin;
break;
case VA_CENTER:
y = -tot_h * 0.5f - r->ymin;
break;
default:
assert(0);
}
}
return y;
}
static void
_layout_traverse_hori(struct row* r, float y, void (*cb)(int unicode, float x, float y, float w, float h, float row_y, float start_x, void* ud), void* ud) {
if (r->glyph_count == 0) {
assert(!r->head && !r->tail);
return;
}
float row_offset = r->offset;
// only one char
if (r->head && !r->head->next) {
row_offset = 0;
}
float dx = row_offset / r->glyph_count;
if (L.style->align_h == HA_TILE) {
float grid = (float)L.style->width / r->glyph_count;
float start_x = _get_start_x(r->width + row_offset) + grid * 0.5f;
float x = start_x;
struct glyph* g = r->head;
while (g) {
if (L.style->align_v == VA_TILE) {
cb(g->unicode, x, y, g->w, g->h, y, start_x, ud);
} else {
cb(g->unicode, x, y + g->y - g->h * 0.5f, g->w, g->h, y, start_x, ud);
}
x += grid + dx;
g = g->next;
}
} else {
float start_x = _get_start_x(r->width + row_offset);
float x = start_x;
struct glyph* g = r->head;
while (g) {
if (L.style->align_v == VA_TILE) {
cb(g->unicode, x + g->x + g->w * 0.5f, y, g->w, g->h, y, start_x, ud);
} else {
cb(g->unicode, x + g->x + g->w * 0.5f, y + g->y - g->h * 0.5f, g->w, g->h, y, start_x, ud);
}
x += g->out_width + dx;
g = g->next;
}
}
}
void
gtxt_layout_traverse(void (*cb)(int unicode, float x, float y, float w, float h, float row_y, float start_x, void* ud), void* ud) {
if (L.row_count == 0) {
assert(!L.head);
return;
}
float dy = 0;
if (L.row_count > 1) {
dy = L.offset_vert / L.row_count;
}
if (L.style->align_v == VA_TILE) {
float grid = (float)L.style->height / L.row_count;
float y = _get_start_y() - grid * 0.5f;
struct row* r = L.head;
while (r) {
y += dy;
_layout_traverse_hori(r, y, cb, ud);
y -= grid;
r = r->next;
}
} else {
float y = _get_start_y();
struct row* r = L.head;
while (r) {
y += dy;
_layout_traverse_hori(r, y, cb, ud);
y -= r->height * L.style->space_v;
r = r->next;
}
}
}
void
gtxt_get_layout_size(float* width, float* height) {
*width = 0;
struct row* r = L.head;
while (r) {
*width = MAX(*width, r->width + r->offset);
r = r->next;
}
*height = _get_tot_height();
}
void
gtxt_layout_enable_hori_offset(bool enable) {
ENABLE_HORI_OFFSET = enable;
}