-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamp.h
More file actions
5113 lines (4388 loc) · 173 KB
/
Copy pathamp.h
File metadata and controls
5113 lines (4388 loc) · 173 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
////////////////////////////////////////////////////////////////////////////////
// MIT License //
// //
// Copyright (c) 2026 Erich Erstu //
// //
// Permission is hereby granted, free of charge, to any person obtaining a //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation //
// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
// and/or sell copies of the Software, and to permit persons to whom the //
// Software is furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
// DEALINGS IN THE SOFTWARE. //
////////////////////////////////////////////////////////////////////////////////
#ifndef AMP_H_12_01_2026
#define AMP_H_12_01_2026
////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <string.h>
#include <stdckdint.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdbit.h>
////////////////////////////////////////////////////////////////////////////////
#define AMP_MAJOR_VERSION 1
#define AMP_MINOR_VERSION 0
#define AMP_REVISION 0
#define AMP_VERSION "1.0.0"
#ifndef AMP_BUF_SIZE
#define AMP_BUF_SIZE sizeof(size_t)
#endif
#define AMP_ESC "\x1b"
struct amp_type;
static constexpr size_t AMP_CELL_GLYPH_SIZE = 5; // 4 bytes for UTF8 + null byte
static constexpr size_t AMP_CELL_MODE_SIZE = 8;
static constexpr size_t AMP_CELL_SIZE = (
AMP_CELL_GLYPH_SIZE + AMP_CELL_MODE_SIZE
);
typedef enum : uint8_t {
AMP_COLOR_NONE = 0,
////////////////////////////////////////////////////////////////////////////
AMP_DARK, AMP_MAROON, AMP_GREEN, AMP_OLIVE, AMP_NAVY,
AMP_PURPLE, AMP_TEAL, AMP_SILVER, AMP_CHARCOAL, AMP_RED,
AMP_LIME, AMP_YELLOW, AMP_BLUE, AMP_FUCHSIA, AMP_AQUA,
AMP_WHITE,
////////////////////////////////////////////////////////////////////////////
AMP_MAX_COLOR
} AMP_COLOR;
struct amp_rgb_type {
uint8_t r;
uint8_t g;
uint8_t b;
};
typedef enum : uint64_t {
AMP_STYLE_NONE = 0,
////////////////////////////////////////////////////////////////////////////
AMP_HIDDEN = (1ULL << 0), AMP_FAINT = (1ULL << 1),
AMP_ITALIC = (1ULL << 2), AMP_UNDERLINE = (1ULL << 3),
AMP_BLINKING = (1ULL << 4), AMP_STRIKETHROUGH = (1ULL << 5),
////////////////////////////////////////////////////////////////////////////
AMP_FG_NONE = (1ULL << 6), AMP_FG_DARK = (1ULL << 7),
AMP_FG_MAROON = (1ULL << 8), AMP_FG_GREEN = (1ULL << 9),
AMP_FG_OLIVE = (1ULL << 10), AMP_FG_NAVY = (1ULL << 11),
AMP_FG_PURPLE = (1ULL << 12), AMP_FG_TEAL = (1ULL << 13),
AMP_FG_SILVER = (1ULL << 14), AMP_FG_CHARCOAL = (1ULL << 15),
AMP_FG_RED = (1ULL << 16), AMP_FG_LIME = (1ULL << 17),
AMP_FG_YELLOW = (1ULL << 18), AMP_FG_BLUE = (1ULL << 19),
AMP_FG_FUCHSIA = (1ULL << 20), AMP_FG_AQUA = (1ULL << 21),
AMP_FG_WHITE = (1ULL << 22),
////////////////////////////////////////////////////////////////////////////
AMP_BG_NONE = (1ULL << 24), AMP_BG_DARK = (1ULL << 25),
AMP_BG_MAROON = (1ULL << 26), AMP_BG_GREEN = (1ULL << 27),
AMP_BG_OLIVE = (1ULL << 28), AMP_BG_NAVY = (1ULL << 29),
AMP_BG_PURPLE = (1ULL << 30), AMP_BG_TEAL = (1ULL << 31),
AMP_BG_SILVER = (1ULL << 32), AMP_BG_CHARCOAL = (1ULL << 33),
AMP_BG_RED = (1ULL << 34), AMP_BG_LIME = (1ULL << 35),
AMP_BG_YELLOW = (1ULL << 36), AMP_BG_BLUE = (1ULL << 37),
AMP_BG_FUCHSIA = (1ULL << 38), AMP_BG_AQUA = (1ULL << 39),
AMP_BG_WHITE = (1ULL << 40),
////////////////////////////////////////////////////////////////////////////
AMP_SOFT_RESET = (1ULL << 42), AMP_HARD_RESET = (1ULL << 43)
} AMP_STYLE;
typedef enum : uint8_t {
AMP_PAL_RGB16 = 0, // Standard 16 colors (most portable).
AMP_PAL_24BIT // True color mode (24 bit color depth).
} AMP_PALETTE;
typedef enum : uint8_t {
AMP_ALIGN_LEFT = 0,
AMP_ALIGN_CENTER,
AMP_ALIGN_RIGHT
} AMP_ALIGN;
typedef enum : uint64_t {
AMP_SETTINGS_NONE = 0,
////////////////////////////////////////////////////////////////////////////
AMP_DEFLATE = (1ULL << 0), // Unrequired empty lines are trimmed.
AMP_FLATTEN = (1ULL << 1) // Merge as many style layers as possible.
} AMP_SETTINGS;
// Public API: /////////////////////////////////////////////////////////////////
static inline size_t amp_calc_size(
uint32_t ansmap_width,
uint32_t ansmap_height
// Returns the size of the data buffer needed for the initialization of an
// ansmap with the given resolution.
);
static inline size_t amp_init(
struct amp_type * ansmap,
uint32_t ansmap_width,
uint32_t ansmap_height,
void * canvas_data,
size_t canvas_data_size
// Initializes the given ansmap data structure. If the canvas data array is
// not big enough for the image, then the end of the image will be cut off.
//
// Returns the size of the data buffer needed for the given resolution.
);
static inline void amp_clear(
struct amp_type * ansmap
// Fills the ansmap with empty string glyphs and resets their style.
);
static inline void amp_set_palette(
struct amp_type * ansmap,
AMP_PALETTE palette
// Sets the color palette for the given ansmap. It is effective only during
// the conversion of the ansmap into ANSI escape sequences.
);
static inline void amp_print_glyph(
struct amp_type * ansmap,
long glyph_x,
long glyph_y,
AMP_STYLE glyph_style,
const char * glyph_str
// Prints a single glyph on the given ansmap.
);
static inline void amp_print_line(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
AMP_ALIGN text_alignment,
const char * text_str
// Prints the provided UTF-8 encoded text on the ansmap. Newlines and other
// non-printable ASCII characters will be replaced by question marks.
);
static inline size_t amp_print_text(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
uint32_t text_max_width,
AMP_ALIGN text_alignment,
const char * text_str
// Prints the provided UTF-8 encoded text on the ansmap. This function wraps
// the line if its width exceeds the maximum allowed width. If the maximum
// width is zero, then wrapping is disabled.
//
// Returns the number of lines printed.
);
static inline ssize_t amp_to_ans(
const struct amp_type * ansmap,
char * ans_dst,
size_t ans_dst_size
// Converts the given ansmap into ANSI escape sequences. The escape codes
// will be copied to the provided data buffer. If the buffer pointer is a
// null pointer, then the output will be written into the program's standard
// output.
//
// Returns the number of bytes that would have been written if the given
// buffer was big enough. If the buffer is too small, then its first byte
// is set to zero. The return value of -1 indicates an error.
);
static inline ssize_t amp_row_to_ans(
const struct amp_type * ansmap,
long row_y,
char * ans_dst,
size_t ans_dst_size
// Converts one row of the given ansmap into ANSI escape sequences. The
// escape codes will be copied to the provided data buffer. If the buffer
// pointer is a null pointer, then the output will be written into the
// program's standard output.
//
// Returns the number of bytes that would have been written if the given
// buffer was big enough. If the buffer is too small, then its first byte
// is set to zero. The return value of -1 indicates an error.
);
static inline ssize_t amp_clip_to_ans(
const struct amp_type * ansmap,
long clip_x,
long clip_y,
uint32_t width,
char * ans_dst,
size_t ans_dst_size
// Converts a segment of a single row in the given ansmap into ANSI escape
// sequences. The escape codes will be copied to the provided data buffer.
// If the buffer pointer is a null pointer, then the output will be written
// into the program's standard output.
//
// Returns the number of bytes that would have been written if the given
// buffer was big enough. If the buffer is too small, then its first byte
// is set to zero. The return value of -1 indicates an error.
);
static inline const char * amp_get_glyph(
const struct amp_type * ansmap,
long glyph_x,
long glyph_y
// Returns a pointer to the null-terminated UTF-8 encoded string of the
// glyph on the given position of the ansmap. If the specified position is
// not on the ansmap, then a null pointer is returned.
);
static inline const char * amp_put_glyph(
struct amp_type * ansmap,
long glyph_x,
long glyph_y,
const char * glyph
// Overwrites a single glyph in the ansmap on the given position and returns
// a pointer to the null-terminated UTF-8 encoded string of the new glyph.
// If the specified position is not on the ansmap, then a null pointer is
// returned.
);
static inline AMP_STYLE amp_get_style(
const struct amp_type * ansmap,
long style_x,
long style_y
// Returns the style bits of a glyph on the ansmap from the given position.
);
static inline bool amp_put_style(
struct amp_type * ansmap,
long style_x,
long style_y,
AMP_STYLE style
// Sets the style of a glyph on the ansmap at the given position.
//
// Returns true on success and false if the position is not on the ansmap.
);
static inline struct amp_rgb_type amp_get_bg_color(
struct amp_type * ansmap,
long cell_x,
long cell_y
// Returns a color data structure holding the background color information
// of a glyph on the ansmap at the given position.
);
static inline bool amp_set_bg_color(
struct amp_type * ansmap,
long cell_x,
long cell_y,
struct amp_rgb_type background_color
// Sets the background color of a glyph on the ansmap at the given position.
//
// Returns true on success and false if the position is not on the ansmap.
);
static inline struct amp_rgb_type amp_get_fg_color(
struct amp_type * ansmap,
long cell_x,
long cell_y
// Returns a color data structure holding the foreground color information
// of a glyph on the ansmap at the given position.
);
static inline bool amp_set_fg_color(
struct amp_type * ansmap,
long cell_x,
long cell_y,
struct amp_rgb_type foreground_color
// Sets the foreground color of a glyph on the ansmap at the given position.
//
// Returns true on success and false if the position is not on the ansmap.
);
static inline struct amp_rgb_type amp_map_rgb(
uint8_t red,
uint8_t green,
uint8_t blue
// Returns a color data structure holding the color information given in
// the arguments.
);
static inline void amp_unmap_rgb(
struct amp_rgb_type color,
uint8_t * red,
uint8_t * green,
uint8_t * blue
// Copies the red, green and blue color components from the given color data
// structure to the addresses specified in the arguments respectively.
);
static inline struct amp_color_type amp_lookup_color(
AMP_COLOR color_index
// Returns a color data structure holding the color information of the given
// color by its index.
);
static inline ssize_t amp_stdout(
const char * str_src,
size_t str_src_size
// Writes the given buffer into the program's standard output fully.
//
// Returns the number of bytes written or -1 to indicate an error.
);
static inline size_t amp_print_rich_text(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
uint32_t text_max_width,
AMP_ALIGN text_alignment,
const char * text_str
// Prints the provided UTF-8 encoded rich text on the ansmap. Inline
// embedded style markers are applied to the text as it is printed. This
// function wraps the line if its width exceeds the maximum allowed width.
// If the maximum width is zero, then wrapping is disabled.
//
// Returns the number of lines printed.
);
static inline uint32_t amp_get_width(
const struct amp_type * ansmap
// Returns the width of the given ansmap image.
);
static inline uint32_t amp_get_height(
const struct amp_type * ansmap
// Returns the height of the given ansmap image.
);
static inline AMP_PALETTE amp_get_palette(
const struct amp_type * ansmap
// Returns the palette of the given ansmap image.
);
static inline ssize_t amp_snprint_textf(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
uint32_t text_max_width,
AMP_ALIGN text_alignment,
char * text_format_buffer,
size_t text_format_buffer_size,
const char * text_format,
...
// Prints the provided UTF-8 encoded formatted text on the ansmap. This
// function wraps the line if its width exceeds the maximum allowed width.
// If the maximum width is zero, then wrapping is disabled. If the provided
// text format buffer could not fit the text after formatting or if an error
// occurred during formatting, then nothing is printed on the ansmap.
//
// Returns the number of bytes that would have been written into the text
// format buffer (excluding the null byte used to end output to strings).
// The return value of -1 indicates that an output error was encountered in
// the underlying call to vsnprintf.
) __attribute__((format (printf, 9, 10)));
static inline ssize_t amp_snprint_rich_textf(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
uint32_t text_max_width,
AMP_ALIGN text_alignment,
char * text_format_buffer,
size_t text_format_buffer_size,
const char * text_format,
...
// Prints the provided UTF-8 encoded formatted text on the ansmap. Inline
// embedded style markers are applied to the text as it is printed. This
// function wraps the line if its width exceeds the maximum allowed width.
// If the maximum width is zero, then wrapping is disabled. If the provided
// text format buffer could not fit the text after formatting or if an error
// occurred during formatting, then nothing is printed on the ansmap.
//
// Returns the number of bytes that would have been written into the text
// format buffer (excluding the null byte used to end output to strings).
// The return value of -1 indicates that an output error was encountered in
// the underlying call to vsnprintf.
) __attribute__((format (printf, 9, 10)));
static inline ssize_t amp_snprint_linef(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
AMP_ALIGN text_alignment,
char * text_format_buffer,
size_t text_format_buffer_size,
const char * text_format,
...
// Prints the provided UTF-8 encoded formatted text on the ansmap. Newlines
// and other non-printable ASCII characters will be replaced by question
// marks. If the provided text format buffer could not fit the text after
// formatting or if an error occurred during formatting, then nothing is
// printed on the ansmap.
//
// Returns the number of bytes that would have been written into the text
// format buffer (excluding the null byte used to end output to strings).
// The return value of -1 indicates that an output error was encountered in
// the underlying call to vsnprintf.
) __attribute__((format (printf, 8, 9)));
static inline ssize_t amp_encode(
const struct amp_type * ansmap,
AMP_SETTINGS flags,
char * buffer,
size_t buffer_size
// Exports the specified ansmap as a human-readable plaintext document. The
// file content is written into the provided buffer, given that the buffer
// is large enough for complete storage. If the buffer pointer is a null
// pointer, then the output will be written into the program's standard
// output.
//
// AMP_SETTINGS flags:
// * AMP_DEFLATE - Unrequired empty lines are trimmed.
// * AMP_FLATTEN - Merge as many style layers as possible.
//
// Returns the number of bytes that was written or would have been written
// (excluding the null byte used to end output to strings). If the buffer is
// too small, then its first byte is set to zero. The return value of -1
// indicates an error.
);
static inline size_t amp_doc_parse_size(
const void * doc_data,
size_t doc_data_size,
uint32_t * amp_width,
uint32_t * amp_height
// Parses the provided data as an AMP document and attempts to determine the
// size of a buffer required for the deserialization of the document. If the
// size was found, then the width and height of the parsed ansmap are
// written to the memory addresses specified by the width and height pointer
// arguments respectively.
//
// Returns the size of the buffer required for ansmap decoding. The return
// value of zero indicates a parsing error.
);
static inline size_t amp_decode(
struct amp_type * ansmap,
const void * data,
size_t data_size
// Imports a human-readable plaintext document from the data array into the
// referenced ansmap. If the ansmap could not fit the image, then the image
// will be cropped to fit.
//
// Returns the original canvas size of the decoded ansmap. The return
// value of zero indicates a parsing error.
);
static inline void amp_draw_ansmap(
struct amp_type * dst_ansmap,
long x_on_dst_ansmap,
long y_on_dst_ansmap,
const struct amp_type * src_ansmap
// Draws a copy of the source ansmap onto the destination ansmap at the
// specified position. It uses a masked drawing mode where transparent cells
// are skipped, so the background image will show through the masked parts
// of the source image. Transparent cells are marked by their glyphs
// containing either a space or an empty string, having no style specified.
);
static inline void amp_draw_ansmap_region(
struct amp_type * dst_ansmap,
long x_on_dst_ansmap,
long y_on_dst_ansmap,
const struct amp_type * src_ansmap,
long x_on_src_ansmap,
long y_on_src_ansmap,
long region_width,
long region_height
// Draws a copy of a region from the source ansmap onto the destination
// ansmap at the specified position. It uses a masked drawing mode where
// transparent cells are skipped, so the background image will show through
// the masked parts of the source image. Transparent cells are marked by
// their glyphs containing either a space or an empty string, having no
// style specified.
);
////////////////////////////////////////////////////////////////////////////////
struct amp_type {
uint8_t buffer[AMP_BUF_SIZE];
uint32_t width;
uint32_t height;
struct {
size_t size;
uint8_t *data;
struct {
size_t size;
uint8_t *data;
} glyph;
struct {
size_t size;
uint8_t *data;
} mode;
} canvas;
AMP_PALETTE palette;
};
struct amp_mode_type {
struct amp_rgb_type fg;
struct amp_rgb_type bg;
struct {
bool fg:1;
bool bg:1;
bool hidden:1;
bool faint:1;
bool italic:1;
bool underline:1;
bool blinking:1;
bool strikethrough:1;
// not serialized:
bool broken:1;
bool reset:1;
} bitset;
};
struct amp_mode_code_type {
struct {
uint8_t data[8]; // 6 for decoration + inverse and bold
uint8_t size;
} style;
struct {
struct {
uint8_t data[5];
uint8_t size;
} fg;
struct {
uint8_t data[5];
uint8_t size;
} bg;
} color;
struct {
bool reset:1; // used when returning mode updates
} bitset;
};
struct amp_style_flag_type {
char glyph[AMP_CELL_GLYPH_SIZE];
AMP_STYLE value;
};
struct amp_color_combo_type {
AMP_COLOR colors[5];
};
// Private API: ////////////////////////////////////////////////////////////////
static inline ssize_t amp_copy_glyph(
const struct amp_type * ansmap,
long x,
long y,
uint8_t * glyph_dst,
size_t glyph_dst_size
);
static inline bool amp_set_mode(
struct amp_type * ansmap,
long x,
long y,
struct amp_mode_type mode
);
static inline struct amp_mode_type amp_get_mode(
const struct amp_type * ansmap,
long x,
long y
);
static inline uint8_t * amp_get_mode_data(
const struct amp_type * amp,
long x,
long y
);
static inline ssize_t amp_get_cell_index(
const struct amp_type * ansmap,
long x,
long y
);
static inline int amp_utf8_code_point_size(
const char * utf8_str,
size_t utf8_str_size
);
static inline size_t amp_utf8_code_point_count(
const char * utf8_str,
size_t utf8_str_size
);
static inline size_t amp_str_seg_style_sign_count(
const char * str,
size_t str_size
);
static inline struct amp_mode_code_type amp_mode_to_codes(
struct amp_mode_type mode,
AMP_PALETTE palette
);
static inline struct amp_mode_code_type amp_mode_code_update(
struct amp_mode_code_type next_codes,
struct amp_mode_code_type prev_codes
);
static inline struct amp_mode_type amp_mode_cell_deserialize(
const uint8_t * src,
size_t src_size
);
static inline bool amp_mode_cell_serialize(
struct amp_mode_type mode,
uint8_t * dst,
size_t dst_size
);
static inline size_t amp_sub_size(
size_t a,
size_t b
);
static inline size_t amp_str_append(
char * str_dst,
size_t str_dst_size,
const char * str_src
);
static inline struct amp_color_type amp_find_color(
struct amp_rgb_type rgb
);
static inline const char * amp_str_seg_first_line_size(
const char * str,
size_t str_size,
size_t * line_size
);
static inline const char * amp_str_seg_skip_any_utf8_symbol(
const char * str,
size_t str_size
);
static inline const char * amp_str_seg_skip_str(
const char * str_seg,
size_t str_seg_size,
const char * str
);
static inline size_t amp_str_seg_width(
const char * str,
size_t str_size
);
static inline size_t amp_rich_str_seg_width(
const char * rich_str,
size_t rich_str_size
);
static inline void amp_print_line_clip(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
AMP_ALIGN text_alignment,
const char * text_str,
size_t text_str_size
);
static inline void amp_print_rich_line_clip(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE * text_style,
AMP_ALIGN text_alignment,
const char * text_str,
size_t text_str_size
);
static inline size_t amp_print_text_clip(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE text_style,
uint32_t text_max_width,
AMP_ALIGN text_alignment,
const char * text_str,
size_t text_str_size
);
static inline size_t amp_print_rich_text_clip(
struct amp_type * ansmap,
long text_x,
long text_y,
AMP_STYLE * text_style,
uint32_t text_max_width,
AMP_ALIGN text_alignment,
const char * text_str,
size_t text_str_size
);
static inline const char * amp_str_seg_skip_spaces(
const char * str,
size_t str_size
);
static inline const char * amp_str_seg_skip_wrap(
const char * str,
size_t str_size,
size_t wrap_width
);
static inline const char * amp_rich_str_seg_skip_wrap(
const char * rich_str,
size_t rich_str_size,
size_t wrap_width
);
static inline const char * amp_str_seg_skip_word(
const char * str,
size_t str_size
);
static inline const char * amp_str_seg_skip_width(
const char * str,
size_t str_size,
size_t max_width
);
static inline const char * amp_str_seg_skip_style_sign(
const char * str,
size_t str_sz
);
static inline const char * amp_str_seg_skip_style_and_spaces(
const char * str,
size_t str_sz
);
static inline const char * amp_str_seg_skip_style_and_word(
const char * str,
size_t str_sz
);
static inline const char * amp_str_seg_skip_styles(
const char * str,
size_t str_sz
);
static inline const char * amp_str_seg_skip_style_and_utf8_symbol(
const char * str,
size_t str_sz
);
static inline
struct amp_inline_style_type amp_lookup_inline_style(
const char * glyph
);
static inline
struct amp_style_flag_type amp_lookup_style_flag(
AMP_STYLE
);
static inline
struct amp_color_combo_type amp_lookup_color_combo(
struct amp_rgb_type rgb
);
static inline ssize_t amp_encode_layer(
const struct amp_type * ansmap,
AMP_SETTINGS settings,
AMP_STYLE style,
char * buffer,
size_t buffer_size
);
static inline ssize_t amp_encode_layer_row(
const struct amp_type * ansmap,
long row_y,
AMP_STYLE style,
char * buffer,
size_t buffer_size
);
static inline ssize_t amp_encode_layer_cell(
const struct amp_type * ansmap,
long x,
long y,
AMP_STYLE style,
char * buffer,
size_t buffer_size
);
static inline AMP_STYLE amp_styles_to_layer(
const struct amp_type * ansmap,
AMP_STYLE whitelist
);
static inline uint32_t amp_doc_seg_parse_width(
const char * str,
size_t str_sz
);
static inline size_t amp_decode_glyphs(
struct amp_type * amp,
const char * str,
size_t str_sz
);
static inline size_t amp_decode_styles(
struct amp_type * amp,
const char * str,
size_t str_sz
);
////////////////////////////////////////////////////////////////////////////////
// AMP_FLAG_INDEX is a constant expression that determines the index of a single
// one-bit in the unsigned integer argument a. It is based on binary search and
// is 50% slower than the stdc_first_trailing_one_ull function (stdbit.h).
#define AMP_FLAG_INDEX(a) ( \
(a) < (1ULL << 32) ? ( \
(a) < (1ULL << 16) ? ( \
(a) < (1ULL << 8) ? ( \
(a) < (1ULL << 4) ? ( \
(a) < (1ULL << 2) ? ( \
(a) < (1ULL << 1) ? ( \
(a) == (1ULL << 0) ? (1) : (0) \
) : ( \
(a) == (1ULL << 1) ? (2) : (0) \
) \
) : ( \
(a) < (1ULL << 3) ? ( \
(a) == (1ULL << 2) ? (3) : (0) \
) : ( \
(a) == (1ULL << 3) ? (4) : (0) \
) \
) \
) : ( \
(a) < (1ULL << 6) ? ( \
(a) < (1ULL << 5) ? ( \
(a) == (1ULL << 4) ? (5) : (0) \
) : ( \
(a) == (1ULL << 5) ? (6) : (0) \
) \
) : ( \
(a) < (1ULL << 7) ? ( \
(a) == (1ULL << 6) ? (7) : (0) \
) : ( \
(a) == (1ULL << 7) ? (8) : (0) \
) \
) \
) \
) : ( \
(a) < (1ULL << 12) ? ( \
(a) < (1ULL << 10) ? ( \
(a) < (1ULL << 9) ? ( \
(a) == (1ULL << 8) ? (9) : (0) \
) : ( \
(a) == (1ULL << 9) ? (10) : (0) \
) \
) : ( \
(a) < (1ULL << 11) ? ( \
(a) == (1ULL << 10) ? (11) : (0) \
) : ( \
(a) == (1ULL << 11) ? (12) : (0) \
) \
) \
) : ( \
(a) < (1ULL << 14) ? ( \
(a) < (1ULL << 13) ? ( \
(a) == (1ULL << 12) ? (13) : (0) \
) : ( \
(a) == (1ULL << 13) ? (14) : (0) \
) \
) : ( \
(a) < (1ULL << 15) ? ( \
(a) == (1ULL << 14) ? (15) : (0) \
) : ( \
(a) == (1ULL << 15) ? (16) : (0) \
) \
) \
) \
) \
) : ( \
(a) < (1ULL << 24) ? ( \
(a) < (1ULL << 20) ? ( \
(a) < (1ULL << 18) ? ( \
(a) < (1ULL << 17) ? ( \
(a) == (1ULL << 16) ? (17) : (0) \
) : ( \
(a) == (1ULL << 17) ? (18) : (0) \
) \
) : ( \
(a) < (1ULL << 19) ? ( \
(a) == (1ULL << 18) ? (19) : (0) \
) : ( \
(a) == (1ULL << 19) ? (20) : (0) \
) \
) \
) : ( \
(a) < (1ULL << 22) ? ( \
(a) < (1ULL << 21) ? ( \
(a) == (1ULL << 20) ? (21) : (0) \
) : ( \
(a) == (1ULL << 21) ? (22) : (0) \
) \
) : ( \
(a) < (1ULL << 23) ? ( \
(a) == (1ULL << 22) ? (23) : (0) \
) : ( \
(a) == (1ULL << 23) ? (24) : (0) \
) \
) \
) \
) : ( \
(a) < (1ULL << 28) ? ( \
(a) < (1ULL << 26) ? ( \
(a) < (1ULL << 25) ? ( \
(a) == (1ULL << 24) ? (25) : (0) \
) : ( \
(a) == (1ULL << 25) ? (26) : (0) \
) \
) : ( \
(a) < (1ULL << 27) ? ( \
(a) == (1ULL << 26) ? (27) : (0) \
) : ( \
(a) == (1ULL << 27) ? (28) : (0) \
) \
) \
) : ( \
(a) < (1ULL << 30) ? ( \
(a) < (1ULL << 29) ? ( \
(a) == (1ULL << 28) ? (29) : (0) \
) : ( \
(a) == (1ULL << 29) ? (30) : (0) \
) \
) : ( \
(a) < (1ULL << 31) ? ( \
(a) == (1ULL << 30) ? (31) : (0) \
) : ( \
(a) == (1ULL << 31) ? (32) : (0) \
) \
) \
) \
) \
) \
) : ( \
(a) < (1ULL << 48) ? ( \
(a) < (1ULL << 40) ? ( \
(a) < (1ULL << 36) ? ( \
(a) < (1ULL << 34) ? ( \
(a) < (1ULL << 33) ? ( \
(a) == (1ULL << 32) ? (33) : (0) \
) : ( \
(a) == (1ULL << 33) ? (34) : (0) \
) \
) : ( \
(a) < (1ULL << 35) ? ( \
(a) == (1ULL << 34) ? (35) : (0) \
) : ( \
(a) == (1ULL << 35) ? (36) : (0) \
) \