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
|
#include <cassert>
#include <cmath>
#include "utest/utest.h"
#include "streq.h"
#include "strneq.h"
#include "style.h"
/// Dummy functions to keep linker happy
int sp_main_gui (int, char const**) { return 0; }
int sp_main_console (int, char const**) { return 0; }
/* Extracted mechanically from http://www.w3.org/TR/SVG11/types.html#ColorKeywords:
*
* tidy -wrap 999 < types.html 2> /dev/null |
* egrep '(prop|color-keyword)-value' |
* sed 's,<td><span class="prop-value">, {",;s/<td><span class="color-keyword-value">rgb(/", {/;s%).*%}},@%;s%</span></td>%%' |
* tr -d \\n |
* tr @ \\n
*/
static struct {
char const *color_keyword;
struct {unsigned r; unsigned g; unsigned b; } rgb;
} const color_keywords[] = {
{"aliceblue", {240, 248, 255}},
{"antiquewhite", {250, 235, 215}},
{"aqua", { 0, 255, 255}},
{"aquamarine", {127, 255, 212}},
{"azure", {240, 255, 255}},
{"beige", {245, 245, 220}},
{"bisque", {255, 228, 196}},
{"black", { 0, 0, 0}},
{"blanchedalmond", {255, 235, 205}},
{"blue", { 0, 0, 255}},
{"blueviolet", {138, 43, 226}},
{"brown", {165, 42, 42}},
{"burlywood", {222, 184, 135}},
{"cadetblue", { 95, 158, 160}},
{"chartreuse", {127, 255, 0}},
{"chocolate", {210, 105, 30}},
{"coral", {255, 127, 80}},
{"cornflowerblue", {100, 149, 237}},
{"cornsilk", {255, 248, 220}},
{"crimson", {220, 20, 60}},
{"cyan", { 0, 255, 255}},
{"darkblue", { 0, 0, 139}},
{"darkcyan", { 0, 139, 139}},
{"darkgoldenrod", {184, 134, 11}},
{"darkgray", {169, 169, 169}},
{"darkgreen", { 0, 100, 0}},
{"darkgrey", {169, 169, 169}},
{"darkkhaki", {189, 183, 107}},
{"darkmagenta", {139, 0, 139}},
{"darkolivegreen", { 85, 107, 47}},
{"darkorange", {255, 140, 0}},
{"darkorchid", {153, 50, 204}},
{"darkred", {139, 0, 0}},
{"darksalmon", {233, 150, 122}},
{"darkseagreen", {143, 188, 143}},
{"darkslateblue", { 72, 61, 139}},
{"darkslategray", { 47, 79, 79}},
{"darkslategrey", { 47, 79, 79}},
{"darkturquoise", { 0, 206, 209}},
{"darkviolet", {148, 0, 211}},
{"deeppink", {255, 20, 147}},
{"deepskyblue", { 0, 191, 255}},
{"dimgray", {105, 105, 105}},
{"dimgrey", {105, 105, 105}},
{"dodgerblue", { 30, 144, 255}},
{"firebrick", {178, 34, 34}},
{"floralwhite", {255, 250, 240}},
{"forestgreen", { 34, 139, 34}},
{"fuchsia", {255, 0, 255}},
{"gainsboro", {220, 220, 220}},
{"ghostwhite", {248, 248, 255}},
{"gold", {255, 215, 0}},
{"goldenrod", {218, 165, 32}},
{"gray", {128, 128, 128}},
{"grey", {128, 128, 128}},
{"green", { 0, 128, 0}},
{"greenyellow", {173, 255, 47}},
{"honeydew", {240, 255, 240}},
{"hotpink", {255, 105, 180}},
{"indianred", {205, 92, 92}},
{"indigo", { 75, 0, 130}},
{"ivory", {255, 255, 240}},
{"khaki", {240, 230, 140}},
{"lavender", {230, 230, 250}},
{"lavenderblush", {255, 240, 245}},
{"lawngreen", {124, 252, 0}},
{"lemonchiffon", {255, 250, 205}},
{"lightblue", {173, 216, 230}},
{"lightcoral", {240, 128, 128}},
{"lightcyan", {224, 255, 255}},
{"lightgoldenrodyellow", {250, 250, 210}},
{"lightgray", {211, 211, 211}},
{"lightgreen", {144, 238, 144}},
{"lightgrey", {211, 211, 211}},
{"lightpink", {255, 182, 193}},
{"lightsalmon", {255, 160, 122}},
{"lightseagreen", { 32, 178, 170}},
{"lightskyblue", {135, 206, 250}},
{"lightslategray", {119, 136, 153}},
{"lightslategrey", {119, 136, 153}},
{"lightsteelblue", {176, 196, 222}},
{"lightyellow", {255, 255, 224}},
{"lime", { 0, 255, 0}},
{"limegreen", { 50, 205, 50}},
{"linen", {250, 240, 230}},
{"magenta", {255, 0, 255}},
{"maroon", {128, 0, 0}},
{"mediumaquamarine", {102, 205, 170}},
{"mediumblue", { 0, 0, 205}},
{"mediumorchid", {186, 85, 211}},
{"mediumpurple", {147, 112, 219}},
{"mediumseagreen", { 60, 179, 113}},
{"mediumslateblue", {123, 104, 238}},
{"mediumspringgreen", { 0, 250, 154}},
{"mediumturquoise", { 72, 209, 204}},
{"mediumvioletred", {199, 21, 133}},
{"midnightblue", { 25, 25, 112}},
{"mintcream", {245, 255, 250}},
{"mistyrose", {255, 228, 225}},
{"moccasin", {255, 228, 181}},
{"navajowhite", {255, 222, 173}},
{"navy", { 0, 0, 128}},
{"oldlace", {253, 245, 230}},
{"olive", {128, 128, 0}},
{"olivedrab", {107, 142, 35}},
{"orange", {255, 165, 0}},
{"orangered", {255, 69, 0}},
{"orchid", {218, 112, 214}},
{"palegoldenrod", {238, 232, 170}},
{"palegreen", {152, 251, 152}},
{"paleturquoise", {175, 238, 238}},
{"palevioletred", {219, 112, 147}},
{"papayawhip", {255, 239, 213}},
{"peachpuff", {255, 218, 185}},
{"peru", {205, 133, 63}},
{"pink", {255, 192, 203}},
{"plum", {221, 160, 221}},
{"powderblue", {176, 224, 230}},
{"purple", {128, 0, 128}},
{"red", {255, 0, 0}},
{"rosybrown", {188, 143, 143}},
{"royalblue", { 65, 105, 225}},
{"saddlebrown", {139, 69, 19}},
{"salmon", {250, 128, 114}},
{"sandybrown", {244, 164, 96}},
{"seagreen", { 46, 139, 87}},
{"seashell", {255, 245, 238}},
{"sienna", {160, 82, 45}},
{"silver", {192, 192, 192}},
{"skyblue", {135, 206, 235}},
{"slateblue", {106, 90, 205}},
{"slategray", {112, 128, 144}},
{"slategrey", {112, 128, 144}},
{"snow", {255, 250, 250}},
{"springgreen", { 0, 255, 127}},
{"steelblue", { 70, 130, 180}},
{"tan", {210, 180, 140}},
{"teal", { 0, 128, 128}},
{"thistle", {216, 191, 216}},
{"tomato", {255, 99, 71}},
{"turquoise", { 64, 224, 208}},
{"violet", {238, 130, 238}},
{"wheat", {245, 222, 179}},
{"white", {255, 255, 255}},
{"whitesmoke", {245, 245, 245}},
{"yellow", {255, 255, 0}},
{"yellowgreen", {154, 205, 50}},
{NULL, {0, 0, 0}}
};
static char const *const display_vals[] = {
"inline", "block", "list-item", "run-in", "compact", "marker", "table", "inline-table",
"table-row-group", "table-header-group", "table-footer-group", "table-row",
"table-column-group", "table-column", "table-cell", "table-caption", "none", NULL
};
static char const *const font_stretch_vals[] = {
"normal",
"wider",
"narrower",
"ultra-condensed",
"extra-condensed",
"condensed",
"semi-condensed",
"semi-expanded",
"expanded",
"extra-expanded",
"ultra-expanded",
NULL
};
static char const *const font_style_vals[] = {"normal", "italic", "oblique", NULL};
static char const *const font_variant_vals[] = {"normal", "small-caps", NULL};
static char const *const font_weight_vals[] = {"normal", "bold", "bolder", "lighter",
"100", "200", "300", "400", "500",
"600", "700", "800", "900", NULL};
static char const *const normal_val[] = {"normal", NULL};
static char const *const none_val[] = {"none", NULL};
static char const *const linecap_vals[] = {"butt", "round", "square", NULL};
static char const *const linejoin_vals[] = {"miter", "round", "bevel", NULL};
static char const *const text_anchor_vals[] = {"start", "middle", "end", NULL};
static char const *const visibility_vals[] = {"visible", "hidden", "collapse", NULL};
static char const *const writing_mode_vals[] = {"lr-tb", "rl-tb", "tb-rl", /*"lr", "rl", "tb",*/ NULL};
/* TODO: Inkscape correctly accepts lr,rl,tb, but reports them as lr-tb etc.
Either change inkscape or write custom test. */
static char const *const fill_rule_vals[] = {"nonzero", "evenodd", NULL};
static char const *const paint_enum_vals[] = {"none", "currentColor", NULL};
static gchar *
merge_then_write_string(gchar const *const str, guint const flags)
{
SPStyle *const style = sp_style_new();
sp_style_merge_from_style_string(style, str);
gchar *const ret = sp_style_write_string(style, flags);
sp_style_unref(style);
return ret;
}
static void
enum_val(char const prop[], char const *const vals[])
{
assert(vals);
assert(vals[0]);
for (unsigned i = 0; vals[i]; ++i) {
char *prop_eq_val = g_strdup_printf("%s:%s", prop, vals[i]);
gchar *ifset_str = merge_then_write_string(prop_eq_val, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(streq(ifset_str, prop_eq_val));
g_free(ifset_str);
g_free(prop_eq_val);
}
}
static void
color_val(char const prop[], char const *const dummy_vals[])
{
assert(dummy_vals == NULL);
char const *const extra_vals[] = {"blue", "#234", "#0100ff", NULL};
enum_val(prop, extra_vals);
#if 0
char const *color_vals[G_N_ELEMENTS(color_keywords) + G_N_ELEMENTS(extra_vals)];
for (unsigned i = 0; i < G_N_ELEMENTS(extra_vals); ++i) {
color_vals[i] = extra_vals[i];
}
for (unsigned i = 0; i < G_N_ELEMENTS(color_keywords); ++i) {
color_vals[G_N_ELEMENTS(extra_vals) + i] = color_keywords[i].color_keyword;
}
enum_val(prop, color_vals);
/* todo: other color stuff (rgb(), #123) */
#endif
}
static void
paint_val(char const prop[], char const *const dummy_vals[])
{
/* Ref: http://www.w3.org/TR/SVG11/painting.html#SpecifyingPaint */
assert(dummy_vals == NULL);
color_val(prop, NULL);
enum_val(prop, paint_enum_vals);
/* todo: uri, `<uri> none' etc. */
}
static void
number_val(char const prop[], double const min, double const max, double const eps)
{
assert(min <= max);
/* TODO */
double const propns[] = {0., .5, .25, .125,
(1.0 / (1 << 7)),
(1.0 / (1 << 8)),
(1.0 / (1 << 15)),
(1.0 / (1 << 23)),
(1.0 / (1 << 24)),
(1.0 / (1 << 25)),
(1.0 / (1 << 26)),
1e-5, 0.13, 1/7., 2/3., 10/11., 1.0};
size_t const prop_len = std::strlen(prop);
for (unsigned i = 0; i < G_N_ELEMENTS(propns); ++i) {
double const propn = propns[i];
double const val = ( propn == 0
? min
: ( propn == 1
? max
: min + (propns[i] * (max - min)) ) );
char val_str[35];
g_ascii_formatd(val_str, sizeof(val_str), "%.17f", val);
char *prop_eq_val = g_strdup_printf("%s:%s", prop, val_str);
gchar *ifset_str = merge_then_write_string(prop_eq_val, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(strneq(ifset_str, prop_eq_val, prop_len + 1));
char *endptr;
double const found_val = g_strtod(ifset_str + prop_len + 1, &endptr);
UTEST_ASSERT(*endptr == '\0');
if (fabs(val) < 1.) {
UTEST_ASSERT(std::fabs(found_val - val) < eps);
} else {
UTEST_ASSERT(std::fabs(found_val / val - 1.) < eps);
}
g_free(ifset_str);
g_free(prop_eq_val);
}
}
static void
miterlimit_val(char const prop[], char const *const dummy_vals[])
{
// Ref: http://www.w3.org/TR/SVG11/painting.html#StrokeMiterlimitProperty
assert(dummy_vals == NULL);
number_val(prop, 1., 20., 2e-7);
#if 0
static char const *const miterlimit_vals[] = {
"1", "1.0", "1.5", "2", "4", "8", "1e1", NULL
};
// bad values: <1, percentages, strings (none etc.).
#endif
}
static void
font_family_val(char const prop[], char const *const dummy_vals[])
{
assert(dummy_vals == NULL);
static char const *const generic_font_family_vals[] = {"serif", "sans-serif", "cursive", "fantasy", "monospace", NULL};
enum_val(prop, generic_font_family_vals);
/* todo: unrecognized fonts, comma-separated lists. */
}
/**
* "unitful length" is used for font-size, which we always write with an explicit unit (typically
* `px') for better CSS interoperability.
*/
static void
unitful_length_val(char const prop[], char const *const dummy_vals[])
{
/* todo */
assert(dummy_vals == NULL);
}
static void
length_val(char const prop[], char const *const dummy_vals[])
{
assert(dummy_vals == NULL);
number_val(prop, 0., 6., 1e-7);
unitful_length_val(prop, dummy_vals);
/* todo: exponential notation. See http://www.w3.org/TR/SVG11/types.html#DataTypeNumber for
where exponential notation is/isn't allowed. */
}
static void
length_or_enum_val(char const prop[], char const *const vals[])
{
length_val(prop, NULL);
enum_val(prop, vals);
}
static void
opacity_val(char const prop[], char const *const dummy_vals[])
{
assert(dummy_vals == NULL);
number_val(prop, 0., 1., 1.0 / (1 << 24));
/* todo: exponential notation */
}
static void
uri_or_enum_val(char const prop[], char const *const vals[])
{
enum_val(prop, vals);
/* todo: uri's */
}
static void
suppress_warning_log_handler(gchar const *log_domain, GLogLevelFlags /*log_level*/,
gchar const *message,
gpointer /*user_data*/)
{
/* todo: We could strncpy message to a static buffer for later testing with
* UTEST_ASSERT(streq(prev_message, exp_message)). */
}
static void
test_mul24(unsigned const a24, unsigned const b24)
{
assert(a24 <= SP_SCALE24_MAX);
assert(b24 <= SP_SCALE24_MAX);
unsigned const manual_prod24 = SP_SCALE24_FROM_FLOAT(SP_SCALE24_TO_FLOAT(a24) *
SP_SCALE24_TO_FLOAT(b24) );
unsigned const prod24 = SP_SCALE24_MUL(a24, b24);
UTEST_ASSERT_SHOW(prod24 == manual_prod24,
("MUL gives 0x%06x=%g, whereas explicit conversions give 0x%06x=%g;\n"
"multiplicands: 0x%06x * 0x%06x (i.e. %g * %g)",
prod24, SP_SCALE24_TO_FLOAT(prod24),
manual_prod24, SP_SCALE24_TO_FLOAT(manual_prod24),
a24, b24, SP_SCALE24_TO_FLOAT(a24), SP_SCALE24_TO_FLOAT(b24)));
}
static void
test_scale24_mul()
{
UTEST_TEST("SP_SCALE24_MUL") {
UTEST_ASSERT(0x1000 < SP_SCALE24_MAX);
UTEST_ASSERT(SP_SCALE24_MAX < SP_SCALE24_MAX + 1); // i.e. no overflow.
for (unsigned i = 0; i <= 10; ++i) {
unsigned const i24 = SP_SCALE24_MAX * i / 10;
UTEST_ASSERT(i24 == SP_SCALE24_FROM_FLOAT(i / 10.0));
for (unsigned j = 0; j <= 10; ++j) {
unsigned const j24 = SP_SCALE24_MAX * j / 10;
test_mul24(i24, j24);
}
}
for (unsigned i = 0; i < 10000; ++i) {
unsigned const a24 = rand() % (SP_SCALE24_MAX + 1);
unsigned const b24 = rand() % (SP_SCALE24_MAX + 1);
test_mul24(a24, b24);
}
}
}
static void
test_merge_opacity()
{
SPStyle &parent = *sp_style_new();
SPStyle &child = *sp_style_new();
unsigned const either = 2;
struct {
bool parent_set;
bool parent_inherit;
float parent_float_val;
bool child_set;
bool child_inherit;
float child_float_val;
unsigned exp_set;
unsigned exp_inherit;
double exp_float_val;
} const cases[] = {
{false, false, 1.0, false, false, 1.0, false, false, 1.0},
{false, false, 1.0, true, true, 1.0, false, false, 1.0},
{false, false, 1.0, true, false, 1.0, false, false, 1.0},
{false, false, 1.0, true, false, 0.5, true, false, 0.5},
{false, false, 1.0, true, false, 0.0, true, false, 0.0},
{true, true, 1.0, false, false, 1.0, true, true, 1.0},
{true, true, 0.7, false, false, 1.0, true, true, 0.7},
{true, true, 0.0, false, false, 1.0, true, true, 0.0},
{true, true, 1.0, true, true, 1.0, true, true, 1.0},
/* child computed value isn't required to be up-to-date, so test what happens when it
* isn't up-to-date. */
{true, true, 1.0, true, true, 0.7, true, true, 1.0},
{true, true, 0.6, true, true, 0.3, true, false, 0.36},
{true, true, 0.0, true, true, 0.0, true, true, 0.0},
{true, true, 0.0, true, true, 0.9, true, true, 0.0},
/* parent inherit, child set to number */
{true, true, 1.0, true, false, 1.0, true, true, 1.0},
{true, true, 1.0, true, false, 0.8, true, false, 0.8},
{true, true, 1.0, true, false, 0.0, true, false, 0.0},
{true, true, 0.9, true, false, 1.0, true, true, 0.9},
{true, true, 0.9, true, false, 0.8, true, false, 0.72},
{true, true, 0.9, true, false, 0.0, true, false, 0.0},
{true, true, 0.0, true, false, 1.0, true, true, 0.0},
{true, true, 0.0, true, false, 0.6, true, either, 0.0},
{true, true, 0.0, true, false, 0.0, true, false, 0.0},
/* parent set to number. */
{true, false, 1.0, false, false, 1.0, either, false, 1.0},
{true, false, 0.3, false, false, 1.0, true, false, 0.3},
{true, false, 0.3, false, false, 1.0, true, false, 0.3},
{true, false, 0.0, false, false, 1.0, true, false, 0.0},
{true, false, 1.0, true, true, 1.0, either, false, 1.0},
{true, false, 1.0, true, true, 0.8, either, false, 1.0},
{true, false, 0.8, true, true, 0.8, true, false, 0.64},
{true, false, 0.8, true, true, 0.5, true, false, 0.64},
{true, false, 0.0, true, true, 0.0, true, false, 0.0},
{true, false, 0.0, true, true, 0.4, true, false, 0.0},
{true, false, 1.0, true, false, 1.0, either, false, 1.0},
{true, false, 1.0, true, false, 0.4, true, false, 0.4},
{true, false, 1.0, true, false, 0.0, true, false, 0.0},
{true, false, 0.7, true, false, 1.0, true, false, 0.7},
{true, false, 0.7, true, false, 0.4, true, false, 0.28},
{true, false, 0.7, true, false, 0.0, true, false, 0.0},
{true, false, 0.0, true, false, 1.0, true, false, 0.0},
{true, false, 0.0, true, false, 0.6, true, false, 0.0},
{true, false, 0.0, true, false, 0.0, true, false, 0.0}
};
UTEST_TEST("sp_style_merge_from_dying_parent: opacity") {
for (unsigned i = 0; i < G_N_ELEMENTS(cases); ++i) {
parent.opacity.set = cases[i].parent_set;
parent.opacity.inherit = cases[i].parent_inherit;
parent.opacity.value = SP_SCALE24_FROM_FLOAT(cases[i].parent_float_val);
unsigned const parent24 = parent.opacity.value;
child.opacity.set = cases[i].child_set;
child.opacity.inherit = cases[i].child_inherit;
child.opacity.value = SP_SCALE24_FROM_FLOAT(cases[i].child_float_val);
unsigned const child24 = child.opacity.value;
sp_style_merge_from_dying_parent(&child, &parent);
if (cases[i].exp_set != either) {
UTEST_ASSERT(child.opacity.set == cases[i].exp_set);
}
if (cases[i].exp_inherit != either) {
UTEST_ASSERT(child.opacity.inherit == cases[i].exp_inherit);
}
unsigned const exp24 = SP_SCALE24_FROM_FLOAT(cases[i].exp_float_val);
UTEST_ASSERT_SHOW(child.opacity.value == exp24,
("i=%u, expected 0x%06x=%g but found 0x%06x=%g; "
"parent 0x%06x=%g, child 0x%06x=%g",
i, exp24, cases[i].exp_float_val,
child.opacity.value, SP_SCALE24_TO_FLOAT(child.opacity.value),
parent24, SP_SCALE24_TO_FLOAT(parent24),
child24, SP_SCALE24_TO_FLOAT(child24)));
}
}
}
static bool
test_style()
{
struct {
char const *property;
char const *ink_dfl;
char const *spec_dfl;
void (*tst_fn)(char const[], char const *const[]);
char const *const *tst_fn_arg;
bool can_explicitly_inherit;
} const props[] = {
{"color", "black", "black", color_val, NULL, true},
// initial value "depends on user agent"
{"display", "inline", "inline", enum_val, display_vals, true},
{"fill", "black", "black", paint_val, NULL, true},
{"fill-opacity", "1", "1", opacity_val, NULL, true},
{"fill-rule", "nonzero", "nonzero", enum_val, fill_rule_vals, true},
{"font-family", "Bitstream Vera Sans", "Bitstream Vera Sans", font_family_val, NULL, true},
// initial value depends on user agent
{"font-size", "medium", "medium", unitful_length_val, NULL, true},
// TODO: abs, rel, pcnt.
{"font-stretch", "normal", "normal", enum_val, font_stretch_vals, true},
{"font-style", "normal", "normal", enum_val, font_style_vals, true},
{"font-variant", "normal", "normal", enum_val, font_variant_vals, true},
{"font-weight", "normal", "normal", enum_val, font_weight_vals, true},
{"letter-spacing", "normal", "normal", length_or_enum_val, normal_val, true},
{"marker", "none", "none", uri_or_enum_val, none_val, true},
{"marker-end", "none", "none", uri_or_enum_val, none_val, true},
{"marker-mid", "none", "none", uri_or_enum_val, none_val, true},
{"marker-start", "none", "none", uri_or_enum_val, none_val, true},
{"opacity", "1", "1", opacity_val, NULL, true},
{"stroke", "none", "none", paint_val, NULL, true},
{"stroke-dasharray", "none", "none", enum_val, none_val, true},
// TODO: http://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty
//{"stroke-dashoffset", "0", "0", length_val, NULL, true},
// fixme: dashoffset currently fails a number of tests, but the relevant code
// is being worked on for something else at the time of writing, so I'm
// delaying fixing it. It should be changed to SPILength.
{"stroke-linecap", "butt", "butt", enum_val, linecap_vals, true},
{"stroke-linejoin", "miter", "miter", enum_val, linejoin_vals, true},
{"stroke-miterlimit", "4", "4", miterlimit_val, NULL, true},
{"stroke-opacity", "1", "1", opacity_val, NULL, true},
{"stroke-width", "1", "1", length_val, NULL, true},
{"text-anchor", "start", "start", enum_val, text_anchor_vals, true},
{"visibility", "visible", "visible", enum_val, visibility_vals, true},
{"writing-mode", "lr-tb", "lr-tb", enum_val, writing_mode_vals, true}
};
char const str0_all_exp[] =
"font-size:medium;"
"font-style:normal;"
"font-variant:normal;"
"font-weight:normal;"
"font-stretch:normal;"
"text-indent:0;"
"text-align:start;"
"text-decoration:none;"
"line-height:normal;"
"letter-spacing:normal;"
"word-spacing:normal;"
"text-transform:none;"
"direction:ltr;"
"block-progression:tb;"
"writing-mode:lr-tb;"
"text-anchor:start;"
"opacity:1;"
"color:black;"
"fill:black;"
"fill-opacity:1;"
"fill-rule:nonzero;"
"stroke:none;"
"stroke-width:1;"
"stroke-linecap:butt;"
"stroke-linejoin:miter;"
"marker:none;"
"marker-start:none;"
"marker-mid:none;"
"marker-end:none;"
"stroke-miterlimit:4;"
"stroke-dasharray:none;"
"stroke-dashoffset:0;"
"stroke-opacity:1;"
"visibility:visible;"
"display:inline;"
"overflow:visible;"
"font-family:Bitstream Vera Sans";
utest_start("style");
UTEST_TEST("sp_style_new, sp_style_write_string") {
SPStyle *style = sp_style_new();
gchar *str0_all = sp_style_write_string(style, SP_STYLE_FLAG_ALWAYS);
gchar *str0_set = sp_style_write_string(style, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(*str0_set == '\0');
UTEST_ASSERT(streq(str0_all, str0_all_exp));
printf(str0_all);
g_free(str0_all);
g_free(str0_set);
sp_style_unref(style);
}
UTEST_TEST("sp_style_merge_from_style_string(whitespace, ifset") {
gchar *str0_set = merge_then_write_string(" \t \t\t", SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(*str0_set == '\0');
g_free(str0_set);
}
UTEST_TEST("sp_style_merge_from_style_string(whitespace, always") {
gchar *str0_all = merge_then_write_string(" \t \t\t", SP_STYLE_FLAG_ALWAYS);
UTEST_ASSERT(streq(str0_all, str0_all_exp));
g_free(str0_all);
}
/* Some tests for invalid style strings. We temporarily suppress all g_warning's. (The
current code uses g_warning instead of proper SVG error handling.) */
guint const log_handler_id = g_log_set_handler(NULL, G_LOG_LEVEL_WARNING,
suppress_warning_log_handler, NULL);
UTEST_TEST("sp_style_merge_from_style_string(\"fill:\", ifset)") {
gchar *str0_set = merge_then_write_string("fill:", SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(*str0_set == '\0');
g_free(str0_set);
}
UTEST_TEST("sp_style_merge_from_style_string(\"font-family:\", always)") {
gchar *str0_all = merge_then_write_string("font-family:", SP_STYLE_FLAG_ALWAYS);
UTEST_ASSERT(streq(str0_all, str0_all_exp));
g_free(str0_all);
}
UTEST_TEST("sp_style_merge_from_style_string(\"fill: \", ifset)") {
gchar *str0_set = merge_then_write_string("fill: ", SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(*str0_set == '\0');
g_free(str0_set);
}
UTEST_TEST("sp_style_merge_from_style_string(\"font-family: \", always)") {
gchar *str0_all = merge_then_write_string("font-family: ", SP_STYLE_FLAG_ALWAYS);
UTEST_ASSERT(streq(str0_all, str0_all_exp));
g_free(str0_all);
}
UTEST_TEST("sp_style_merge_from_style_string(\":none\", ifset)") {
gchar *str0_set = merge_then_write_string(":none", SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(*str0_set == '\0');
g_free(str0_set);
}
g_log_remove_handler(NULL, log_handler_id);
/* The following tests involve invalid style, but aren't expected to trigger
g_log calls. */
/* invalid color setting */
{
char const *bad[] = {"#4321", "currentColor", "#87654321", "#42", "aquam"};
for (unsigned i = 0; i < G_N_ELEMENTS(bad); ++i) {
gchar *tst_name = g_strdup_printf("invalid color setting: %s", bad[i]);
gchar *style_str = g_strdup_printf("color:%s;color:#123;color:%s",
bad[i], bad[i]);
UTEST_TEST(tst_name) {
gchar *str0_set = merge_then_write_string(style_str, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(streq(str0_set, "color:#123"));
g_free(str0_set);
}
g_free(style_str);
g_free(tst_name);
}
}
/* End of invalid style string examples. */
#if 1 /* previously failed because of dashoffset:0 vs dashoffset:0.00000000 */
UTEST_TEST("sp_style_merge_from_style_string(default): ifset") {
gchar *ifset_str = merge_then_write_string(str0_all_exp, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(streq(ifset_str, str0_all_exp));
g_free(ifset_str);
}
UTEST_TEST("sp_style_merge_from_style_string(default): always") {
gchar *ifset_str = merge_then_write_string(str0_all_exp, SP_STYLE_FLAG_ALWAYS);
UTEST_ASSERT(streq(ifset_str, str0_all_exp));
g_free(ifset_str);
}
#endif
UTEST_TEST("sp_style_merge_from_style_string") {
/* Try setting default values, check that the all string is unaffected
but that the ifset string is affected. */
for (unsigned i = 0; i < G_N_ELEMENTS(props); ++i) {
char *prop_eq_val = g_strdup_printf("%s:%s", props[i].property, props[i].spec_dfl);
char *exp_set_str = g_strdup_printf("%s:%s", props[i].property, props[i].ink_dfl);
gchar *str0_all = merge_then_write_string(prop_eq_val, SP_STYLE_FLAG_ALWAYS);
gchar *str0_set = merge_then_write_string(prop_eq_val, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(streq(str0_all, str0_all_exp));
UTEST_ASSERT(streq(str0_set, exp_set_str));
g_free(str0_set);
g_free(str0_all);
g_free(exp_set_str);
g_free(prop_eq_val);
}
/* Check that explicit `inherit' is correctly preserved by write(ifset). */
for (unsigned i = 0; i < G_N_ELEMENTS(props); ++i) {
if (!props[i].can_explicitly_inherit) {
continue;
}
char *prop_eq_val = g_strdup_printf("%s:inherit", props[i].property);
gchar *ifset_str = merge_then_write_string(prop_eq_val, SP_STYLE_FLAG_IFSET);
UTEST_ASSERT(streq(ifset_str, prop_eq_val));
g_free(ifset_str);
g_free(prop_eq_val);
}
for (unsigned i = 0; i < G_N_ELEMENTS(props); ++i) {
props[i].tst_fn(props[i].property, props[i].tst_fn_arg);
}
}
test_scale24_mul();
test_merge_opacity();
return utest_end();
}
int main()
{
return ( test_style()
? EXIT_SUCCESS
: EXIT_FAILURE );
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|