gsk4/auto/path.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{FillRule, PathPoint, Stroke, ffi};
6use glib::translate::*;
7
8glib::wrapper! {
9 /// /picture
10 #[derive(Debug)]
11 pub struct Path(Shared<ffi::GskPath>);
12
13 match fn {
14 ref => |ptr| ffi::gsk_path_ref(ptr),
15 unref => |ptr| ffi::gsk_path_unref(ptr),
16 type_ => || ffi::gsk_path_get_type(),
17 }
18}
19
20impl Path {
21 #[cfg(feature = "v4_22")]
22 #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
23 #[doc(alias = "gsk_path_equal")]
24 fn equal(&self, path2: &Path) -> bool {
25 unsafe {
26 from_glib(ffi::gsk_path_equal(
27 self.to_glib_none().0,
28 path2.to_glib_none().0,
29 ))
30 }
31 }
32
33 /// Computes the bounds of the given path.
34 ///
35 /// The returned bounds may be larger than necessary, because this
36 /// function aims to be fast, not accurate. The bounds are guaranteed
37 /// to contain the path. For accurate bounds, use
38 /// [`tight_bounds()`][Self::tight_bounds()].
39 ///
40 /// It is possible that the returned rectangle has 0 width and/or height.
41 /// This can happen when the path only describes a point or an
42 /// axis-aligned line.
43 ///
44 /// If the path is empty, false is returned and @bounds are set to
45 /// graphene_rect_zero(). This is different from the case where the path
46 /// is a single point at the origin, where the @bounds will also be set to
47 /// the zero rectangle but true will be returned.
48 ///
49 /// # Returns
50 ///
51 /// true if the path has bounds, false if the path is known
52 /// to be empty and have no bounds
53 ///
54 /// ## `bounds`
55 /// return location for the bounds
56 #[doc(alias = "gsk_path_get_bounds")]
57 #[doc(alias = "get_bounds")]
58 pub fn bounds(&self) -> Option<graphene::Rect> {
59 unsafe {
60 let mut bounds = graphene::Rect::uninitialized();
61 let ret = from_glib(ffi::gsk_path_get_bounds(
62 self.to_glib_none().0,
63 bounds.to_glib_none_mut().0,
64 ));
65 if ret { Some(bounds) } else { None }
66 }
67 }
68
69 /// Computes the closest point on the path to the given point.
70 ///
71 /// If there is no point closer than the given threshold,
72 /// false is returned.
73 /// ## `point`
74 /// the point
75 /// ## `threshold`
76 /// maximum allowed distance
77 ///
78 /// # Returns
79 ///
80 /// true if @point was set to the closest point
81 /// on @self, false if no point is closer than @threshold
82 ///
83 /// ## `result`
84 /// return location for the closest point
85 ///
86 /// ## `distance`
87 /// return location for the distance
88 #[doc(alias = "gsk_path_get_closest_point")]
89 #[doc(alias = "get_closest_point")]
90 pub fn closest_point(
91 &self,
92 point: &graphene::Point,
93 threshold: f32,
94 ) -> Option<(PathPoint, f32)> {
95 unsafe {
96 let mut result = PathPoint::uninitialized();
97 let mut distance = std::mem::MaybeUninit::uninit();
98 let ret = from_glib(ffi::gsk_path_get_closest_point(
99 self.to_glib_none().0,
100 point.to_glib_none().0,
101 threshold,
102 result.to_glib_none_mut().0,
103 distance.as_mut_ptr(),
104 ));
105 if ret {
106 Some((result, distance.assume_init()))
107 } else {
108 None
109 }
110 }
111 }
112
113 /// Gets the end point of the path.
114 ///
115 /// An empty path has no points, so false
116 /// is returned in this case.
117 ///
118 /// # Returns
119 ///
120 /// true if @result was filled
121 ///
122 /// ## `result`
123 /// return location for point
124 #[doc(alias = "gsk_path_get_end_point")]
125 #[doc(alias = "get_end_point")]
126 pub fn end_point(&self) -> Option<PathPoint> {
127 unsafe {
128 let mut result = PathPoint::uninitialized();
129 let ret = from_glib(ffi::gsk_path_get_end_point(
130 self.to_glib_none().0,
131 result.to_glib_none_mut().0,
132 ));
133 if ret { Some(result) } else { None }
134 }
135 }
136
137 //#[cfg(feature = "v4_22")]
138 //#[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
139 //#[doc(alias = "gsk_path_get_next")]
140 //#[doc(alias = "get_next")]
141 //pub fn is_next(&self, point: /*Unimplemented*/PathPoint) -> bool {
142 // unsafe { TODO: call ffi:gsk_path_get_next() }
143 //}
144
145 //#[cfg(feature = "v4_22")]
146 //#[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
147 //#[doc(alias = "gsk_path_get_previous")]
148 //#[doc(alias = "get_previous")]
149 //pub fn is_previous(&self, point: /*Unimplemented*/PathPoint) -> bool {
150 // unsafe { TODO: call ffi:gsk_path_get_previous() }
151 //}
152
153 /// Gets the start point of the path.
154 ///
155 /// An empty path has no points, so false
156 /// is returned in this case.
157 ///
158 /// # Returns
159 ///
160 /// true if @result was filled
161 ///
162 /// ## `result`
163 /// return location for point
164 #[doc(alias = "gsk_path_get_start_point")]
165 #[doc(alias = "get_start_point")]
166 pub fn start_point(&self) -> Option<PathPoint> {
167 unsafe {
168 let mut result = PathPoint::uninitialized();
169 let ret = from_glib(ffi::gsk_path_get_start_point(
170 self.to_glib_none().0,
171 result.to_glib_none_mut().0,
172 ));
173 if ret { Some(result) } else { None }
174 }
175 }
176
177 /// Computes the bounds for stroking the given path with the
178 /// given parameters.
179 ///
180 /// The returned bounds may be larger than necessary, because this
181 /// function aims to be fast, not accurate. The bounds are guaranteed
182 /// to contain the area affected by the stroke, including protrusions
183 /// like miters.
184 /// ## `stroke`
185 /// stroke parameters
186 ///
187 /// # Returns
188 ///
189 /// true if the path has bounds, false if the path is known
190 /// to be empty and have no bounds.
191 ///
192 /// ## `bounds`
193 /// the bounds to fill in
194 #[doc(alias = "gsk_path_get_stroke_bounds")]
195 #[doc(alias = "get_stroke_bounds")]
196 pub fn stroke_bounds(&self, stroke: &Stroke) -> Option<graphene::Rect> {
197 unsafe {
198 let mut bounds = graphene::Rect::uninitialized();
199 let ret = from_glib(ffi::gsk_path_get_stroke_bounds(
200 self.to_glib_none().0,
201 stroke.to_glib_none().0,
202 bounds.to_glib_none_mut().0,
203 ));
204 if ret { Some(bounds) } else { None }
205 }
206 }
207
208 /// Computes the tight bounds of the given path.
209 ///
210 /// This function works harder than [`bounds()`][Self::bounds()] to
211 /// produce the smallest possible bounds.
212 ///
213 /// # Returns
214 ///
215 /// true if the path has bounds, false if the path is known
216 /// to be empty and have no bounds
217 ///
218 /// ## `bounds`
219 /// return location for the bounds
220 #[cfg(feature = "v4_22")]
221 #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
222 #[doc(alias = "gsk_path_get_tight_bounds")]
223 #[doc(alias = "get_tight_bounds")]
224 pub fn tight_bounds(&self) -> Option<graphene::Rect> {
225 unsafe {
226 let mut bounds = graphene::Rect::uninitialized();
227 let ret = from_glib(ffi::gsk_path_get_tight_bounds(
228 self.to_glib_none().0,
229 bounds.to_glib_none_mut().0,
230 ));
231 if ret { Some(bounds) } else { None }
232 }
233 }
234
235 /// Returns whether a point is inside the fill area of a path.
236 ///
237 /// Note that this function assumes that filling a contour
238 /// implicitly closes it.
239 /// ## `point`
240 /// the point to test
241 /// ## `fill_rule`
242 /// the fill rule to follow
243 ///
244 /// # Returns
245 ///
246 /// true if @point is inside
247 #[doc(alias = "gsk_path_in_fill")]
248 pub fn in_fill(&self, point: &graphene::Point, fill_rule: FillRule) -> bool {
249 unsafe {
250 from_glib(ffi::gsk_path_in_fill(
251 self.to_glib_none().0,
252 point.to_glib_none().0,
253 fill_rule.into_glib(),
254 ))
255 }
256 }
257
258 /// Returns if the path represents a single closed contour.
259 ///
260 /// # Returns
261 ///
262 /// true if the path is closed
263 #[doc(alias = "gsk_path_is_closed")]
264 pub fn is_closed(&self) -> bool {
265 unsafe { from_glib(ffi::gsk_path_is_closed(self.to_glib_none().0)) }
266 }
267
268 /// Checks if the path is empty, i.e. contains no lines or curves.
269 ///
270 /// # Returns
271 ///
272 /// true if the path is empty
273 #[doc(alias = "gsk_path_is_empty")]
274 pub fn is_empty(&self) -> bool {
275 unsafe { from_glib(ffi::gsk_path_is_empty(self.to_glib_none().0)) }
276 }
277
278 /// Appends the path to a cairo context for drawing with Cairo.
279 ///
280 /// This may cause some suboptimal conversions to be performed as
281 /// Cairo does not support all features of [`Path`][crate::Path].
282 ///
283 /// This function does not clear the existing Cairo path. Call
284 /// cairo_new_path() if you want this.
285 /// ## `cr`
286 /// a cairo context
287 #[doc(alias = "gsk_path_to_cairo")]
288 pub fn to_cairo(&self, cr: &cairo::Context) {
289 unsafe {
290 ffi::gsk_path_to_cairo(self.to_glib_none().0, mut_override(cr.to_glib_none().0));
291 }
292 }
293
294 /// Converts the path into a human-readable string.
295 ///
296 /// You can use this function in a debugger to get a quick overview
297 /// of the path.
298 ///
299 /// This is a wrapper around `Gsk::Path::print()`, see that function
300 /// for details.
301 ///
302 /// # Returns
303 ///
304 /// a new string for @self
305 #[doc(alias = "gsk_path_to_string")]
306 #[doc(alias = "to_string")]
307 pub fn to_str(&self) -> glib::GString {
308 unsafe { from_glib_full(ffi::gsk_path_to_string(self.to_glib_none().0)) }
309 }
310
311 /// zier from the current point to `(x2, y2)` with control point `(x1, y1)` and weight `w`.
312 ///
313 /// All the commands have lowercase variants that interpret coordinates
314 /// relative to the current point.
315 ///
316 /// The `O` command is an extension that is not supported in SVG.
317 /// ## `string`
318 /// a string
319 ///
320 /// # Returns
321 ///
322 /// a new [`Path`][crate::Path], or `NULL` if @string could not be parsed
323 #[doc(alias = "gsk_path_parse")]
324 pub fn parse(string: &str) -> Result<Path, glib::BoolError> {
325 assert_initialized_main_thread!();
326 unsafe {
327 Option::<_>::from_glib_full(ffi::gsk_path_parse(string.to_glib_none().0))
328 .ok_or_else(|| glib::bool_error!("Can't parse Path"))
329 }
330 }
331}
332
333#[cfg(feature = "v4_22")]
334#[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
335impl PartialEq for Path {
336 #[inline]
337 fn eq(&self, other: &Self) -> bool {
338 self.equal(other)
339 }
340}
341
342#[cfg(feature = "v4_22")]
343#[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
344impl Eq for Path {}
345
346impl std::fmt::Display for Path {
347 #[inline]
348 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
349 f.write_str(&self.to_str())
350 }
351}