summaryrefslogtreecommitdiff
path: root/source/html/css-apply.c
blob: a1b8d800026d6b7657a8c45cec7e9facb63cb24d (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
#include "mupdf/html.h"

static void add_property(struct style *style, const char *name, struct value *value, int spec);

struct rule *
fz_new_css_rule(fz_context *ctx, struct selector *selector, struct property *declaration)
{
	struct rule *rule = fz_malloc_struct(ctx, struct rule);
	rule->selector = selector;
	rule->declaration = declaration;
	rule->next = NULL;
	return rule;
}

struct selector *
fz_new_css_selector(fz_context *ctx, const char *name)
{
	struct selector *sel = fz_malloc_struct(ctx, struct selector);
	sel->name = name ? strdup(name) : NULL;
	sel->combine = 0;
	sel->cond = NULL;
	sel->left = NULL;
	sel->right = NULL;
	sel->next = NULL;
	return sel;
}

struct condition *
fz_new_css_condition(fz_context *ctx, int type, const char *key, const char *val)
{
	struct condition *cond = fz_malloc_struct(ctx, struct condition);
	cond->type = type;
	cond->key = key ? strdup(key) : NULL;
	cond->val = val ? strdup(val) : NULL;
	cond->next = NULL;
	return cond;
}

struct property *
fz_new_css_property(fz_context *ctx, const char *name, struct value *value, int spec)
{
	struct property *prop = fz_malloc_struct(ctx, struct property);
	prop->name = strdup(name);
	prop->value = value;
	prop->spec = spec;
	prop->next = NULL;
	return prop;
}

struct value *
fz_new_css_value(fz_context *ctx, int type, const char *data)
{
	struct value *val = fz_malloc_struct(ctx, struct value);
	val->type = type;
	val->data = strdup(data);
	val->args = NULL;
	val->next = NULL;
	return val;
}

/*
 * Compute specificity
 */

static int
count_condition_ids(struct condition *cond)
{
	int n = 0;
	while (cond)
	{
		if (cond->type == '#')
			n ++;
		cond = cond->next;
	}
	return n;
}

static int
count_selector_ids(struct selector *sel)
{
	int n = count_condition_ids(sel->cond);
	if (sel->left && sel->right)
	{
		n += count_selector_ids(sel->left);
		n += count_selector_ids(sel->right);
	}
	return n;
}

static int
count_condition_atts(struct condition *cond)
{
	int n = 0;
	while (cond)
	{
		if (cond->type != '#' && cond->type != ':')
			n ++;
		cond = cond->next;
	}
	return n;
}

static int
count_selector_atts(struct selector *sel)
{
	int n = count_condition_atts(sel->cond);
	if (sel->left && sel->right)
	{
		n += count_selector_atts(sel->left);
		n += count_selector_atts(sel->right);
	}
	return n;
}

static int
count_condition_names(struct condition *cond)
{
	int n = 0;
	while (cond)
	{
		if (cond->type == ':')
			n ++;
		cond = cond->next;
	}
	return n;
}

static int
count_selector_names(struct selector *sel)
{
	int n = count_condition_names(sel->cond);
	if (sel->left && sel->right)
	{
		n += count_selector_names(sel->left);
		n += count_selector_names(sel->right);
	}
	else if (sel->name)
	{
		n ++;
	}
	return n;
}

#define INLINE_SPECIFICITY 1000

static int
selector_specificity(struct selector *sel)
{
	int b = count_selector_ids(sel);
	int c = count_selector_atts(sel);
	int d = count_selector_names(sel);
	return b * 100 + c * 10 + d;
}

/*
 * Pretty printing
 */

void
print_value(struct value *val)
{
	printf("%s", val->data);
	if (val->args)
	{
		printf("(");
		print_value(val->args);
		printf(")");
	}
	if (val->next)
	{
		printf(" ");
		print_value(val->next);
	}
}

void
print_property(struct property *prop)
{
	printf("\t%s: ", prop->name);
	print_value(prop->value);
	printf(" !%d;\n", prop->spec);
}

void
print_condition(struct condition *cond)
{
	if (cond->type == '=')
		printf("[%s=%s]", cond->key, cond->val);
	else if (cond->type == '[')
		printf("[%s]", cond->key);
	else
		printf("%c%s", cond->type, cond->val);
	if (cond->next)
		print_condition(cond->next);
}

void
print_selector(struct selector *sel)
{
	if (sel->combine)
	{
putchar('(');
		print_selector(sel->left);
		if (sel->combine == ' ')
			printf(" ");
		else
			printf(" %c ", sel->combine);
		print_selector(sel->right);
putchar(')');
	}
	else if (sel->name)
		printf("%s", sel->name);
	else
		printf("*");
	if (sel->cond)
	{
		print_condition(sel->cond);
	}
}

void
print_rule(struct rule *rule)
{
	struct selector *sel;
	struct property *prop;

	for (sel = rule->selector; sel; sel = sel->next)
	{
		print_selector(sel);
		printf(" !%d", selector_specificity(sel));
		if (sel->next)
			printf(", ");
	}

	printf("\n{\n");
	for (prop = rule->declaration; prop; prop = prop->next)
	{
		print_property(prop);
	}
	printf("}\n");
}

void
print_rules(struct rule *rule)
{
	while (rule)
	{
		print_rule(rule);
		rule = rule->next;
	}
}

/*
 * Selector matching
 */

static int
match_id_condition(fz_xml *node, const char *p)
{
	const char *s = fz_xml_att(node, "id");
	if (s && !strcmp(s, p))
		return 1;
	return 0;
}

static int
match_class_condition(fz_xml *node, const char *p)
{
	const char *s = fz_xml_att(node, "class");
	char buf[1024];
	if (s) {
		strcpy(buf, s);
		s = strtok(buf, " ");
		while (s) {
			if (!strcmp(s, p))
				return 1;
			s = strtok(NULL, " ");
		}
	}
	return 0;
}

static int
match_condition(struct condition *cond, fz_xml *node)
{
	if (!cond)
		return 1;

	switch (cond->type) {
	default: return 0;
	case ':': return 0; /* don't support pseudo-classes */
	case '#': if (!match_id_condition(node, cond->val)) return 0; break;
	case '.': if (!match_class_condition(node, cond->val)) return 0; break;
	}

	return match_condition(cond->next, node);
}

static int
match_selector(struct selector *sel, fz_xml *node)
{
	if (!node)
		return 0;

	if (sel->combine)
	{
		/* descendant */
		if (sel->combine == ' ')
		{
			fz_xml *parent = fz_xml_up(node);
			while (parent)
			{
				if (match_selector(sel->left, parent))
					if (match_selector(sel->right, node))
						return 1;
				parent = fz_xml_up(parent);
			}
			return 0;
		}

		/* child */
		if (sel->combine == '>')
		{
			fz_xml *parent = fz_xml_up(node);
			if (!parent)
				return 0;
			if (!match_selector(sel->left, parent))
				return 0;
			if (!match_selector(sel->right, node))
				return 0;
		}

		/* adjacent */
		if (sel->combine == '+')
		{
			fz_xml *prev = fz_xml_prev(node);
			while (prev && !fz_xml_tag(prev))
				prev = fz_xml_prev(prev);
			if (!prev)
				return 0;
			if (!fz_xml_tag(prev))
				return 0;
			if (!match_selector(sel->left, prev))
				return 0;
			if (!match_selector(sel->right, node))
				return 0;
		}
	}

	if (sel->name)
	{
		if (strcmp(sel->name, fz_xml_tag(node)))
			return 0;
	}

	if (sel->cond)
	{
		if (!match_condition(sel->cond, node))
			return 0;
	}

	return 1;
}

/*
 * Annotating nodes with properties and expanding shorthand forms.
 */

static const char *border_width_kw[] = {
	"thin", "medium", "thick",
};

static const char *border_style_kw[] = {
	"none", "hidden", "dotted", "dashed", "solid", "double", "groove", "ridge", "inset", "outset",
};

static const char *color_kw[] = {
	"transparent", "maroon", "red", "orange", "yellow", "olive", "purple", "fuchsia", "white",
	"lime", "green", "navy", "blue", "aqua", "teal", "black", "silver", "gray",
};

static int
count_values(struct value *value)
{
	int n = 0;
	while (value)
	{
		n++;
		value = value->next;
	}
	return n;
}

static void
add_shorthand_trbl(struct style *style, struct value *value, int spec,
	const char *name_t, const char *name_r, const char *name_b, const char *name_l)
{
	int n = count_values(value);

	if (n == 1)
	{
		add_property(style, name_t, value, spec);
		add_property(style, name_r, value, spec);
		add_property(style, name_b, value, spec);
		add_property(style, name_l, value, spec);
	}

	if (n == 2)
	{
		struct value *a = value;
		struct value *b = value->next;

		add_property(style, name_t, a, spec);
		add_property(style, name_r, b, spec);
		add_property(style, name_b, a, spec);
		add_property(style, name_l, b, spec);
	}

	if (n == 3)
	{
		struct value *a = value;
		struct value *b = value->next;
		struct value *c = value->next->next;

		add_property(style, name_t, a, spec);
		add_property(style, name_r, b, spec);
		add_property(style, name_b, c, spec);
		add_property(style, name_l, b, spec);
	}

	if (n == 4)
	{
		struct value *a = value;
		struct value *b = value->next;
		struct value *c = value->next->next;
		struct value *d = value->next->next->next;

		add_property(style, name_t, a, spec);
		add_property(style, name_r, b, spec);
		add_property(style, name_b, c, spec);
		add_property(style, name_l, d, spec);
	}
}

static void
add_shorthand_margin(struct style *style, struct value *value, int spec)
{
	add_shorthand_trbl(style, value, spec,
		"margin-top", "margin-right", "margin-bottom", "margin-left");
}

static void
add_shorthand_padding(struct style *style, struct value *value, int spec)
{
	add_shorthand_trbl(style, value, spec,
		"padding-top", "padding-right", "padding-bottom", "padding-left");
}

static void
add_shorthand_border_width(struct style *style, struct value *value, int spec)
{
	add_shorthand_trbl(style, value, spec,
		"border-width-top", "border-width-right", "border-width-bottom", "border-width-left");
}

static void
add_shorthand_border(struct style *style, struct value *value, int spec)
{
	int i;

	while (value)
	{
		if (value->type == CSS_COLOR)
		{
			add_property(style, "border-color", value, spec);
		}
		else if (value->type == CSS_KEYWORD)
		{
			for (i = 0; i < nelem(border_width_kw); ++i)
			{
				if (!strcmp(value->data, border_width_kw[i]))
				{
					add_property(style, "border-width-top", value, spec);
					add_property(style, "border-width-right", value, spec);
					add_property(style, "border-width-bottom", value, spec);
					add_property(style, "border-width-left", value, spec);
					goto next;
				}
			}
			for (i = 0; i < nelem(border_style_kw); ++i)
			{
				if (!strcmp(value->data, border_style_kw[i]))
				{
					add_property(style, "border-style", value, spec);
					goto next;
				}
			}
			for (i = 0; i < nelem(color_kw); ++i)
			{
				if (!strcmp(value->data, color_kw[i]))
				{
					add_property(style, "border-color", value, spec);
					goto next;
				}
			}
		}
		else
		{
			add_property(style, "border-width-top", value, spec);
			add_property(style, "border-width-right", value, spec);
			add_property(style, "border-width-bottom", value, spec);
			add_property(style, "border-width-left", value, spec);
		}
next:
		value = value->next;
	}
}

static void
add_property(struct style *style, const char *name, struct value *value, int spec)
{
	int i;

	if (!strcmp(name, "margin"))
	{
		add_shorthand_margin(style, value, spec);
		return;
	}
	if (!strcmp(name, "padding"))
	{
		add_shorthand_padding(style, value, spec);
		return;
	}
	if (!strcmp(name, "border-width"))
	{
		add_shorthand_border_width(style, value, spec);
		return;
	}
	if (!strcmp(name, "border"))
	{
		add_shorthand_border(style, value, spec);
		return;
	}

	/* shorthand expansions: */
	/* TODO: border-color */
	/* TODO: border-style */
	/* TODO: font */
	/* TODO: list-style */
	/* TODO: background */

	for (i = 0; i < style->count; ++i)
	{
		if (!strcmp(style->prop[i].name, name))
		{
			if (style->prop[i].spec <= spec)
			{
				style->prop[i].value = value;
				style->prop[i].spec = spec;
			}
			return;
		}
	}

	if (style->count + 1 >= nelem(style->prop))
	{
		// fz_warn(ctx, "too many css properties");
		return;
	}

	style->prop[style->count].name = name;
	style->prop[style->count].value = value;
	style->prop[style->count].spec = spec;
	++style->count;
}

void
apply_styles(fz_context *ctx, struct style *style, struct rule *rule, fz_xml *node)
{
	struct selector *sel;
	struct property *prop;
	const char *s;

	while (rule)
	{
		sel = rule->selector;
		while (sel)
		{
			if (match_selector(sel, node))
			{
				for (prop = rule->declaration; prop; prop = prop->next)
					add_property(style, prop->name, prop->value, selector_specificity(sel));
				break;
			}
			sel = sel->next;
		}
		rule = rule->next;
	}

	s = fz_xml_att(node, "style");
	if (s)
	{
		prop = fz_parse_css_properties(ctx, s);
		while (prop)
		{
			add_property(style, prop->name, prop->value, INLINE_SPECIFICITY);
			prop = prop->next;
		}
		// TODO: free props
	}
}

static const char *inherit_list[] = {
	"color", "direction",
	"font-family", "font-size", "font-style", "font-variant", "font-weight",
	"letter-spacing", "line-height",
	"list-style-image", "list-style-position", "list-style-type",
	"orphans", "quotes", "text-align", "text-indent", "text-transform",
	"visibility", "white-space", "widows", "word-spacing",

	/* this is not supposed to be inherited: */
	"vertical-align",
};

static struct value *
get_raw_property(struct style *node, const char *name)
{
	int i;
	for (i = 0; i < node->count; ++i)
		if (!strcmp(node->prop[i].name, name))
			return node->prop[i].value;
	return NULL;
}

static int
should_inherit_property(const char *name)
{
	int l = 0;
	int r = nelem(inherit_list) - 1;
	while (l <= r)
	{
		int m = (l + r) >> 1;
		int c = strcmp(name, inherit_list[m]);
		if (c < 0)
			r = m - 1;
		else if (c > 0)
			l = m + 1;
		else
			return 1;
	}
	return 0;
}

static struct value *
get_style_property(struct style *node, const char *name)
{
	struct value *value;

	value = get_raw_property(node, name);
	if (node->up)
	{
		if (value && !strcmp(value->data, "inherit"))
			return get_style_property(node->up, name);
		if (!value && should_inherit_property(name))
			return get_style_property(node->up, name);
	}
	return value;
}

static const char *
get_style_property_string(struct style *node, const char *name, const char *initial)
{
	struct value *value;
	value = get_style_property(node, name);
	if (!value)
		return initial;
	return value->data;
}

static struct number
make_number(float v, int u)
{
	struct number n;
	n.value = v;
	n.unit = u;
	return n;
}

static struct number
number_from_value(struct value *value, float initial, int initial_unit)
{
	char *p;

	if (!value)
		return make_number(initial, initial_unit);

	if (value->type == CSS_PERCENT)
		return make_number(strtof(value->data, NULL), N_PERCENT);

	if (value->type == CSS_NUMBER)
		return make_number(strtof(value->data, NULL), N_NUMBER);

	if (value->type == CSS_LENGTH)
	{
		float x = strtof(value->data, &p);

		if (p[0] == 'e' && p[1] == 'm')
			return make_number(x, N_SCALE);
		if (p[0] == 'e' && p[1] == 'x')
			return make_number(x / 2, N_SCALE);

		if (p[0] == 'i' && p[1] == 'n')
			return make_number(x * 72, N_NUMBER);
		if (p[0] == 'c' && p[1] == 'm')
			return make_number(x * 7200 / 254, N_NUMBER);
		if (p[0] == 'm' && p[1] == 'm')
			return make_number(x * 720 / 254, N_NUMBER);
		if (p[0] == 'p' && p[1] == 'c')
			return make_number(x * 12, N_NUMBER);

		if (p[0] == 'p' && p[1] == 't')
			return make_number(x, N_NUMBER);
		if (p[0] == 'p' && p[1] == 'x')
			return make_number(x, N_NUMBER);

		return make_number(x, N_NUMBER);
	}

	return make_number(initial, initial_unit);
}

static struct number
number_from_property(struct style *node, const char *property, float initial, int initial_unit)
{
	return number_from_value(get_style_property(node, property), initial, initial_unit);
}

static struct number
border_width_from_property(struct style *node, const char *property)
{
	struct value *value = get_style_property(node, property);
	if (value)
	{
		if (!strcmp(value->data, "thin"))
			return make_number(1, N_NUMBER);
		if (!strcmp(value->data, "medium"))
			return make_number(2, N_NUMBER);
		if (!strcmp(value->data, "thick"))
			return make_number(4, N_NUMBER);
		return number_from_value(value, 0, N_NUMBER);
	}
	return make_number(2, N_NUMBER); /* initial: 'medium' */
}

float
fz_from_css_number(struct number number, float em, float width)
{
	switch (number.unit) {
	default:
	case N_NUMBER: return number.value;
	case N_SCALE: return number.value * em;
	case N_PERCENT: return number.value * 0.01 * width;
	}
}

float
fz_from_css_number_scale(struct number number, float scale, float em, float width)
{
	switch (number.unit) {
	default:
	case N_NUMBER: return number.value * scale;
	case N_SCALE: return number.value * em;
	case N_PERCENT: return number.value * 0.01 * width;
	}
}

static struct color
make_color(int r, int g, int b, int a)
{
	struct color c;
	c.r = r;
	c.g = g;
	c.b = b;
	c.a = a;
	return c;
}

static int tohex(int c)
{
	if (c - '0' < 10)
		return c - '0';
	return (c | 32) - 'a' + 10;
}

static struct color
color_from_value(struct value *value, struct color initial)
{
	if (!value)
		return initial;
	if (value->type == CSS_COLOR)
	{
		int r = tohex(value->data[0]) * 16 + tohex(value->data[1]);
		int g = tohex(value->data[2]) * 16 + tohex(value->data[3]);
		int b = tohex(value->data[4]) * 16 + tohex(value->data[5]);
		return make_color(r, g, b, 255);
	}
	if (value->type == CSS_KEYWORD)
	{
		if (!strcmp(value->data, "transparent"))
			return make_color(0, 0, 0, 0);
		if (!strcmp(value->data, "maroon"))
			return make_color(0x80, 0x00, 0x00, 255);
		if (!strcmp(value->data, "red"))
			return make_color(0xFF, 0x00, 0x00, 255);
		if (!strcmp(value->data, "orange"))
			return make_color(0xFF, 0xA5, 0x00, 255);
		if (!strcmp(value->data, "yellow"))
			return make_color(0xFF, 0xFF, 0x00, 255);
		if (!strcmp(value->data, "olive"))
			return make_color(0x80, 0x80, 0x00, 255);
		if (!strcmp(value->data, "purple"))
			return make_color(0x80, 0x00, 0x80, 255);
		if (!strcmp(value->data, "fuchsia"))
			return make_color(0xFF, 0x00, 0xFF, 255);
		if (!strcmp(value->data, "white"))
			return make_color(0xFF, 0xFF, 0xFF, 255);
		if (!strcmp(value->data, "lime"))
			return make_color(0x00, 0xFF, 0x00, 255);
		if (!strcmp(value->data, "green"))
			return make_color(0x00, 0x80, 0x00, 255);
		if (!strcmp(value->data, "navy"))
			return make_color(0x00, 0x00, 0x80, 255);
		if (!strcmp(value->data, "blue"))
			return make_color(0x00, 0x00, 0xFF, 255);
		if (!strcmp(value->data, "aqua"))
			return make_color(0x00, 0xFF, 0xFF, 255);
		if (!strcmp(value->data, "teal"))
			return make_color(0x00, 0x80, 0x80, 255);
		if (!strcmp(value->data, "black"))
			return make_color(0x00, 0x00, 0x00, 255);
		if (!strcmp(value->data, "silver"))
			return make_color(0xC0, 0xC0, 0xC0, 255);
		if (!strcmp(value->data, "gray"))
			return make_color(0x80, 0x80, 0x80, 255);
		return make_color(0, 0, 0, 255);
	}
	return initial;
}

static struct color
color_from_property(struct style *node, const char *property, struct color initial)
{
	return color_from_value(get_style_property(node, property), initial);
}

int
fz_get_css_style_property_display(struct style *node)
{
	struct value *value = get_style_property(node, "display");
	if (value)
	{
		if (!strcmp(value->data, "none"))
			return DIS_NONE;
		if (!strcmp(value->data, "inline"))
			return DIS_INLINE;
		if (!strcmp(value->data, "block"))
			return DIS_BLOCK;
		if (!strcmp(value->data, "list-item"))
			return DIS_LIST_ITEM;
	}
	return DIS_INLINE;
}

static int
get_style_property_white_space(struct style *node)
{
	struct value *value = get_style_property(node, "white-space");
	if (value)
	{
		if (!strcmp(value->data, "normal")) return WS_NORMAL;
		if (!strcmp(value->data, "pre")) return WS_PRE;
		if (!strcmp(value->data, "nowrap")) return WS_NOWRAP;
		if (!strcmp(value->data, "pre-wrap")) return WS_PRE_WRAP;
		if (!strcmp(value->data, "pre-line")) return WS_PRE_LINE;
	}
	return WS_NORMAL;
}

void
default_computed_style(struct computed_style *style)
{
	memset(style, 0, sizeof *style);
	style->text_align = TA_LEFT;
	style->vertical_align = VA_BASELINE;
	style->white_space = WS_NORMAL;
	style->font_size = make_number(1, N_SCALE);
}

void
compute_style(fz_context *ctx, fz_html_font_set *set, struct computed_style *style, struct style *node)
{
	struct value *value;

	struct color black = { 0, 0, 0, 255 };
	struct color transparent = { 0, 0, 0, 0 };

	default_computed_style(style);

	style->white_space = get_style_property_white_space(node);

	value = get_style_property(node, "text-align");
	if (value)
	{
		if (!strcmp(value->data, "left"))
			style->text_align = TA_LEFT;
		if (!strcmp(value->data, "right"))
			style->text_align = TA_RIGHT;
		if (!strcmp(value->data, "center"))
			style->text_align = TA_CENTER;
		if (!strcmp(value->data, "justify"))
			style->text_align = TA_JUSTIFY;
	}

	value = get_style_property(node, "vertical-align");
	if (value)
	{
		if (!strcmp(value->data, "baseline"))
			style->vertical_align = VA_BASELINE;
		if (!strcmp(value->data, "sub"))
			style->vertical_align = VA_SUB;
		if (!strcmp(value->data, "super"))
			style->vertical_align = VA_SUPER;
		if (!strcmp(value->data, "top"))
			style->vertical_align = VA_TOP;
		if (!strcmp(value->data, "bottom"))
			style->vertical_align = VA_BOTTOM;
	}

	value = get_style_property(node, "font-size");
	if (value)
	{
		if (!strcmp(value->data, "xx-large")) style->font_size = make_number(20, N_NUMBER);
		else if (!strcmp(value->data, "x-large")) style->font_size = make_number(16, N_NUMBER);
		else if (!strcmp(value->data, "large")) style->font_size = make_number(14, N_NUMBER);
		else if (!strcmp(value->data, "medium")) style->font_size = make_number(12, N_NUMBER);
		else if (!strcmp(value->data, "small")) style->font_size = make_number(10, N_NUMBER);
		else if (!strcmp(value->data, "x-small")) style->font_size = make_number(8, N_NUMBER);
		else if (!strcmp(value->data, "xx-small")) style->font_size = make_number(6, N_NUMBER);
		else if (!strcmp(value->data, "larger")) style->font_size = make_number(1.25f, N_SCALE);
		else if (!strcmp(value->data, "smaller")) style->font_size = make_number(0.8f, N_SCALE);
		else style->font_size = number_from_value(value, 12, N_NUMBER);
	}
	else
	{
		style->font_size = make_number(1, N_SCALE);
	}

	value = get_style_property(node, "border-style");
	if (value)
	{
		if (!strcmp(value->data, "none"))
			style->border_style = BS_NONE;
		if (!strcmp(value->data, "hidden"))
			style->border_style = BS_NONE;
		if (!strcmp(value->data, "solid"))
			style->border_style = BS_SOLID;
	}

	style->line_height = number_from_property(node, "line-height", 1.2, N_SCALE);

	style->text_indent = number_from_property(node, "text-indent", 0, N_NUMBER);

	style->margin[0] = number_from_property(node, "margin-top", 0, N_NUMBER);
	style->margin[1] = number_from_property(node, "margin-right", 0, N_NUMBER);
	style->margin[2] = number_from_property(node, "margin-bottom", 0, N_NUMBER);
	style->margin[3] = number_from_property(node, "margin-left", 0, N_NUMBER);

	style->padding[0] = number_from_property(node, "padding-top", 0, N_NUMBER);
	style->padding[1] = number_from_property(node, "padding-right", 0, N_NUMBER);
	style->padding[2] = number_from_property(node, "padding-bottom", 0, N_NUMBER);
	style->padding[3] = number_from_property(node, "padding-left", 0, N_NUMBER);

	style->border_width[0] = border_width_from_property(node, "border-width-top");
	style->border_width[1] = border_width_from_property(node, "border-width-right");
	style->border_width[2] = border_width_from_property(node, "border-width-bottom");
	style->border_width[3] = border_width_from_property(node, "border-width-left");

	style->color = color_from_property(node, "color", black);
	style->background_color = color_from_property(node, "background-color", transparent);
	style->border_color = color_from_property(node, "border-color", style->color);

	{
		const char *font_family = get_style_property_string(node, "font-family", "serif");
		const char *font_variant = get_style_property_string(node, "font-variant", "normal");
		const char *font_style = get_style_property_string(node, "font-style", "normal");
		const char *font_weight = get_style_property_string(node, "font-weight", "normal");
		style->font = fz_load_html_font(ctx, set, font_family, font_variant, font_style, font_weight);
	}
}

void
print_style(struct computed_style *style)
{
	printf("style {\n");
	printf("\tfont-size = %g%c;\n", style->font_size.value, style->font_size.unit);
	printf("\tfont = %s;\n", style->font->name);
	printf("\tline-height = %g%c;\n", style->line_height.value, style->line_height.unit);
	printf("\ttext-indent = %g%c;\n", style->text_indent.value, style->text_indent.unit);
	printf("\ttext-align = %d;\n", style->text_align);
	printf("\tvertical-align = %d;\n", style->vertical_align);
	printf("\tmargin = %g%c %g%c %g%c %g%c;\n",
		style->margin[0].value, style->margin[0].unit,
		style->margin[1].value, style->margin[1].unit,
		style->margin[2].value, style->margin[2].unit,
		style->margin[3].value, style->margin[3].unit);
	printf("\tpadding = %g%c %g%c %g%c %g%c;\n",
		style->padding[0].value, style->padding[0].unit,
		style->padding[1].value, style->padding[1].unit,
		style->padding[2].value, style->padding[2].unit,
		style->padding[3].value, style->padding[3].unit);
	printf("}\n");
}