]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny.c: initialize the libcss context in svgtiny_parse()
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 4 Oct 2023 04:11:11 +0000 (00:11 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 4 Oct 2023 11:31:34 +0000 (07:31 -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 2ca5a58664f46077a5ce4358b91f16b28b06eb8c..3b886fe770502d384e17cf7ffb8e5b4e075a693b 100644 (file)
@@ -17,6 +17,8 @@
 #include <dom/dom.h>
 #include <dom/bindings/xml/xmlparser.h>
 
+#include <libcss/libcss.h>
+
 #include "svgtiny.h"
 #include "svgtiny_internal.h"
 
@@ -610,6 +612,7 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram,
                const char *buffer, size_t size, const char *url,
                int viewport_width, int viewport_height)
 {
+       css_error css_code;
        dom_document *document;
        dom_exception exc;
        dom_xml_parser *parser;
@@ -695,6 +698,17 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram,
        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;
+               }
+       }
+
 #define SVGTINY_STRING_ACTION2(s,n)                                    \
        if (dom_string_create_interned((const uint8_t *) #n,            \
                                       strlen(#n), &state.interned_##s) \
@@ -727,6 +741,10 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram,
 
        dom_node_unref(svg);
        dom_node_unref(document);
+       css_code = css_select_ctx_destroy(state.select_ctx);
+       if (css_code != CSS_OK) {
+               code = svgtiny_LIBCSS_ERROR;
+       }
 
 cleanup:
        svgtiny_cleanup_state_local(&state);