1use crate::color::{Color, ColorFormat, ColorSpace};
8use crate::error::{Result, check};
9use crate::ffi;
10
11fn expect_handle<T>(handle: T, is_null: bool, what: &str) -> T {
16 assert!(!is_null, "ink: failed to allocate {what}");
17 handle
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27#[non_exhaustive]
28pub enum StockBrush {
29 Marker,
31 PressurePen,
33 Highlighter,
35 DashedLine,
38}
39
40impl StockBrush {
41 pub fn family(self) -> BrushFamily {
43 self.family_version(0)
44 }
45
46 pub fn family_version(self, version: u32) -> BrushFamily {
48 let version = i32::try_from(version).unwrap_or(0);
49 let handle = unsafe {
50 match self {
51 Self::Marker => ffi::StockBrushMarker(version),
52 Self::PressurePen => ffi::StockBrushPressurePen(version),
53 Self::Highlighter => ffi::StockBrushHighlighter(version),
54 Self::DashedLine => ffi::StockBrushDashedLine(version),
55 }
56 };
57 BrushFamily {
58 handle: expect_handle(handle, handle.handle.is_null(), "brush family"),
59 }
60 }
61}
62
63pub struct BrushFamily {
65 handle: ffi::BrushFamilyHandle,
66}
67
68impl BrushFamily {
69 pub(crate) fn as_ffi(&self) -> ffi::BrushFamilyHandle {
70 self.handle
71 }
72
73 pub(crate) fn from_ffi(handle: ffi::BrushFamilyHandle) -> Self {
74 Self {
75 handle: expect_handle(handle, handle.handle.is_null(), "brush family"),
76 }
77 }
78}
79
80impl Clone for BrushFamily {
81 fn clone(&self) -> Self {
82 Self::from_ffi(unsafe { ffi::BrushFamilyClone(self.handle) })
83 }
84}
85
86impl Drop for BrushFamily {
87 fn drop(&mut self) {
88 unsafe { ffi::BrushFamilyDestroy(self.handle) };
89 }
90}
91
92impl core::fmt::Debug for BrushFamily {
93 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
94 f.debug_struct("BrushFamily").finish_non_exhaustive()
95 }
96}
97
98pub struct Brush {
105 handle: ffi::BrushHandle,
106}
107
108impl Brush {
109 pub fn new(family: &BrushFamily, color: Color, size: f32, epsilon: f32) -> Result<Self> {
115 let mut out = ffi::BrushHandle {
116 handle: core::ptr::null_mut(),
117 };
118 let status = unsafe {
119 ffi::BrushCreate(
120 family.as_ffi(),
121 color.to_ffi(),
122 color.space.to_ffi(),
123 color.format.to_ffi(),
124 size,
125 epsilon,
126 &mut out,
127 )
128 };
129 check(status)?;
130 Ok(Self {
131 handle: expect_handle(out, out.handle.is_null(), "brush"),
132 })
133 }
134
135 pub(crate) fn as_ffi(&self) -> ffi::BrushHandle {
136 self.handle
137 }
138
139 pub(crate) fn from_ffi(handle: ffi::BrushHandle) -> Self {
140 Self {
141 handle: expect_handle(handle, handle.handle.is_null(), "brush"),
142 }
143 }
144
145 pub fn size(&self) -> f32 {
147 unsafe { ffi::BrushSize(self.handle) }
148 }
149
150 pub fn epsilon(&self) -> f32 {
152 unsafe { ffi::BrushEpsilon(self.handle) }
153 }
154
155 pub fn color(&self) -> Color {
157 self.color_in(ColorFormat::GammaEncoded)
158 }
159
160 pub fn color_in(&self, format: ColorFormat) -> Color {
162 let rgba = unsafe { ffi::BrushColor(self.handle, format.to_ffi()) };
163 let space = ColorSpace::from_ffi(unsafe { ffi::BrushColorSpace(self.handle) });
164 Color::from_ffi(rgba, space, format)
165 }
166
167 pub fn coat_count(&self) -> u32 {
170 unsafe { ffi::BrushCoatCount(self.handle) }
171 }
172
173 pub fn family(&self) -> BrushFamily {
175 BrushFamily::from_ffi(unsafe { ffi::BrushGetFamily(self.handle) })
176 }
177
178 pub fn set_size(&mut self, size: f32) -> Result<()> {
180 check(unsafe { ffi::BrushSetSize(self.handle, size) })
181 }
182
183 pub fn set_epsilon(&mut self, epsilon: f32) -> Result<()> {
185 check(unsafe { ffi::BrushSetEpsilon(self.handle, epsilon) })
186 }
187
188 pub fn set_color(&mut self, color: Color) {
190 unsafe {
191 ffi::BrushSetColor(
192 self.handle,
193 color.to_ffi(),
194 color.space.to_ffi(),
195 color.format.to_ffi(),
196 );
197 }
198 }
199
200 pub fn set_family(&mut self, family: &BrushFamily) {
202 unsafe { ffi::BrushSetFamily(self.handle, family.as_ffi()) };
203 }
204}
205
206impl Clone for Brush {
207 fn clone(&self) -> Self {
208 Self::from_ffi(unsafe { ffi::BrushClone(self.handle) })
209 }
210}
211
212impl Drop for Brush {
213 fn drop(&mut self) {
214 unsafe { ffi::BrushDestroy(self.handle) };
215 }
216}
217
218impl core::fmt::Debug for Brush {
219 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
220 f.debug_struct("Brush")
221 .field("size", &self.size())
222 .field("epsilon", &self.epsilon())
223 .field("color", &self.color())
224 .field("coat_count", &self.coat_count())
225 .finish()
226 }
227}