commit c527a753aca14569d4261e89eb60175264ee8f3f
parent 8c19a63e3cb9bdd8a6545b0ddad936d60fa2759a
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date: Mon, 7 Jul 2025 12:42:31 -0300
example: rasterize the svg to png
This is a more complete and useful demonstration.
Diffstat:
1 file changed, 33 insertions(+), 13 deletions(-)
diff --git a/example/example.odin b/example/example.odin
@@ -1,9 +1,12 @@
package main
+import "core:c"
import "core:fmt"
+import "core:math"
import "core:os/os2"
import "core:path/filepath"
-import "core:slice"
+
+import "vendor:stb/image"
import nanosvg "../"
@@ -24,20 +27,37 @@ main :: proc() {
name = "example/sample.svg"
}
+ // Parse the svg.
img := nanosvg.ParseFromFile(name, "px", 96)
defer nanosvg.Delete(img)
- fmt.println(img)
-
- for shape := img.shapes; shape != nil; shape = shape.next {
- fmt.println()
- fmt.println(shape)
- for path := shape.paths; path != nil; path = path.next {
- fmt.println(path)
- for path in slice.from_ptr(path.pts, int(path.npts)) {
- fmt.println(path)
- }
- }
- }
+ // Rasterize the svg.
+ r := nanosvg.CreateRasterizer()
+ defer nanosvg.DeleteRasterizer(r)
+
+ buf := make([]byte, int(math.ceil(img.width) * img.height) * 4)
+ defer delete(buf)
+
+ nanosvg.Rasterize(
+ r,
+ img,
+ 0,
+ 0,
+ 1,
+ raw_data(buf),
+ c.int(img.width),
+ c.int(img.height),
+ c.int(math.ceil(img.width) * 4),
+ )
+
+ // Write png to disk.
+ image.write_png(
+ "out.png",
+ c.int(img.width),
+ c.int(img.height),
+ 4,
+ raw_data(buf),
+ c.int(math.ceil(img.width) * 4),
+ )
}