]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny.c: initialise/finalise the libcss context
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 7 Jun 2025 13:51:16 +0000 (09:51 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 8 Jun 2025 02:18:20 +0000 (22:18 -0400)
Before we begin parsing, we have to initialize our new css_select_ctx
by calling css_select_ctx_create(). Later, when parsing is complete,
we css_select_ctx_destroy() it.

src/svgtiny.c

index 44ca0fedad0dc1b0828dff36f29d13709ae4b33c..a3080ea098a8698ff7521fc122abd7898872440c 100644 (file)
@@ -18,6 +18,8 @@
 #include <dom/dom.h>
 #include <dom/bindings/xml/xmlparser.h>
 
+#include <libcss/libcss.h>
+
 #include "svgtiny.h"
 #include "svgtiny_internal.h"
 
@@ -1077,7 +1079,9 @@ initialise_parse_state(struct svgtiny_parse_state *state,
                       int viewport_height)
 {
        float x, y, width, height;
+       css_error css_code;
 
+       /* initialize the state struct with zeros */
        memset(state, 0, sizeof(*state));
 
        state->diagram = diagram;
@@ -1112,12 +1116,22 @@ initialise_parse_state(struct svgtiny_parse_state *state,
        state->fill = 0x000000;
        state->stroke = svgtiny_TRANSPARENT;
        state->stroke_width = 1;
+
+       /* Initialize the CSS context */
+       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;
+       }
+
        return svgtiny_OK;
 }
 
 
 static svgtiny_code finalise_parse_state(struct svgtiny_parse_state *state)
 {
+       css_error css_code;
        svgtiny_cleanup_state_local(state);
 
 #define SVGTINY_STRING_ACTION2(s,n)                            \
@@ -1125,6 +1139,12 @@ static svgtiny_code finalise_parse_state(struct svgtiny_parse_state *state)
                dom_string_unref(state->interned_##s);
 #include "svgtiny_strings.h"
 #undef SVGTINY_STRING_ACTION2
+
+       css_code = css_select_ctx_destroy(state->select_ctx);
+       if (css_code != CSS_OK) {
+               return svgtiny_LIBCSS_ERROR;
+       }
+
        return svgtiny_OK;
 }