-
Notifications
You must be signed in to change notification settings - Fork 53
/
css-apply.c
1821 lines (1650 loc) · 46.1 KB
/
css-apply.c
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
// Copyright (C) 2004-2024 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include "html-imp.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
static const char *border_width_kw[] = {
"medium",
"thick",
"thin",
};
static const char *border_style_kw[] = {
"dashed",
"dotted",
"double",
"groove",
"hidden",
"inset",
"none",
"outset",
"ridge",
"solid",
};
static const char *color_kw[] = {
"aqua",
"black",
"blue",
"fuchsia",
"gray",
"green",
"lime",
"maroon",
"navy",
"olive",
"orange",
"purple",
"red",
"silver",
"teal",
"transparent",
"white",
"yellow",
};
static const char *list_style_type_kw[] = {
"armenian",
"circle",
"decimal",
"decimal-leading-zero",
"disc",
"georgian",
"lower-alpha",
"lower-greek",
"lower-latin",
"lower-roman",
"none",
"square",
"upper-alpha",
"upper-greek",
"upper-latin",
"upper-roman",
};
static const char *list_style_position_kw[] = {
"inside",
"outside",
};
static const char *font_style_kw[] = {
"italic",
"oblique",
};
static const char *font_variant_kw[] = {
"small-caps",
};
static const char *font_weight_kw[] = {
"bold",
"bolder",
"lighter",
};
static const char *font_size_kw[] = {
"large",
"larger",
"medium",
"small",
"smaller",
"x-large",
"x-small",
"xx-large",
"xx-small",
};
/* Properties to ignore when scanning through font-family. We set font-family
* to the full font shorthand value list because Adobe generates DS strings
* where the font-family comes before the font-size (and not at the end as it's
* supposed to). This lets us scan the font shorthand list without trying to
* look up fonts named "bold", etc.
*/
static const char *font_family_ignore[] = {
",",
"/",
"bold",
"bolder",
"italic",
"large",
"larger",
"lighter",
"medium",
"oblique",
"small",
"small-caps",
"smaller",
"x-large",
"x-small",
"xx-large",
"xx-small",
};
static int
keyword_in_list(const char *name, const char **list, int n)
{
int l = 0;
int r = n - 1;
while (l <= r)
{
int m = (l + r) >> 1;
int c = strcmp(name, list[m]);
if (c < 0)
r = m - 1;
else if (c > 0)
l = m + 1;
else
return 1;
}
return 0;
}
static int
is_bold_from_font_weight(const char *weight)
{
return !strcmp(weight, "bold") || !strcmp(weight, "bolder") || atoi(weight) > 400;
}
static int
is_italic_from_font_style(const char *style)
{
return !strcmp(style, "italic") || !strcmp(style, "oblique");
}
/*
* Compute specificity
*/
static int
count_condition_ids(fz_css_condition *cond)
{
int n = 0;
while (cond)
{
if (cond->type == '#')
n ++;
cond = cond->next;
}
return n;
}
static int
count_selector_ids(fz_css_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(fz_css_condition *cond)
{
int n = 0;
while (cond)
{
if (cond->type != '#' && cond->type != ':')
n ++;
cond = cond->next;
}
return n;
}
static int
count_selector_atts(fz_css_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(fz_css_condition *cond)
{
int n = 0;
while (cond)
{
if (cond->type == ':')
n ++;
cond = cond->next;
}
return n;
}
static int
count_selector_names(fz_css_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 10000
static int
selector_specificity(fz_css_selector *sel, int important)
{
int b = count_selector_ids(sel);
int c = count_selector_atts(sel);
int d = count_selector_names(sel);
return important * 1000 + b * 100 + c * 10 + d;
}
/*
* Selector matching
*/
static int
match_att_exists_condition(fz_xml *node, const char *key)
{
const char *s = fz_xml_att(node, key);
return s != NULL;
}
static int
match_att_is_condition(fz_xml *node, const char *key, const char *val)
{
const char *att = fz_xml_att(node, key);
return att && !strcmp(val, att);
}
static int
match_att_has_condition(fz_xml *node, const char *att, const char *needle)
{
const char *haystack = fz_xml_att(node, att);
const char *ss;
size_t n;
if (haystack) {
ss = strstr(haystack, needle);
if (ss)
{
n = strlen(needle);
/* Look for exact matches or matching words. */
if ((ss[n] == ' ' || ss[n] == 0) && (ss == haystack || ss[-1] == ' '))
return 1;
}
}
return 0;
}
static int
match_condition(fz_css_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_att_is_condition(node, "id", cond->val)) return 0; break;
case '.': if (!match_att_has_condition(node, "class", cond->val)) return 0; break;
case '[': if (!match_att_exists_condition(node, cond->key)) return 0; break;
case '=': if (!match_att_is_condition(node, cond->key, cond->val)) return 0; break;
case '~': if (!match_att_has_condition(node, cond->key, cond->val)) return 0; break;
case '|': if (!match_att_is_condition(node, cond->key, cond->val)) return 0; break;
}
return match_condition(cond->next, node);
}
static int
match_selector(fz_css_selector *sel, fz_xml *node)
{
if (!node)
return 0;
if (sel->combine)
{
/* descendant */
if (sel->combine == ' ')
{
fz_xml *parent = fz_xml_up(node);
if (!parent || !match_selector(sel->right, node))
return 0;
while (parent)
{
if (match_selector(sel->left, parent))
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 (!fz_xml_is_tag(node, sel->name))
return 0;
}
if (sel->cond)
{
if (!match_condition(sel->cond, node))
return 0;
}
return 1;
}
/*
* Annotating nodes with properties and expanding shorthand forms.
*/
static int
count_values(fz_css_value *value)
{
int n = 0;
while (value)
{
n++;
value = value->next;
}
return n;
}
static void add_property(fz_css_match *match, int name, fz_css_value *value, int spec);
static void
add_shorthand_trbl(fz_css_match *match, fz_css_value *value, int spec,
int name_t, int name_r, int name_b, int name_l)
{
int n = count_values(value);
if (n == 1)
{
add_property(match, name_t, value, spec);
add_property(match, name_r, value, spec);
add_property(match, name_b, value, spec);
add_property(match, name_l, value, spec);
}
if (n == 2)
{
fz_css_value *a = value;
fz_css_value *b = value->next;
add_property(match, name_t, a, spec);
add_property(match, name_r, b, spec);
add_property(match, name_b, a, spec);
add_property(match, name_l, b, spec);
}
if (n == 3)
{
fz_css_value *a = value;
fz_css_value *b = value->next;
fz_css_value *c = value->next->next;
add_property(match, name_t, a, spec);
add_property(match, name_r, b, spec);
add_property(match, name_b, c, spec);
add_property(match, name_l, b, spec);
}
if (n == 4)
{
fz_css_value *a = value;
fz_css_value *b = value->next;
fz_css_value *c = value->next->next;
fz_css_value *d = value->next->next->next;
add_property(match, name_t, a, spec);
add_property(match, name_r, b, spec);
add_property(match, name_b, c, spec);
add_property(match, name_l, d, spec);
}
}
static void
add_shorthand_margin(fz_css_match *match, fz_css_value *value, int spec)
{
add_shorthand_trbl(match, value, spec,
PRO_MARGIN_TOP,
PRO_MARGIN_RIGHT,
PRO_MARGIN_BOTTOM,
PRO_MARGIN_LEFT);
}
static void
add_shorthand_padding(fz_css_match *match, fz_css_value *value, int spec)
{
add_shorthand_trbl(match, value, spec,
PRO_PADDING_TOP,
PRO_PADDING_RIGHT,
PRO_PADDING_BOTTOM,
PRO_PADDING_LEFT);
}
static void
add_shorthand_border_width(fz_css_match *match, fz_css_value *value, int spec)
{
add_shorthand_trbl(match, value, spec,
PRO_BORDER_TOP_WIDTH,
PRO_BORDER_RIGHT_WIDTH,
PRO_BORDER_BOTTOM_WIDTH,
PRO_BORDER_LEFT_WIDTH);
}
static void
add_shorthand_border_color(fz_css_match *match, fz_css_value *value, int spec)
{
add_shorthand_trbl(match, value, spec,
PRO_BORDER_TOP_COLOR,
PRO_BORDER_RIGHT_COLOR,
PRO_BORDER_BOTTOM_COLOR,
PRO_BORDER_LEFT_COLOR);
}
static void
add_shorthand_border_style(fz_css_match *match, fz_css_value *value, int spec)
{
add_shorthand_trbl(match, value, spec,
PRO_BORDER_TOP_STYLE,
PRO_BORDER_RIGHT_STYLE,
PRO_BORDER_BOTTOM_STYLE,
PRO_BORDER_LEFT_STYLE);
}
static void
add_shorthand_border(fz_css_match *match, fz_css_value *value, int spec, int T, int R, int B, int L)
{
while (value)
{
if (value->type == CSS_HASH)
{
if (T) add_property(match, PRO_BORDER_TOP_COLOR, value, spec);
if (R) add_property(match, PRO_BORDER_RIGHT_COLOR, value, spec);
if (B) add_property(match, PRO_BORDER_BOTTOM_COLOR, value, spec);
if (L) add_property(match, PRO_BORDER_LEFT_COLOR, value, spec);
}
else if (value->type == CSS_KEYWORD)
{
if (keyword_in_list(value->data, border_width_kw, nelem(border_width_kw)))
{
if (T) add_property(match, PRO_BORDER_TOP_WIDTH, value, spec);
if (R) add_property(match, PRO_BORDER_RIGHT_WIDTH, value, spec);
if (B) add_property(match, PRO_BORDER_BOTTOM_WIDTH, value, spec);
if (L) add_property(match, PRO_BORDER_LEFT_WIDTH, value, spec);
}
else if (keyword_in_list(value->data, border_style_kw, nelem(border_style_kw)))
{
if (T) add_property(match, PRO_BORDER_TOP_STYLE, value, spec);
if (R) add_property(match, PRO_BORDER_RIGHT_STYLE, value, spec);
if (B) add_property(match, PRO_BORDER_BOTTOM_STYLE, value, spec);
if (L) add_property(match, PRO_BORDER_LEFT_STYLE, value, spec);
}
else if (keyword_in_list(value->data, color_kw, nelem(color_kw)))
{
if (T) add_property(match, PRO_BORDER_TOP_COLOR, value, spec);
if (R) add_property(match, PRO_BORDER_RIGHT_COLOR, value, spec);
if (B) add_property(match, PRO_BORDER_BOTTOM_COLOR, value, spec);
if (L) add_property(match, PRO_BORDER_LEFT_COLOR, value, spec);
}
}
else
{
if (T) add_property(match, PRO_BORDER_TOP_WIDTH, value, spec);
if (R) add_property(match, PRO_BORDER_RIGHT_WIDTH, value, spec);
if (B) add_property(match, PRO_BORDER_BOTTOM_WIDTH, value, spec);
if (L) add_property(match, PRO_BORDER_LEFT_WIDTH, value, spec);
}
value = value->next;
}
}
static void
add_shorthand_list_style(fz_css_match *match, fz_css_value *value, int spec)
{
while (value)
{
if (value->type == CSS_KEYWORD)
{
if (keyword_in_list(value->data, list_style_type_kw, nelem(list_style_type_kw)))
{
add_property(match, PRO_LIST_STYLE_TYPE, value, spec);
}
else if (keyword_in_list(value->data, list_style_position_kw, nelem(list_style_position_kw)))
{
add_property(match, PRO_LIST_STYLE_POSITION, value, spec);
}
}
value = value->next;
}
}
static fz_css_value static_value_normal = { CSS_KEYWORD, "normal", NULL, NULL };
static fz_css_value *
add_shorthand_font_size(fz_css_match *match, fz_css_value *value, int spec)
{
/* font-size */
add_property(match, PRO_FONT_SIZE, value, spec);
/* / line-height */
if (value->next && value->next->next && !strcmp(value->next->data, "/"))
{
value = value->next->next;
add_property(match, PRO_LINE_HEIGHT, value, spec);
}
return value;
}
static void
add_shorthand_font(fz_css_match *match, fz_css_value *value, int spec)
{
fz_css_value *font_style = NULL;
fz_css_value *font_variant = NULL;
fz_css_value *font_weight = NULL;
/* add the start as font-family for most robust scanning of matching font names */
add_property(match, PRO_FONT_FAMILY, value, spec);
/* then look for known style/variant/weight keywords and font-size/line-height */
for (; value; value = value->next)
{
/* style/variant/weight/size */
if (value->type == CSS_KEYWORD)
{
if (keyword_in_list(value->data, font_style_kw, nelem(font_style_kw)))
font_style = value;
else if (keyword_in_list(value->data, font_variant_kw, nelem(font_variant_kw)))
font_variant = value;
else if (keyword_in_list(value->data, font_weight_kw, nelem(font_weight_kw)))
font_weight = value;
else if (keyword_in_list(value->data, font_size_kw, nelem(font_size_kw)))
value = add_shorthand_font_size(match, value, spec);
}
else if (value->type == CSS_NUMBER)
font_weight = value;
else if (value->type == CSS_LENGTH || value->type == CSS_PERCENT)
value = add_shorthand_font_size(match, value, spec);
}
/* set all properties to their initial values if not specified! */
if (font_style)
add_property(match, PRO_FONT_STYLE, font_style, spec);
else
add_property(match, PRO_FONT_STYLE, &static_value_normal, spec);
if (font_variant)
add_property(match, PRO_FONT_VARIANT, font_variant, spec);
else
add_property(match, PRO_FONT_VARIANT, &static_value_normal, spec);
if (font_weight)
add_property(match, PRO_FONT_WEIGHT, font_weight, spec);
else
add_property(match, PRO_FONT_WEIGHT, &static_value_normal, spec);
}
static void
add_property(fz_css_match *match, int name, fz_css_value *value, int spec)
{
/* shorthand expansions: */
switch (name)
{
case PRO_MARGIN:
add_shorthand_margin(match, value, spec);
return;
case PRO_PADDING:
add_shorthand_padding(match, value, spec);
return;
case PRO_BORDER_WIDTH:
add_shorthand_border_width(match, value, spec);
return;
case PRO_BORDER_COLOR:
add_shorthand_border_color(match, value, spec);
return;
case PRO_BORDER_STYLE:
add_shorthand_border_style(match, value, spec);
return;
case PRO_BORDER:
add_shorthand_border(match, value, spec, 1, 1, 1, 1);
return;
case PRO_BORDER_TOP:
add_shorthand_border(match, value, spec, 1, 0, 0, 0);
return;
case PRO_BORDER_RIGHT:
add_shorthand_border(match, value, spec, 0, 1, 0, 0);
return;
case PRO_BORDER_BOTTOM:
add_shorthand_border(match, value, spec, 0, 0, 1, 0);
return;
case PRO_BORDER_LEFT:
add_shorthand_border(match, value, spec, 0, 0, 0, 1);
return;
case PRO_LIST_STYLE:
add_shorthand_list_style(match, value, spec);
return;
case PRO_FONT:
add_shorthand_font(match, value, spec);
return;
/* TODO: background */
}
if (name < NUM_PROPERTIES && match->spec[name] <= spec)
{
match->value[name] = value;
match->spec[name] = spec;
}
}
void
fz_match_css(fz_context *ctx, fz_css_match *match, fz_css_match *up, fz_css *css, fz_xml *node)
{
fz_css_rule *rule;
fz_css_selector *sel;
fz_css_property *prop;
const char *s;
int i;
match->up = up;
for (i = 0; i < NUM_PROPERTIES; ++i)
{
match->spec[i] = -1;
match->value[i] = NULL;
}
for (rule = css->rule; rule; rule = rule->next)
{
sel = rule->selector;
while (sel)
{
if (match_selector(sel, node))
{
for (prop = rule->declaration; prop; prop = prop->next)
add_property(match, prop->name, prop->value, selector_specificity(sel, prop->important));
break;
}
sel = sel->next;
}
}
if (fz_use_document_css(ctx))
{
s = fz_xml_att(node, "style");
if (s)
{
fz_try(ctx)
{
prop = fz_parse_css_properties(ctx, css->pool, s);
while (prop)
{
add_property(match, prop->name, prop->value, INLINE_SPECIFICITY);
prop = prop->next;
}
/* We can "leak" the property here, since it is freed along with the pool allocator. */
}
fz_catch(ctx)
{
fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
fz_report_error(ctx);
fz_warn(ctx, "ignoring style attribute");
}
}
}
}
void
fz_match_css_at_page(fz_context *ctx, fz_css_match *match, fz_css *css)
{
fz_css_rule *rule;
fz_css_selector *sel;
fz_css_property *prop;
int i;
match->up = NULL;
for (i = 0; i < NUM_PROPERTIES; ++i)
{
match->spec[i] = -1;
match->value[i] = NULL;
}
for (rule = css->rule; rule; rule = rule->next)
{
sel = rule->selector;
while (sel)
{
if (sel->name && !strcmp(sel->name, "@page"))
{
for (prop = rule->declaration; prop; prop = prop->next)
add_property(match, prop->name, prop->value, selector_specificity(sel, prop->important));
break;
}
sel = sel->next;
}
}
}
void
fz_add_css_font_face(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const char *base_uri, fz_css_property *declaration)
{
fz_html_font_face *custom;
fz_css_property *prop;
fz_font *font = NULL;
fz_buffer *buf = NULL;
fz_stream *stm = NULL;
int is_bold, is_italic, is_small_caps;
char path[2048];
const char *family = "serif";
const char *weight = "normal";
const char *style = "normal";
const char *variant = "normal";
const char *src = NULL;
for (prop = declaration; prop; prop = prop->next)
{
if (prop->name == PRO_FONT_FAMILY) family = prop->value->data;
if (prop->name == PRO_FONT_WEIGHT) weight = prop->value->data;
if (prop->name == PRO_FONT_STYLE) style = prop->value->data;
if (prop->name == PRO_FONT_VARIANT) variant = prop->value->data;
if (prop->name == PRO_SRC) src = prop->value->data;
}
if (!src)
return;
is_bold = is_bold_from_font_weight(weight);
is_italic = is_italic_from_font_style(style);
is_small_caps = !strcmp(variant, "small-caps");
fz_strlcpy(path, base_uri, sizeof path);
fz_strlcat(path, "/", sizeof path);
fz_strlcat(path, src, sizeof path);
fz_urldecode(path);
for (custom = set->custom; custom; custom = custom->next)
if (!strcmp(custom->src, path) && !strcmp(custom->family, family) &&
custom->is_bold == is_bold &&
custom->is_italic == is_italic &&
custom->is_small_caps == is_small_caps)
return; /* already loaded */
fz_var(buf);
fz_var(font);
fz_var(stm);
fz_try(ctx)
{
if (fz_has_archive_entry(ctx, zip, path))
buf = fz_read_archive_entry(ctx, zip, path);
else
{
stm = fz_try_open_file(ctx, src);
if (stm == NULL)
fz_throw(ctx, FZ_ERROR_FORMAT, "cannot locate font '%s' specified by css", src);
buf = fz_read_all(ctx, stm, 0);
}
font = fz_new_font_from_buffer(ctx, NULL, buf, 0, 0);
fz_add_html_font_face(ctx, set, family, is_bold, is_italic, is_small_caps, path, font);
}
fz_always(ctx)
{
fz_drop_buffer(ctx, buf);
fz_drop_stream(ctx, stm);
fz_drop_font(ctx, font);
}
fz_catch(ctx)
{
fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
fz_report_error(ctx);
fz_warn(ctx, "cannot load font-face: %s", src);
}
}
void
fz_add_css_font_faces(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const char *base_uri, fz_css *css)
{
fz_css_rule *rule;
fz_css_selector *sel;
for (rule = css->rule; rule; rule = rule->next)
{
if (!rule->loaded)
{
rule->loaded = 1;
sel = rule->selector;
while (sel)
{
if (sel->name && !strcmp(sel->name, "@font-face"))
{
fz_add_css_font_face(ctx, set, zip, base_uri, rule->declaration);
break;
}
sel = sel->next;
}
}
}
}
static int
is_inheritable_property(int name)
{
return
name == PRO_COLOR ||
name == PRO_DIRECTION ||
name == PRO_FONT_FAMILY ||
name == PRO_FONT_STYLE ||
name == PRO_FONT_VARIANT ||
name == PRO_FONT_WEIGHT ||
name == PRO_LEADING ||
name == PRO_LETTER_SPACING ||
name == PRO_LINE_HEIGHT ||
name == PRO_LIST_STYLE_IMAGE ||
name == PRO_LIST_STYLE_POSITION ||
name == PRO_LIST_STYLE_TYPE ||
name == PRO_ORPHANS ||
name == PRO_OVERFLOW_WRAP ||
name == PRO_QUOTES ||
name == PRO_TEXT_ALIGN ||
name == PRO_TEXT_INDENT ||
name == PRO_TEXT_TRANSFORM ||
name == PRO_VISIBILITY ||
name == PRO_WHITE_SPACE ||
name == PRO_WIDOWS ||
name == PRO_WORD_SPACING ||
// Strictly speaking, text-decoration is not an inherited property,
// but since when drawing an underlined element, all children are also underlined,
// we may as well make it inherited.
name == PRO_TEXT_DECORATION;
}
static fz_css_value *
value_from_inheritable_property(fz_css_match *match, int name)
{
fz_css_value *value = match->value[name];
if (match->up)
{
if (value && !strcmp(value->data, "inherit"))
return value_from_inheritable_property(match->up, name);
if (!value)
return value_from_inheritable_property(match->up, name);
}
return value;
}
static fz_css_value *
value_from_property(fz_css_match *match, int name)
{
fz_css_value *value = match->value[name];
if (match->up)
{
if (value && !strcmp(value->data, "inherit"))
if (name != PRO_FONT_SIZE) /* never inherit 'font-size' textually */
return value_from_property(match->up, name);
if (!value && is_inheritable_property(name))
return value_from_inheritable_property(match->up, name);
}
return value;
}
static const char *
string_from_property(fz_css_match *match, int name, const char *initial)
{
fz_css_value *value = value_from_property(match, name);
if (!value)
return initial;
return value->data;
}
static fz_css_number
make_number(float v, int u)
{
fz_css_number n;
n.value = v;
n.unit = u;
return n;
}
static fz_css_number
make_undefined_number(void)
{
fz_css_number n;
n.value = 0;
n.unit = N_UNDEFINED;
return n;
}
/* Fast but inaccurate strtof. */
static float
fz_css_strtof(char *s, char **endptr)
{
float sign = 1;
float v = 0;
float n = 0;
float d = 1;
if (*s == '-')
{
sign = -1;
++s;
}
while (*s >= '0' && *s <= '9')
{
v = v * 10 + (*s - '0');
++s;
}