-
Notifications
You must be signed in to change notification settings - Fork 2
/
sttFont.h
2432 lines (2270 loc) · 110 KB
/
sttFont.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
// This file is autogenerated. See look at the .lzz files in the src/ directory for a more human-friendly version
// sttFont.hh
//
#ifndef LZZ_sttFont_hh
#define LZZ_sttFont_hh
#ifndef STTR_ENABLED
namespace sttr {
template<typename T> inline const char * getTypeName() { return "sttr_not_defined"; }
template<typename T> inline char * getTypeSignature() { return (char*) 0; }
}
#endif
#ifndef STB_TRUETYPE_INCLUDE_HANDLED
////////////////////////////////////////
// STB TRUETYPE
// Handle double-including
#ifdef STB_TRUETYPE_IMPLEMENTATION
#undef STB_TRUETYPE_IMPLEMENTATION
#endif
#include "stb_truetype.h"
////////////////////////////////////////
#endif
// Defines
#ifndef SSF_MAP
#include <map>
#define SSF_MAP std::map
#endif
#ifndef SSF_VECTOR
#include <vector>
#define SSF_VECTOR std::vector
#endif
#ifndef SSF_STRING
#include <string>
#define SSF_STRING std::string
#endif
#ifndef SSF_STRING_VIEW
#include <string_view>
#define SSF_STRING_VIEW std::string_view
#endif
#ifndef SSF_ATOMIC_INT
#include <atomic>
#define SSF_ATOMIC_INT std::atomic<int>
#endif
// new and delete macros
// all calls in this library are done with "foo * f = SSF_NEW(f)"
// implement your custom allocator by defining SSF_NEW and SSF_DEL
#ifndef SSF_NEW
#define SSF_NEW(X) new X
#endif
#ifndef SSF_NEW_ARR
#define SSF_NEW_ARR(X,S) new X[S]
#endif
#ifndef SSF_DEL
#define SSF_DEL(X) delete X
#endif
#ifndef SSF_DEL_ARR
#define SSF_DEL_ARR(X) delete[] X
#endif
struct sttfont_uintQuad {
uint32_t first, second, third, fourth;
inline sttfont_uintQuad() : first(0), second(0), third(0), fourth(0) {}
inline sttfont_uintQuad(uint32_t a, uint32_t b = 0, uint32_t c = 0, uint32_t d = 0) : first(a), second(b), third(c), fourth(d) {}
};
// workaround for temp arrays not being a thing on msvc
// used for stack allocated temporary arrays in function or block scope
template <typename T, int S>
struct sttfont_tmpArr {
T stackBuff[S];
T* heapBuff;
T* arr;
inline void reserve(const uint64_t sz) {
if (heapBuff) abort();
if (sz > S) {
heapBuff = SSF_NEW_ARR(T, sz);
arr = heapBuff;
}
}
inline sttfont_tmpArr() : heapBuff(NULL), arr(&stackBuff[0]) {}
inline sttfont_tmpArr(const uint64_t sz) : heapBuff(NULL), arr(&stackBuff[0]) { reserve(sz); }
inline ~sttfont_tmpArr() { if (heapBuff) SSF_DEL_ARR(heapBuff); }
};
#include <cstdint>
// move semantics - makes lzz happy
#define SSF_STRING_MS SSF_STRING&&
#define sttfont_formatted_text_item_MS sttfont_formatted_text_item&&
#define sttfont_formatted_text_MS sttfont_formatted_text&&
// misc
#ifdef INT32_MIN
#define SSF_INT_MIN INT32_MIN
#else
#define SSF_INT_MIN 0x8000000;
#endif
struct sttfont_formatted_text;
class sttfont_font_cache;
namespace stt {
struct allocatorI;
}
// purpose: an allocator that puts string objects onto a stack memory buffer
// when the buffer falls out of scope then the buffer is released
#ifndef SSF_STT_STL_ALLOCATOR_ENABLED
#define SSF_STT_STL_ALLOCATOR_ENABLED 0
#endif
#define LZZ_INLINE inline
struct sttfont_lookupHint
{
unsigned int index;
unsigned int workingLen;
int workingX;
int workingY;
uint32_t uCharLast;
bool writeOut;
sttfont_lookupHint ();
};
struct sttfont_format_callback
{
virtual void callbackOnDraw (sttfont_formatted_text const & text, int index, int x, int y, int xOffsetInitial, int xOffsetAfter, int segmentWidth, int segmentHeight);
};
struct sttfont_format_reset
{
};
struct sttfont_format
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
uint8_t format;
uint8_t flags;
uint16_t padding;
static uint8_t const FORMAT_NONE;
static uint8_t const FORMAT_BOLD;
static uint8_t const FORMAT_ITALIC;
static uint8_t const FORMAT_UNDERLINE;
static uint8_t const FORMAT_STRIKETHROUGH;
static uint8_t const FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS;
static uint8_t const FORMAT_FLAGS_COLOUR_SET;
sttfont_format ();
sttfont_format (uint8_t const _format);
sttfont_format (uint8_t const _format, uint8_t const _r, uint8_t const _g, uint8_t const _b, uint8_t const _a = 255);
sttfont_format clone () const;
bool operator == (sttfont_format const & other) const;
void setBold (bool const on);
void setItalic (bool const on);
void setUnderline (bool const on);
void setStrikethrough (bool const on);
bool isBold () const;
bool isItaic () const;
bool isUnderline () const;
bool isStrikethrough () const;
bool hasFormats () const;
void resetColour ();
void combine (sttfont_format const & other);
void combineWithColour (sttfont_format const & other);
static sttfont_format color (uint8_t const _r, uint8_t const _g, uint8_t const _b, uint8_t const _a = 255);
static sttfont_format colour (uint8_t const _r, uint8_t const _g, uint8_t const _b, uint8_t const _a = 255);
static sttfont_format color_luasafe (uint8_t const _r, uint8_t const _g, uint8_t const _b);
static sttfont_format colour_luasafe (uint8_t const _r, uint8_t const _g, uint8_t const _b);
static sttfont_format const bold;
static sttfont_format const italic;
static sttfont_format const underline;
static sttfont_format const strikethrough;
static sttfont_format const red;
static sttfont_format const green;
static sttfont_format const blue;
static sttfont_format const yellow;
static sttfont_format const black;
static sttfont_format const white;
static sttfont_format const magenta;
static sttfont_format const cyan;
static sttfont_format const grey;
static sttfont_format const light_red;
static sttfont_format const light_green;
static sttfont_format const light_blue;
static sttfont_format const light_yellow;
static sttfont_format const light_black;
static sttfont_format const light_white;
static sttfont_format const light_magenta;
static sttfont_format const light_cyan;
static sttfont_format const light_grey;
static sttfont_format const dark_red;
static sttfont_format const dark_green;
static sttfont_format const dark_blue;
static sttfont_format const dark_yellow;
static sttfont_format const dark_black;
static sttfont_format const dark_white;
static sttfont_format const dark_magenta;
static sttfont_format const dark_cyan;
static sttfont_format const dark_grey;
static int const COLOUR_MODE_NORMAL = 0;
static int const COLOUR_MODE_DARKEN = 1;
static int const COLOUR_MODE_LIGHTEN = 2;
static sttfont_format const & selectColour (int const colourMode, sttfont_format const & colourIf0, sttfont_format const & colourIf1, sttfont_format const & colourIf2);
static sttfont_format_reset const reset;
void swap (sttfont_format & other);
SSF_STRING getTtyFormatCodeBegin () const;
SSF_STRING getTtyFormatCodeEnd () const;
void ttyFmtWorker (SSF_STRING & rs) const;
SSF_STRING debugDump () const;
static void sttr_register ();
void * sttr_getClassSig () const;
char const * const sttr_getClassName () const;
};
struct sttfont_formatted_text_item
{
SSF_STRING text;
sttfont_format format;
sttfont_format_callback * callback;
sttfont_formatted_text_item ();
sttfont_formatted_text_item (SSF_STRING const & _text, sttfont_format const & _format);
sttfont_formatted_text_item (SSF_STRING_MS _text, sttfont_format const & _format);
sttfont_formatted_text_item & setCallback (sttfont_format_callback * _cb);
void setAllocator (stt::allocatorI * alloc);
static void sttr_register ();
void * sttr_getClassSig () const;
char const * const sttr_getClassName () const;
};
struct sttfont_formatted_uipair
{
unsigned int a;
unsigned int b;
static void sttr_register ();
void * sttr_getClassSig () const;
char const * const sttr_getClassName () const;
};
struct sttfont_formatted_text
{
SSF_VECTOR <sttfont_formatted_text_item> mItems;
sttfont_format activeFormat;
sttfont_formatted_text ();
sttfont_formatted_text (stt::allocatorI * alloc);
sttfont_formatted_text (sttfont_formatted_text const & obj);
sttfont_formatted_text (SSF_STRING const & text);
sttfont_formatted_text (SSF_STRING_MS text);
sttfont_formatted_text (char const * text);
sttfont_formatted_text (char const * text, uint32_t const maxLen);
sttfont_formatted_text (sttfont_formatted_text_item_MS text);
sttfont_formatted_text (sttfont_formatted_text_item const & text);
sttfont_formatted_text (sttfont_formatted_text_MS obj);
sttfont_formatted_text & operator = (sttfont_formatted_text_MS obj);
sttfont_formatted_text & operator = (sttfont_formatted_text const & obj);
void resetFormat ();
sttfont_formatted_text & operator << (SSF_STRING_VIEW const & text);
sttfont_formatted_text & operator << (SSF_STRING const & text);
sttfont_formatted_text & operator << (SSF_STRING_MS text);
sttfont_formatted_text & operator << (char const * text);
sttfont_formatted_text & operator += (SSF_STRING_VIEW const & text);
sttfont_formatted_text & operator += (SSF_STRING const & text);
sttfont_formatted_text & operator += (SSF_STRING_MS text);
sttfont_formatted_text & operator += (char const * text);
sttfont_formatted_text & operator << (sttfont_format const & format);
sttfont_formatted_text & operator << (sttfont_format_reset const & reset);
sttfont_formatted_text & operator << (sttfont_formatted_text_item const & obj);
sttfont_formatted_text & operator << (sttfont_formatted_text_item_MS obj);
sttfont_formatted_text & appendCBuff (char const * text, uint32_t const maxLen);
void allocatorAwareAppend (sttfont_formatted_text_item const & obj);
void allocatorAwareAppendMv (sttfont_formatted_text_item_MS obj);
void allocatorAwareAssign (sttfont_formatted_text const & other);
void allocatorAwareAssignMv (sttfont_formatted_text_MS other);
static void sttr_register ();
void * sttr_getClassSig () const;
char const * const sttr_getClassName () const;
void setAllocator (stt::allocatorI * alloc);
void setCustomAllocatorForTextItem (sttfont_formatted_text_item & ti);
static void copyInterned (stt::allocatorI * alloc, sttfont_formatted_text & dst, sttfont_formatted_text const & src, bool const internStrings);
void markInterned ();
sttfont_formatted_text copy () const;
void swap (sttfont_formatted_text & other);
size_t size () const;
size_t length () const;
bool isEmpty () const;
SSF_STRING getString () const;
SSF_STRING getTtyString () const;
SSF_STRING getStringTruncated (unsigned int const maxLen) const;
void append_luasafe (sttfont_formatted_text const & obj);
void append_plaintext_MS (SSF_STRING_MS str, sttfont_format const * fmt);
void append_plaintext_BUF (char const * str, uint32_t const len, sttfont_format const * fmt);
void append_plaintext (char const * str, uint32_t const len, sttfont_format const * fmt);
void append_plaintext_str (SSF_STRING const & str, sttfont_format const * fmt);
void append (sttfont_formatted_text const & obj);
void append (sttfont_formatted_text_MS obj);
void clear ();
void overrideColour_worker (sttfont_format const & fmt, bool const force);
void overrideColour (sttfont_format const & fmt);
void forceOverrideColour (sttfont_format const & fmt);
void consolidateSegments ();
protected:
void consolidateSegments_worker ();
public:
bool back (unsigned int const num);
sttfont_formatted_uipair getIndexAt_luasafe (unsigned int const position, sttfont_lookupHint * mHint) const;
void getIndexAt (unsigned int const position, unsigned int & indexOut, unsigned int & localPosOut, sttfont_lookupHint * mHint = NULL) const;
sttfont_formatted_uipair utf8_charsizeAt_luasafe (unsigned int const position, sttfont_lookupHint * mHint);
void utf8_charsizeAt (unsigned int const position, unsigned int & posOut, unsigned int & sizeOut, sttfont_lookupHint * mHint = NULL);
void insert (unsigned int const position, SSF_STRING const & str, sttfont_lookupHint * mHint = NULL);
void insert (unsigned int const position, SSF_STRING_MS str, sttfont_lookupHint * mHint = NULL);
void insert_luasafe (unsigned int const position, sttfont_formatted_text const & str, sttfont_lookupHint * mHint);
void insert (unsigned int const position, sttfont_formatted_text const & str, sttfont_lookupHint * mHint = NULL);
void insert (unsigned int const position, sttfont_formatted_text_MS str, sttfont_lookupHint * mHint = NULL);
void remove_luasafe (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint);
void remove (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint = NULL);
SSF_STRING substr (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint = NULL) const;
SSF_STRING substr_luasafe (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint) const;
sttfont_formatted_text extract (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint = NULL) const;
sttfont_formatted_text extract_luasafe (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint) const;
void tokenise (SSF_VECTOR <sttfont_formatted_text> & arrOut, uint32_t const delimiter, bool const checkQuoteMarks = true, uint32_t const escapeChar = '\\', bool const includeDelimiterInToken = false) const;
void tokenise_luasafe (SSF_VECTOR <sttfont_formatted_text> * arrOut, uint32_t const delimiter, bool const checkQuoteMarks, uint32_t const escapeChar, bool const includeDelimiterInToken) const;
};
struct sttfont_uint32_t_range
{
uint32_t start;
uint32_t end;
static void populateRangesLatin (SSF_VECTOR <sttfont_uint32_t_range> & mRanges);
static void populateRangesCyrillic (SSF_VECTOR <sttfont_uint32_t_range> & mRanges);
};
struct sttfont_prerendered_text
{
int width;
int height;
sttfont_prerendered_text ();
virtual ~ sttfont_prerendered_text ();
virtual void freeTexture ();
virtual int draw (int const x, int const y);
virtual int drawWithColorMod (int const x, int const y, uint8_t const r, uint8_t const g, uint8_t const b, uint8_t const a = 255);
virtual int draw (sttfont_font_cache * fc, int const x, int const y);
virtual int drawWithColorMod (sttfont_font_cache * fc, int const x, int const y, uint8_t const r, uint8_t const g, uint8_t const b, uint8_t const a);
};
struct sttfont_glyph
{
int advance;
int leftSideBearing;
int width;
int height;
int xOffset;
int yOffset;
sttfont_glyph ();
};
struct sttfont_memory
{
char * data;
size_t size;
bool ownsData;
void alloc (size_t const _size);
void transferTo (sttfont_memory & destination);
void cloneTo (sttfont_memory & other);
sttfont_memory ();
~ sttfont_memory ();
};
struct sttfont_font_list
{
stbtt_fontinfo mFont;
sttfont_memory mMemory;
uint8_t format;
SSF_VECTOR <sttfont_font_list*> mFormatedVariants;
sttfont_font_list * next;
sttfont_font_list ();
~ sttfont_font_list ();
void fetchFontForCodepoint (uint32_t const codepoint, uint8_t const format, stbtt_fontinfo * * mFontOut, int * indexOut);
};
class sttfont_font_cache
{
public:
sttfont_font_list mFont;
int ascent;
int descent;
int lineGap;
int baseline;
int rowSize;
int tabWidth;
float scale;
float underlineThickness;
float strikethroughThickness;
float underlinePosition;
float strikethroughPosition;
int faceSize;
int tabWidthInSpaces;
void * userData;
sttfont_font_cache ();
virtual ~ sttfont_font_cache ();
void setFaceSize (int const _faceSize);
int getScaledRowSize () const;
void syncFrom (sttfont_font_cache const & other);
void loadFont (char const * ttf_buffer, int index = 0);
void loadFontManaged (sttfont_memory & memory, int index = 0);
void addFont (char const * ttf_buffer, int index = 0);
void addFontManaged (sttfont_memory & memory, int index = 0);
void addFormatFont (uint8_t formatMask, char const * ttf_buffer, int index = 0);
void addFormatFontManaged (uint8_t formatMask, sttfont_memory & memory, int index = 0);
protected:
struct addFontWrap
{
char const * ttf_buffer;
sttfont_memory * memory;
int index;
addFontWrap (char const * c);
};
void addFont_worker (addFontWrap & fwm, bool isFormatVariant, uint8_t formatMask = 0);
public:
void genGlyph (uint32_t const codepoint, uint8_t const format, sttfont_glyph * gOut, unsigned char * * bitmapOut = NULL);
virtual void pregenGlyphs (SSF_VECTOR <sttfont_uint32_t_range> & mRanges, uint8_t const format);
virtual void genGlyph_writeData (uint32_t const codepoint, sttfont_glyph * gOut, unsigned char * bitmap2, int w, int h);
virtual sttfont_glyph * getGlyph (uint64_t const target);
sttfont_glyph * getGenGlyph (uint32_t const codepoint, uint8_t const format);
virtual sttfont_glyph * genGlyph_createAndInsert (uint64_t const target, uint32_t const codepoint, uint8_t const format);
int getKerningAdvance (uint32_t const cp1, uint32_t const cp2);
static int utf8_charsize (char const * c);
static int utf8_charsize (uint32_t const codepoint);
static uint32_t utf8_read (char const * c, uint32_t & seek, uint32_t const maxLen);
int drawText (int const x, int const y, char const * c, uint32_t const maxLen = -1);
int drawText (int const x, int const y, SSF_STRING const & str);
int drawText (int const x, int const y, sttfont_format const format, char const * c, uint32_t const maxLen = -1);
int drawText (int const x, int const y, sttfont_format const format, SSF_STRING const & str);
int drawText (int const x, int const y, int & widthOut, int & heightOut, char const * c, uint32_t const maxLen = -1);
int drawText (int const x, int const y, sttfont_format const format, SSF_STRING const & str, int & widthOut, int & heightOut);
int drawText (int const x, int const y, int & widthOut, int & heightOut, sttfont_format const format, char const * c, uint32_t const maxLen = -1);
int drawText (int const x, int const y, SSF_STRING const & str, int & widthOut, int & heightOut);
int drawText (int const x, int const y, sttfont_formatted_text const & text);
int drawText (int const x, int const y, sttfont_formatted_text const & text, int & widthOut, int & heightOut);
int getTextSize (int & w, int & h, char const * c, uint32_t const maxLen = -1, sttfont_lookupHint * mHint = NULL, int const * const maxWidth = NULL);
int getTextSize (int & w, int & h, SSF_STRING const & str, sttfont_lookupHint * mHint = NULL, int const * const maxWidth = NULL);
int getTextSize (int & w, int & h, sttfont_formatted_text const & str, sttfont_lookupHint * mHint = NULL, int const * const maxWidth = NULL);
int countNewlines (SSF_STRING const & str);
int getNumberOfRows (SSF_STRING const & str);
int getNumberOfRows (sttfont_formatted_text const & str);
int getTextHeight (SSF_STRING const & str);
int getTextHeight (sttfont_formatted_text const & str);
int getTextWidth (SSF_STRING const & str, sttfont_lookupHint * mHint = NULL, int const * const maxWidth = NULL);
int getTextWidth (sttfont_formatted_text const & str, sttfont_lookupHint * mHint = NULL, int const * const maxWidth = NULL);
virtual void onStartDrawing ();
virtual void onCompletedDrawing ();
int processString (int const x, int const y, char const * c, uint32_t const maxLen, sttfont_format const * const format, bool const isDrawing, int * const widthOut = NULL, int * const heightOut = NULL, int const * const maxWidth = NULL, sttfont_lookupHint * mHint = NULL, int const * const threshX = NULL, int const * const threshY = NULL, int * const caretPosition = NULL, int initialXOffset = 0);
int processString_worker (int const x, int const y, char const * c, uint32_t const maxLen, sttfont_format const * const format, bool const isDrawing, int * const widthOut, int * const heightOut, int const * const maxWidth, sttfont_lookupHint * mHint, int const * const threshX, int const * const threshY, int * const caretPosition, int initialXOffset);
int processFormatted (sttfont_formatted_text const & text, int x, int y, bool const isDrawing, int * const widthOut = NULL, int * const heightOut = NULL, int const * const maxHeight = NULL, sttfont_lookupHint * mHint = NULL, int const * const threshX = NULL, int const * const threshY = NULL, int * const caretPosition = NULL, int initialXOffset = 0);
int getCaretPos (SSF_STRING const & str, int const relMouseX, int const relMouseY, sttfont_lookupHint * mHint = NULL);
int getCaretPos (sttfont_formatted_text const & str, int const relMouseX, int const relMouseY, sttfont_lookupHint * mHint = NULL);
bool isTofu (sttfont_glyph * G);
sttfont_glyph * getGlyphOrTofu (uint32_t const codepoint, uint8_t const format);
void processCodepoint (int & x, int & y, uint32_t const codepoint, sttfont_format const * const format, bool isDrawing, int kerningAdv, int & overdraw);
virtual void drawCodepoint (sttfont_glyph const * const GS, int const x, int const y, uint32_t const codepoint, sttfont_format const * const format, uint8_t const formatCode, int const kerningAdv, int & overdraw);
virtual void renderTextToObject (sttfont_prerendered_text * textOut, char const * c, uint32_t const maxLen = -1);
virtual void renderTextToObject (sttfont_prerendered_text * textOut, SSF_STRING const & str);
virtual void renderTextToObject (sttfont_prerendered_text * textOut, sttfont_formatted_text const & str);
void breakString (sttfont_formatted_text const & stringIn, SSF_VECTOR <sttfont_formatted_text> & arrOut, int const xs, bool const tokeniseNewLines = true, SSF_VECTOR <sttfont_uintQuad> * breakPoints = NULL);
void breakString (SSF_STRING const & stringIn, SSF_VECTOR <SSF_STRING> & arrOut, int const xs, bool const tokeniseNewLines = true, SSF_VECTOR <sttfont_uintQuad> * breakPoints = NULL);
};
LZZ_INLINE sttfont_format::sttfont_format ()
: r (255), g (255), b (255), a (255), format (0), flags (0), padding (0)
{}
LZZ_INLINE sttfont_format::sttfont_format (uint8_t const _format)
: r (255), g (255), b (255), a (255), format (_format), flags (0), padding (0)
{}
LZZ_INLINE sttfont_format::sttfont_format (uint8_t const _format, uint8_t const _r, uint8_t const _g, uint8_t const _b, uint8_t const _a)
: r (_r), g (_g), b (_b), a (_a), format (_format), flags (FORMAT_FLAGS_COLOUR_SET), padding (0)
{}
LZZ_INLINE sttfont_format sttfont_format::clone () const
{ sttfont_format r; r = *this; return r; }
LZZ_INLINE bool sttfont_format::operator == (sttfont_format const & other) const
{ // use default operator
return (r == other.r) && (g == other.g) && (b == other.b) && (a == other.a) && (format == other.format) && (flags == other.flags);
}
LZZ_INLINE void sttfont_format::setBold (bool const on)
{ if (on) format |= FORMAT_BOLD; else format &= ~FORMAT_BOLD; }
LZZ_INLINE void sttfont_format::setItalic (bool const on)
{ if (on) format |= FORMAT_ITALIC; else format &= ~FORMAT_ITALIC; }
LZZ_INLINE void sttfont_format::setUnderline (bool const on)
{ if (on) format |= FORMAT_UNDERLINE; else format &= ~FORMAT_UNDERLINE; }
LZZ_INLINE void sttfont_format::setStrikethrough (bool const on)
{ if (on) format |= FORMAT_STRIKETHROUGH; else format &= ~FORMAT_STRIKETHROUGH; }
LZZ_INLINE bool sttfont_format::isBold () const
{ return format & FORMAT_BOLD; }
LZZ_INLINE bool sttfont_format::isItaic () const
{ return format & FORMAT_ITALIC; }
LZZ_INLINE bool sttfont_format::isUnderline () const
{ return format & FORMAT_UNDERLINE; }
LZZ_INLINE bool sttfont_format::isStrikethrough () const
{ return format & FORMAT_STRIKETHROUGH; }
LZZ_INLINE bool sttfont_format::hasFormats () const
{ return format; }
LZZ_INLINE void sttfont_formatted_text::append_plaintext (char const * str, uint32_t const len, sttfont_format const * fmt)
{
append_plaintext_BUF(str,len, fmt);
}
LZZ_INLINE void sttfont_formatted_text::append_plaintext_str (SSF_STRING const & str, sttfont_format const * fmt)
{
append_plaintext_BUF(str.data(), str.size(), fmt);
}
LZZ_INLINE void sttfont_formatted_text::append (sttfont_formatted_text const & obj)
{ return append_luasafe(obj); }
LZZ_INLINE void sttfont_formatted_text::insert (unsigned int const position, sttfont_formatted_text const & str, sttfont_lookupHint * mHint)
{ insert_luasafe(position, str, mHint); }
LZZ_INLINE void sttfont_formatted_text::remove (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint)
{ remove_luasafe(position, num, mHint); }
LZZ_INLINE SSF_STRING sttfont_formatted_text::substr_luasafe (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint) const
{
return substr(position, num, mHint);
}
LZZ_INLINE sttfont_formatted_text sttfont_formatted_text::extract_luasafe (unsigned int const position, unsigned int const num, sttfont_lookupHint * mHint) const
{ return extract(position, num, mHint); }
LZZ_INLINE void sttfont_formatted_text::tokenise_luasafe (SSF_VECTOR <sttfont_formatted_text> * arrOut, uint32_t const delimiter, bool const checkQuoteMarks, uint32_t const escapeChar, bool const includeDelimiterInToken) const
{
return tokenise(*arrOut, delimiter, checkQuoteMarks, escapeChar, includeDelimiterInToken);
}
#undef LZZ_INLINE
#endif
////////////////////////////////////////////////////////////////////////
#ifdef SDL_STB_FONT_IMPL
#ifndef SDL_STB_FONT_IMPL_DOUBLE_GUARD_sttFont
#define SDL_STB_FONT_IMPL_DOUBLE_GUARD_sttFont
// sttFont.cpp
//
#include <stdio.h>
#ifndef STB_TRUETYPE_INCLUDE_HANDLED
////////////////////////////////////////
// STB TRUETYPE
#define STB_TRUETYPE_IMPLEMENTATION
#include <math.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "stb_truetype.h"
////////////////////////////////////////
#endif
#define LZZ_INLINE inline
sttfont_lookupHint::sttfont_lookupHint ()
: index (0), workingLen (0), workingX (0), workingY (0), uCharLast (0), writeOut (false)
{}
void sttfont_format_callback::callbackOnDraw (sttfont_formatted_text const & text, int index, int x, int y, int xOffsetInitial, int xOffsetAfter, int segmentWidth, int segmentHeight)
{}
uint8_t const sttfont_format::FORMAT_NONE = 0 << 0;
uint8_t const sttfont_format::FORMAT_BOLD = 1 << 0;
uint8_t const sttfont_format::FORMAT_ITALIC = 1 << 1;
uint8_t const sttfont_format::FORMAT_UNDERLINE = 1 << 2;
uint8_t const sttfont_format::FORMAT_STRIKETHROUGH = 1 << 3;
uint8_t const sttfont_format::FORMAT_RENDER_EVEN_IF_CALLBACK_EXISTS = 1 << 7;
uint8_t const sttfont_format::FORMAT_FLAGS_COLOUR_SET = 1 << 0;
void sttfont_format::resetColour ()
{ r = 255, b = 255, g = 255, a = 255; flags &= ~FORMAT_FLAGS_COLOUR_SET; }
void sttfont_format::combine (sttfont_format const & other)
{
// merges flags and blends colours multiplicatively
format |= other.format;
flags |= other.flags;
r = 255*((r/255.0)*(other.r/255.0));
g = 255*((g/255.0)*(other.g/255.0));
b = 255*((b/255.0)*(other.b/255.0));
a = 255*((a/255.0)*(other.a/255.0));
}
void sttfont_format::combineWithColour (sttfont_format const & other)
{
// merges formats and overrides colour
format |= other.format;
flags |= other.flags;
r = other.r;
g = other.g;
b = other.b;
a = other.a;
}
sttfont_format sttfont_format::color (uint8_t const _r, uint8_t const _g, uint8_t const _b, uint8_t const _a)
{ sttfont_format ret(FORMAT_NONE,_r,_g,_b,_a); ret.flags |= FORMAT_FLAGS_COLOUR_SET; return ret; }
sttfont_format sttfont_format::colour (uint8_t const _r, uint8_t const _g, uint8_t const _b, uint8_t const _a)
{ sttfont_format ret(FORMAT_NONE,_r,_g,_b,_a); ret.flags |= FORMAT_FLAGS_COLOUR_SET; return ret; }
sttfont_format sttfont_format::color_luasafe (uint8_t const _r, uint8_t const _g, uint8_t const _b)
{ return color(_r,_g,_b,255); }
sttfont_format sttfont_format::colour_luasafe (uint8_t const _r, uint8_t const _g, uint8_t const _b)
{ return colour(_r,_g,_b,255); }
sttfont_format const sttfont_format::bold = sttfont_format(FORMAT_BOLD);
sttfont_format const sttfont_format::italic = sttfont_format(FORMAT_ITALIC);
sttfont_format const sttfont_format::underline = sttfont_format(FORMAT_UNDERLINE);
sttfont_format const sttfont_format::strikethrough = sttfont_format(FORMAT_STRIKETHROUGH);
sttfont_format const sttfont_format::red = sttfont_format(FORMAT_NONE,255, 0, 0,255);
sttfont_format const sttfont_format::green = sttfont_format(FORMAT_NONE, 0,255, 0,255);
sttfont_format const sttfont_format::blue = sttfont_format(FORMAT_NONE, 0, 0,255,255);
sttfont_format const sttfont_format::yellow = sttfont_format(FORMAT_NONE,255,255, 0,255);
sttfont_format const sttfont_format::black = sttfont_format(FORMAT_NONE, 0, 0, 0,255);
sttfont_format const sttfont_format::white = sttfont_format(FORMAT_NONE,255,255,255,255);
sttfont_format const sttfont_format::magenta = sttfont_format(FORMAT_NONE,255, 0,255,255);
sttfont_format const sttfont_format::cyan = sttfont_format(FORMAT_NONE, 0,255,255,255);
sttfont_format const sttfont_format::grey = sttfont_format(FORMAT_NONE,128,128,128,255);
sttfont_format const sttfont_format::light_red = sttfont_format(FORMAT_NONE,255, 128, 128,255);
sttfont_format const sttfont_format::light_green = sttfont_format(FORMAT_NONE, 128,255, 128,255);
sttfont_format const sttfont_format::light_blue = sttfont_format(FORMAT_NONE, 128, 128,255,255);
sttfont_format const sttfont_format::light_yellow = sttfont_format(FORMAT_NONE,255,255, 128,255);
sttfont_format const sttfont_format::light_black = sttfont_format(FORMAT_NONE, 128, 128, 128,255);
sttfont_format const sttfont_format::light_white = sttfont_format(FORMAT_NONE,255,255,255,255);
sttfont_format const sttfont_format::light_magenta = sttfont_format(FORMAT_NONE,255, 128,255,255);
sttfont_format const sttfont_format::light_cyan = sttfont_format(FORMAT_NONE, 128,255,255,255);
sttfont_format const sttfont_format::light_grey = sttfont_format(FORMAT_NONE,192,192,192,255);
sttfont_format const sttfont_format::dark_red = sttfont_format(FORMAT_NONE,128, 0, 0,255);
sttfont_format const sttfont_format::dark_green = sttfont_format(FORMAT_NONE, 0,128, 0,255);
sttfont_format const sttfont_format::dark_blue = sttfont_format(FORMAT_NONE, 0, 0,128,255);
sttfont_format const sttfont_format::dark_yellow = sttfont_format(FORMAT_NONE,128,128, 0,255);
sttfont_format const sttfont_format::dark_black = sttfont_format(FORMAT_NONE, 0, 0, 0,255);
sttfont_format const sttfont_format::dark_white = sttfont_format(FORMAT_NONE,128,128,128,255);
sttfont_format const sttfont_format::dark_magenta = sttfont_format(FORMAT_NONE,128, 0,128,255);
sttfont_format const sttfont_format::dark_cyan = sttfont_format(FORMAT_NONE, 0,128,128,255);
sttfont_format const sttfont_format::dark_grey = sttfont_format(FORMAT_NONE,64,64,64,255);
int const sttfont_format::COLOUR_MODE_NORMAL;
int const sttfont_format::COLOUR_MODE_DARKEN;
int const sttfont_format::COLOUR_MODE_LIGHTEN;
sttfont_format const & sttfont_format::selectColour (int const colourMode, sttfont_format const & colourIf0, sttfont_format const & colourIf1, sttfont_format const & colourIf2)
{
if (colourMode == 1) return colourIf1;
if (colourMode == 2) return colourIf2;
return colourIf0;
}
sttfont_format_reset const sttfont_format::reset;
void sttfont_format::swap (sttfont_format & other)
{
sttfont_format tmp = *this;
*this = other;
other = tmp;
}
SSF_STRING sttfont_format::getTtyFormatCodeBegin () const
{
if (!(hasFormats() || r != 255 || g != 255 || b != 255))
return "";
SSF_STRING rs = "\033[";
ttyFmtWorker(rs);
return rs + "m";
}
SSF_STRING sttfont_format::getTtyFormatCodeEnd () const
{
if (!(hasFormats() || r != 255 || g != 255 || b != 255))
return "";
return "\033[0m";
}
void sttfont_format::ttyFmtWorker (SSF_STRING & rs) const
{
bool first = false;
if (r != 255 || g != 255 || b != 255) {
if (first) rs += ";";
char arr[64];
snprintf(arr, 63, "38;2;%i;%i;%i", r, g, b);
rs += arr;
first = true;
}
if (isBold()) {
if (first) rs += ";";
rs += "1";
first = true;
}
if (isItaic()) {
if (first) rs += ";";
rs += "3";
first = true;
}
if (isUnderline()) {
if (first) rs += ";";
rs += "4";
first = true;
}
if (isStrikethrough()) {
if (first) rs += ";";
rs += "9";
first = true;
}
}
SSF_STRING sttfont_format::debugDump () const
{
SSF_STRING rs;
ttyFmtWorker(rs);
return "[fmt:" + rs + "]";
}
void sttfont_format::sttr_register ()
{
#ifdef STTR_ENABLED
#define STTR_REGF_ALIAS(C,X,A,F) regField<C,decltype(&C::X),F>(&C::X,#A).setUserFlags(F)
// Reflection stuff - see snappertt/sttr on github for more info. You don't need STTR to use this library
sttr::RegNamespace & R = *sttr::getGlobalNamespace();
R.beginClass<sttfont_format>("sttfont_format")
.STTR_REGF(sttfont_format,r,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_format,g,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_format,b,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_format,a,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_format,format,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_format,flags,STTR_JSON_ENABLED)
.STTR_REG(sttfont_format,clone)
.STTR_REG(sttfont_format,combine)
.STTR_REG(sttfont_format,combineWithColour)
.STTR_REG(sttfont_format,setBold)
.STTR_REG(sttfont_format,setItalic)
.STTR_REG(sttfont_format,setUnderline)
.STTR_REG(sttfont_format,setStrikethrough)
.STTR_REG(sttfont_format,isBold)
.STTR_REG(sttfont_format,isItaic)
.STTR_REG(sttfont_format,isUnderline)
.STTR_REG(sttfont_format,isStrikethrough)
.STTR_REG(sttfont_format,hasFormats)
.STTR_REG(sttfont_format,resetColour)
.STTR_REGF_ALIAS(sttfont_format,colour_luasafe,colour,0)
.STTR_REGF_ALIAS(sttfont_format,color_luasafe,color,0)
.STTR_REG(sttfont_format,bold)
.STTR_REG(sttfont_format,italic)
.STTR_REG(sttfont_format,underline)
.STTR_REG(sttfont_format,strikethrough)
.STTR_REG(sttfont_format,red)
.STTR_REG(sttfont_format,green)
.STTR_REG(sttfont_format,blue)
.STTR_REG(sttfont_format,yellow)
.STTR_REG(sttfont_format,black)
.STTR_REG(sttfont_format,white)
.STTR_REG(sttfont_format,magenta)
.STTR_REG(sttfont_format,cyan)
.STTR_REG(sttfont_format,grey)
.STTR_REG(sttfont_format,light_red)
.STTR_REG(sttfont_format,light_green)
.STTR_REG(sttfont_format,light_blue)
.STTR_REG(sttfont_format,light_yellow)
.STTR_REG(sttfont_format,light_black)
.STTR_REG(sttfont_format,light_white)
.STTR_REG(sttfont_format,light_magenta)
.STTR_REG(sttfont_format,light_cyan)
.STTR_REG(sttfont_format,light_grey)
.STTR_REG(sttfont_format,dark_red)
.STTR_REG(sttfont_format,dark_green)
.STTR_REG(sttfont_format,dark_blue)
.STTR_REG(sttfont_format,dark_yellow)
.STTR_REG(sttfont_format,dark_black)
.STTR_REG(sttfont_format,dark_white)
.STTR_REG(sttfont_format,dark_magenta)
.STTR_REG(sttfont_format,dark_cyan)
.STTR_REG(sttfont_format,dark_grey)
.STTR_REG(sttfont_format,COLOUR_MODE_NORMAL)
.STTR_REG(sttfont_format,COLOUR_MODE_DARKEN)
.STTR_REG(sttfont_format,COLOUR_MODE_LIGHTEN)
.STTR_REG(sttfont_format,selectColour)
.STTR_REG(sttfont_format,reset)
.STTR_REG(sttfont_format,getTtyFormatCodeBegin)
.STTR_REG(sttfont_format,getTtyFormatCodeEnd)
.STTR_REG(sttfont_format,debugDump)
.endClass();
#undef STTR_REGF_ALIAS
#endif
}
void * sttfont_format::sttr_getClassSig () const
{ return ( void * ) sttr :: getTypeSignature < sttfont_format > ( ) ; }
char const * const sttfont_format::sttr_getClassName () const
{ return sttr :: getTypeName < sttfont_format > ( ) ; }
sttfont_formatted_text_item::sttfont_formatted_text_item ()
: callback (0)
{}
sttfont_formatted_text_item::sttfont_formatted_text_item (SSF_STRING const & _text, sttfont_format const & _format)
: text (_text), format (_format), callback (0)
{}
sttfont_formatted_text_item::sttfont_formatted_text_item (SSF_STRING_MS _text, sttfont_format const & _format)
: text (std::move(_text)), format (_format), callback (0)
{}
sttfont_formatted_text_item & sttfont_formatted_text_item::setCallback (sttfont_format_callback * _cb)
{ callback = _cb; return *this; }
void sttfont_formatted_text_item::setAllocator (stt::allocatorI * alloc)
{
#if SSF_STT_STL_ALLOCATOR_ENABLED
text.setAllocator(alloc);
#endif
}
void sttfont_formatted_text_item::sttr_register ()
{
#ifdef STTR_ENABLED
sttr::RegNamespace & R = *sttr::getGlobalNamespace();
R.beginClass<sttfont_formatted_text_item>("sttfont_formatted_text_item")
.STTR_REGF(sttfont_formatted_text_item,text,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_formatted_text_item,format,STTR_JSON_ENABLED)
.endClass();
#endif
}
void * sttfont_formatted_text_item::sttr_getClassSig () const
{ return ( void * ) sttr :: getTypeSignature < sttfont_formatted_text_item > ( ) ; }
char const * const sttfont_formatted_text_item::sttr_getClassName () const
{ return sttr :: getTypeName < sttfont_formatted_text_item > ( ) ; }
void sttfont_formatted_uipair::sttr_register ()
{
#ifdef STTR_ENABLED
sttr::RegNamespace & R = *sttr::getGlobalNamespace();
R.beginClass<sttfont_formatted_uipair>("sttfont_formatted_uipair")
.STTR_REGF(sttfont_formatted_uipair,a,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_formatted_uipair,b,STTR_JSON_ENABLED)
.endClass();
#endif
}
void * sttfont_formatted_uipair::sttr_getClassSig () const
{ return ( void * ) sttr :: getTypeSignature < sttfont_formatted_text_item > ( ) ; }
char const * const sttfont_formatted_uipair::sttr_getClassName () const
{ return sttr :: getTypeName < sttfont_formatted_text_item > ( ) ; }
sttfont_formatted_text::sttfont_formatted_text ()
{}
sttfont_formatted_text::sttfont_formatted_text (stt::allocatorI * alloc)
{
#if SSF_STT_STL_ALLOCATOR_ENABLED
if (alloc)
mItems.setAllocator(alloc);
#endif
}
sttfont_formatted_text::sttfont_formatted_text (sttfont_formatted_text const & obj)
{ allocatorAwareAssign(obj); }
sttfont_formatted_text::sttfont_formatted_text (SSF_STRING const & text)
{ *this << text; }
sttfont_formatted_text::sttfont_formatted_text (SSF_STRING_MS text)
{ *this << text; }
sttfont_formatted_text::sttfont_formatted_text (char const * text)
{ *this << text; }
sttfont_formatted_text::sttfont_formatted_text (char const * text, uint32_t const maxLen)
{ appendCBuff(text, maxLen); }
sttfont_formatted_text::sttfont_formatted_text (sttfont_formatted_text_item_MS text)
{ allocatorAwareAppendMv(std::move(text)); }
sttfont_formatted_text::sttfont_formatted_text (sttfont_formatted_text_item const & text)
{ allocatorAwareAppend(text); }
sttfont_formatted_text::sttfont_formatted_text (sttfont_formatted_text_MS obj)
{ allocatorAwareAssignMv(std::move(obj)); }
sttfont_formatted_text & sttfont_formatted_text::operator = (sttfont_formatted_text_MS obj)
{ allocatorAwareAssignMv(std::move(obj)); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator = (sttfont_formatted_text const & obj)
{ allocatorAwareAssign(obj); return *this; }
void sttfont_formatted_text::resetFormat ()
{ activeFormat = sttfont_format(); }
sttfont_formatted_text & sttfont_formatted_text::operator << (SSF_STRING_VIEW const & text)
{ append_plaintext_BUF(text.data(), text.length(), &activeFormat); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (SSF_STRING const & text)
{ append_plaintext_str(text, &activeFormat); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (SSF_STRING_MS text)
{ append_plaintext_MS(std::move(text), &activeFormat); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (char const * text)
{ appendCBuff(text, -1); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator += (SSF_STRING_VIEW const & text)
{ append_plaintext_BUF(text.data(), text.length(), &activeFormat); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator += (SSF_STRING const & text)
{ append_plaintext_str(text, &activeFormat); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator += (SSF_STRING_MS text)
{ append_plaintext_MS(std::move(text), &activeFormat); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator += (char const * text)
{ appendCBuff(text, -1); resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (sttfont_format const & format)
{ activeFormat.combine(format); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (sttfont_format_reset const & reset)
{ resetFormat(); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (sttfont_formatted_text_item const & obj)
{ allocatorAwareAppend(obj); return *this; }
sttfont_formatted_text & sttfont_formatted_text::operator << (sttfont_formatted_text_item_MS obj)
{ allocatorAwareAppendMv(std::move(obj)); return *this; }
sttfont_formatted_text & sttfont_formatted_text::appendCBuff (char const * text, uint32_t const maxLen)
{
const char* p = text;
while (*p) p++;
uint32_t len = (p - text);
if (len > maxLen) len = maxLen;
append_plaintext_BUF(text, len, NULL);
return *this;
}
void sttfont_formatted_text::allocatorAwareAppend (sttfont_formatted_text_item const & obj)
{
// appends into this using this' allocator
#if SSF_STT_STL_ALLOCATOR_ENABLED
sttfont_formatted_text_item tmp;
setCustomAllocatorForTextItem(tmp);
tmp = obj; // copy into custom allocated memory
mItems.push_back(std::move(tmp));
#else
mItems.push_back(obj);
#endif
}
void sttfont_formatted_text::allocatorAwareAppendMv (sttfont_formatted_text_item_MS obj)
{
#if SSF_STT_STL_ALLOCATOR_ENABLED
if (mItems.getCustomAllocator())
return allocatorAwareAppend(obj);
#else
mItems.push_back(std::move(obj));
#endif
}
void sttfont_formatted_text::allocatorAwareAssign (sttfont_formatted_text const & other)
{
// copies other's data -> this, keeping this' allocator
#if SSF_STT_STL_ALLOCATOR_ENABLED
stt::allocatorI * a = mItems.getCustomAllocator();
if (a)
return copyInterned(a, *this, other, false);
#endif
mItems = other.mItems;
activeFormat = other.activeFormat;
}
void sttfont_formatted_text::allocatorAwareAssignMv (sttfont_formatted_text_MS other)
{
// moving other -> this, keeping this' allocator
#if SSF_STT_STL_ALLOCATOR_ENABLED
if (mItems.getCustomAllocator())
return allocatorAwareAssign(other);
//if (other.mItems.getCustomAllocator())
// mItems = std::move(other.items); // stt::vector(stt::vector&&) will auto-move allocator
#endif
mItems = std::move(other.mItems);
activeFormat = other.activeFormat;
}
void sttfont_formatted_text::sttr_register ()
{
#ifdef STTR_ENABLED
#define STTR_REGF_ALIAS(C,X,A,F) regField<C,decltype(&C::X),F>(&C::X,#A).setUserFlags(F)
sttr::RegNamespace & R = *sttr::getGlobalNamespace();
R.beginClass<sttfont_formatted_text>("sttfont_formatted_text")
.STTR_REGF(sttfont_formatted_text,mItems,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_formatted_text,activeFormat,STTR_JSON_ENABLED)
.STTR_REGF(sttfont_formatted_text,swap,0)
.STTR_REGF(sttfont_formatted_text,copy,0)
.STTR_REGF(sttfont_formatted_text,size,0)
.STTR_REGF(sttfont_formatted_text,length,0)
.STTR_REGF(sttfont_formatted_text,isEmpty,0)
.STTR_REGF(sttfont_formatted_text,getString,0)
.STTR_REGF(sttfont_formatted_text,getTtyString,0)
.STTR_REGF(sttfont_formatted_text,getStringTruncated,0)
.STTR_REGF(sttfont_formatted_text,append_luasafe,0)
.STTR_REGF(sttfont_formatted_text,append_plaintext_str,0)
.STTR_REGF(sttfont_formatted_text,clear,0)
.STTR_REGF(sttfont_formatted_text,overrideColour,0)
.STTR_REGF(sttfont_formatted_text,consolidateSegments,0)
.STTR_REGF(sttfont_formatted_text,back,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,getIndexAt_luasafe,getIndexAt,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,utf8_charsizeAt_luasafe,utf8_charsizeAt,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,insert_luasafe,insert,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,remove_luasafe,remove,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,substr_luasafe,substr,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,extract_luasafe,extract,0)
.STTR_REGF_ALIAS(sttfont_formatted_text,tokenise_luasafe,tokenise,0)
.endClass();
#undef STTR_REGF_ALIAS
#endif
}
void * sttfont_formatted_text::sttr_getClassSig () const
{ return ( void * ) sttr :: getTypeSignature < sttfont_formatted_text > ( ) ; }
char const * const sttfont_formatted_text::sttr_getClassName () const
{ return sttr :: getTypeName < sttfont_formatted_text > ( ) ; }
void sttfont_formatted_text::setAllocator (stt::allocatorI * alloc)
{
// For use with stt-stl
#if SSF_STT_STL_ALLOCATOR_ENABLED
mItems.setAllocator(alloc);
#endif
}
void sttfont_formatted_text::setCustomAllocatorForTextItem (sttfont_formatted_text_item & ti)
{
// Internal function that propgoates mItem.sso.d.store.mAllocator to any text its
#if SSF_STT_STL_ALLOCATOR_ENABLED
stt::allocatorI* alloc = mItems.getCustomAllocator();
if (alloc)
ti.setAllocator(alloc);
#endif
}
void sttfont_formatted_text::copyInterned (stt::allocatorI * alloc, sttfont_formatted_text & dst, sttfont_formatted_text const & src, bool const internStrings)
{
#if SSF_STT_STL_ALLOCATOR_ENABLED
// copies to an empty container which will use allocator alloc
// strings within this item can be optionally marked as interned
assert(dst.isEmpty());
assert(alloc);
dst.mItems.setAllocator(alloc);
uint32_t nItems = src.mItems.size();
dst.mItems.resize(nItems);
for (uint32_t i = 0; i < nItems; ++i) {
dst.mItems[i].text.setAllocator(alloc);
dst.mItems[i].text = src.mItems[i].text;
if (internStrings)
dst.mItems[i].text.markInterned(); // do not deallocate through destructor
dst.mItems[i].format = src.mItems[i].format;
}
dst.activeFormat = src.activeFormat;
#else
// copy
dst = src;
#endif
}
void sttfont_formatted_text::markInterned ()
{
#if SSF_STT_STL_ALLOCATOR_ENABLED
// marks all elements as interned
uint32_t nItems = mItems.size();