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
use nappgui_sys::{dctx_bitmap, dctx_image, draw_antialias, draw_clear, draw_matrixf};
use crate::types::{PixFormat, Trans2D};
use super::{Color, Image};
/// Drawing context.
pub struct DCtx {
pub(crate) inner: *mut nappgui_sys::DCtx,
}
impl DCtx {
pub(crate) fn new(ptr: *mut nappgui_sys::DCtx) -> Self {
if ptr.is_null() {
panic!("DCtx is NULL");
}
Self { inner: ptr }
}
/// Create a memory context, in order to generate an image.
///
/// # Remark
/// When we finish drawing, we must call dctx_image to get the picture.
pub fn bitmap(width: u32, height: u32, format: PixFormat) -> Self {
let ptr = unsafe { dctx_bitmap(width, height, format as _) };
DCtx::new(ptr)
}
/// Get the result image after drawing in the context created with dctx_bitmap.
pub fn image(mut self) -> Image {
let ptr = unsafe { dctx_image(&mut self.inner) };
Image::from_raw(ptr)
}
/// Clears the entire context area, using a solid color.
pub fn clear(&self, color: Color) {
unsafe { draw_clear(self.inner, color.inner) }
}
/// Set the context reference system (affine transformation).
///
/// # Remark
/// The origin of coordinates is in the upper left corner. The Y axis increases down.
pub fn matrix(&self, t2d: &[Trans2D]) {
unsafe {
draw_matrixf(self.inner, t2d.as_ptr() as _);
}
}
/// Set the reference system in Cartesian coordinates.
pub fn matrix_cartesian(&self, t2d: &[Trans2D]) {
unsafe {
draw_matrixf(self.inner, t2d.as_ptr() as _);
}
}
/// Enable or disable antialiasing.
///
/// # Remark
/// The antialias can change in each primitive. It is not necessary to establish a policy
/// for the whole drawing. See Antialiasing.
pub fn antialias(&self, enable: bool) {
unsafe {
draw_antialias(self.inner, enable as _);
}
}
}