+/**
+ * Parse element styles.
+ *
+ * First we parse any inline "style" attributes. We then compose the
+ * element's style with any parent styles. Finally, we compute any
+ * styles that we support and set the corresponding fields in the
+ * parser state.
+ */
+css_select_results *svgtiny_parse_styles(dom_element *node,
+ struct svgtiny_parse_state *state)
+{
+ css_error code;
+ uint8_t fill_opacity_type;
+ css_fixed fill_opacity;
+ uint8_t stroke_opacity_type;
+ css_fixed stroke_opacity;
+
+ /* We store the result of svgtiny_parse_style_inline() in
+ * inline_sheet, and that function returns NULL on error; in
+ * particular you do not need to css_stylesheet_destroy() the
+ * result if it is NULL, and css_stylesheet_destroy() checks
+ * for that case. */
+ css_stylesheet *inline_sheet = NULL;
+
+ /* Initialize this to NULL for the same reason: so that we can
+ * safely destroy it later even if we never populated it. */
+ css_select_results *styles = NULL;
+
+ /* The result of composing this node's styles with its
+ * parent's styles. */
+ css_computed_style *composed = NULL;
+
+ dom_exception exc;
+ dom_string *attr;
+
+ exc = dom_element_get_attribute(node, state->interned_style, &attr);
+ if (exc != DOM_NO_ERR) {
+ return NULL;
+ }
+ if (attr != NULL) {
+ inline_sheet = svgtiny_parse_style_inline(
+ (uint8_t *)dom_string_data(attr),
+ dom_string_byte_length(attr));
+ dom_string_unref(attr);
+ }
+
+ code = svgtiny_select_style(state, node, inline_sheet, &styles);
+ css_stylesheet_destroy(inline_sheet);
+ if (code != CSS_OK) {
+ return NULL;
+ }
+
+ if (state->parent_style != NULL) {
+ code = css_computed_style_compose(
+ state->parent_style,
+ styles->styles[CSS_PSEUDO_ELEMENT_NONE],
+ &state->unit_ctx,
+ &composed);
+
+ if (code != CSS_OK || composed == NULL) {
+ /* This function promises to return a
+ * fully-composed set of styles, so if
+ * we can't do that, we should fail. */
+ css_select_results_destroy(styles);
+ return NULL;
+ }
+
+ /* Replace my original computed styles with the
+ * composed ones */
+ css_computed_style_destroy(
+ styles->styles[CSS_PSEUDO_ELEMENT_NONE]);
+ styles->styles[CSS_PSEUDO_ELEMENT_NONE] = composed;
+ }
+
+ fill_opacity_type = css_computed_fill_opacity(
+ styles->styles[CSS_PSEUDO_ELEMENT_NONE],
+ &fill_opacity);
+ stroke_opacity_type = css_computed_stroke_opacity(
+ styles->styles[CSS_PSEUDO_ELEMENT_NONE],
+ &stroke_opacity);
+
+ state->fill_opacity = FIXTOFLT(fill_opacity);
+ state->stroke_opacity = FIXTOFLT(stroke_opacity);
+
+ return styles;
+}