odin-nanosvg

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

example.odin (1093B)


      1 package main
      2 
      3 import "core:c"
      4 import "core:fmt"
      5 import "core:math"
      6 import "core:os/os2"
      7 import "core:path/filepath"
      8 
      9 import "vendor:stb/image"
     10 
     11 import nanosvg "../"
     12 
     13 main :: proc() {
     14 	context.allocator = context.temp_allocator
     15 	defer free_all()
     16 
     17 	dir, dir_err := os2.get_working_directory(context.temp_allocator)
     18 	if dir_err != nil {
     19 		panic(fmt.tprintf("getting working directory: %v", dir_err))
     20 	}
     21 
     22 	name: cstring
     23 
     24 	if filepath.base(dir) == "example" {
     25 		name = "sample.svg"
     26 	} else {
     27 		name = "example/sample.svg"
     28 	}
     29 
     30 	// Parse the svg.
     31 	img := nanosvg.ParseFromFile(name, "px", 96)
     32 	defer nanosvg.Delete(img)
     33 
     34 	// Rasterize the svg.
     35 	r := nanosvg.CreateRasterizer()
     36 	defer nanosvg.DeleteRasterizer(r)
     37 
     38 	buf := make([]byte, int(math.ceil(img.width) * img.height) * 4)
     39 	defer delete(buf)
     40 
     41 	nanosvg.Rasterize(
     42 		r,
     43 		img,
     44 		0,
     45 		0,
     46 		1,
     47 		raw_data(buf),
     48 		c.int(img.width),
     49 		c.int(img.height),
     50 		c.int(math.ceil(img.width) * 4),
     51 	)
     52 
     53 	// Write png to disk.
     54 	image.write_png(
     55 		"out.png",
     56 		c.int(img.width),
     57 		c.int(img.height),
     58 		4,
     59 		raw_data(buf),
     60 		c.int(math.ceil(img.width) * 4),
     61 	)
     62 }
     63