No description
- Zig 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
dvui now vendors its own direct TVG->triangle renderer, so root_dvui.zig/dvui_render.zig/earcut.zig are removed. The z2d raster renderer (rendering.zig + stroke.zig) is example-only code, so it moves to examples/ (as z2d_render.zig) and imports svg2tvg as an external module instead of local root.zig. svg2tvg core now only does SVG->TVG conversion + the TVG format; it no longer depends on z2d at all. build.zig/build.zig.zon and examples/build.zig updated accordingly, and examples/demo.zig is trimmed to the z2d-only raster demo (the dvui_render vs z2d comparison is gone since dvui hosts that renderer itself now). |
||
| examples | ||
| src | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| LICENSE | ||
| README.md | ||
svg2tvg - convert svg to tvg and render tvg to image!
- written in zig 0.16.0
- dependency name: svg2tvg, module name: svg2tvg
status / background
- the main goal is to convert and render icons
- there is a z2d based renderer, that has options to overwrite color and stroke width, but its source is not vendored anymore in src/ to simplify dependencies, instead it lives in the examples/ folder, feel free to copy it from there if you need raster based rendering!
- if you need triangle based rendering using svg2tvg, its implemented and vendored inside dvui's icon rendering pipeline
- only a small subset of svg is supported
- related project: zig-lib-icons
- as of now used in the dvui project as part of rendering svg/tvg icons
api
- tvg_from_svg(...) - convert svg to tvg
- renderStream(...) - render tvg to an image
- RenderOptions - Options for rendering
usage:
test "convert to tvg and render" {
const svg2tvg = @import("svg2tvg");
const gpa = std.testing.allocator;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const alloc = arena.allocator();
const Wrapper = struct {
width: i64,
height: i64,
img: *MyImage,
pub fn setPixel(self: *@This(), x: i64, y: i64, color: [4]u8) void {
const pix: MyPixel = .init_from_u8_slice(&color);
self.img.set_pixel(@intCast(x), @intCast(y), pix);
}
};
const width_and_height = 200;
var myImage = try MyImageType.init(alloc, width_and_height, width_and_height);
const my_svg_icon_data = icons.svg.feather.activity;
var w = std.ArrayList(u8).init(alloc);
try svg2tvg.tvg_from_svg(alloc, w.writer(), svg_bytes);
var image_wrapper = Wrapper{
.img = img,
.width = @intCast(img.get_width()),
.height = @intCast(img.get_height()),
};
var fb = std.io.fixedBufferStream(w.items);
try svg2tvg.renderStream(alloc, &image_wrapper, fb.reader(), .{}); <-- you can use options here
...
}
Credit:
- Chris Marchesi: https://github.com/vancluever/z2d (used to render tvg as image)
- Ian Johnson: https://github.com/ianprime0509/zig-xml (used for parsing xml)
- Chris Marchesi: https://github.com/vancluever/zig-svg (for svg color attribute parsing)
- https://github.com/TinyVG/sdk/tree/main (used for writing tvg)
Licence
- this library is under MIT
- svg.zig from https://github.com/vancluever/zig-svg is MPL licenced
- z2d is MPL licenced
- zig-xml is under 0BSD
- tinyVG/sdk is under MIT