summaryrefslogtreecommitdiff
path: root/src/select/format_list_style.c
blob: 46861a4f4131289ffeb1a5e0f6f8526e408c8b27 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
/*
 * This file is part of LibCSS
 * Licensed under the MIT License,
 *		  http://www.opensource.org/licenses/mit-license.php
 * Copyright 2021 Vincent Sanders <vince@netsurf-browser.org>
 */

#include "select/propget.h"
#include "utils/utils.h"

#define SYMBOL_SIZE 5
typedef char symbol_t[SYMBOL_SIZE];

struct list_counter_style {
	const char *name; /**< style name for debug purposes */
	struct {
		const int start; /**< first acceptable value for this style */
		const int end; /**< last acceptable value for this style */
	} range;
	struct {
		const unsigned int length;
		const symbol_t value;
	} pad;
	struct {
		const char *pre;
		const char *post;
	} negative;
	const char *prefix;
	const char *suffix;
	const symbol_t *symbols; /**< array of symbols which represent this style */
	const int *weights; /**< symbol weights for additive schemes */
	const size_t items; /**< items in symbol and weight table */
	size_t (*calc)(uint8_t *ares, const size_t alen, int value, const struct list_counter_style *cstyle); /**< function to calculate the system */
};

/**
 * numeric representation of the value using a system
 */
struct numeric {
	uint8_t *val; /* buffer containing the numeric values */
	size_t len; /* length of alen */
	size_t used; /* number of numeric values used */
	bool negative; /* if the value is negative */
};

/**
 * Copy a null-terminated UTF-8 string to buffer at offset, if there is space
 *
 * \param[in] buf    The output buffer
 * \param[in] buflen The length of \a buf
 * \param[in] pos    Current position in \a buf
 * \param[in] str    The string to copy into \a buf
 * \return The number of bytes needed in the output buffer which may be
 *         larger than \a buflen but the buffer will not be overrun
 */
static inline size_t
copy_string(char *buf, const size_t buflen, size_t pos, const char *str)
{
	size_t sidx = 0; /* current string index */

	while (str[sidx] != '\0') {
		if (pos < buflen) {
			buf[pos] = str[sidx];
		}
		pos++;
		sidx++;
	}

	return sidx;
}

/**
 * Copy a UTF-8 symbol to buffer at offset, if there is space
 *
 * \param[in] buf    The output buffer
 * \param[in] buflen The length of \a buf
 * \param[in] pos    Current position in \a buf
 * \param[in] symbol The symbol to copy into \a buf
 * \return The number of bytes needed in the output buffer which may be
 *         larger than \a buflen but the buffer will not be overrun
 */
static inline size_t
copy_symbol(char *buf, const size_t buflen, size_t pos, const symbol_t symbol)
{
	size_t sidx = 0; /* current symbol index */

	while ((sidx < sizeof(symbol_t)) && (symbol[sidx] != '\0')) {
		if (pos < buflen) {
			buf[pos] = symbol[sidx];
		}
		pos++;
		sidx++;
	}

	return sidx;
}

/**
 * maps numeric values to output values with a symbol table
 *
 * Takes a list of numeric values and for each one outputs the
 *   compete symbol (in utf8) to an output buffer.
 *
 * \param buf The output buffer
 * \param buflen the length of \a buf
 * \param aval array of alphabet values
 * \param alen The number of values in \a alen
 * \param symtab The symbol table
 * \param symtablen The number of symbols in \a symtab
 * \return The number of bytes needed in the output buffer whichmay be
 *         larger than \a buflen but the buffer will not be overrun
 */
static size_t
nval_to_symbols(struct numeric *nval,
		const struct list_counter_style *cstyle,
		char *buf, const size_t buflen)
{
	size_t oidx = 0;
	size_t aidx; /* numeral index */
	const char *suffix = "."; /* default sufffix string */
	const char *negative_pre = "-"; /* default negative string */

	/* prefix */
	if (cstyle->prefix != NULL) {
		oidx += copy_string(buf, buflen, oidx, cstyle->prefix);
	}

	/* negative pre */
	if (nval->negative) {
		if (cstyle->negative.pre != NULL) {
			negative_pre = cstyle->negative.pre;
		}
		oidx += copy_string(buf, buflen, oidx, negative_pre);
	}

	/* add padding if required */
	if (nval->used < cstyle->pad.length) {
		size_t pidx; /* padding index */
		for (pidx = cstyle->pad.length - nval->used; pidx > 0; pidx--) {
			oidx += copy_symbol(buf, buflen, oidx,
					cstyle->pad.value);
		}
	}

	/* map symbols */
	for (aidx=0; aidx < nval->used; aidx++) {
		oidx += copy_symbol(buf, buflen, oidx,
				cstyle->symbols[nval->val[aidx]]);
	}

	/* negative post */
	if ((nval->negative) && (cstyle->negative.post != NULL)) {
		oidx += copy_string(buf, buflen, oidx, cstyle->negative.post);
	}

	/* suffix */
	if (cstyle->suffix != NULL) {
		suffix = cstyle->suffix;
	}
	oidx += copy_string(buf, buflen, oidx, suffix);

	return oidx;
}


/**
 * generate numeric symbol values
 *
 * fills array with numeric values that represent the input value
 *
 * \param ares Buffer to recive the converted values
 * \param alen the length of \a ares buffer
 * \param value The value to convert
 * \param cstyle The counter style in use
 * \return The length a complete conversion which may be larger than \a alen
 */
static size_t
calc_numeric_system(uint8_t *ares,
		    const size_t alen,
		    int value,
		    const struct list_counter_style *cstyle)
{
	size_t idx = 0;
	uint8_t *first;
	uint8_t *last;

	if (value == 0) {
		if (alen >= 1) {
			ares[0] = 0;
		}
		return 1;
	}

	value = abs(value);


	/* generate alphabet values in ascending order */
	while (value > 0) {
		if (idx < alen) {
			ares[idx] = value % cstyle->items;
		}
		idx++;
		value = value / cstyle->items;
	}

	/* put the values in decending order */
	first = ares;
	if (idx < alen) {
		last = first + (idx - 1);
	} else {
		last = first + (alen - 1);
	}
	while (first < last) {
		*first ^= *last;
		*last ^= *first;
		*first ^= *last;
		first++;
		last--;
	}

	return idx;
}


/**
 * generate cyclic symbol values
 *
 * fills array with cyclic values that represent the input value
 *
 * \param ares Buffer to recive the converted values
 * \param alen the length of \a ares buffer
 * \param value The value to convert
 * \param cstyle The counter style in use
 * \return The length a complete conversion which may be larger than \a alen
 */
static size_t
calc_cyclic_system(uint8_t *ares,
		    const size_t alen,
		    int value,
		    const struct list_counter_style *cstyle)
{
	if (alen == 0) {
		return 0;
	}
	if (cstyle->items == 1) {
		/* there is only one symbol so select it */
		ares[0] = 0;
	} else {
		ares[0] = (value - 1) % cstyle->items;
	}
	return 1;
}


/**
 * generate addative symbol values
 *
 * fills array with numeric values that represent the input value
 *
 * \param ares Buffer to recive the converted values
 * \param alen the length of \a ares buffer
 * \param value The value to convert
 * \param wlen The number of weights
 * \return The length a complete conversion which may be larger than \a alen
 */
static size_t
calc_additive_system(uint8_t *ares,
		     const size_t alen,
		     int value,
		     const struct list_counter_style *cstyle)
{
	size_t widx; /* weight index */
	size_t aidx = 0;
	size_t idx;
	size_t times; /* number of times a weight occours */

	/* ensure value is within acceptable range of this system */
	if ((value < cstyle->range.start) || (value > cstyle->range.end)) {
		return 0;
	}

	/* implementation cannot cope with negatives */
	if (value < 1) {
		return 0;
	}

	/* iterate over the available weights */
	for (widx = 0; widx < cstyle->items;widx++) {
		times = value / cstyle->weights[widx];
		if (times > 0) {
			for (idx=0;idx < times;idx++) {
				if (aidx < alen) {
					ares[aidx] = widx;
				}
				aidx++;
			}

			value -= times * cstyle->weights[widx];
		}
	}

	return aidx;
}


/**
 * generate alphabet symbol values for latin and greek labelling
 *
 * fills array with alphabet values suitable for the input value
 *
 * \param ares Buffer to recive the converted values
 * \param alen the length of \a ares buffer
 * \param value The value to convert
 * \param slen The number of symbols in the alphabet
 * \return The length a complete conversion which may be larger than \a alen
 */
static size_t
calc_alphabet_system(uint8_t *ares,
		     const size_t alen,
		     int value,
		     const struct list_counter_style *cstyle)
{
	size_t idx = 0;
	uint8_t *first;
	uint8_t *last;

	/* generate alphabet values in ascending order */
	while (value > 0) {
		--value;
		if (idx < alen) {
			ares[idx] = value % cstyle->items;
		}
		idx++;
		value = value / cstyle->items;
	}

	/* put the values in decending order */
	first = ares;
	if (idx < alen) {
		last = first + (idx - 1);
	} else {
		last = first + (alen - 1);
	}
	while (first < last) {
		*first ^= *last;
		*last ^= *first;
		*first ^= *last;
		first++;
		last--;
	}

	return idx;
}


/**
 * Roman numeral conversion
 *
 * \return The number of numerals that are nesesary for full output
 */
static size_t
calc_roman_system(uint8_t *buf,
		  const size_t maxlen,
		  int value,
		  const struct list_counter_style *cstyle)
{
	const int S[]  = {    0,   2,   4,   2,   4,   2,   4 };
	size_t k = 0; /* index into output buffer */
	unsigned int i = 0; /* index into maps */
	int r;
	int r2 = 0;
	size_t L;

	assert(cstyle->items == 7);

	/* ensure value is within acceptable range of this system */
	if ((value < cstyle->range.start) || (value > cstyle->range.end)) {
		return 0;
	}

	L = cstyle->items - 1;

	while (value > 0) {
		if (cstyle->weights[i] <= value) {
			r = value / cstyle->weights[i];
			value = value - (r * cstyle->weights[i]);
			if (i < L) {
				/* lookahead */
				r2 = value / cstyle->weights[i+1];
			}
			if (i < L && r2 >= S[i+1]) {
				/* will violate repeat boundary on next pass */
				value = value - (r2 * cstyle->weights[i+1]);
				if (k < maxlen) buf[k++] = i+1;
				if (k < maxlen) buf[k++] = i-1;
			} else if (S[i] && r >= S[i]) {
				/* violated repeat boundary on this pass */
				if (k < maxlen) buf[k++] = i;
				if (k < maxlen) buf[k++] = i-1;
			} else {
				while (r-- > 0 && k < maxlen) {
					buf[k++] = i;
				}
			}
		}
		i++;
	}

	return k;
}


/* tables for all the counter styles */


static const symbol_t georgian_symbols[] = {
	                                        "ჵ",
	"ჰ", "ჯ", "ჴ", "ხ", "ჭ", "წ", "ძ", "ც", "ჩ",
	"შ", "ყ", "ღ", "ქ", "ფ", "ჳ", "ტ", "ს", "რ",
	"ჟ", "პ", "ო", "ჲ", "ნ", "მ", "ლ", "კ", "ი",
	"თ", "ჱ", "ზ", "ვ", "ე", "დ", "გ", "ბ", "ა",
};
static const int georgian_weights[] = {
	                                                10000,
	9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000,
	900,  800,  700,  600,  500,  400,  300,  200,  100,
	90,   80,   70,   60,   50,   40,   30,   20,   10,
	9,    8,    7,    6,    5,    4,    3,    2,    1
};
static const struct list_counter_style lcs_georgian =	{
	.name="georgian",
	.range = {
		.start = 1,
		.end = 19999,},
	.symbols = georgian_symbols,
	.weights = georgian_weights,
	.items = (sizeof(georgian_symbols) / SYMBOL_SIZE),
	.calc = calc_additive_system,
};


static const symbol_t upper_armenian_symbols[] = {
	"Ք", "Փ", "Ւ", "Ց", "Ր", "Տ", "Վ", "Ս", "Ռ",
	"Ջ", "Պ", "Չ", "Ո", "Շ", "Ն", "Յ", "Մ", "Ճ",
	"Ղ", "Ձ", "Հ", "Կ", "Ծ", "Խ", "Լ", "Ի", "Ժ",
	"Թ", "Ը", "Է", "Զ", "Ե", "Դ", "Գ", "Բ", "Ա"
};
static const int armenian_weights[] = {
	9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000,
	900,  800,  700,  600,  500,  400,  300,  200,  100,
	90,   80,   70,   60,   50,   40,   30,   20,   10,
	9,    8,    7,    6,    5,    4,    3,    2,    1
};
static const struct list_counter_style lcs_upper_armenian = {
	.name = "upper-armenian",
	.range = {
		.start = 1,
		.end = 9999,},
	.symbols = upper_armenian_symbols,
	.weights = armenian_weights,
	.items = (sizeof(upper_armenian_symbols) / SYMBOL_SIZE),
	.calc = calc_additive_system,
};


static const symbol_t lower_armenian_symbols[] = {
	"ք", "փ", "ւ", "ց", "ր", "տ", "վ", "ս", "ռ",
	"ջ", "պ", "չ", "ո", "շ", "ն", "յ", "մ", "ճ",
	"ղ", "ձ", "հ", "կ", "ծ", "խ", "լ", "ի", "ժ",
	"թ", "ը", "է", "զ", "ե", "դ", "գ", "բ", "ա"
};
static const struct list_counter_style lcs_lower_armenian = {
	.name = "lower-armenian",
	.range = {
		.start = 1,
		.end = 9999,},
	.symbols = lower_armenian_symbols,
	.weights = armenian_weights,
	.items = (sizeof(lower_armenian_symbols) / SYMBOL_SIZE),
	.calc = calc_additive_system,
};


static const symbol_t decimal_symbols[] = {
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
};
static const struct list_counter_style lcs_decimal = {
	.name = "decimal",
	.symbols = decimal_symbols,
	.items = (sizeof(decimal_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};


static const struct list_counter_style lcs_decimal_leading_zero = {
	.name = "decimal-leading-zero",
	.pad = {
		.length = 2,
		.value = "0",},
	.symbols = decimal_symbols,
	.items = (sizeof(decimal_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};


static const symbol_t lower_greek_symbols[] = {
	"α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ",
	"λ", "μ", "ν", "ξ", "ο", "π", "ρ", "σ", "τ", "υ",
	"φ", "χ", "ψ", "ω"
};
static const struct list_counter_style lcs_lower_greek = {
	.name = "lower-greek",
	.symbols = lower_greek_symbols,
	.items = (sizeof(lower_greek_symbols) / SYMBOL_SIZE),
	.calc = calc_alphabet_system,
};


static const symbol_t upper_alpha_symbols[] = {
	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
	"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
	"U", "V", "W", "X", "Y", "Z"
};
static const struct list_counter_style lcs_upper_alpha = {
	.name = "upper-alpha",
	.symbols = upper_alpha_symbols,
	.items = (sizeof(upper_alpha_symbols) / SYMBOL_SIZE),
	.calc = calc_alphabet_system,
};


static const symbol_t lower_alpha_symbols[] = {
	"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
	"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
	"u", "v", "w", "x", "y", "z"
};
static struct list_counter_style lcs_lower_alpha = {
	.name = "lower-alpha",
	.symbols = lower_alpha_symbols,
	.items = (sizeof(lower_alpha_symbols) / SYMBOL_SIZE),
	.calc = calc_alphabet_system,
};


static const int roman_weights[] = {
	1000, 500, 100,  50,  10,   5,   1
};
static const symbol_t upper_roman_symbols[] = {
	"M", "D", "C", "L", "X", "V", "I"
};
static const struct list_counter_style lcs_upper_roman = {
	.name = "upper-roman",
	.range = {
		.start = 1,
		.end = 3999,},
	.symbols = upper_roman_symbols,
	.weights = roman_weights,
	.items = (sizeof(upper_roman_symbols) / SYMBOL_SIZE),
	.calc = calc_roman_system,
};


static const symbol_t lower_roman_symbols[] = {
	"m", "d", "c", "l", "x", "v", "i"
};
static const struct list_counter_style lcs_lower_roman = {
	.name = "lower-roman",
	.range = {
		.start = 1,
		.end = 3999,},
	.symbols = lower_roman_symbols,
	.weights = roman_weights,
	.items = (sizeof(lower_roman_symbols) / SYMBOL_SIZE),
	.calc = calc_roman_system,
};

static const symbol_t disc_symbols[] = { "\xE2\x80\xA2"}; /* 2022 BULLET */
static const struct list_counter_style lcs_disc = {
	.name = "disc",
	.symbols = disc_symbols,
	.items = (sizeof(disc_symbols) / SYMBOL_SIZE),
	.suffix = " ",
	.calc = calc_cyclic_system,
};

static const symbol_t circle_symbols[] = { "\342\227\213"}; /* 25CB WHITE CIRCLE */
static const struct list_counter_style lcs_circle = {
	.name = "circle",
	.symbols = circle_symbols,
	.items = (sizeof(circle_symbols) / SYMBOL_SIZE),
	.suffix = " ",
	.calc = calc_cyclic_system,
};

static const symbol_t square_symbols[] = { "\342\226\252"}; /* 25AA BLACK SMALL SQUARE */
static const struct list_counter_style lcs_square = {
	.name = "square",
	.symbols = square_symbols,
	.items = (sizeof(square_symbols) / SYMBOL_SIZE),
	.suffix = " ",
	.calc = calc_cyclic_system,
};

static const symbol_t binary_symbols[] = { "0", "1" };
static const struct list_counter_style lcs_binary = {
	.name = "binary",
	.symbols = binary_symbols,
	.items = (sizeof(binary_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t octal_symbols[] = {
	"0", "1", "2", "3", "4", "5", "6", "7"
};
static const struct list_counter_style lcs_octal = {
	.name = "octal",
	.symbols = octal_symbols,
	.items = (sizeof(octal_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};


static const symbol_t lower_hexadecimal_symbols[] = {
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
	"a", "b", "c", "d", "e", "f"
};
static const struct list_counter_style lcs_lower_hexadecimal = {
	.name = "lower-hexadecimal",
	.symbols = lower_hexadecimal_symbols,
	.items = (sizeof(lower_hexadecimal_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t upper_hexadecimal_symbols[] = {
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
	"a", "b", "c", "d", "e", "f"
};
static const struct list_counter_style lcs_upper_hexadecimal = {
	.name = "upper-hexadecimal",
	.symbols = upper_hexadecimal_symbols,
	.items = (sizeof(upper_hexadecimal_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t arabic_indic_symbols[] = {
	"\xd9\xa0", "\xd9\xa1", "\xd9\xa2", "\xd9\xa3", "\xd9\xa4", "\xd9\xa5", "\xd9\xa6", "\xd9\xa7", "\xd9\xa8", "\xd9\xa9"
};
static const struct list_counter_style lcs_arabic_indic = {
	.name = "arabic-indic",
	.symbols = arabic_indic_symbols,
	.items = (sizeof(arabic_indic_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t bengali_symbols[] = {
	"০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯"
};
static const struct list_counter_style lcs_bengali = {
	.name = "bengali",
	.symbols = bengali_symbols,
	.items = (sizeof(bengali_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t cambodian_symbols[] = {
	"០", "១", "២", "៣", "៤", "៥", "៦", "៧", "៨", "៩"
};
static const struct list_counter_style lcs_cambodian = {
	.name = "cambodian",
	.symbols = cambodian_symbols,
	.items = (sizeof(cambodian_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t cjk_decimal_symbols[] = {
	"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"
};
static const struct list_counter_style lcs_cjk_decimal = {
	.name = "cjk-decimal",
	.symbols = cjk_decimal_symbols,
	.suffix = "、",
	.items = (sizeof(cjk_decimal_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t devanagari_symbols[] = {
	"०", "१", "२", "३", "४", "५", "६", "७", "८", "९"
};
static const struct list_counter_style lcs_devanagari = {
	.name = "devanagari",
	.symbols = devanagari_symbols,
	.items = (sizeof(devanagari_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t gujarati_symbols[] = {
	"૦", "૧", "૨", "૩", "૪", "૫", "૬", "૭", "૮", "૯"
};
static const struct list_counter_style lcs_gujarati = {
	.name = "gujarati",
	.symbols = gujarati_symbols,
	.items = (sizeof(gujarati_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t gurmukhi_symbols[] = {
	"੦", "੧", "੨", "੩", "੪", "੫", "੬", "੭", "੮", "੯"
};
static const struct list_counter_style lcs_gurmukhi = {
	.name = "gurmukhi",
	.symbols = gurmukhi_symbols,
	.items = (sizeof(gurmukhi_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const int hebrew_weights[] = {
				                                          10000,
	9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000,                   1000,
	                              400,  300,  200,                    100,
	90,   80,   70,   60,   50,   40,   30,   20, 19, 18, 17, 16, 15, 10,
	9,    8,    7,    6,    5,    4,    3,    2,                      1
};
static const symbol_t hebrew_symbols[] = {
	"\xD7\x99\xD7\xB3",
	"\xD7\x98\xD7\xB3", "\xD7\x97\xD7\xB3", "\xD7\x96\xD7\xB3", "\xD7\x95\xD7\xB3", "\xD7\x94\xD7\xB3", "\xD7\x93\xD7\xB3", "\xD7\x92\xD7\xB3", "\xD7\x91\xD7\xB3", "\xD7\x90\xD7\xB3",
	"\xD7\xAA", "\xD7\xA9", "\xD7\xA8", "\xD7\xA7",
	"\xD7\xA6", "\xD7\xA4", "\xD7\xA2", "\xD7\xA1", "\xD7\xA0", "\xD7\x9E", "\xD7\x9C", /* 20 */"\xD7\x9B", "\xD7\x99\xD7\x98", "\xD7\x99\xD7\x97", "\xD7\x99\xD7\x96", "\xD7\x98\xD7\x96", "\xD7\x98\xD7\x95", "\xD7\x99",
	"\xD7\x98", "\xD7\x97", "\xD7\x96", "\xD7\x95", "\xD7\x94", "\xD7\x93", "\xD7\x92", "\xD7\x91", "\xD7\x90"
};
static const struct list_counter_style lcs_hebrew = {
	.name = "hebrew",
	.range = {
		.start = 1,
		.end = 10999,},
	.symbols = hebrew_symbols,
	.weights = hebrew_weights,
	.items = (sizeof(hebrew_symbols) / SYMBOL_SIZE),
	.calc = calc_additive_system,
};

static const symbol_t kannada_symbols[] = {
	"\xE0\xB3\xA6", "\xE0\xB3\xA7", "\xE0\xB3\xA8", "\xE0\xB3\xA9", "\xE0\xB3\xAA", "\xE0\xB3\xAB", "\xE0\xB3\xAC", "\xE0\xB3\xAD", "\xE0\xB3\xAE", "\xE0\xB3\xAF"
};
static const struct list_counter_style lcs_kannada = {
	.name = "kannada",
	.symbols = kannada_symbols,
	.items = (sizeof(kannada_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t lao_symbols[] = {
	"໐", "໑", "໒", "໓", "໔", "໕", "໖", "໗", "໘", "໙"
};
static const struct list_counter_style lcs_lao = {
	.name = "lao",
	.symbols = lao_symbols,
	.items = (sizeof(lao_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t malayalam_symbols[] = {
	"൦", "൧", "൨", "൩", "൪", "൫", "൬", "൭", "൮", "൯"
};
static const struct list_counter_style lcs_malayalam = {
	.name = "malayalam",
	.symbols = malayalam_symbols,
	.items = (sizeof(malayalam_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t mongolian_symbols[] = {
	"᠐", "᠑", "᠒", "᠓", "᠔", "᠕", "᠖", "᠗", "᠘", "᠙"
};
static const struct list_counter_style lcs_mongolian = {
	.name = "mongolian",
	.symbols = mongolian_symbols,
	.items = (sizeof(mongolian_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t myanmar_symbols[] = {
	"၀", "၁", "၂", "၃", "၄", "၅", "၆", "၇", "၈", "၉"
};
static const struct list_counter_style lcs_myanmar = {
	.name = "myanmar",
	.symbols = myanmar_symbols,
	.items = (sizeof(myanmar_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t oriya_symbols[] = {
	"୦", "୧", "୨", "୩", "୪", "୫", "୬", "୭", "୮", "୯"
};
static const struct list_counter_style lcs_oriya = {
	.name = "oriya",
	.symbols = oriya_symbols,
	.items = (sizeof(oriya_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t persian_symbols[] = {
	"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"
};
static const struct list_counter_style lcs_persian = {
	.name = "persian",
	.symbols = persian_symbols,
	.items = (sizeof(persian_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t tamil_symbols[] = {
	"௦", "௧", "௨", "௩", "௪", "௫", "௬", "௭", "௮", "௯"
};
static const struct list_counter_style lcs_tamil = {
	.name = "tamil",
	.symbols = tamil_symbols,
	.items = (sizeof(tamil_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t telugu_symbols[] = {
	"౦", "౧", "౨", "౩", "౪", "౫", "౬", "౭", "౮", "౯"
};
static const struct list_counter_style lcs_telugu = {
	.name = "telugu",
	.symbols = telugu_symbols,
	.items = (sizeof(telugu_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t thai_symbols[] = {
	"๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"
};
static const struct list_counter_style lcs_thai = {
	.name = "thai",
	.symbols = thai_symbols,
	.items = (sizeof(thai_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t tibetan_symbols[] = {
	"༠", "༡", "༢", "༣", "༤", "༥", "༦", "༧", "༨", "༩"
};
static const struct list_counter_style lcs_tibetan = {
	.name = "tibetan",
	.symbols = tibetan_symbols,
	.items = (sizeof(tibetan_symbols) / SYMBOL_SIZE),
	.calc = calc_numeric_system,
};

static const symbol_t cjk_earthly_branch_symbols[] = {
	"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
};
static struct list_counter_style lcs_cjk_earthly_branch = {
	.name = "cjk-earthly-branch",
	.symbols = cjk_earthly_branch_symbols,
	.items = (sizeof(cjk_earthly_branch_symbols) / SYMBOL_SIZE),
	.suffix = "、",
	.calc = calc_alphabet_system,
};

static const symbol_t cjk_heavenly_stem_symbols[] = {
	"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"
};
static struct list_counter_style lcs_cjk_heavenly_stem = {
	.name = "cjk-heavenly-stem",
	.symbols = cjk_heavenly_stem_symbols,
	.items = (sizeof(cjk_heavenly_stem_symbols) / SYMBOL_SIZE),
	.suffix = "、",
	.calc = calc_alphabet_system,
};

static const symbol_t hiragana_symbols[] = {
	"あ", "い", "う", "え", "お", "か", "き", "く", "け", "こ", "さ", "し", "す", "せ", "そ", "た", "ち", "つ", "て", "と", "な", "に", "ぬ", "ね", "の", "は", "ひ", "ふ", "へ", "ほ", "ま", "み", "む", "め", "も", "や", "ゆ", "よ", "ら", "り", "る", "れ", "ろ", "わ", "ゐ", "ゑ", "を", "ん"
};
static struct list_counter_style lcs_hiragana = {
	.name = "hiragana",
	.symbols = hiragana_symbols,
	.items = (sizeof(hiragana_symbols) / SYMBOL_SIZE),
	.suffix = "、",
	.calc = calc_alphabet_system,
};

static const symbol_t hiragana_iroha_symbols[] = {
	"い", "ろ", "は", "に", "ほ", "へ", "と", "ち", "り", "ぬ", "る", "を", "わ", "か", "よ", "た", "れ", "そ", "つ", "ね", "な", "ら", "む", "う", "ゐ", "の", "お", "く", "や", "ま", "け", "ふ", "こ", "え", "て", "あ", "さ", "き", "ゆ", "め", "み", "し", "ゑ", "ひ", "も", "せ", "す"
};
static struct list_counter_style lcs_hiragana_iroha = {
	.name = "hiragana-iroha",
	.symbols = hiragana_iroha_symbols,
	.items = (sizeof(hiragana_iroha_symbols) / SYMBOL_SIZE),
	.suffix = "、",
	.calc = calc_alphabet_system,
};

static const symbol_t katakana_symbols[] = {
	"ア", "イ", "ウ", "エ", "オ", "カ", "キ", "ク", "ケ", "コ", "サ", "シ", "ス", "セ", "ソ", "タ", "チ", "ツ", "テ", "ト", "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "ヒ", "フ", "ヘ", "ホ", "マ", "ミ", "ム", "メ", "モ", "ヤ", "ユ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ", "ヰ", "ヱ", "ヲ", "ン"
};
static struct list_counter_style lcs_katakana = {
	.name = "katakana",
	.symbols = katakana_symbols,
	.items = (sizeof(katakana_symbols) / SYMBOL_SIZE),
	.suffix = "、",
	.calc = calc_alphabet_system,
};

static const symbol_t katakana_iroha_symbols[] = {
	"イ", "ロ", "ハ", "ニ", "ホ", "ヘ", "ト", "チ", "リ", "ヌ", "ル", "ヲ", "ワ", "カ", "ヨ", "タ", "レ", "ソ", "ツ", "ネ", "ナ", "ラ", "ム", "ウ", "ヰ", "ノ", "オ", "ク", "ヤ", "マ", "ケ", "フ", "コ", "エ", "テ", "ア", "サ", "キ", "ユ", "メ", "ミ", "シ", "ヱ", "ヒ", "モ", "セ", "ス"
};
static struct list_counter_style lcs_katakana_iroha = {
	.name = "katakana-iroha",
	.symbols = katakana_iroha_symbols,
	.items = (sizeof(katakana_iroha_symbols) / SYMBOL_SIZE),
	.suffix = "、",
	.calc = calc_alphabet_system,
};


static const struct list_counter_style *
counter_style_from_computed_style(const css_computed_style *style)
{
	switch (get_list_style_type(style)) {
	case CSS_LIST_STYLE_TYPE_DISC:
		return &lcs_disc;
	case CSS_LIST_STYLE_TYPE_CIRCLE:
		return &lcs_circle;
	case CSS_LIST_STYLE_TYPE_SQUARE:
		return &lcs_square;
	case CSS_LIST_STYLE_TYPE_DECIMAL:
		return &lcs_decimal;
	case CSS_LIST_STYLE_TYPE_DECIMAL_LEADING_ZERO:
		return &lcs_decimal_leading_zero;
	case CSS_LIST_STYLE_TYPE_LOWER_ROMAN:
		return &lcs_lower_roman;
	case CSS_LIST_STYLE_TYPE_UPPER_ROMAN:
		return &lcs_upper_roman;
	case CSS_LIST_STYLE_TYPE_LOWER_GREEK:
		return &lcs_lower_greek;
	case CSS_LIST_STYLE_TYPE_LOWER_ALPHA:
	case CSS_LIST_STYLE_TYPE_LOWER_LATIN:
		return &lcs_lower_alpha;
	case CSS_LIST_STYLE_TYPE_UPPER_ALPHA:
	case CSS_LIST_STYLE_TYPE_UPPER_LATIN:
		return &lcs_upper_alpha;
	case CSS_LIST_STYLE_TYPE_UPPER_ARMENIAN:
	case CSS_LIST_STYLE_TYPE_ARMENIAN:
		return &lcs_upper_armenian;
	case CSS_LIST_STYLE_TYPE_GEORGIAN:
		return &lcs_georgian;
	case CSS_LIST_STYLE_TYPE_NONE:
		return NULL;
	case CSS_LIST_STYLE_TYPE_BINARY:
		return &lcs_binary;
	case CSS_LIST_STYLE_TYPE_OCTAL:
		return &lcs_octal;
	case CSS_LIST_STYLE_TYPE_LOWER_HEXADECIMAL:
		return &lcs_lower_hexadecimal;
	case CSS_LIST_STYLE_TYPE_UPPER_HEXADECIMAL:
		return &lcs_upper_hexadecimal;
	case CSS_LIST_STYLE_TYPE_ARABIC_INDIC:
		return &lcs_arabic_indic;
	case CSS_LIST_STYLE_TYPE_LOWER_ARMENIAN:
		return &lcs_lower_armenian;
	case CSS_LIST_STYLE_TYPE_BENGALI:
		return &lcs_bengali;
	case CSS_LIST_STYLE_TYPE_CAMBODIAN:
	case CSS_LIST_STYLE_TYPE_KHMER:
		return &lcs_cambodian;
	case CSS_LIST_STYLE_TYPE_CJK_DECIMAL:
		return &lcs_cjk_decimal;
	case CSS_LIST_STYLE_TYPE_DEVANAGARI:
		return &lcs_devanagari;
	case CSS_LIST_STYLE_TYPE_GUJARATI:
		return &lcs_gujarati;
	case CSS_LIST_STYLE_TYPE_GURMUKHI:
		return &lcs_gurmukhi;
	case CSS_LIST_STYLE_TYPE_HEBREW:
		return &lcs_hebrew;
	case CSS_LIST_STYLE_TYPE_KANNADA:
		return &lcs_kannada;
	case CSS_LIST_STYLE_TYPE_LAO:
		return &lcs_lao;
	case CSS_LIST_STYLE_TYPE_MALAYALAM:
		return &lcs_malayalam;
	case CSS_LIST_STYLE_TYPE_MONGOLIAN:
		return &lcs_mongolian;
	case CSS_LIST_STYLE_TYPE_MYANMAR:
		return &lcs_myanmar;
	case CSS_LIST_STYLE_TYPE_ORIYA:
		return &lcs_oriya;
	case CSS_LIST_STYLE_TYPE_PERSIAN:
		return &lcs_persian;
	case CSS_LIST_STYLE_TYPE_TAMIL:
		return &lcs_tamil;
	case CSS_LIST_STYLE_TYPE_TELUGU:
		return &lcs_telugu;
	case CSS_LIST_STYLE_TYPE_THAI:
		return &lcs_thai;
	case CSS_LIST_STYLE_TYPE_TIBETAN:
		return &lcs_tibetan;
	case CSS_LIST_STYLE_TYPE_CJK_EARTHLY_BRANCH:
		return &lcs_cjk_earthly_branch;
	case CSS_LIST_STYLE_TYPE_CJK_HEAVENLY_STEM:
		return &lcs_cjk_heavenly_stem;
	case CSS_LIST_STYLE_TYPE_HIAGANA:
		return &lcs_hiragana;
	case CSS_LIST_STYLE_TYPE_HIAGANA_IROHA:
		return &lcs_hiragana_iroha;
	case CSS_LIST_STYLE_TYPE_KATAKANA:
		return &lcs_katakana;
	case CSS_LIST_STYLE_TYPE_KATAKANA_IROHA:
		return &lcs_katakana_iroha;
	}
	return NULL;
}


/* exported interface defined in select.h */
css_error css_computed_format_list_style(
		const css_computed_style *style,
		int value,
		char *buffer,
		size_t buffer_length,
		size_t *format_length)
{
	uint8_t aval[20];
	const struct list_counter_style *cstyle;
	struct numeric nval = {
		.val = aval,
		.len = sizeof(aval),
		.used = 0,
		.negative = false
	};

	nval.negative = (value < 0)?true:false;

	cstyle = counter_style_from_computed_style(style);
	if (cstyle == NULL) {
		return CSS_BADPARM;
	}

	nval.used = cstyle->calc(aval, sizeof(aval), value, cstyle);

	/* ensure it is possible to calculate with the selected system */
	if ((nval.used == 0) || (nval.used >= sizeof(aval))) {
		/* retry in decimal */
		cstyle = &lcs_decimal;

		nval.used = cstyle->calc(aval, sizeof(aval), value, cstyle);
		if ((nval.used == 0) || (nval.used >= sizeof(aval))) {
			/* failed in decimal, give up */
			return CSS_INVALID;
		}
	}

	*format_length = nval_to_symbols(&nval,
					 cstyle,
					 buffer,
					 buffer_length);

	return CSS_OK;
}