odin-nanosvg

Odin bindings to nanosvg
Log | Files | Refs | Submodules | README | LICENSE

README.md (1413B)


      1 # odin-nanosvg
      2 
      3 Odin bindings for [nanosvg](https://github.com/memononen/nanosvg).
      4 
      5 ## clone
      6 
      7 `git clone --recurse-submodules https://git.sr.ht/~jackmordaunt/odin-nanosvg`
      8 
      9 ## build
     10 
     11 You can rely on the pre-built release binaries shipped with this repo,
     12 or you can build them yourself using the script `build_libs.sh`.
     13 
     14 `nanosvg` contains only the parsing logic.
     15 `nanosvgrast` additionally contains a simple cpu rasterizer.
     16 
     17 By default we link to `nanosvgrast`. If you don't need the rasterizer you can trim it out with `-define:ENABLE_RASTERIZER=false`.
     18 
     19 For debug symbols call `build_libs.sh debug`.
     20 
     21 NOTE: the build script uses `zig` for easy cross compilation.
     22 
     23 ## usage
     24 
     25 
     26 ```odin
     27 package main
     28 
     29 import "core:c"
     30 import "core:fmt"
     31 import "core:math"
     32 
     33 import "vendor:stb/image"
     34 
     35 import nanosvg "../"
     36 
     37 main :: proc() {
     38 	// Parse the svg.
     39 	img := nanosvg.ParseFromFile("sample.svg", "px", 96)
     40 	defer nanosvg.Delete(img)
     41 
     42 	r := nanosvg.CreateRasterizer()
     43 	defer nanosvg.DeleteRasterizer(r)
     44 
     45 	buf := make([]byte, int(math.ceil(img.width) * img.height) * 4)
     46 	defer delete(buf)
     47 
     48 	// Rasterize the svg.
     49 	nanosvg.Rasterize(
     50 		r,
     51 		img,
     52 		0,
     53 		0,
     54 		1,
     55 		raw_data(buf),
     56 		c.int(img.width),
     57 		c.int(img.height),
     58 		c.int(math.ceil(img.width) * 4),
     59 	)
     60 
     61 	// Write rasterized png to disk.
     62 	image.write_png(
     63 		"out.png",
     64 		c.int(img.width),
     65 		c.int(img.height),
     66 		4,
     67 		raw_data(buf),
     68 		c.int(math.ceil(img.width) * 4),
     69 	)
     70 }
     71 ```