odin-nanosvg

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

nanosvg.odin (3865B)


      1 package nanosvg
      2 
      3 import "core:c"
      4 
      5 ENABLE_RASTERIZER :: #config(ENABLE_RASTERIZER, true)
      6 
      7 when ENABLE_RASTERIZER {
      8 	when ODIN_OS == .Windows {
      9 		foreign import Nanosvg "bin/windows/nanosvgrast.lib"
     10 	} else when ODIN_OS == .Linux {
     11 		foreign import Nanosvg "bin/linux/nanosvgrast.a"
     12 	} else when ODIN_OS == .Darwin && ODIN_ARCH == .arm64 {
     13 		foreign import Nanosvg "bin/macos-arm64/nanosvgrast.a"
     14 	} else when ODIN_OS == .Darwin && ODIN_ARCH != .arm64 {
     15 		foreign import Nanosvg "bin/macos/nanosvgrast.a"
     16 	} else when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 {
     17 		foreign import Nanosvg "bin/wasm/nanosvgrast.o"
     18 	}
     19 } else {
     20 	when ODIN_OS == .Windows {
     21 		foreign import Nanosvg "bin/windows/nanosvg.lib"
     22 	} else when ODIN_OS == .Linux {
     23 		foreign import Nanosvg "bin/linux/nanosvg.a"
     24 	} else when ODIN_OS == .Darwin && ODIN_ARCH == .arm64 {
     25 		foreign import Nanosvg "bin/macos-arm64/nanosvg.a"
     26 	} else when ODIN_OS == .Darwin && ODIN_ARCH != .arm64 {
     27 		foreign import Nanosvg "bin/macos/nanosvg.a"
     28 	} else when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 {
     29 		foreign import Nanosvg "bin/wasm/nanosvg.o"
     30 	}
     31 }
     32 
     33 EnumBackingType :: c.int
     34 
     35 PaintType :: enum EnumBackingType {
     36 	Undefined       = -1,
     37 	None            = 0,
     38 	Color           = 1,
     39 	Linear_Gradient = 2,
     40 	Radial_Gradient = 3,
     41 }
     42 
     43 LineJoin :: enum EnumBackingType {
     44 	Miter = 0,
     45 	Round = 1,
     46 	Bevel = 2,
     47 }
     48 
     49 LineCap :: enum EnumBackingType {
     50 	Butt   = 0,
     51 	Round  = 1,
     52 	Square = 2,
     53 }
     54 
     55 FillRule :: enum EnumBackingType {
     56 	Non_Zero = 0,
     57 	Even_Odd = 1,
     58 }
     59 
     60 Flags :: enum EnumBackingType {
     61 	Visible = 0x01,
     62 }
     63 
     64 GradientStop :: struct {
     65 	color:  c.uint32_t,
     66 	offset: c.float,
     67 }
     68 
     69 Gradient :: struct {
     70 	xform:  [6]c.float,
     71 	spread: c.char,
     72 	fx, fy: c.float,
     73 	nstops: c.int,
     74 	stops:  [^]GradientStop,
     75 }
     76 
     77 Paint :: struct {
     78 	type:       PaintType,
     79 	using data: PaintData,
     80 }
     81 
     82 PaintData :: struct #raw_union {
     83 	color:    c.uint32_t,
     84 	gradient: ^Gradient,
     85 }
     86 
     87 Path :: struct {
     88 	pts:    [^]c.float,
     89 	npts:   c.int,
     90 	closed: c.char,
     91 	bounds: [4]c.float,
     92 	next:   ^Path,
     93 }
     94 
     95 Shape :: struct {
     96 	id:                           [64]c.char,
     97 	fill:                         Paint,
     98 	stroke:                       Paint,
     99 	opacity:                      c.float,
    100 	stroke_width:                 c.float,
    101 	stroke_dash_offset:           c.float,
    102 	stroke_dash_array:            [8]c.float,
    103 	stroke_dash_count:            c.char,
    104 	stroke_line_join:             LineJoin,
    105 	stroke_line_cap:              LineCap,
    106 	miter_limit:                  c.float,
    107 	fill_rule:                    FillRule,
    108 	flags:                        bit_set[Flags],
    109 	bounding_box:                 [4]c.float,
    110 	optional_fill_gradient_id:    [64]c.char,
    111 	optional_stroke_gradient_id:  [64]c.char,
    112 	root_gradient_transformation: [6]c.float,
    113 	paths:                        ^Path,
    114 	next:                         ^Shape,
    115 }
    116 
    117 Image :: struct {
    118 	width:  c.float,
    119 	height: c.float,
    120 	shapes: ^Shape,
    121 }
    122 
    123 @(link_prefix = "nsvg", default_calling_convention = "c")
    124 foreign Nanosvg {
    125 	// Parses SVG file from a file, returns SVG image as paths.
    126 	ParseFromFile :: proc(filename: cstring, units: cstring, dpi: c.float) -> ^Image ---
    127 	// Parses SVG file from a null terminated string, returns SVG image as paths.
    128 	// Important note: changes the string.
    129 	Parse :: proc(input: cstring, units: cstring, dpi: c.float) -> ^Image ---
    130 	// Duplicates a path.
    131 	DuplicatePath :: proc(p: ^Path) -> ^Path ---
    132 	// Deletes an image.
    133 	Delete :: proc(image: ^Image) ---
    134 }
    135 
    136 when ENABLE_RASTERIZER {
    137 	// Rasterize is opaque because the caller doesn't interact with it's data.
    138 	Rasterizer :: struct {}
    139 
    140 	@(link_prefix = "nsvg", default_calling_convention = "c")
    141 	foreign Nanosvg {
    142 		CreateRasterizer :: proc() -> ^Rasterizer ---
    143 		DeleteRasterizer :: proc(r: ^Rasterizer) ---
    144 		Rasterize :: proc(r: ^Rasterizer, image: ^Image, offset_x, offset_y, scale: c.float, dst: ^c.uchar, w, h, stride: c.int) ---
    145 	}
    146 }
    147