diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2014-11-26 02:47:36 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2014-12-03 12:25:52 +0100 |
commit | 1d2d217346c246f7283a1274bb7313e41084337b (patch) | |
tree | ae4635dc341e2b20e53a08d1018fc00eab944a15 | |
parent | a27f2e924f7c8d55fa62b89833c61621a9ca1b87 (diff) | |
download | mupdf-1d2d217346c246f7283a1274bb7313e41084337b.tar.xz |
html: Namespace prefix CSS functions.
-rw-r--r-- | include/mupdf/html.h | 19 | ||||
-rw-r--r-- | source/html/css-apply.c | 213 | ||||
-rw-r--r-- | source/html/css-parse.c | 32 | ||||
-rw-r--r-- | source/html/html-font.c | 2 | ||||
-rw-r--r-- | source/html/html-layout.c | 38 |
5 files changed, 106 insertions, 198 deletions
diff --git a/include/mupdf/html.h b/include/mupdf/html.h index eeb713a9..2b1d127f 100644 --- a/include/mupdf/html.h +++ b/include/mupdf/html.h @@ -156,22 +156,23 @@ struct flow 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); -struct rule *new_rule(struct selector *selector, struct property *declaration); -struct selector *new_selector(const char *name); -struct condition *new_condition(int type, const char *key, const char *val); -struct property *new_property(const char *name, struct value *value, int spec); -struct value *new_value(int type, const char *value); +struct rule *fz_new_css_rule(fz_context *ctx, struct selector *selector, struct property *declaration); +struct selector *fz_new_css_selector(fz_context *ctx, const char *name); +struct condition *fz_new_css_condition(fz_context *ctx, int type, const char *key, const char *val); +struct property *fz_new_css_property(fz_context *ctx, const char *name, struct value *value, int spec); +struct value *fz_new_css_value(fz_context *ctx, int type, const char *value); -int get_style_property_display(struct style *node); +int fz_get_css_style_property_display(struct style *node); void apply_styles(fz_context *ctx, struct style *style, struct rule *rule, fz_xml *node); void default_computed_style(struct computed_style *cstyle); void compute_style(fz_context *ctx, fz_html_font_set *set, struct computed_style *cstyle, struct style *style); -float from_number(struct number, float em, float width); -float from_number_scale(struct number number, float scale, float em, float width); + +float fz_from_css_number(struct number, float em, float width); +float fz_from_css_number_scale(struct number number, float scale, float em, float width); fz_html_font_set *fz_new_html_font_set(fz_context *ctx); -fz_font *fz_html_load_font(fz_context *ctx, fz_html_font_set *set, +fz_font *fz_load_html_font(fz_context *ctx, fz_html_font_set *set, const char *family, const char *variant, const char *style, const char *weight); void fz_free_html_font_set(fz_context *ctx, fz_html_font_set *htx); diff --git a/source/html/css-apply.c b/source/html/css-apply.c index 289811c6..a1b8d800 100644 --- a/source/html/css-apply.c +++ b/source/html/css-apply.c @@ -3,73 +3,58 @@ static void add_property(struct style *style, const char *name, struct value *value, int spec); struct rule * -new_rule(struct selector *selector, struct property *declaration) +fz_new_css_rule(fz_context *ctx, struct selector *selector, struct property *declaration) { - struct rule *rule; - - rule = malloc(sizeof(struct rule)); + struct rule *rule = fz_malloc_struct(ctx, struct rule); rule->selector = selector; rule->declaration = declaration; rule->next = NULL; - return rule; } struct selector * -new_selector(const char *name) +fz_new_css_selector(fz_context *ctx, const char *name) { - struct selector *sel; - - sel = malloc(sizeof(struct selector)); + 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 * -new_condition(int type, const char *key, const char *val) +fz_new_css_condition(fz_context *ctx, int type, const char *key, const char *val) { - struct condition *cond; - - cond = malloc(sizeof(struct condition)); + 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 * -new_property(const char *name, struct value *value, int spec) +fz_new_css_property(fz_context *ctx, const char *name, struct value *value, int spec) { - struct property *prop; - - prop = malloc(sizeof(struct property)); + 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 * -new_value(int type, const char *data) +fz_new_css_value(fz_context *ctx, int type, const char *data) { - struct value *val; - - val = malloc(sizeof(struct value)); + struct value *val = fz_malloc_struct(ctx, struct value); val->type = type; val->data = strdup(data); val->args = NULL; val->next = NULL; - return val; } @@ -158,7 +143,7 @@ count_selector_names(struct selector *sel) #define INLINE_SPECIFICITY 1000 -int +static int selector_specificity(struct selector *sel) { int b = count_selector_ids(sel); @@ -269,7 +254,7 @@ print_rules(struct rule *rule) * Selector matching */ -int +static int match_id_condition(fz_xml *node, const char *p) { const char *s = fz_xml_att(node, "id"); @@ -278,7 +263,7 @@ match_id_condition(fz_xml *node, const char *p) return 0; } -int +static int match_class_condition(fz_xml *node, const char *p) { const char *s = fz_xml_att(node, "class"); @@ -295,7 +280,7 @@ match_class_condition(fz_xml *node, const char *p) return 0; } -int +static int match_condition(struct condition *cond, fz_xml *node) { if (!cond) @@ -311,7 +296,7 @@ match_condition(struct condition *cond, fz_xml *node) return match_condition(cond->next, node); } -int +static int match_selector(struct selector *sel, fz_xml *node) { if (!node) @@ -407,153 +392,75 @@ count_values(struct value *value) } static void -add_shorthand_margin(struct style *style, struct value *value, int spec) +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, "margin-top", value, spec); - add_property(style, "margin-right", value, spec); - add_property(style, "margin-bottom", value, spec); - add_property(style, "margin-left", value, spec); + 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 = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); + struct value *a = value; + struct value *b = value->next; - add_property(style, "margin-top", a, spec); - add_property(style, "margin-right", b, spec); - add_property(style, "margin-bottom", a, spec); - add_property(style, "margin-left", b, spec); + 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 = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - struct value *c = new_value(value->next->next->type, value->next->next->data); - - add_property(style, "margin-top", a, spec); - add_property(style, "margin-right", b, spec); - add_property(style, "margin-bottom", c, spec); - add_property(style, "margin-left", b, spec); + 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 = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - struct value *c = new_value(value->next->next->type, value->next->next->data); - struct value *d = new_value(value->next->next->next->type, value->next->next->next->data); - - add_property(style, "margin-top", a, spec); - add_property(style, "margin-right", b, spec); - add_property(style, "margin-bottom", c, spec); - add_property(style, "margin-left", d, spec); + 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_padding(struct style *style, struct value *value, int spec) +add_shorthand_margin(struct style *style, struct value *value, int spec) { - int n = count_values(value); - - if (n == 1) - { - add_property(style, "padding-top", value, spec); - add_property(style, "padding-right", value, spec); - add_property(style, "padding-bottom", value, spec); - add_property(style, "padding-left", value, spec); - } - - if (n == 2) - { - struct value *a = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - - add_property(style, "padding-top", a, spec); - add_property(style, "padding-right", b, spec); - add_property(style, "padding-bottom", a, spec); - add_property(style, "padding-left", b, spec); - } - - if (n == 3) - { - struct value *a = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - struct value *c = new_value(value->next->next->type, value->next->next->data); - - add_property(style, "padding-top", a, spec); - add_property(style, "padding-right", b, spec); - add_property(style, "padding-bottom", c, spec); - add_property(style, "padding-left", b, spec); - } + add_shorthand_trbl(style, value, spec, + "margin-top", "margin-right", "margin-bottom", "margin-left"); +} - if (n == 4) - { - struct value *a = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - struct value *c = new_value(value->next->next->type, value->next->next->data); - struct value *d = new_value(value->next->next->next->type, value->next->next->next->data); - - add_property(style, "padding-top", a, spec); - add_property(style, "padding-right", b, spec); - add_property(style, "padding-bottom", c, spec); - add_property(style, "padding-left", d, spec); - } +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) { - int n = count_values(value); - - if (n == 1) - { - 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); - } - - if (n == 2) - { - struct value *a = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - - add_property(style, "border-width-top", a, spec); - add_property(style, "border-width-right", b, spec); - add_property(style, "border-width-bottom", a, spec); - add_property(style, "border-width-left", b, spec); - } - - if (n == 3) - { - struct value *a = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - struct value *c = new_value(value->next->next->type, value->next->next->data); - - add_property(style, "border-width-top", a, spec); - add_property(style, "border-width-right", b, spec); - add_property(style, "border-width-bottom", c, spec); - add_property(style, "border-width-left", b, spec); - } - - if (n == 4) - { - struct value *a = new_value(value->type, value->data); - struct value *b = new_value(value->next->type, value->next->data); - struct value *c = new_value(value->next->next->type, value->next->next->data); - struct value *d = new_value(value->next->next->next->type, value->next->next->next->data); - - add_property(style, "border-width-top", a, spec); - add_property(style, "border-width-right", b, spec); - add_property(style, "border-width-bottom", c, spec); - add_property(style, "border-width-left", d, spec); - } + add_shorthand_trbl(style, value, spec, + "border-width-top", "border-width-right", "border-width-bottom", "border-width-left"); } static void @@ -846,7 +753,7 @@ border_width_from_property(struct style *node, const char *property) } float -from_number(struct number number, float em, float width) +fz_from_css_number(struct number number, float em, float width) { switch (number.unit) { default: @@ -857,7 +764,7 @@ from_number(struct number number, float em, float width) } float -from_number_scale(struct number number, float scale, float em, float width) +fz_from_css_number_scale(struct number number, float scale, float em, float width) { switch (number.unit) { default: @@ -947,7 +854,7 @@ color_from_property(struct style *node, const char *property, struct color initi } int -get_style_property_display(struct style *node) +fz_get_css_style_property_display(struct style *node) { struct value *value = get_style_property(node, "display"); if (value) @@ -964,7 +871,7 @@ get_style_property_display(struct style *node) return DIS_INLINE; } -int +static int get_style_property_white_space(struct style *node) { struct value *value = get_style_property(node, "white-space"); @@ -1087,7 +994,7 @@ compute_style(fz_context *ctx, fz_html_font_set *set, struct computed_style *sty 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_html_load_font(ctx, set, font_family, font_variant, font_style, font_weight); + style->font = fz_load_html_font(ctx, set, font_family, font_variant, font_style, font_weight); } } diff --git a/source/html/css-parse.c b/source/html/css-parse.c index d4aa39ec..35d11a1d 100644 --- a/source/html/css-parse.c +++ b/source/html/css-parse.c @@ -373,7 +373,7 @@ static struct value *parse_value(struct lexbuf *buf) if (buf->lookahead == CSS_KEYWORD) { - v = new_value(CSS_KEYWORD, buf->string); + v = fz_new_css_value(buf->ctx, CSS_KEYWORD, buf->string); next(buf); if (accept(buf, '(')) @@ -394,15 +394,15 @@ static struct value *parse_value(struct lexbuf *buf) case CSS_STRING: case CSS_COLOR: case CSS_URI: - v = new_value(buf->lookahead, buf->string); + v = fz_new_css_value(buf->ctx, buf->lookahead, buf->string); next(buf); return v; } if (accept(buf, ',')) - return new_value(',', ","); + return fz_new_css_value(buf->ctx, ',', ","); if (accept(buf, '/')) - return new_value('/', "/"); + return fz_new_css_value(buf->ctx, '/', "/"); fz_throw(buf->ctx, FZ_ERROR_GENERIC, "syntax error: expected value"); } @@ -431,7 +431,7 @@ static struct property *parse_declaration(struct lexbuf *buf) if (buf->lookahead != CSS_KEYWORD) fz_throw(buf->ctx, FZ_ERROR_GENERIC, "syntax error: expected keyword in property"); - p = new_property(buf->string, NULL, 0); + p = fz_new_css_property(buf->ctx, buf->string, NULL, 0); next(buf); expect(buf, ':'); @@ -487,7 +487,7 @@ static struct condition *parse_condition(struct lexbuf *buf) { if (buf->lookahead != CSS_KEYWORD) fz_throw(buf->ctx, FZ_ERROR_GENERIC, "syntax error: expected keyword after ':'"); - c = new_condition(':', "pseudo", buf->string); + c = fz_new_css_condition(buf->ctx, ':', "pseudo", buf->string); next(buf); return c; } @@ -496,7 +496,7 @@ static struct condition *parse_condition(struct lexbuf *buf) { if (buf->lookahead != CSS_KEYWORD) fz_throw(buf->ctx, FZ_ERROR_GENERIC, "syntax error: expected keyword after '.'"); - c = new_condition('.', "class", buf->string); + c = fz_new_css_condition(buf->ctx, '.', "class", buf->string); next(buf); return c; } @@ -505,7 +505,7 @@ static struct condition *parse_condition(struct lexbuf *buf) { if (buf->lookahead != CSS_KEYWORD) fz_throw(buf->ctx, FZ_ERROR_GENERIC, "syntax error: expected keyword after '#'"); - c = new_condition('#', "id", buf->string); + c = fz_new_css_condition(buf->ctx, '#', "id", buf->string); next(buf); return c; } @@ -515,7 +515,7 @@ static struct condition *parse_condition(struct lexbuf *buf) if (buf->lookahead != CSS_KEYWORD) fz_throw(buf->ctx, FZ_ERROR_GENERIC, "syntax error: expected keyword after '['"); - c = new_condition('[', buf->string, NULL); + c = fz_new_css_condition(buf->ctx, '[', buf->string, NULL); next(buf); if (accept(buf, '=')) @@ -562,14 +562,14 @@ static struct selector *parse_simple_selector(struct lexbuf *buf) if (accept(buf, '*')) { - s = new_selector(NULL); + s = fz_new_css_selector(buf->ctx, NULL); if (iscond(buf->lookahead)) s->cond = parse_condition_list(buf); return s; } else if (buf->lookahead == CSS_KEYWORD) { - s = new_selector(buf->string); + s = fz_new_css_selector(buf->ctx, buf->string); next(buf); if (iscond(buf->lookahead)) s->cond = parse_condition_list(buf); @@ -577,7 +577,7 @@ static struct selector *parse_simple_selector(struct lexbuf *buf) } else if (iscond(buf->lookahead)) { - s = new_selector(NULL); + s = fz_new_css_selector(buf->ctx, NULL); s->cond = parse_condition_list(buf); return s; } @@ -593,7 +593,7 @@ static struct selector *parse_adjacent_selector(struct lexbuf *buf) if (accept(buf, '+')) { b = parse_adjacent_selector(buf); - s = new_selector(NULL); + s = fz_new_css_selector(buf->ctx, NULL); s->combine = '+'; s->left = a; s->right = b; @@ -610,7 +610,7 @@ static struct selector *parse_child_selector(struct lexbuf *buf) if (accept(buf, '>')) { b = parse_child_selector(buf); - s = new_selector(NULL); + s = fz_new_css_selector(buf->ctx, NULL); s->combine = '>'; s->left = a; s->right = b; @@ -627,7 +627,7 @@ static struct selector *parse_descendant_selector(struct lexbuf *buf) if (buf->lookahead != ',' && buf->lookahead != '{' && buf->lookahead != EOF) { b = parse_descendant_selector(buf); - s = new_selector(NULL); + s = fz_new_css_selector(buf->ctx, NULL); s->combine = ' '; s->left = a; s->right = b; @@ -657,7 +657,7 @@ static struct rule *parse_rule(struct lexbuf *buf) expect(buf, '{'); p = parse_declaration_list(buf); expect(buf, '}'); - return new_rule(s, p); + return fz_new_css_rule(buf->ctx, s, p); } static void parse_media_list(struct lexbuf *buf) diff --git a/source/html/html-font.c b/source/html/html-font.c index ce8c2ed1..e5d42bdc 100644 --- a/source/html/html-font.c +++ b/source/html/html-font.c @@ -9,7 +9,7 @@ static const char *font_names[16] = { }; fz_font * -fz_html_load_font(fz_context *ctx, fz_html_font_set *set, +fz_load_html_font(fz_context *ctx, fz_html_font_set *set, const char *family, const char *variant, const char *style, const char *weight) { unsigned char *data; diff --git a/source/html/html-layout.c b/source/html/html-layout.c index 31c41a71..384a2720 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -255,7 +255,7 @@ static void generate_boxes(fz_context *ctx, fz_html_font_set *set, fz_archive *z { apply_styles(ctx, &style, rule, node); - display = get_style_property_display(&style); + display = fz_get_css_style_property_display(&style); if (!strcmp(tag, "br")) { @@ -344,10 +344,10 @@ static void measure_word(fz_context *ctx, struct flow *node, float em) int c, g; float w; - em = from_number(node->style->font_size, em, em); + em = fz_from_css_number(node->style->font_size, em, em); node->x = 0; node->y = 0; - node->h = from_number_scale(node->style->line_height, em, em, em); + node->h = fz_from_css_number_scale(node->style->line_height, em, em, em); w = 0; s = node->text; @@ -471,8 +471,8 @@ static void layout_flow(fz_context *ctx, struct box *box, struct box *top, float float baseline; int align; - em = from_number(box->style.font_size, em, em); - indent = box->is_first_flow ? from_number(top->style.text_indent, em, top->w) : 0; + em = fz_from_css_number(box->style.font_size, em, em); + indent = box->is_first_flow ? fz_from_css_number(top->style.text_indent, em, top->w) : 0; align = top->style.text_align; box->x = top->x; @@ -548,24 +548,24 @@ static void layout_block(fz_context *ctx, struct box *box, struct box *top, floa float *border = box->border; float *padding = box->padding; - em = from_number(box->style.font_size, em, em); + em = fz_from_css_number(box->style.font_size, em, em); - margin[0] = from_number(box->style.margin[0], em, top->w); - margin[1] = from_number(box->style.margin[1], em, top->w); - margin[2] = from_number(box->style.margin[2], em, top->w); - margin[3] = from_number(box->style.margin[3], em, top->w); + margin[0] = fz_from_css_number(box->style.margin[0], em, top->w); + margin[1] = fz_from_css_number(box->style.margin[1], em, top->w); + margin[2] = fz_from_css_number(box->style.margin[2], em, top->w); + margin[3] = fz_from_css_number(box->style.margin[3], em, top->w); - padding[0] = from_number(box->style.padding[0], em, top->w); - padding[1] = from_number(box->style.padding[1], em, top->w); - padding[2] = from_number(box->style.padding[2], em, top->w); - padding[3] = from_number(box->style.padding[3], em, top->w); + padding[0] = fz_from_css_number(box->style.padding[0], em, top->w); + padding[1] = fz_from_css_number(box->style.padding[1], em, top->w); + padding[2] = fz_from_css_number(box->style.padding[2], em, top->w); + padding[3] = fz_from_css_number(box->style.padding[3], em, top->w); if (box->style.border_style) { - border[0] = from_number(box->style.border_width[0], em, top->w); - border[1] = from_number(box->style.border_width[1], em, top->w); - border[2] = from_number(box->style.border_width[2], em, top->w); - border[3] = from_number(box->style.border_width[3], em, top->w); + border[0] = fz_from_css_number(box->style.border_width[0], em, top->w); + border[1] = fz_from_css_number(box->style.border_width[1], em, top->w); + border[2] = fz_from_css_number(box->style.border_width[2], em, top->w); + border[3] = fz_from_css_number(box->style.border_width[3], em, top->w); } else border[0] = border[1] = border[2] = border[3] = 0; @@ -602,7 +602,7 @@ static void layout_block(fz_context *ctx, struct box *box, struct box *top, floa { /* TODO: interaction with page breaks */ if (prev_br) - box->h += from_number_scale(box->style.line_height, em, em, em); + box->h += fz_from_css_number_scale(box->style.line_height, em, em, em); prev_br = 1; } else if (child->type == BOX_FLOW) |