Skip to content

Commit

Permalink
Added the stylesheet option to the C API (#873)
Browse files Browse the repository at this point in the history
Fixes #870
  • Loading branch information
michabay05 authored Jan 6, 2025
1 parent 6df6351 commit ddd942b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/c-api/examples/cairo/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ int main(int argc, char **argv)
abort();
}

// Initialize resvg's library logging system
resvg_init_log();

resvg_options *opt = resvg_options_create();
resvg_options_load_system_fonts(opt);

// Optionally, you can add some CSS to control the SVG rendering.
resvg_options_set_stylesheet(opt, "svg { fill: black; }");

resvg_render_tree *tree;
// Construct a tree from the svg file and pass in some options
int err = resvg_parse_tree_from_file(argv[1], opt, &tree);
resvg_options_destroy(opt);
if (err != RESVG_OK)
Expand All @@ -33,6 +38,7 @@ int main(int argc, char **argv)
int width = (int)size.width;
int height = (int)size.height;

// Using the dimension info, allocate enough pixels to account for the entire image
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);

/* resvg doesn't support stride, so cairo_surface_t should have no padding */
Expand All @@ -50,9 +56,11 @@ int main(int argc, char **argv)
surface_data[i + 2] = r;
}

// Save image
cairo_surface_write_to_png(surface, argv[2]);
cairo_surface_destroy(surface);

// De-initialize the allocated memory
cairo_surface_destroy(surface);
resvg_tree_destroy(tree);

return 0;
Expand Down
14 changes: 14 additions & 0 deletions crates/c-api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ pub extern "C" fn resvg_options_set_dpi(opt: *mut resvg_options, dpi: f32) {
cast_opt(opt).dpi = dpi;
}

/// @brief Provides the content of a stylesheet that will be used when resolving CSS attributes.
///
/// Must be UTF-8. Can be set to NULL.
///
/// Default: NULL
#[no_mangle]
pub extern "C" fn resvg_options_set_stylesheet(opt: *mut resvg_options, content: *const c_char) {
if content.is_null() {
cast_opt(opt).style_sheet = None;
} else {
cast_opt(opt).style_sheet = Some(cstr_to_str(content).unwrap().into());
}
}

/// @brief Sets the default font family.
///
/// Will be used when no `font-family` attribute is set in the SVG.
Expand Down
9 changes: 9 additions & 0 deletions crates/c-api/resvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ void resvg_options_set_resources_dir(resvg_options *opt, const char *path);
*/
void resvg_options_set_dpi(resvg_options *opt, float dpi);

/**
* @brief Provides the content of a stylesheet that will be used when resolving CSS attributes.
*
* Must be UTF-8. Can be set to NULL.
*
* Default: NULL
*/
void resvg_options_set_stylesheet(resvg_options *opt, const char *content);

/**
* @brief Sets the default font family.
*
Expand Down

0 comments on commit ddd942b

Please sign in to comment.