X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fsvgtiny.c;h=98a84d44d7921635a03f67d7188a453a2aaf8ae2;hb=099cae0989f0ef3db52de99fb0d1e562465f5a02;hp=d16a761300ad7b0657eb7b59478a085af06b1cae;hpb=b94bca14bb7cd65bda2a92454a17a2e0d0b7234c;p=libsvgtiny.git diff --git a/src/svgtiny.c b/src/svgtiny.c index d16a761..98a84d4 100644 --- a/src/svgtiny.c +++ b/src/svgtiny.c @@ -71,6 +71,8 @@ static void svgtiny_parse_font_attributes(dom_element *node, struct svgtiny_parse_state *state); static void svgtiny_parse_transform_attributes(dom_element *node, struct svgtiny_parse_state *state); +static css_select_results *svgtiny_parse_styles(dom_element *node, + struct svgtiny_parse_state *state); static svgtiny_code svgtiny_add_path(float *p, unsigned int n, struct svgtiny_parse_state *state); static void _svgtiny_parse_color(const char *s, svgtiny_colour *c, @@ -626,7 +628,22 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram, dom_element *svg; dom_string *svg_name; lwc_string *svg_name_lwc; - struct svgtiny_parse_state state; + struct svgtiny_parse_state state = { + /* Initialize the unit context here because it has a + * const member and doing it any other way subverts + * the type system. The magic numbers below were taken + * from the libcss example program without much + * thought, because at the moment we don't support any + * properties with units. */ + .unit_ctx = { + .font_size_default = FLTTOFIX(16.0), + .font_size_minimum = FLTTOFIX(6.0), + .device_dpi = FLTTOFIX(96.0), + .root_style = NULL, + .pw = NULL, + .measure = NULL, + } + }; float x, y, width, height; svgtiny_code code; @@ -686,7 +703,7 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram, dom_node_unref(document); return svgtiny_LIBDOM_ERROR; } - if (!dom_string_caseless_lwc_isequal(svg_name, svg_name_lwc)) { + if (!dom_string_lwc_isequal(svg_name, svg_name_lwc)) { lwc_string_unref(svg_name_lwc); dom_string_unref(svg_name); dom_node_unref(svg); @@ -697,24 +714,28 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram, lwc_string_unref(svg_name_lwc); dom_string_unref(svg_name); - /* get graphic dimensions */ + /* initialize the state struct with zeros */ memset(&state, 0, sizeof(state)); + + /* get graphic dimensions */ state.diagram = diagram; state.document = document; state.viewport_width = viewport_width; state.viewport_height = viewport_height; - /* Initialize CSS context */ - if (state.select_ctx == NULL) { - css_code = css_select_ctx_create(&state.select_ctx); - if (css_code != CSS_OK) { - dom_node_unref(svg); - dom_node_unref(document); - return svgtiny_LIBCSS_ERROR; - } + css_code = css_select_ctx_create(&state.select_ctx); + if (css_code != CSS_OK) { + dom_node_unref(svg); + dom_node_unref(document); + return svgtiny_LIBCSS_ERROR; } + /* ...and the unit context, whose other fields were + * initialized along with the parser state itself */ + state.unit_ctx.viewport_width = FLTTOFIX(viewport_width); + state.unit_ctx.viewport_height = FLTTOFIX(viewport_height); + #define SVGTINY_STRING_ACTION2(s,n) \ if (dom_string_create_interned((const uint8_t *) #n, \ strlen(#n), &state.interned_##s) \ @@ -725,6 +746,19 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram, #include "svgtiny_strings.h" #undef SVGTINY_STRING_ACTION2 + /* Intern SVG's xmlns separately because it's an lwc_string + * and not a dom_string. We initialize its pointer to NULL + * because the "cleanup:" test to see if it needs to be free'd + * looks for NULL. Returning a LIBDOM_ERROR on failure is not + * perfect but it's the closest of the available options. */ + state.interned_svg_xmlns = NULL; + if (lwc_intern_string("http://www.w3.org/2000/svg", + 26, + &state.interned_svg_xmlns) != lwc_error_ok) { + code = svgtiny_LIBDOM_ERROR; + goto cleanup; + } + svgtiny_parse_position_attributes(svg, state, &x, &y, &width, &height); diagram->width = width; diagram->height = height; @@ -762,6 +796,11 @@ cleanup: dom_string_unref(state.interned_##s); #include "svgtiny_strings.h" #undef SVGTINY_STRING_ACTION2 + + if (state.interned_svg_xmlns != NULL) { + lwc_string_unref(state.interned_svg_xmlns); + } + return code; } @@ -906,7 +945,7 @@ svgtiny_code svgtiny_preparse_styles(dom_element *svg, return svgtiny_LIBDOM_ERROR; } - if (dom_string_caseless_isequal(state.interned_style, + if (dom_string_isequal(state.interned_style, nodename)) { /* We have a