Skip to content

nico-robert/tresvg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tresvg - Tcl SVG rendering

Tcl wrapper around resvg

From resvg repository :
resvg is an SVG rendering library. It can be used as a Rust library, as a C library, and as a CLI application to render static SVG files.
The core idea is to make a fast, small, portable SVG library with the goal to support the whole SVG spec.

Compatibility :

  • Tcl 8.6 or higher

Dependencies :

Note

critcl and Tk are optional for rendering SVG image to photo command.

Cross-Platform :

  • Windows, Linux, macOS support.

Example :

tcl-cffi demo :

package require tresvg

set svg hello.svg

# Initialize log (optional)
tresvg::initLog

# Create options
set opt [tresvg::optionsCreate]

# Parse svg file
set tree [tresvg::parseTreeFromFile $svg $opt]

# Apply transform
set transform [tresvg::transformIdentity]

# Get image size
set size [tresvg::getImageSize $tree]
set width  [expr {int([dict get $size width])}]
set height [expr {int([dict get $size height])}]

# Render
set pixmap [tresvg::render $tree $transform $width $height]

# Save to PNG
tresvg::toPNG $pixmap $width $height "/Users/nico/temp/output.png"

# Cleanup
tresvg::optionsDestroy $opt
tresvg::treeDestroy $tree

critcl demo :

package require Tk ; # Important to load Tk before 'tresvg' package.
package require tresvg

set svg hello.svg
set img [image create photo]

# Others args are optional (see listing commands below)
tresvg::toTkImg $svg $img

# Others examples of 'tresvg::toTkImg' command :
tresvg::toTkImg $svg $img -width 100 -height 50
tresvg::toTkImg $svg $img -scale 2 -dpi 300
tresvg::toTkImg $svg $img -mtx {1 0 0 1 0 0}
tresvg::toTkImg $svg $img -width 100 -height 50 -modeScale "fit"
tresvg::toTkImg $svg $img -initLog "true"

label .l -image $img
pack .l

Commands :

Tcl/Tk commands help
tresvg::toPNG pixmap width height filename
tresvg::toBase64 pixmap width height
tresvg::toTkImg 'svgFile or svgData' tkImage ?args?`

Commands listing :

resvg-c tcl-cffi critcl args help
resvg_transform_identity tresvg::transformIdentity default matrix -
resvg_init_log tresvg::initLog -initLog "bool_value" Use it if you want to see any warnings.
resvg_options_create tresvg::optionsCreate - Creates a new #resvg_options object.
resvg_options_destroy tresvg::optionsDestroy - Destroys a #resvg_options object.
resvg_tree_destroy tresvg::treeDestroy - Destroys a #resvg_tree object.
resvg_options_set_resources_dir tresvg::optionsSetResourcesDir -resourcesDir $path Sets the path to the resources directory.
resvg_options_set_dpi tresvg::optionsSetDpi -dpi $dpi Sets the DPI of the rendering.
resvg_options_set_stylesheet tresvg::optionsSetStylesheet -styleSheet $css Sets the CSS styles used by the SVG rendering.
resvg_options_set_font_family tresvg::optionsSetFontFamily -fontFamily $family Sets the font family.
resvg_options_set_font_size tresvg::optionsSetFontSize -fontSize $size Sets the font size.
resvg_options_set_serif_family tresvg::optionsSetSerifFamily -serifFontFamily $family Sets the serif font family.
resvg_options_set_sans_serif_family tresvg::optionsSetSansSerifFamily -sansSerifFontFamily $family Sets the sans-serif font family.
resvg_options_set_cursive_family tresvg::optionsSetCursiveFamily -cursiveFontFamily $family Sets the cursive font family.
resvg_options_set_fantasy_family tresvg::optionsSetFantasyFamily -fantasyFontFamily $family Sets the fantasy font family.
resvg_options_set_monospace_family tresvg::optionsSetMonospaceFamily -monospaceFontFamily $family Sets the monospace font family.
resvg_options_set_languages tresvg::optionsSetLanguages -languages $languages Sets the languages.
resvg_options_set_shape_rendering_mode tresvg::optionsSetShapeRenderingMode -shapeRenderingMode $mode Sets the shape rendering mode.
resvg_options_set_text_rendering_mode tresvg::optionsSetTextRenderingMode -textRenderingMode $mode Sets the text rendering mode.
resvg_options_set_image_rendering_mode tresvg::optionsSetImageRenderingMode -imageRenderingMode $mode Sets the image rendering mode.
resvg_options_load_font_data tresvg::optionsLoadFontData ❌ not implemented yet -
resvg_options_load_font_file tresvg::optionsLoadFontFile -loadFontFile $fontfile Loads a font file.
resvg_options_load_system_fonts tresvg::optionsLoadSystemFonts -loadSystemFonts "bool_value" Loads the system fonts.
resvg_parse_tree_from_file tresvg::parseTreeFromFile - Parses a SVG file.
resvg_parse_tree_from_data tresvg::parseTreeFromData tresvg::toTkImg $svgString Parses a SVG data.
resvg_is_image_empty tresvg::isImageEmpty ❌ not implemented yet Gets if an image is empty.
resvg_get_image_size tresvg::getImageSize ❌ not implemented yet Gets the size of an image.
resvg_get_object_bbox tresvg::getObjectBbox ❌ not implemented yet Gets the bounding box of an object.
resvg_get_image_bbox tresvg::getImageBbox ❌ not implemented yet Gets the bounding box of an image.
resvg_node_exists tresvg::nodeExists ❌ not implemented yet Gets if a node exists.
resvg_get_node_transform tresvg::getNodeTransform ❌ not implemented yet Gets the transform of a node.
resvg_get_node_bbox tresvg::getNodeBBox ❌ not implemented yet Gets the bounding box of a node.
resvg_get_node_stroke_bbox tresvg::getNodeStrokeBBox ❌ not implemented yet Gets the stroke bounding box of a node.
resvg_tree_destroy tresvg::treeDestroy - Destroys a #resvg_tree object.
resvg_render tresvg::render tresvg::toTkImg $svg Renders a SVG file or data to Tk image photo.
resvg_render_node tresvg::renderNode ❌ not implemented yet Renders a node.
struct resvg_transform [list a b c d e f] -mtx {a b c d e f} Sets the transform matrix.
- - -width $width Sets the width of the image.
- - -height $height Sets the height of the image.
- - -scale $scale Sets the scale of the image.
- - -modeScale "fit" Sets the mode scale of the image.

Building :

You can either build the library yourself (if you have Rust installed or if you want to install it), or use this repository’s GitHub Actions to automatically build platform-specific binaries for you.


Option 1 — Build locally (Rust required)

If you already have Rust installed, you can build the C library directly from source:

cd resvg-X.XX.X/crates/c-api
cargo build --release

This command compiles the resvg C API and produces the libraries under:

target/release/

After building, copy the generated dynamic or static libraries into your tresvg package directory under one of the following platform-specific folders:

  • Linux: your/path/to/lib/tresvg/linux-x86_64
  • Windows: your/path/to/lib/tresvg/win32-x86_64
  • macOS (Intel): your/path/to/lib/tresvg/macosx-x86_64
  • macOS (ARM): your/path/to/lib/tresvg/macosx-arm

⚠️ Compilation under Windows to work with critcl: :

# Rust toolchain with GNU target
rustup target add x86_64-pc-windows-gnu
cargo build --release --target x86_64-pc-windows-gnu

This will produce dynamic libraries in /target/x86_64-pc-windows-gnu/release


Option 2 — Use prebuilt binaries (no Rust required)

If you prefer not to install or compile Rust yourself, you can simply use this GitHub repository (fork it).
It includes a GitHub Actions workflow that automatically builds and uploads binaries for all supported platforms whenever changes are pushed.

You can enable the workflow in your own fork to generate them automatically.

Tip

Make sure to match your platform architecture (e.g. x86_64 vs arm64).
The header file resvg.h is included in each build under the your_platform/include/ directory.

License :

MIT.

Acknowledgments :

To RazrFalcon, the author of resvg, who helped me understand his library a few years ago.

Release :

  • 21-Oct-2025 : 0.1
    • Initial release.
  • 26-Oct-2025 : 0.15
    • Adds a better support for critcl backend.
    • Adds others commands for tcl-cffi backend.
    • Adds gitHub Actions workflow to build resvg binaries.
    • Cosmetic changes.
  • 29-Oct-2025 : 0.17
    • Uses critcl if present to replace the tresvg::encodePNG command for tcl-cffi backend,
      by using stbi_write_png_to_mem function from stb_image_write.h header file if also exists.
    • Cosmetic changes.