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
use std::ffi::CString;
use std::i32;
use std::path::PathBuf;
use std::ops::{Deref, DerefMut};
use std::slice;

#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
mod ffi;

pub use ffi::{
    AspectRatioMode,
    CompositionMode,
    CoordinateMode,
    FillRule,
    FontStretch,
    FontStyle,
    FontWeight,
    PenCapStyle as LineCap,
    PenJoinStyle as LineJoin,
    Spread,
    PathSegmentType,
};

pub use ffi::qtc_qpainter;


pub struct GuiApp(*mut ffi::qtc_qguiapp);

impl GuiApp {
    pub fn new(app_name: &str) -> GuiApp {
        let c_app_name = CString::new(app_name).unwrap();
        unsafe { GuiApp(ffi::qtc_create_gui(c_app_name.as_ptr())) }
    }
}

impl Drop for GuiApp {
    fn drop(&mut self) {
        unsafe { ffi::qtc_destroy_gui(self.0) }
    }
}


pub struct Image(*mut ffi::qtc_qimage);

impl Image {
    pub fn new(width: u32, height: u32) -> Option<Image> {
        unsafe { Self::from_ptr(ffi::qtc_qimage_create(width, height)) }
    }

    pub fn from_file(path: &PathBuf) -> Option<Image> {
        let c_path = CString::new(path.to_str().unwrap()).unwrap();

        unsafe { Self::from_ptr(ffi::qtc_qimage_from_file(c_path.as_ptr())) }
    }

    pub fn from_data(data: &[u8]) -> Option<Image> {
        unsafe { Self::from_ptr(ffi::qtc_qimage_from_data(data.as_ptr(), data.len() as i32)) }
    }

    unsafe fn from_ptr(img: *mut ffi::qtc_qimage) -> Option<Image> {
        if img.is_null() {
            None
        } else {
            Some(Image(img))
        }
    }

    pub fn fill(&mut self, r: u8, g: u8, b: u8, a: u8) {
        unsafe { ffi::qtc_qimage_fill(self.0, r, g, b, a) }
    }

    pub fn set_dpi(&mut self, dpi: f64) {
        unsafe { ffi::qtc_qimage_set_dpi(self.0, dpi) }
    }

    pub fn save(&self, path: &str) -> bool {
        let c_path = CString::new(path).unwrap();
        unsafe { ffi::qtc_qimage_save(self.0, c_path.as_ptr()) }
    }

    pub fn resize(&self, width: u32, height: u32, ratio: AspectRatioMode) -> Option<Image> {
        unsafe { Self::from_ptr(ffi::qtc_qimage_resize(self.0, width, height, ratio)) }
    }

    pub fn copy(&self, x: u32, y: u32, width: u32, height: u32) -> Option<Image> {
        unsafe { Self::from_ptr(ffi::qtc_qimage_copy(self.0, x, y, width, height)) }
    }

    pub fn data_mut(&mut self) -> ImageData {
        unsafe {
            let ptr = ffi::qtc_qimage_get_data(self.0);
            let len = ffi::qtc_qimage_get_byte_count(self.0) as usize;

            ImageData {
                slice: slice::from_raw_parts_mut(ptr, len),
            }
        }
    }

    pub fn width(&self) -> u32 {
        unsafe { ffi::qtc_qimage_get_width(self.0) }
    }

    pub fn height(&self) -> u32 {
        unsafe { ffi::qtc_qimage_get_height(self.0) }
    }
}

impl Drop for Image {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qimage_destroy(self.0) }
    }
}


pub struct ImageData<'a> {
    slice: &'a mut [u8],
}

impl<'a> Deref for ImageData<'a> {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        self.slice
    }
}

impl<'a> DerefMut for ImageData<'a> {
    fn deref_mut(&mut self) -> &mut [u8] {
        self.slice
    }
}


pub struct Painter(*mut ffi::qtc_qpainter, bool);

impl Painter {
    pub fn new(img: &Image) -> Painter {
        unsafe { Painter(ffi::qtc_qpainter_create(img.0), true) }
    }

    pub unsafe fn from_raw(ptr: *mut ffi::qtc_qpainter) -> Painter {
        Painter(ptr, false)
    }

    pub fn set_font(&self, font: &Font) {
        unsafe { ffi::qtc_qpainter_set_font(self.0, font.0) }
    }

    pub fn font_metrics(&self) -> FontMetricsF {
        unsafe { FontMetricsF(ffi::qtc_qpainter_get_fontmetricsf(self.0)) }
    }

    pub fn set_pen(&self, pen: Pen) {
        unsafe { ffi::qtc_qpainter_set_pen(self.0, pen.0) }
    }

    pub fn reset_pen(&self) {
        unsafe { ffi::qtc_qpainter_reset_pen(self.0) }
    }

    pub fn set_brush(&self, brush: Brush) {
        unsafe { ffi::qtc_qpainter_set_brush(self.0, brush.0) }
    }

    pub fn reset_brush(&self) {
        unsafe { ffi::qtc_qpainter_reset_brush(self.0) }
    }

    pub fn set_opacity(&self, opacity: f64) {
        unsafe { ffi::qtc_qpainter_set_opacity(self.0, opacity) }
    }

    pub fn draw_path(&self, path: &PainterPath) {
        unsafe { ffi::qtc_qpainter_draw_path(self.0, path.0) }
    }

    pub fn draw_image(&self, x: f64, y: f64, img: &Image) {
        unsafe { ffi::qtc_qpainter_draw_image(self.0, x, y, img.0) }
    }

    pub fn draw_text(&self, x: f64, y: f64, text: &str) {
        let c_text = CString::new(text).unwrap();
        unsafe { ffi::qtc_qpainter_draw_text(self.0, x, y, c_text.as_ptr()) }
    }

    pub fn draw_rect(&self, x: f64, y: f64, w: f64, h: f64) {
        unsafe { ffi::qtc_qpainter_draw_rect(self.0, x, y, w, h) }
    }

    pub fn scale(&self, sx: f64, sy: f64) {
        unsafe { ffi::qtc_qpainter_scale(self.0, sx, sy) }
    }

    pub fn get_transform(&self) -> Transform {
        unsafe { Transform(ffi::qtc_qpainter_get_transform(self.0)) }
    }

    pub fn set_transform(&self, ts: &Transform) {
        unsafe { ffi::qtc_qpainter_set_transform(self.0, ts.0, false) }
    }

    pub fn apply_transform(&self, ts: &Transform) {
        unsafe { ffi::qtc_qpainter_set_transform(self.0, ts.0, true) }
    }

    pub fn set_clip_path(&self, path: &PainterPath) {
        unsafe { ffi::qtc_qpainter_set_clip_path(self.0, path.0) }
    }

    pub fn reset_clip_path(&self) {
        unsafe { ffi::qtc_qpainter_reset_clip_path(self.0) }
    }

    pub fn set_composition_mode(&self, mode: CompositionMode) {
        unsafe { ffi::qtc_qpainter_set_composition_mode(self.0, mode) }
    }

    pub fn end(&self) {
        unsafe { ffi::qtc_qpainter_end(self.0) }
    }
}

impl Drop for Painter {
    fn drop(&mut self) {
        if self.1 {
            unsafe { ffi::qtc_qpainter_destroy(self.0) }
        }
    }
}


pub struct PainterPath(*mut ffi::qtc_qpainterpath);

impl PainterPath {
    pub fn new() -> PainterPath {
        unsafe { PainterPath(ffi::qtc_qpainterpath_create()) }
    }

    pub fn move_to(&mut self, x: f64, y: f64) {
        unsafe { ffi::qtc_qpainterpath_move_to(self.0, x, y) }
    }

    pub fn line_to(&mut self, x: f64, y: f64) {
        unsafe { ffi::qtc_qpainterpath_line_to(self.0, x, y) }
    }

    pub fn curve_to(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) {
        unsafe { ffi::qtc_qpainterpath_curve_to(self.0, x1, y1, x2, y2, x, y) }
    }

    pub fn close_path(&mut self) {
        unsafe { ffi::qtc_qpainterpath_close_path(self.0) }
    }

    pub fn add_text(&mut self, x: f64, y: f64, font: &Font, text: &str) {
        let c_text = CString::new(text).unwrap();
        unsafe { ffi::qtc_qpainterpath_add_text(self.0, x, y, font.0, c_text.as_ptr()) }
    }

    pub fn len(&self) -> i32 {
        unsafe { ffi::qtc_qpainterpath_element_count(self.0) }
    }

    pub fn get(&self, index: i32) -> (PathSegmentType, f64, f64) {
        let seg = unsafe { ffi::qtc_qpainterpath_element_at(self.0, index) };
        (seg.kind, seg.x, seg.y)
    }

    pub fn bounding_box(&self) -> (f64, f64, f64, f64) {
        let rect = unsafe { ffi::qtc_qpainterpath_get_bbox(self.0) };

        (rect.x, rect.y, rect.w, rect.h)
    }

    pub fn set_fill_rule(&mut self, rule: FillRule) {
        unsafe { ffi::qtc_qpainterpath_set_fill_rule(self.0, rule) }
    }
}

impl Drop for PainterPath {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qpainterpath_destroy(self.0) }
    }
}


pub struct Transform(*mut ffi::qtc_qtransform);

impl Transform {
    pub fn new(a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> Transform {
        unsafe { Transform(ffi::qtc_qtransform_create_from(a, b, c, d, e, f)) }
    }

    pub fn data(&self) -> (f64, f64, f64, f64, f64, f64) {
        let ts = unsafe { ffi::qtc_qtransform_get_data(self.0) };
        (ts.a, ts.b, ts.c, ts.d, ts.e, ts.f)
    }
}

impl Default for Transform {
    fn default() -> Transform {
        unsafe { Transform(ffi::qtc_qtransform_create()) }
    }
}

impl Drop for Transform {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qtransform_destroy(self.0) }
    }
}


pub struct Pen(*mut ffi::qtc_qpen);

impl Pen {
    pub fn new() -> Pen {
        unsafe { Pen(ffi::qtc_qpen_create()) }
    }

    pub fn set_color(&mut self, r: u8, g: u8, b: u8, a: u8) {
        unsafe { ffi::qtc_qpen_set_color(self.0, r, g, b, a) }
    }

    pub fn set_brush(&mut self, brush: Brush) {
        unsafe { ffi::qtc_qpen_set_brush(self.0, brush.0) }
    }

    pub fn set_line_cap(&mut self, s: LineCap) {
        unsafe { ffi::qtc_qpen_set_line_cap(self.0, s) }
    }

    pub fn set_line_join(&mut self, s: LineJoin) {
        unsafe { ffi::qtc_qpen_set_line_join(self.0, s) }
    }

    pub fn set_width(&mut self, width: f64) {
        unsafe { ffi::qtc_qpen_set_width(self.0, width) }
    }

    pub fn set_miter_limit(&mut self, limit: f64) {
        unsafe { ffi::qtc_qpen_set_miter_limit(self.0, limit) }
    }

    pub fn set_dash_offset(&mut self, offset: f64) {
        unsafe { ffi::qtc_qpen_set_dash_offset(self.0, offset) }
    }

    pub fn set_dash_array(&mut self, offset: &[f64]) {
        assert!(offset.len() < i32::MAX as usize);
        unsafe { ffi::qtc_qpen_set_dash_array(self.0, offset.as_ptr(), offset.len() as i32) }
    }
}

impl Drop for Pen {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qpen_destroy(self.0) }
    }
}


pub struct Brush(*mut ffi::qtc_qbrush);

impl Brush {
    pub fn new() -> Brush {
        unsafe { Brush(ffi::qtc_qbrush_create()) }
    }

    pub fn set_color(&mut self, r: u8, g: u8, b: u8, a: u8) {
        unsafe { ffi::qtc_qbrush_set_color(self.0, r, g, b, a) }
    }

    pub fn set_linear_gradient(&mut self, lg: LinearGradient) {
        unsafe { ffi::qtc_qbrush_set_linear_gradient(self.0, lg.0) }
    }

    pub fn set_radial_gradient(&mut self, rg: RadialGradient) {
        unsafe { ffi::qtc_qbrush_set_radial_gradient(self.0, rg.0) }
    }

    pub fn set_pattern(&mut self, img: Image) {
        unsafe { ffi::qtc_qbrush_set_pattern(self.0, img.0) }
    }

    pub fn set_transform(&mut self, ts: Transform) {
        unsafe { ffi::qtc_qbrush_set_transform(self.0, ts.0) }
    }
}

impl Drop for Brush {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qbrush_destroy(self.0) }
    }
}


pub trait Gradient {
    fn set_color_at(&mut self, offset: f64, r: u8, g: u8, b: u8, a: u8);
    fn set_spread(&mut self, spread: Spread);
    fn set_units(&mut self, mode: CoordinateMode);
}


pub struct LinearGradient(*mut ffi::qtc_qlineargradient);

impl LinearGradient {
    pub fn new(x1: f64, y1: f64, x2: f64, y2: f64) -> LinearGradient {
        unsafe { LinearGradient(ffi::qtc_qlineargradient_create(x1, y1, x2, y2)) }
    }
}

impl Gradient for LinearGradient {
    fn set_color_at(&mut self, offset: f64, r: u8, g: u8, b: u8, a: u8) {
        unsafe { ffi::qtc_qlineargradient_set_color_at(self.0, offset, r, g, b, a) }
    }

    fn set_spread(&mut self, spread: Spread) {
        unsafe { ffi::qtc_qlineargradient_set_spread(self.0, spread) }
    }

    fn set_units(&mut self, mode: CoordinateMode) {
        unsafe { ffi::qtc_qlineargradient_set_units(self.0, mode)  }
    }
}

impl Drop for LinearGradient {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qlineargradient_destroy(self.0) }
    }
}


pub struct RadialGradient(*mut ffi::qtc_qradialgradient);

impl RadialGradient {
    pub fn new(cx: f64, cy: f64, fx: f64, fy: f64, r: f64) -> RadialGradient {
        unsafe { RadialGradient(ffi::qtc_qradialgradient_create(cx, cy, fx, fy, r)) }
    }
}

impl Gradient for RadialGradient {
    fn set_color_at(&mut self, offset: f64, r: u8, g: u8, b: u8, a: u8) {
        unsafe { ffi::qtc_qradialgradient_set_color_at(self.0, offset, r, g, b, a) }
    }

    fn set_spread(&mut self, spread: Spread) {
        unsafe { ffi::qtc_qradialgradient_set_spread(self.0, spread) }
    }

    fn set_units(&mut self, mode: CoordinateMode) {
        unsafe { ffi::qtc_qradialgradient_set_units(self.0, mode) }
    }
}

impl Drop for RadialGradient {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qradialgradient_destroy(self.0) }
    }
}


pub struct Font(*mut ffi::qtc_qfont);

impl Font {
    pub fn new() -> Font {
        unsafe { Font(ffi::qtc_qfont_create()) }
    }

    pub fn set_family(&mut self, family: &str) {
        let c_family = CString::new(family).unwrap();
        unsafe { ffi::qtc_qfont_set_family(self.0, c_family.as_ptr()); }
    }

    pub fn set_style(&mut self, style: FontStyle) {
        unsafe { ffi::qtc_qfont_set_style(self.0, style); }
    }

    pub fn set_small_caps(&mut self, flag: bool) {
        unsafe { ffi::qtc_qfont_set_small_caps(self.0, flag); }
    }

    pub fn set_weight(&mut self, weight: FontWeight) {
        unsafe { ffi::qtc_qfont_set_weight(self.0, weight); }
    }

    pub fn set_stretch(&mut self, stretch: FontStretch) {
        unsafe { ffi::qtc_qfont_set_stretch(self.0, stretch); }
    }

    pub fn set_size(&mut self, size: f64) {
        unsafe { ffi::qtc_qfont_set_size(self.0, size); }
    }

    pub fn print_debug(&self) {
        unsafe { ffi::qtc_qfont_print_debug(self.0); }
    }
}

impl Drop for Font {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qfont_destroy(self.0) }
    }
}


pub struct FontMetricsF(*mut ffi::qtc_qfontmetricsf);

impl FontMetricsF {
    pub fn height(&self) -> f64 {
        unsafe { ffi::qtc_qfontmetricsf_height(self.0) }
    }

    pub fn width(&self, text: &str) -> f64 {
        let c_text = CString::new(text).unwrap();
        unsafe { ffi::qtc_qfontmetricsf_width(self.0, c_text.as_ptr()) }
    }

    pub fn ascent(&self) -> f64 {
        unsafe { ffi::qtc_qfontmetricsf_get_ascent(self.0) }
    }

    pub fn underline_pos(&self) -> f64 {
        unsafe { ffi::qtc_qfontmetricsf_get_underline_pos(self.0) }
    }

    pub fn overline_pos(&self) -> f64 {
        unsafe { ffi::qtc_qfontmetricsf_get_overline_pos(self.0) }
    }

    pub fn strikeout_pos(&self) -> f64 {
        unsafe { ffi::qtc_qfontmetricsf_get_strikeout_pos(self.0) }
    }

    pub fn line_width(&self) -> f64 {
        unsafe { ffi::qtc_qfontmetricsf_get_line_width(self.0) }
    }

    pub fn full_width(&self, text: &str) -> f64 {
        let c_text = CString::new(text).unwrap();
        unsafe { ffi::qtc_qfontmetricsf_full_width(self.0, c_text.as_ptr()) }
    }

    pub fn bounding_box(&self, text: &str) -> (f64, f64, f64, f64) {
        let c_text = CString::new(text).unwrap();
        let rect = unsafe { ffi::qtc_qfontmetricsf_get_bbox(self.0, c_text.as_ptr()) };

        (rect.x, rect.y, rect.w, rect.h)
    }
}

impl Drop for FontMetricsF {
    fn drop(&mut self) {
        unsafe { ffi::qtc_qfontmetricsf_destroy(self.0) }
    }
}