Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #870: Added the stylesheet option to the C API #873

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 as f32;
}

/// @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
Loading