diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2014-10-13 16:16:28 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2014-12-03 12:25:51 +0100 |
commit | 5cb1a29be4993d72cf6ab2af128135e2b7e40dd2 (patch) | |
tree | 30c513041af51dafd7d859664bd28922150053b3 | |
parent | 9d1482bc78d72ba330c2130170b49b4e18702623 (diff) | |
download | mupdf-5cb1a29be4993d72cf6ab2af128135e2b7e40dd2.tar.xz |
html: Parse inline style attributes.
-rw-r--r-- | include/mupdf/html.h | 4 | ||||
-rw-r--r-- | source/html/css-apply.c | 14 | ||||
-rw-r--r-- | source/html/css-parse.c | 12 | ||||
-rw-r--r-- | source/html/handler.c | 2 | ||||
-rw-r--r-- | source/html/layout.c | 25 |
5 files changed, 41 insertions, 16 deletions
diff --git a/include/mupdf/html.h b/include/mupdf/html.h index f8882b94..8a1eaa3c 100644 --- a/include/mupdf/html.h +++ b/include/mupdf/html.h @@ -88,6 +88,7 @@ struct property *new_property(const char *name, struct value *value, int spec); struct value *new_value(int type, const char *value); struct rule *fz_parse_css(fz_context *ctx, struct rule *old, const char *source); +struct property *fz_parse_css_properties(fz_context *ctx, const char *source); enum { NONE, BLOCK, INLINE, LIST_ITEM }; enum { STATIC, RELATIVE, ABSOLUTE, FIXED }; @@ -119,7 +120,8 @@ struct computed_style int text_indent; }; -void apply_styles(struct rule *rule, struct style *style, fz_xml *node); +void apply_styles(struct style *style, struct rule *rule, fz_xml *node); +void apply_inline_style(struct style *style, struct property *prop); void compute_style(struct computed_style *cstyle, struct style *style); #endif diff --git a/source/html/css-apply.c b/source/html/css-apply.c index 7f3028a9..550ade85 100644 --- a/source/html/css-apply.c +++ b/source/html/css-apply.c @@ -156,6 +156,8 @@ count_selector_names(struct selector *sel) return n; } +#define INLINE_SPECIFICITY 1000 + int selector_specificity(struct selector *sel) { @@ -595,7 +597,7 @@ add_property(struct style *style, const char *name, struct value *value, int spe } void -apply_styles(struct rule *rule, struct style *style, fz_xml *node) +apply_styles(struct style *style, struct rule *rule, fz_xml *node) { struct selector *sel; struct property *prop; @@ -617,6 +619,16 @@ apply_styles(struct rule *rule, struct style *style, fz_xml *node) } } +void +apply_inline_style(struct style *style, struct property *prop) +{ + while (prop) + { + add_property(style, prop->name, prop->value, INLINE_SPECIFICITY); + prop = prop->next; + } +} + static const char *inherit_list[] = { "color", "direction", "font-family", "font-size", "font-style", "font-variant", "font-weight", diff --git a/source/html/css-parse.c b/source/html/css-parse.c index 5f7fd254..4ceb10b5 100644 --- a/source/html/css-parse.c +++ b/source/html/css-parse.c @@ -448,14 +448,14 @@ static struct property *parse_declaration_list(struct lexbuf *buf) { struct property *p, *pp; - if (buf->lookahead == '}') + if (buf->lookahead == '}' || buf->lookahead == EOF) return NULL; pp = parse_declaration(buf); while (accept(buf, ';')) { - if (buf->lookahead != '}' && buf->lookahead != ';') + if (buf->lookahead != '}' && buf->lookahead != ';' && buf->lookahead != EOF) { p = parse_declaration(buf); p->next = pp; @@ -725,6 +725,14 @@ static struct rule *parse_stylesheet(struct lexbuf *buf, struct rule *chain) return chain; } +struct property *fz_parse_css_properties(fz_context *ctx, const char *source) +{ + struct lexbuf buf; + css_lex_init(ctx, &buf, source); + next(&buf); + return parse_declaration_list(&buf); +} + struct rule *fz_parse_css(fz_context *ctx, struct rule *chain, const char *source) { struct lexbuf buf; diff --git a/source/html/handler.c b/source/html/handler.c index 77b362d3..f5002437 100644 --- a/source/html/handler.c +++ b/source/html/handler.c @@ -21,7 +21,7 @@ html_page * html_load_page(html_document *doc, int number) { printf("html: load page %d\n", number); - return "nothing"; + return (html_page*)"nothing"; } void diff --git a/source/html/layout.c b/source/html/layout.c index 91e20b9f..14edc5ef 100644 --- a/source/html/layout.c +++ b/source/html/layout.c @@ -93,7 +93,7 @@ static void layout_text(struct rule *rule, struct style *style, fz_xml *node) printf("%s\n", fz_xml_text(node)); } -static void layout_tree(struct rule *rule, struct style *up, fz_xml *node) +static void layout_tree(fz_context *ctx, struct rule *rule, struct style *up, fz_xml *node) { while (node) { @@ -103,16 +103,19 @@ static void layout_tree(struct rule *rule, struct style *up, fz_xml *node) if (fz_xml_tag(node)) { - printf("open '%s'\n", fz_xml_tag(node)); struct computed_style cstyle; - apply_styles(rule, &style, node); + const char *s; - // TODO: check inline style attribute! - //s = fz_xml_att(node, "style"); - //if (s) { - // istyle = parse_declarations(s); - // apply_styles(istyle); - //} + printf("open '%s'\n", fz_xml_tag(node)); + apply_styles(&style, rule, node); + + s = fz_xml_att(node, "style"); + if (s) + { + struct property *props = fz_parse_css_properties(ctx, s); + apply_inline_style(&style, props); + // free props + } compute_style(&cstyle, &style); print_style(&cstyle); @@ -124,7 +127,7 @@ static void layout_tree(struct rule *rule, struct style *up, fz_xml *node) // TODO: <img> if (fz_xml_down(node)) - layout_tree(rule, &style, fz_xml_down(node)); + layout_tree(ctx, rule, &style, fz_xml_down(node)); printf("end\n"); node = fz_xml_next(node); @@ -149,5 +152,5 @@ html_layout_document(html_document *doc, float w, float h) print_rules(css); - layout_tree(css, NULL, doc->root); + layout_tree(doc->ctx, css, NULL, doc->root); } |