Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#![doc(html_root_url = "https://github.com/KevinKelley/nanovg-rs")]

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused_qualifications)]
#![allow(unused_imports)]
#![allow(unused_attributes)]
#![allow(dead_code)]
//#![warn(missing_doc)]
#![deny(unused_parens)]
#![deny(non_upper_case_globals)]
#![deny(unused_results)]

#[macro_use]
extern crate bitflags;
extern crate libc;

use std::fmt;
use std::ptr;
use std::str;
use std::ffi::CString;

use libc::{c_char, c_int, c_void, c_float};

use ffi::NVGcolor;
use ffi::NVGpaint;
use ffi::NVGglyphPosition;
use ffi::NVGtextRow;

mod ffi;

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u32)]
pub enum Winding {
    CCW                     = ffi::NVG_CCW,
    CW                      = ffi::NVG_CW,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u32)]
pub enum Solidity {
    SOLID                   = ffi::NVG_SOLID,
    HOLE                    = ffi::NVG_HOLE,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u32)]
pub enum LineCap {
    BUTT                    = ffi::NVG_BUTT,
    ROUND                   = ffi::NVG_ROUND,
    SQUARE                  = ffi::NVG_SQUARE,
    BEVEL                   = ffi::NVG_BEVEL,
    MITER                   = ffi::NVG_MITER,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u32)]
pub enum PatternRepeat {
    NOREPEAT                = ffi::NVG_NOREPEAT,
    REPEATX                 = ffi::NVG_REPEATX,
    REPEATY                 = ffi::NVG_REPEATY,
}

bitflags!{
    flags Align: u32 {
        const LEFT         = ffi::NVG_ALIGN_LEFT,
        const CENTER       = ffi::NVG_ALIGN_CENTER,
        const RIGHT        = ffi::NVG_ALIGN_RIGHT,
        const TOP          = ffi::NVG_ALIGN_TOP,
        const MIDDLE       = ffi::NVG_ALIGN_MIDDLE,
        const BOTTOM       = ffi::NVG_ALIGN_BOTTOM,
        const BASELINE     = ffi::NVG_ALIGN_BASELINE
    }
}

bitflags!{
    flags CreationFlags: u32 {
        const ANTIALIAS        = ffi::NVG_ANTIALIAS,
        const STENCIL_STROKES  = ffi::NVG_STENCIL_STROKES
    }
}

bitflags!{
    flags ImageFlags: u32 {
        const GENERATE_MIPMAPS = ffi::NVG_IMAGE_GENERATE_MIPMAPS
    }
}

// Color

#[derive(Clone, Copy, PartialEq)]
pub struct Color {
    nvg: NVGcolor
}

impl Color {
    #[inline]
    fn wrap(nvg: NVGcolor) -> Color { Color { nvg: nvg } }

    #[inline]
    pub fn r(&self) -> f32 { self.nvg.r }

    #[inline]
    pub fn g(&self) -> f32 { self.nvg.g }

    #[inline]
    pub fn b(&self) -> f32 { self.nvg.b }

    #[inline]
    pub fn a(&self) -> f32 { self.nvg.a }

    pub fn rgb(r: u8, g: u8, b: u8) -> Color {
        Color::wrap(unsafe { ffi::nvgRGB(r, g, b) })
    }
    pub fn rgb_f(r: f32, g: f32, b: f32) -> Color {
        Color { nvg: ffi::NVGcolor { r: r, g: g, b: b, a: 1.0 } }
    }
    pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
        Color::wrap(unsafe { ffi::nvgRGBA(r, g, b, a) })
    }
    pub fn rgba_f(r: f32, g: f32, b: f32, a: f32) -> Color {
        Color { nvg: ffi::NVGcolor { r: r, g: g, b: b, a: a } }
    }
    pub fn lerp_rgba(c0: Color, c1: Color, u: f32) -> Color {
        Color::wrap(unsafe { ffi::nvgLerpRGBA(c0.nvg, c1.nvg, u) })
    }
    pub fn trans_rgba(c0: Color, a: u8) -> Color {
        Color::wrap(unsafe { ffi::nvgTransRGBA(c0.nvg, a) })
    }
    pub fn trans_rgba_f(c0: Color, a: f32) -> Color {
        Color::wrap(unsafe { ffi::nvgTransRGBAf(c0.nvg, a) })
    }
    pub fn hsl(h: f32, s: f32, l: f32) -> Color {
        Color::wrap(unsafe { ffi::nvgHSL(h, s, l) })
    }
    pub fn hsla(h: f32, s: f32, l: f32, a: u8) -> Color {
        Color::wrap(unsafe { ffi:: nvgHSLA(h, s, l, a) })
    }
}

impl fmt::Debug for Color {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "r:{}, g:{}, b:{}, a:{}", self.r(), self.g(), self.b(), self.a())
    }
}

// Paint

pub struct Paint {
    nvg: NVGpaint
}

impl Paint {
    #[inline]
    fn wrap(nvg: NVGpaint) -> Paint { Paint { nvg: nvg } }
}

impl fmt::Debug for Paint {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let p: *const NVGpaint = &self.nvg;
        write!(f, "Paint @ {:?}", p)
    }
}

// Image

pub struct Image {
    handle: c_int
}

impl Image {
    #[inline]
    fn wrap(handle: c_int) -> Image { Image { handle: handle } }
}

impl fmt::Debug for Image {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Image #{}", self.handle)
    }
}

//impl Drop for Image {
//    fn drop(&mut self) {
//        Context::delete_image(nvg, self.handle);
//        self.handle = ffi::STB_IMAGE_INVALID;
//    }
//}

// Font

pub struct Font {
    handle: c_int
}

impl Font {
    #[inline]
    fn wrap(handle: c_int) -> Font { Font { handle: handle } }
}

impl fmt::Debug for Font {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Font #{}", self.handle)
    }
}

//impl Drop for Font {
//    fn drop(&mut self) {
//        // seems there's no API in nanovg for unloading fonts!
//        self.handle = ffi::FONS_INVALID;
//    }
//}

// TextRow

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct TextRow {
    start_index: usize,
    end_index: usize,
    next_index: usize,
    width: c_float,
    minx: c_float,
    maxx: c_float,
}

impl TextRow {
    pub fn start_index(&self) -> usize { self.start_index }
    pub fn end_index(&self) -> usize { self.end_index }
    pub fn next_index(&self) -> usize { self.next_index }
    pub fn width(&self) -> f32 { self.width }
    pub fn minx(&self) -> f32 { self.minx }
    pub fn maxx(&self) -> f32 { self.maxx }
}

// GlyphPosition

#[derive(Clone, Copy, PartialEq, Debug)]
pub struct GlyphPosition {
    byte_index: usize,   // start index of this glyph in string
    x: f32,             // glyph's x position
    minx: f32,          // glyph spans from minx to max x
    maxx: f32           // (span may or may not actually contain x, depending on the font)
}

impl GlyphPosition {
    pub fn byte_index(&self) -> usize { self.byte_index }
    pub fn x(&self) -> f32 { self.x }
    pub fn minx(&self) -> f32 { self.minx }
    pub fn maxx(&self) -> f32 { self.maxx }
}

// Transform

#[derive(Clone, Copy)]
pub struct Transform {
    array: [f32; 6]
}

impl fmt::Debug for Transform {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Transform(tx: {}, ty: {}, sx: {}, sy: {}, kx: {}, ky: {})",
               self.e(), self.f(), self.a(), self.d(), self.c(), self.b())
    }
}

//macro_rules! accessors(
//    ($($name:ident -> $idx:expr),+) => (
//        $(#[inline] pub fn $name(&self) -> f32 { self.array[$idx] })+
//    )
//)

//macro_rules! mutators(
//    ($($name:ident ($($p:ident : $t:ty),+) via $d:ident),+) => (
//        $(
//        pub fn $name(mut self, $($p:$t),+) -> Transform {
//            let mut t = Transform::new_zero();
//            t.$d($($p),+);
//            self.set_premultiply(&t);
//            self
//        }
//        )+
//    )
//)

impl Transform {
    #[inline]
    fn as_mut_ptr(&mut self) -> *mut f32 { self.array.as_mut_ptr() }

    #[inline]
    fn as_ptr(&self) -> *const f32 { self.array.as_ptr() }

    #[inline]
    pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [f32] { &mut self.array }

    #[inline]
    pub fn into_array(self) -> [f32; 6] { self.array }

    #[inline]
    pub fn from_array(array: [f32; 6]) -> Transform {
        Transform { array: array }
    }

    #[inline]
    pub fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Transform {
        Transform { array: [a, b, c, d, e, f] }
    }

    #[inline]
    fn new_zero() -> Transform {
        Transform { array: [0.0; 6] }
    }

    pub fn new_identity() -> Transform {
        let mut t = Transform::new_zero();
        t.set_identity();
        t
    }

    pub fn new_from_slice(slice: &[f32]) -> Option<Transform> {
        if slice.len() >= 6 {
            Some(Transform::new(slice[0], slice[1], slice[2], slice[3], slice[4], slice[5]))
        }
        else {
            None
        }
    }

    //accessors!(a -> 0, b -> 1, c -> 2, d -> 3, e -> 4, f -> 5)
    #[inline] pub fn a(&self) -> f32 { self.array[0] }
    #[inline] pub fn b(&self) -> f32 { self.array[1] }
    #[inline] pub fn c(&self) -> f32 { self.array[2] }
    #[inline] pub fn d(&self) -> f32 { self.array[3] }
    #[inline] pub fn e(&self) -> f32 { self.array[4] }
    #[inline] pub fn f(&self) -> f32 { self.array[5] }

    pub fn set_identity(&mut self) {
        unsafe { ffi::nvgTransformIdentity(self.as_mut_ptr()) }
    }

    pub fn set_translate(&mut self, tx: f32, ty: f32) {
        unsafe { ffi::nvgTransformTranslate(self.as_mut_ptr(), tx, ty) }
    }

    pub fn set_scale(&mut self, sx: f32, sy: f32) {
        unsafe { ffi::nvgTransformScale(self.as_mut_ptr(), sx, sy) }
    }

    pub fn set_rotate(&mut self, a: f32) {
        unsafe { ffi::nvgTransformRotate(self.as_mut_ptr(), a) }
    }

    pub fn set_skew_x(&mut self, a: f32) {
        unsafe { ffi::nvgTransformSkewX(self.as_mut_ptr(), a) }
    }

    pub fn set_skew_y(&mut self, a: f32) {
        unsafe { ffi::nvgTransformSkewY(self.as_mut_ptr(), a) }
    }

    pub fn set_multiply(&mut self, src: &Transform) {
        unsafe { ffi::nvgTransformMultiply(self.as_mut_ptr(), src.as_ptr()) }
    }

    pub fn set_premultiply(&mut self, src: &Transform) {
        unsafe { ffi::nvgTransformPremultiply(self.as_mut_ptr(), src.as_ptr()) }
    }

    pub fn set_inverse(&mut self, src: &Transform) -> bool {
        unsafe { ffi::nvgTransformInverse(self.as_mut_ptr(), src.as_ptr()) == 1 }
    }

//    mutators!(
//        translate(tx: f32, ty: f32) via set_translate,
//        scale(sx: f32, sy: f32) via set_scale,
//        rotate(a: f32) via set_rotate,
//        skew_x(a: f32) via set_skew_x,
//        skew_y(a: f32) via set_skew_y,
//        multiply(src: &Transform) via set_multiply,
//        premultiply(src: &Transform) via set_premultiply
//    )
    pub fn translate(mut self, tx: f32, ty: f32) -> Transform {
        let mut t = Transform::new_zero();
        t.set_translate(tx, ty);
        self.set_premultiply(&t);
        self
    }
    pub fn scale(mut self, sx: f32, sy: f32) -> Transform {
        let mut t = Transform::new_zero();
        t.set_scale(sx, sy);
        self.set_premultiply(&t);
        self
    }
    pub fn rotate(mut self, a: f32) -> Transform {
        let mut t = Transform::new_zero();
        t.set_rotate(a);
        self.set_premultiply(&t);
        self
    }
    pub fn skew_x(mut self, a: f32) -> Transform {
        let mut t = Transform::new_zero();
        t.set_skew_x(a);
        self.set_premultiply(&t);
        self
    }
    pub fn skew_y(mut self, a: f32) -> Transform {
        let mut t = Transform::new_zero();
        t.set_skew_y(a);
        self.set_premultiply(&t);
        self
    }
    pub fn multiply(mut self, src: &Transform) -> Transform {
        let mut t = Transform::new_zero();
        t.set_multiply(src);
        self.set_premultiply(&t);
        self
    }
    pub fn premultiply(mut self, src: &Transform) -> Transform {
        let mut t = Transform::new_zero();
        t.set_premultiply(src);
        self.set_premultiply(&t);
        self
    }


    pub fn inverted(mut self) -> Result<Transform, Transform> {
        let copy = self;
        if self.set_inverse(&copy) {
            Ok(self)
        } else {
            Err(copy)
        }
    }

    pub fn transform_point(&self, (srcx, srcy): (f32, f32)) -> (f32, f32) {
        let (mut dstx, mut dsty) = (0.0f32, 0.0f32);
        unsafe { ffi::nvgTransformPoint(&mut dstx, &mut dsty, self.as_ptr(), srcx, srcy); }
        (dstx, dsty)
    }
}

// Context

pub struct Context {
    ptr: *mut ffi::NVGcontext
}

// NoSend has been removed, NoCopy is unstable, and negative trait bounds are not implemented.
// There is no 'clean' way to replace these two lines.

// impl !Send for Context {}
// impl !Sync for Context {}

impl fmt::Debug for Context {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "NVGcontext @ {:?}", self.ptr)
    }
}

#[cfg(any(feature = "gl2", feature = "gl3",
          feature = "gles2", feature = "gles3"))]
impl Drop for Context {
    #[cfg(feature = "gl2")]
    fn drop(&mut self) {
        self.delete_gl2();
        self.ptr = ptr::null_mut();
    }

    #[cfg(feature = "gl3")]
    fn drop(&mut self) {
        self.delete_gl3();
        self.ptr = ptr::null_mut();
    }

    #[cfg(feature = "gles2")]
    fn drop(&mut self) {
        self.delete_gles2();
        self.ptr = ptr::null_mut();
    }

    #[cfg(feature = "gles3")]
    fn drop(&mut self) {
        self.delete_gles3();
        self.ptr = ptr::null_mut();
    }
}

impl Context {
    #[cfg(feature = "gl2")]
    pub fn create_gl2(flags: CreationFlags) -> Context {
        Context {
            ptr: unsafe { ffi::nvgCreateGL2(flags.bits) }
        }
    }

    #[cfg(feature = "gl2")]
    fn delete_gl2(&self) {
        unsafe { ffi::nvgDeleteGL2(self.ptr) }
    }

    #[cfg(feature = "gl3")]
    pub fn create_gl3(flags: CreationFlags) -> Context {
        Context {
            ptr: unsafe { ffi::nvgCreateGL3(flags.bits) }
        }
    }

    #[cfg(feature = "gl3")]
    fn delete_gl3(&self) {
        unsafe { ffi::nvgDeleteGL3(self.ptr) }
    }

    #[cfg(feature = "gles2")]
    pub fn create_gles2(flags: CreationFlags) -> Context {
        Context {
            ptr: unsafe { ffi::nvgCreateGLES2(flags.bits) }
        }
    }

    #[cfg(feature = "gles2")]
    fn delete_gles2(&self) {
        unsafe { ffi::nvgDeleteGLES2(self.ptr) }
    }

    #[cfg(feature = "gles3")]
    pub fn create_gles3(flags: CreationFlags) -> Context {
        Context {
            ptr: unsafe { ffi::nvgCreateGLES3(flags.bits) }
        }
    }

    #[cfg(feature = "gles3")]
    fn delete_gles3(&self) {
        unsafe { ffi::nvgDeleteGLES3(self.ptr) }
    }

    pub fn begin_frame(&self, window_width: u32, window_height: u32, device_pixel_ratio: f32) {
        unsafe { ffi::nvgBeginFrame(self.ptr,
                        window_width as i32, window_height as i32, device_pixel_ratio) }
    }
    pub fn end_frame(&self) {
        unsafe { ffi::nvgEndFrame(self.ptr) }
    }

    pub fn save(&self) {
        unsafe { ffi::nvgSave(self.ptr) }
    }
    pub fn restore(&self) {
        unsafe { ffi::nvgRestore(self.ptr) }
    }
    pub fn reset(&self) {
        unsafe { ffi::nvgReset(self.ptr) }
    }

    pub fn stroke_color(&self, color: Color) {
        unsafe { ffi::nvgStrokeColor(self.ptr, color.nvg) }
    }
    pub fn stroke_paint(&self, paint: Paint) {
        unsafe { ffi::nvgStrokePaint(self.ptr, paint.nvg) }
    }
    pub fn fill_color(&self, color: Color) {
        unsafe { ffi::nvgFillColor(self.ptr, color.nvg) }
    }
    pub fn fill_paint(&self, paint: Paint) {
        unsafe { ffi::nvgFillPaint(self.ptr, paint.nvg) }
    }
    pub fn miter_limit(&self, limit: f32) {
        unsafe { ffi::nvgMiterLimit(self.ptr, limit) }
    }
    pub fn stroke_width(&self, size: f32) {
        unsafe { ffi::nvgStrokeWidth(self.ptr, size) }
    }
    pub fn line_cap(&self, cap: LineCap) {
        unsafe { ffi::nvgLineCap(self.ptr, cap as c_int) }
    }
    pub fn line_join(&self, join: LineCap) {
        unsafe { ffi::nvgLineJoin(self.ptr, join as c_int) }
    }
    pub fn global_alpha(&self, alpha: f32) {
        unsafe { ffi::nvgGlobalAlpha(self.ptr, alpha) }
    }

    pub fn reset_transform(&self) {
        unsafe { ffi::nvgResetTransform(self.ptr) }
    }
    pub fn transform(&self, t: Transform) {
        unsafe { ffi::nvgTransform(self.ptr, t.a(), t.b(), t.c(), t.d(), t.e(), t.f()) }
    }
    pub fn translate(&self, x: f32, y: f32) {
        unsafe { ffi::nvgTranslate(self.ptr, x, y) }
    }
    pub fn rotate(&self, angle: f32) {
        unsafe { ffi::nvgRotate(self.ptr, angle) }
    }
    pub fn skew_x(&self, angle: f32) {
        unsafe { ffi::nvgSkewX(self.ptr, angle) }
    }
    pub fn skew_y(&self, angle: f32) {
        unsafe { ffi::nvgSkewY(self.ptr, angle) }
    }
    pub fn scale(&self, x: f32, y: f32) {
        unsafe { ffi::nvgScale(self.ptr, x, y) }
    }
    pub fn current_transform(&self) -> Transform {
        let mut arr = [0.0f32; 6];
		unsafe { ffi::nvgCurrentTransform(self.ptr, arr.as_mut_ptr()) }
        Transform::from_array(arr)
    }

    #[inline]
    pub fn create_image(&self, filename: &str) -> Option<Image> {
        self.create_image_flags(filename, ImageFlags::empty())
    }

    pub fn create_image_flags(&self, filename: &str, flags: ImageFlags) -> Option<Image> {
        let c_filename = match CString::new(filename.as_bytes()){
            Ok(e) => e,
            Err(_) => return None,
        };
        let handle = unsafe { ffi::nvgCreateImage(self.ptr, c_filename.as_ptr(), flags.bits() as c_int) };
        // stb_image returns 0 for failure; unlike fontstash which returns -1
        match handle {
            ffi::STB_IMAGE_INVALID => { None },
            _ => { Some(Image::wrap(handle)) }
        }
    }

    #[inline]
    pub fn create_image_mem(&self, data: &[u8]) -> Option<Image> {
        self.create_image_mem_flags(data, ImageFlags::empty())
    }

    pub fn create_image_mem_flags(&self, data: &[u8], flags: ImageFlags) -> Option<Image> {
        let handle = unsafe { ffi::nvgCreateImageMem(self.ptr, flags.bits() as c_int, data.as_ptr(), data.len() as c_int) };
        match handle {
            ffi::STB_IMAGE_INVALID => { None },
            _ => { Some(Image::wrap(handle)) }
        }
    }

    #[inline]
    pub fn create_image_rgba(&self, w: u32, h: u32, data: &[u8]) -> Option<Image> {
        self.create_image_rgba_flags(w, h, data, ImageFlags::empty())
    }

    pub fn create_image_rgba_flags(&self, w: u32, h: u32, data: &[u8], flags: ImageFlags) -> Option<Image> {
        let handle = unsafe {
            ffi::nvgCreateImageRGBA(self.ptr, w as i32, h as i32, flags.bits() as c_int, data.as_ptr())
        };
        match handle {
            ffi::STB_IMAGE_INVALID => { None },
            _ => { Some(Image::wrap(handle)) }
        }
    }

    pub fn update_image(&self, image: &Image, data: &[u8]) {
        unsafe { ffi::nvgUpdateImage(self.ptr, image.handle, data.as_ptr()) }
    }

    pub fn image_size(&self, image: &Image) -> (u32, u32) {
        let (mut w, mut h) = (0, 0);
        unsafe { ffi::nvgImageSize(self.ptr, image.handle, &mut w, &mut h) };
        (w as u32, h as u32)
    }

    pub fn delete_image(&self, image: Image) {
        unsafe { ffi::nvgDeleteImage(self.ptr, image.handle) }
    }

    pub fn linear_gradient(&self, sx: f32, sy: f32, ex: f32, ey: f32, icol: Color, ocol: Color) -> Paint {
        Paint::wrap(unsafe { ffi::nvgLinearGradient(self.ptr, sx, sy, ex, ey, icol.nvg, ocol.nvg) })
    }
    pub fn box_gradient(&self, x: f32, y: f32, w: f32, h: f32, r: f32, f: f32, icol: Color, ocol: Color) -> Paint {
        Paint::wrap(unsafe { ffi::nvgBoxGradient(self.ptr, x, y, w, h, r, f, icol.nvg, ocol.nvg) })
    }
    pub fn radial_gradient(&self, cx: f32, cy: f32, inr: f32, outr: f32, icol: Color, ocol: Color) -> Paint {
        Paint::wrap(unsafe { ffi::nvgRadialGradient(self.ptr, cx, cy, inr, outr, icol.nvg, ocol.nvg) })
    }
    pub fn image_pattern(&self, ox: f32, oy: f32, ex: f32, ey: f32, angle: f32, image: &Image, repeat: PatternRepeat, alpha: f32) -> Paint {
        Paint::wrap(unsafe { ffi::nvgImagePattern(self.ptr, ox, oy, ex, ey, angle, image.handle, repeat as c_int, alpha) })
    }

    pub fn scissor(&self, x: f32, y: f32, w: f32, h: f32) {
        unsafe { ffi::nvgScissor(self.ptr, x, y, w, h) }
    }
    pub fn reset_scissor(&self) {
        unsafe { ffi::nvgResetScissor(self.ptr) }
    }

    pub fn begin_path(&self) {
        unsafe { ffi::nvgBeginPath(self.ptr) }
    }
    pub fn move_to(&self, x: f32, y: f32) {
        unsafe { ffi::nvgMoveTo(self.ptr, x, y) }
    }
    pub fn line_to(&self, x: f32, y: f32) {
        unsafe { ffi::nvgLineTo(self.ptr, x, y) }
    }
    pub fn bezier_to(&self, c1x: f32, c1y: f32, c2x: f32, c2y: f32, x: f32, y: f32) {
        unsafe { ffi::nvgBezierTo(self.ptr, c1x, c1y, c2x, c2y, x, y) }
    }
    pub fn quad_to(&self, cx: f32, cy: f32, x: f32, y: f32) {
        unsafe { ffi::nvgQuadTo(self.ptr, cx, cy, x, y) }
    }
    pub fn arc_to(&self, x1: f32, y1: f32, x2: f32, y2: f32, radius: f32) {
        unsafe { ffi::nvgArcTo(self.ptr, x1, y1, x2, y2, radius) }
    }
    pub fn close_path(&self) {
        unsafe { ffi::nvgClosePath(self.ptr) }
    }
    pub fn path_winding(&self, dir: Solidity) {
        unsafe { ffi::nvgPathWinding(self.ptr, dir as c_int) }
    }

    pub fn arc(&self, cx: f32, cy: f32, r: f32, a0: f32, a1: f32, dir: Winding) {
        unsafe { ffi::nvgArc(self.ptr, cx, cy, r, a0, a1, dir as c_int) }
    }
    pub fn rect(&self, x: f32, y: f32, w: f32, h: f32) {
        unsafe { ffi::nvgRect(self.ptr, x, y, w, h) }
    }
    pub fn rounded_rect(&self, x: f32, y: f32, w: f32, h: f32, r: f32) {
        unsafe { ffi::nvgRoundedRect(self.ptr, x, y, w, h, r) }
    }
    pub fn ellipse(&self, cx: f32, cy: f32, rx: f32, ry: f32) {
        unsafe { ffi::nvgEllipse(self.ptr, cx, cy, rx, ry) }
    }
    pub fn circle(&self, cx: f32, cy: f32, r: f32) {
        unsafe { ffi::nvgCircle(self.ptr, cx, cy, r) }
    }
    pub fn fill(&self) {
        unsafe { ffi::nvgFill(self.ptr) }
    }
    pub fn stroke(&self) {
        unsafe { ffi::nvgStroke(self.ptr) }
    }

    pub fn create_font(&self, name: &str, filename: &str) -> Option<Font> {
        let c_name = match CString::new(name.as_bytes()){
            Ok(o) => o,
            Err(_) => return None
        };
        let c_filename = match CString::new(filename.as_bytes()){
            Ok(o) => o,
            Err(_) => return None
        };
        let handle = unsafe { ffi::nvgCreateFont(self.ptr, c_name.as_ptr(), c_filename.as_ptr()) };
        match handle {
            ffi::FONT_INVALID => None,
            _ => Some(Font::wrap(handle))
        }
    }

    pub fn create_font_mem(&self, name: &str, data: &[u8]) -> Option<Font> {
        let c_name = match CString::new(name.as_bytes()){
            Ok(o) => o,
            Err(_) => return None
        };
        let handle = unsafe {
            ffi::nvgCreateFontMem(self.ptr, c_name.as_ptr(),
                                  data.as_ptr() as *mut u8, data.len() as c_int,
                                  0 /* do not free */)
        };
        match handle {
            ffi::FONT_INVALID => None,
            _ => Some(Font::wrap(handle))
        }
    }

    pub fn find_font(&self, name: &str) -> Option<Font> {
        let c_name = match CString::new(name.as_bytes()){
            Ok(o) => o,
            Err(_) => return None
        };
        let handle = unsafe { ffi::nvgFindFont(self.ptr, c_name.as_ptr()) };
        match handle {
            ffi::FONT_INVALID => None,
            _ => Some(Font::wrap(handle))
        }
    }
    pub fn font_size(&self, size: f32) {
        unsafe { ffi::nvgFontSize(self.ptr, size) }
    }
    pub fn font_blur(&self, blur: f32) {
        unsafe { ffi::nvgFontBlur(self.ptr, blur) }
    }
    pub fn text_letter_spacing(&self, spacing: f32) {
        unsafe { ffi::nvgTextLetterSpacing(self.ptr, spacing) }
    }
    pub fn text_line_height(&self, line_height: f32) {
        unsafe { ffi::nvgTextLineHeight(self.ptr, line_height) }
    }
    pub fn text_align(&self, align: Align) {
        unsafe { ffi::nvgTextAlign(self.ptr, align.bits) }
    }
    pub fn font_face_id(&self, font: &Font) {
        unsafe { ffi::nvgFontFaceId(self.ptr, font.handle) }
    }
    pub fn font_face(&self, font: &str) {
        let c_font = match CString::new(font.as_bytes()){
            Ok(o) => o,
            Err(_) => return
        };
        unsafe { ffi::nvgFontFace(self.ptr, c_font.as_ptr()) }
    }
    pub fn text(&self, x: f32, y: f32, text: &str) -> f32 {
        let c_text = match CString::new(text.as_bytes()){
            Ok(o) => o,
            _ => return 0.
        };
        unsafe { ffi::nvgText(self.ptr, x, y, c_text.as_ptr(), ptr::null()) }
    }
    pub fn text_box(&self, x: f32, y: f32, break_row_width: f32, text: &str) {
        let c_text = match CString::new(text.as_bytes()){
            Ok(o) => o,
            _ => return
        };
        unsafe { ffi::nvgTextBox(self.ptr, x, y, break_row_width, c_text.as_ptr(), ptr::null()) }
    }
    // Measures the specified text string. Parameter bounds should be a pointer to float[4],
    // if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
    // Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
    // Measured values are returned in local coordinate space.
    pub fn text_bounds(&self, x: f32, y: f32, text: &str, bounds: &mut [f32; 4]) -> f32 {
        let c_text = match CString::new(text.as_bytes()){
            Ok(o) => o,
            _ => return 0.
        };
        unsafe { ffi::nvgTextBounds(self.ptr, x, y, c_text.as_ptr(), ptr::null(), bounds.as_mut_ptr()) }
    }
    // Measures the needed advance for text, without computing complete bounds
    pub fn text_advance(&self, x:f32, y:f32, text: &str) -> f32 {
        let c_text = match CString::new(text.as_bytes()){
            Ok(o) => o,
            _ => return 0.
        };
        unsafe { ffi::nvgTextBounds(self.ptr, x, y, c_text.as_ptr(), ptr::null(), ptr::null_mut()) }
    }
    // Measures the specified multi-text string. Parameter bounds should be float[4],
    // if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
    // Measured values are returned in local coordinate space.
    pub fn text_box_bounds(&self, x: f32, y: f32, break_row_width: f32, text: &str, bounds: &mut [f32; 4]) {
        let c_text = match CString::new(text.as_bytes()){
            Ok(o) => o,
            _ => return
        };
        //let bptr: *mut f32 = match bounds {
        //    Some(vec) => { bptr = vec.as_mut_ptr() }
        //    None => ptr::null()
        //}
        unsafe { ffi::nvgTextBoxBounds(self.ptr, x, y, break_row_width, c_text.as_ptr(), ptr::null(), bounds.as_mut_ptr()) }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////
    /// return a vector of position info for all glyphs in 'text'.
    /// 'text' is utf8-encoded unicode, so the number of glyphs isn't necessarily the byte-length of the text.
    pub fn text_glyph_positions(&self, x: f32, y: f32, text: &str) -> Vec<GlyphPosition> {
        let mut positions: Vec<NVGglyphPosition> = Vec::with_capacity(text.len());
        for _ in 0..text.len() { // we may not need all of them, but if text is ascii, we will
            positions.push(NVGglyphPosition {
                byte_ptr: ptr::null(),
                x: 0.0,
                minx: 0.0,
                maxx: 0.0
            })
        }
        let st = text.as_ptr() as *const i8;
        let en = unsafe { st.offset(text.len() as isize) };

        let actual_n = unsafe {
            ffi::nvgTextGlyphPositions(self.ptr, x, y, st, en, positions.as_mut_ptr(), positions.len() as c_int)
        };
        assert!(actual_n >= 0);
        let actual_n = actual_n as usize;

        // convert pointers to indexes
        let mut ret_vec:Vec<GlyphPosition> = Vec::with_capacity(actual_n);
        for i in (0..actual_n) {
            let nvg = positions[i];
            ret_vec.push(GlyphPosition {
                byte_index: relative_index(text, nvg.byte_ptr),
                x: nvg.x,
                minx: nvg.minx,
                maxx: nvg.maxx
            });
        }

        return ret_vec;
    }

    pub fn text_break_lines(&self, text: &str, break_row_width: f32, max_rows: usize) -> Vec<TextRow> {
        let st = text.as_ptr() as *const i8;
        let en = unsafe { st.offset(text.len() as isize) };
        let mut rows: Vec<NVGtextRow> = Vec::with_capacity(max_rows);
        for _ in (0..max_rows) {
            rows.push(NVGtextRow {
                start: ptr::null(),
                end:   ptr::null(),
                next:  ptr::null(),
                width: 0.0,
                minx:  0.0,
                maxx:  0.0,
            })
        }

        let actual_n = unsafe {
            ffi::nvgTextBreakLines(self.ptr, st, en, break_row_width, rows.as_mut_ptr(), max_rows as c_int)
        };
        assert!(actual_n >= 0);
        let actual_n = actual_n as usize;

        // convert pointers to indexes
        let mut ret_vec:Vec<TextRow> = Vec::with_capacity(actual_n);
        for i in (0..actual_n) {
            let nvg = rows[i];
            ret_vec.push(TextRow {
                start_index: relative_index(text, nvg.start),
                end_index:   relative_index(text, nvg.end),
                next_index:  relative_index(text, nvg.next),
                width: nvg.width,
                minx: nvg.minx,
                maxx: nvg.maxx
            });
        }

        return ret_vec;
    }

    pub fn text_metrics(&self, ascender: *mut f32, descender: *mut f32, lineh: *mut f32) {
        unsafe { ffi::nvgTextMetrics(self.ptr, ascender, descender, lineh) }
    }

    pub fn debug_dump_path_cache(&self) {
        unsafe { ffi::nvgDebugDumpPathCache(self.ptr) }
    }

}

// given a utf8 string, and a ptr that walks through it,
// return instead the corresponding byte-index into the string.
pub fn relative_index(text: &str, p: *const i8) -> usize {
    let st = text.as_ptr();
    let stix: usize = st as usize;
    let pix: usize = p as usize;
    assert!(pix >= stix);               // require that 'p' point somewhere in the
    assert!(pix - stix <= text.len());  // string, or at most 1 past end (where C null would be)
    pix - stix
}

pub fn deg_to_rad(deg: f32) -> f32 {
    unsafe { ffi::nvgDegToRad(deg) }
}
pub fn rad_to_deg(rad: f32) -> f32 {
    unsafe { ffi::nvgRadToDeg(rad) }
}