1#[cfg(feature = "use_glib")]
4use std::marker::PhantomData;
5use std::{ffi::CString, fmt, mem::MaybeUninit, ops, ptr, slice};
6
7#[cfg(feature = "use_glib")]
8use glib::translate::*;
9
10use crate::{
11 ffi, utils::status_to_result, Antialias, Content, Error, FillRule, FontExtents, FontFace,
12 FontOptions, FontSlant, FontWeight, Glyph, LineCap, LineJoin, Matrix, Operator, Path, Pattern,
13 Rectangle, ScaledFont, Surface, TextCluster, TextClusterFlags, TextExtents,
14};
15
16pub struct RectangleList {
17 ptr: *mut ffi::cairo_rectangle_list_t,
18}
19
20impl ops::Deref for RectangleList {
21 type Target = [Rectangle];
22
23 #[inline]
24 fn deref(&self) -> &[Rectangle] {
25 unsafe {
26 let ptr = (*self.ptr).rectangles as *mut Rectangle;
27 let len = (*self.ptr).num_rectangles;
28
29 if ptr.is_null() || len == 0 {
30 &[]
31 } else {
32 slice::from_raw_parts(ptr, len as usize)
33 }
34 }
35 }
36}
37
38impl Drop for RectangleList {
39 #[inline]
40 fn drop(&mut self) {
41 unsafe {
42 ffi::cairo_rectangle_list_destroy(self.ptr);
43 }
44 }
45}
46
47impl fmt::Debug for RectangleList {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 use std::ops::Deref;
50 f.debug_tuple("RectangleList").field(&self.deref()).finish()
51 }
52}
53
54#[derive(Debug)]
55#[repr(transparent)]
56#[doc(alias = "cairo_t")]
57pub struct Context(ptr::NonNull<ffi::cairo_t>);
58
59#[cfg(feature = "use_glib")]
60#[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
61impl IntoGlibPtr<*mut ffi::cairo_t> for Context {
62 #[inline]
63 unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_t {
64 (&*std::mem::ManuallyDrop::new(self)).to_glib_none().0
65 }
66}
67
68#[cfg(feature = "use_glib")]
69#[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
70impl<'a> ToGlibPtr<'a, *mut ffi::cairo_t> for &'a Context {
71 type Storage = PhantomData<&'a Context>;
72
73 #[inline]
74 fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_t, &'a Context> {
75 Stash(self.0.as_ptr(), PhantomData)
76 }
77
78 #[inline]
79 fn to_glib_full(&self) -> *mut ffi::cairo_t {
80 unsafe { ffi::cairo_reference(self.0.as_ptr()) }
81 }
82}
83
84#[cfg(feature = "use_glib")]
85#[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
86impl FromGlibPtrNone<*mut ffi::cairo_t> for Context {
87 #[inline]
88 unsafe fn from_glib_none(ptr: *mut ffi::cairo_t) -> Context {
89 Self::from_raw_none(ptr)
90 }
91}
92
93#[cfg(feature = "use_glib")]
94#[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
95impl FromGlibPtrBorrow<*mut ffi::cairo_t> for Context {
96 #[inline]
97 unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_t) -> crate::Borrowed<Context> {
98 Self::from_raw_borrow(ptr)
99 }
100}
101
102#[cfg(feature = "use_glib")]
103#[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
104impl FromGlibPtrFull<*mut ffi::cairo_t> for Context {
105 #[inline]
106 unsafe fn from_glib_full(ptr: *mut ffi::cairo_t) -> Context {
107 Self::from_raw_full(ptr)
108 }
109}
110
111#[cfg(feature = "use_glib")]
112gvalue_impl!(
113 Context,
114 ffi::cairo_t,
115 ffi::gobject::cairo_gobject_context_get_type
116);
117
118impl Clone for Context {
119 #[inline]
120 fn clone(&self) -> Context {
121 unsafe { Self::from_raw_none(self.to_raw_none()) }
122 }
123}
124
125impl Drop for Context {
126 #[inline]
127 fn drop(&mut self) {
128 unsafe {
129 ffi::cairo_destroy(self.0.as_ptr());
130 }
131 }
132}
133
134impl Context {
135 #[inline]
136 pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_t) -> Context {
137 debug_assert!(!ptr.is_null());
138 ffi::cairo_reference(ptr);
139 Context(ptr::NonNull::new_unchecked(ptr))
140 }
141
142 #[inline]
143 pub unsafe fn from_raw_borrow(ptr: *mut ffi::cairo_t) -> crate::Borrowed<Context> {
144 debug_assert!(!ptr.is_null());
145 crate::Borrowed::new(Context(ptr::NonNull::new_unchecked(ptr)))
146 }
147
148 #[inline]
149 pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_t) -> Context {
150 debug_assert!(!ptr.is_null());
151 Context(ptr::NonNull::new_unchecked(ptr))
152 }
153
154 #[inline]
155 pub fn to_raw_none(&self) -> *mut ffi::cairo_t {
156 self.0.as_ptr()
157 }
158
159 #[doc(alias = "cairo_status")]
160 #[inline]
161 pub fn status(&self) -> Result<(), Error> {
162 let status = unsafe { ffi::cairo_status(self.0.as_ptr()) };
163 status_to_result(status)
164 }
165
166 pub fn new(target: impl AsRef<Surface>) -> Result<Context, Error> {
167 let ctx = unsafe { Self::from_raw_full(ffi::cairo_create(target.as_ref().to_raw_none())) };
168 ctx.status().map(|_| ctx)
169 }
170
171 #[doc(alias = "cairo_save")]
172 pub fn save(&self) -> Result<(), Error> {
173 unsafe { ffi::cairo_save(self.0.as_ptr()) }
174 self.status()
175 }
176
177 #[doc(alias = "cairo_restore")]
178 pub fn restore(&self) -> Result<(), Error> {
179 unsafe { ffi::cairo_restore(self.0.as_ptr()) }
180 self.status()
181 }
182
183 #[doc(alias = "get_target")]
184 #[doc(alias = "cairo_get_target")]
185 pub fn target(&self) -> Surface {
186 unsafe { Surface::from_raw_none(ffi::cairo_get_target(self.0.as_ptr())) }
187 }
188
189 #[doc(alias = "cairo_push_group")]
190 pub fn push_group(&self) {
191 unsafe { ffi::cairo_push_group(self.0.as_ptr()) }
192 }
193
194 #[doc(alias = "cairo_push_group_with_content")]
195 pub fn push_group_with_content(&self, content: Content) {
196 unsafe { ffi::cairo_push_group_with_content(self.0.as_ptr(), content.into()) }
197 }
198
199 #[doc(alias = "cairo_pop_group")]
200 pub fn pop_group(&self) -> Result<Pattern, Error> {
201 let pattern = unsafe { Pattern::from_raw_full(ffi::cairo_pop_group(self.0.as_ptr())) };
202 self.status().map(|_| pattern)
203 }
204
205 #[doc(alias = "cairo_pop_group_to_source")]
206 pub fn pop_group_to_source(&self) -> Result<(), Error> {
207 unsafe { ffi::cairo_pop_group_to_source(self.0.as_ptr()) };
208 self.status()
209 }
210
211 #[doc(alias = "get_group_target")]
212 #[doc(alias = "cairo_get_group_target")]
213 pub fn group_target(&self) -> Surface {
214 unsafe { Surface::from_raw_none(ffi::cairo_get_group_target(self.0.as_ptr())) }
215 }
216
217 #[doc(alias = "cairo_set_source_rgb")]
218 pub fn set_source_rgb(&self, red: f64, green: f64, blue: f64) {
219 unsafe { ffi::cairo_set_source_rgb(self.0.as_ptr(), red, green, blue) }
220 }
221
222 #[doc(alias = "cairo_set_source_rgba")]
223 pub fn set_source_rgba(&self, red: f64, green: f64, blue: f64, alpha: f64) {
224 unsafe { ffi::cairo_set_source_rgba(self.0.as_ptr(), red, green, blue, alpha) }
225 }
226
227 #[doc(alias = "cairo_set_source")]
228 pub fn set_source(&self, source: impl AsRef<Pattern>) -> Result<(), Error> {
229 let source = source.as_ref();
230 source.status()?;
231 unsafe {
232 ffi::cairo_set_source(self.0.as_ptr(), source.to_raw_none());
233 }
234 self.status()
235 }
236
237 #[doc(alias = "get_source")]
238 #[doc(alias = "cairo_get_source")]
239 pub fn source(&self) -> Pattern {
240 unsafe { Pattern::from_raw_none(ffi::cairo_get_source(self.0.as_ptr())) }
241 }
242
243 #[doc(alias = "cairo_set_source_surface")]
244 pub fn set_source_surface(
245 &self,
246 surface: impl AsRef<Surface>,
247 x: f64,
248 y: f64,
249 ) -> Result<(), Error> {
250 let surface = surface.as_ref();
251 surface.status()?;
252 unsafe {
253 ffi::cairo_set_source_surface(self.0.as_ptr(), surface.to_raw_none(), x, y);
254 }
255 self.status()
256 }
257
258 #[doc(alias = "cairo_set_antialias")]
259 pub fn set_antialias(&self, antialias: Antialias) {
260 unsafe { ffi::cairo_set_antialias(self.0.as_ptr(), antialias.into()) }
261 }
262
263 #[doc(alias = "get_antialias")]
264 #[doc(alias = "cairo_get_antialias")]
265 pub fn antialias(&self) -> Antialias {
266 unsafe { Antialias::from(ffi::cairo_get_antialias(self.0.as_ptr())) }
267 }
268
269 #[doc(alias = "cairo_set_dash")]
270 pub fn set_dash(&self, dashes: &[f64], offset: f64) {
271 unsafe {
272 ffi::cairo_set_dash(
273 self.0.as_ptr(),
274 dashes.as_ptr(),
275 dashes.len() as i32,
276 offset,
277 )
278 }
279 }
280
281 #[doc(alias = "get_dash_count")]
282 #[doc(alias = "cairo_get_dash_count")]
283 pub fn dash_count(&self) -> i32 {
284 unsafe { ffi::cairo_get_dash_count(self.0.as_ptr()) }
285 }
286
287 #[doc(alias = "get_dash")]
288 #[doc(alias = "cairo_get_dash")]
289 pub fn dash(&self) -> (Vec<f64>, f64) {
290 let dash_count = self.dash_count() as usize;
291 let mut dashes: Vec<f64> = Vec::with_capacity(dash_count);
292 let mut offset: f64 = 0.0;
293
294 unsafe {
295 ffi::cairo_get_dash(self.0.as_ptr(), dashes.as_mut_ptr(), &mut offset);
296 dashes.set_len(dash_count);
297 (dashes, offset)
298 }
299 }
300
301 #[doc(alias = "get_dash_dashes")]
302 pub fn dash_dashes(&self) -> Vec<f64> {
303 let (dashes, _) = self.dash();
304 dashes
305 }
306
307 #[doc(alias = "get_dash_offset")]
308 pub fn dash_offset(&self) -> f64 {
309 let (_, offset) = self.dash();
310 offset
311 }
312
313 #[doc(alias = "cairo_set_fill_rule")]
314 pub fn set_fill_rule(&self, fill_rule: FillRule) {
315 unsafe {
316 ffi::cairo_set_fill_rule(self.0.as_ptr(), fill_rule.into());
317 }
318 }
319
320 #[doc(alias = "get_fill_rule")]
321 #[doc(alias = "cairo_get_fill_rule")]
322 pub fn fill_rule(&self) -> FillRule {
323 unsafe { FillRule::from(ffi::cairo_get_fill_rule(self.0.as_ptr())) }
324 }
325
326 #[doc(alias = "cairo_set_line_cap")]
327 pub fn set_line_cap(&self, arg: LineCap) {
328 unsafe { ffi::cairo_set_line_cap(self.0.as_ptr(), arg.into()) }
329 }
330
331 #[doc(alias = "get_line_cap")]
332 #[doc(alias = "cairo_get_line_cap")]
333 pub fn line_cap(&self) -> LineCap {
334 unsafe { LineCap::from(ffi::cairo_get_line_cap(self.0.as_ptr())) }
335 }
336
337 #[doc(alias = "cairo_set_line_join")]
338 pub fn set_line_join(&self, arg: LineJoin) {
339 unsafe { ffi::cairo_set_line_join(self.0.as_ptr(), arg.into()) }
340 }
341
342 #[doc(alias = "get_line_join")]
343 #[doc(alias = "cairo_get_line_join")]
344 pub fn line_join(&self) -> LineJoin {
345 unsafe { LineJoin::from(ffi::cairo_get_line_join(self.0.as_ptr())) }
346 }
347
348 #[doc(alias = "cairo_set_line_width")]
349 pub fn set_line_width(&self, arg: f64) {
350 unsafe { ffi::cairo_set_line_width(self.0.as_ptr(), arg) }
351 }
352
353 #[doc(alias = "get_line_width")]
354 #[doc(alias = "cairo_get_line_width")]
355 pub fn line_width(&self) -> f64 {
356 unsafe { ffi::cairo_get_line_width(self.0.as_ptr()) }
357 }
358
359 #[cfg(feature = "v1_18")]
360 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
361 #[doc(alias = "cairo_set_hairline")]
362 pub fn set_hairline(&self, set_hairline: bool) {
363 unsafe { ffi::cairo_set_hairline(self.0.as_ptr(), set_hairline.into()) }
364 }
365
366 #[cfg(feature = "v1_18")]
367 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
368 #[doc(alias = "get_hairline")]
369 #[doc(alias = "cairo_get_hairline")]
370 pub fn hairline(&self) -> bool {
371 unsafe { ffi::cairo_get_hairline(self.0.as_ptr()) }.as_bool()
372 }
373
374 #[doc(alias = "cairo_set_miter_limit")]
375 pub fn set_miter_limit(&self, arg: f64) {
376 unsafe { ffi::cairo_set_miter_limit(self.0.as_ptr(), arg) }
377 }
378
379 #[doc(alias = "get_miter_limit")]
380 #[doc(alias = "cairo_get_miter_limit")]
381 pub fn miter_limit(&self) -> f64 {
382 unsafe { ffi::cairo_get_miter_limit(self.0.as_ptr()) }
383 }
384
385 #[doc(alias = "cairo_set_operator")]
386 pub fn set_operator(&self, op: Operator) {
387 unsafe {
388 ffi::cairo_set_operator(self.0.as_ptr(), op.into());
389 }
390 }
391
392 #[doc(alias = "get_operator")]
393 #[doc(alias = "cairo_get_operator")]
394 pub fn operator(&self) -> Operator {
395 unsafe { Operator::from(ffi::cairo_get_operator(self.0.as_ptr())) }
396 }
397
398 #[doc(alias = "cairo_set_tolerance")]
399 pub fn set_tolerance(&self, arg: f64) {
400 unsafe { ffi::cairo_set_tolerance(self.0.as_ptr(), arg) }
401 }
402
403 #[doc(alias = "get_tolerance")]
404 #[doc(alias = "cairo_get_tolerance")]
405 pub fn tolerance(&self) -> f64 {
406 unsafe { ffi::cairo_get_tolerance(self.0.as_ptr()) }
407 }
408
409 #[doc(alias = "cairo_clip")]
410 pub fn clip(&self) {
411 unsafe { ffi::cairo_clip(self.0.as_ptr()) }
412 }
413
414 #[doc(alias = "cairo_clip_preserve")]
415 pub fn clip_preserve(&self) {
416 unsafe { ffi::cairo_clip_preserve(self.0.as_ptr()) }
417 }
418
419 #[doc(alias = "cairo_clip_extents")]
420 pub fn clip_extents(&self) -> Result<(f64, f64, f64, f64), Error> {
421 let mut x1: f64 = 0.0;
422 let mut y1: f64 = 0.0;
423 let mut x2: f64 = 0.0;
424 let mut y2: f64 = 0.0;
425
426 unsafe {
427 ffi::cairo_clip_extents(self.0.as_ptr(), &mut x1, &mut y1, &mut x2, &mut y2);
428 }
429 self.status().map(|_| (x1, y1, x2, y2))
430 }
431
432 #[doc(alias = "cairo_in_clip")]
433 pub fn in_clip(&self, x: f64, y: f64) -> Result<bool, Error> {
434 let in_clip = unsafe { ffi::cairo_in_clip(self.0.as_ptr(), x, y).as_bool() };
435 self.status().map(|_| in_clip)
436 }
437
438 #[doc(alias = "cairo_reset_clip")]
439 pub fn reset_clip(&self) {
440 unsafe { ffi::cairo_reset_clip(self.0.as_ptr()) }
441 }
442
443 #[doc(alias = "cairo_copy_clip_rectangle_list")]
444 pub fn copy_clip_rectangle_list(&self) -> Result<RectangleList, Error> {
445 unsafe {
446 let rectangle_list = ffi::cairo_copy_clip_rectangle_list(self.0.as_ptr());
447
448 status_to_result((*rectangle_list).status)?;
449
450 Ok(RectangleList {
451 ptr: rectangle_list,
452 })
453 }
454 }
455
456 #[doc(alias = "cairo_fill")]
457 pub fn fill(&self) -> Result<(), Error> {
458 unsafe { ffi::cairo_fill(self.0.as_ptr()) };
459 self.status()
460 }
461
462 #[doc(alias = "cairo_fill_preserve")]
463 pub fn fill_preserve(&self) -> Result<(), Error> {
464 unsafe { ffi::cairo_fill_preserve(self.0.as_ptr()) };
465 self.status()
466 }
467
468 #[doc(alias = "cairo_fill_extents")]
469 pub fn fill_extents(&self) -> Result<(f64, f64, f64, f64), Error> {
470 let mut x1: f64 = 0.0;
471 let mut y1: f64 = 0.0;
472 let mut x2: f64 = 0.0;
473 let mut y2: f64 = 0.0;
474
475 unsafe {
476 ffi::cairo_fill_extents(self.0.as_ptr(), &mut x1, &mut y1, &mut x2, &mut y2);
477 }
478 self.status().map(|_| (x1, y1, x2, y2))
479 }
480
481 #[doc(alias = "cairo_in_fill")]
482 pub fn in_fill(&self, x: f64, y: f64) -> Result<bool, Error> {
483 let in_fill = unsafe { ffi::cairo_in_fill(self.0.as_ptr(), x, y).as_bool() };
484 self.status().map(|_| in_fill)
485 }
486
487 #[doc(alias = "cairo_mask")]
488 pub fn mask(&self, pattern: impl AsRef<Pattern>) -> Result<(), Error> {
489 let pattern = pattern.as_ref();
490 pattern.status()?;
491 unsafe { ffi::cairo_mask(self.0.as_ptr(), pattern.to_raw_none()) };
492 self.status()
493 }
494
495 #[doc(alias = "cairo_mask_surface")]
496 pub fn mask_surface(&self, surface: impl AsRef<Surface>, x: f64, y: f64) -> Result<(), Error> {
497 let surface = surface.as_ref();
498 surface.status()?;
499 unsafe {
500 ffi::cairo_mask_surface(self.0.as_ptr(), surface.to_raw_none(), x, y);
501 };
502 self.status()
503 }
504
505 #[doc(alias = "cairo_paint")]
506 pub fn paint(&self) -> Result<(), Error> {
507 unsafe { ffi::cairo_paint(self.0.as_ptr()) };
508 self.status()
509 }
510
511 #[doc(alias = "cairo_paint_with_alpha")]
512 pub fn paint_with_alpha(&self, alpha: f64) -> Result<(), Error> {
513 unsafe { ffi::cairo_paint_with_alpha(self.0.as_ptr(), alpha) };
514 self.status()
515 }
516
517 #[doc(alias = "cairo_stroke")]
518 pub fn stroke(&self) -> Result<(), Error> {
519 unsafe { ffi::cairo_stroke(self.0.as_ptr()) };
520 self.status()
521 }
522
523 #[doc(alias = "cairo_stroke_preserve")]
524 pub fn stroke_preserve(&self) -> Result<(), Error> {
525 unsafe { ffi::cairo_stroke_preserve(self.0.as_ptr()) };
526 self.status()
527 }
528
529 #[doc(alias = "cairo_stroke_extents")]
530 pub fn stroke_extents(&self) -> Result<(f64, f64, f64, f64), Error> {
531 let mut x1: f64 = 0.0;
532 let mut y1: f64 = 0.0;
533 let mut x2: f64 = 0.0;
534 let mut y2: f64 = 0.0;
535
536 unsafe {
537 ffi::cairo_stroke_extents(self.0.as_ptr(), &mut x1, &mut y1, &mut x2, &mut y2);
538 }
539 self.status().map(|_| (x1, y1, x2, y2))
540 }
541
542 #[doc(alias = "cairo_in_stroke")]
543 pub fn in_stroke(&self, x: f64, y: f64) -> Result<bool, Error> {
544 let in_stroke = unsafe { ffi::cairo_in_stroke(self.0.as_ptr(), x, y).as_bool() };
545 self.status().map(|_| in_stroke)
546 }
547
548 #[doc(alias = "cairo_copy_page")]
549 pub fn copy_page(&self) -> Result<(), Error> {
550 unsafe { ffi::cairo_copy_page(self.0.as_ptr()) };
551 self.status()
552 }
553
554 #[doc(alias = "cairo_show_page")]
555 pub fn show_page(&self) -> Result<(), Error> {
556 unsafe { ffi::cairo_show_page(self.0.as_ptr()) };
557 self.status()
558 }
559
560 #[doc(alias = "get_reference_count")]
561 pub fn reference_count(&self) -> u32 {
562 unsafe { ffi::cairo_get_reference_count(self.0.as_ptr()) }
563 }
564
565 #[doc(alias = "cairo_translate")]
568 pub fn translate(&self, tx: f64, ty: f64) {
569 unsafe { ffi::cairo_translate(self.0.as_ptr(), tx, ty) }
570 }
571
572 #[doc(alias = "cairo_scale")]
573 pub fn scale(&self, sx: f64, sy: f64) {
574 unsafe { ffi::cairo_scale(self.0.as_ptr(), sx, sy) }
575 }
576
577 #[doc(alias = "cairo_rotate")]
578 pub fn rotate(&self, angle: f64) {
579 unsafe { ffi::cairo_rotate(self.0.as_ptr(), angle) }
580 }
581
582 #[doc(alias = "cairo_transform")]
583 pub fn transform(&self, matrix: Matrix) {
584 unsafe {
585 ffi::cairo_transform(self.0.as_ptr(), matrix.ptr());
586 }
587 }
588
589 #[doc(alias = "cairo_set_matrix")]
590 pub fn set_matrix(&self, matrix: Matrix) {
591 unsafe {
592 ffi::cairo_set_matrix(self.0.as_ptr(), matrix.ptr());
593 }
594 }
595
596 #[doc(alias = "get_matrix")]
597 #[doc(alias = "cairo_get_matrix")]
598 pub fn matrix(&self) -> Matrix {
599 let mut matrix = Matrix::null();
600 unsafe {
601 ffi::cairo_get_matrix(self.0.as_ptr(), matrix.mut_ptr());
602 }
603 matrix
604 }
605
606 #[doc(alias = "cairo_identity_matrix")]
607 pub fn identity_matrix(&self) {
608 unsafe { ffi::cairo_identity_matrix(self.0.as_ptr()) }
609 }
610
611 #[doc(alias = "cairo_user_to_device")]
612 pub fn user_to_device(&self, mut x: f64, mut y: f64) -> (f64, f64) {
613 unsafe {
614 ffi::cairo_user_to_device(self.0.as_ptr(), &mut x, &mut y);
615 (x, y)
616 }
617 }
618
619 #[doc(alias = "cairo_user_to_device_distance")]
620 pub fn user_to_device_distance(&self, mut dx: f64, mut dy: f64) -> Result<(f64, f64), Error> {
621 unsafe {
622 ffi::cairo_user_to_device_distance(self.0.as_ptr(), &mut dx, &mut dy);
623 };
624 self.status().map(|_| (dx, dy))
625 }
626
627 #[doc(alias = "cairo_device_to_user")]
628 pub fn device_to_user(&self, mut x: f64, mut y: f64) -> Result<(f64, f64), Error> {
629 unsafe {
630 ffi::cairo_device_to_user(self.0.as_ptr(), &mut x, &mut y);
631 }
632 self.status().map(|_| (x, y))
633 }
634
635 #[doc(alias = "cairo_device_to_user_distance")]
636 pub fn device_to_user_distance(&self, mut dx: f64, mut dy: f64) -> Result<(f64, f64), Error> {
637 unsafe {
638 ffi::cairo_device_to_user_distance(self.0.as_ptr(), &mut dx, &mut dy);
639 }
640 self.status().map(|_| (dx, dy))
641 }
642
643 #[doc(alias = "cairo_select_font_face")]
646 pub fn select_font_face(&self, family: &str, slant: FontSlant, weight: FontWeight) {
647 unsafe {
648 let family = CString::new(family).unwrap();
649 ffi::cairo_select_font_face(
650 self.0.as_ptr(),
651 family.as_ptr(),
652 slant.into(),
653 weight.into(),
654 )
655 }
656 }
657
658 #[doc(alias = "cairo_set_font_size")]
659 pub fn set_font_size(&self, size: f64) {
660 unsafe { ffi::cairo_set_font_size(self.0.as_ptr(), size) }
661 }
662
663 #[doc(alias = "cairo_set_font_matrix")]
665 pub fn set_font_matrix(&self, matrix: Matrix) {
666 unsafe { ffi::cairo_set_font_matrix(self.0.as_ptr(), matrix.ptr()) }
667 }
668
669 #[doc(alias = "get_font_matrix")]
670 #[doc(alias = "cairo_get_font_matrix")]
671 pub fn font_matrix(&self) -> Matrix {
672 let mut matrix = Matrix::null();
673 unsafe {
674 ffi::cairo_get_font_matrix(self.0.as_ptr(), matrix.mut_ptr());
675 }
676 matrix
677 }
678
679 #[doc(alias = "cairo_set_font_options")]
680 pub fn set_font_options(&self, options: &FontOptions) {
681 unsafe { ffi::cairo_set_font_options(self.0.as_ptr(), options.to_raw_none()) }
682 }
683
684 #[doc(alias = "get_font_options")]
685 #[doc(alias = "cairo_get_font_options")]
686 pub fn font_options(&self) -> Result<FontOptions, Error> {
687 let out = FontOptions::new()?;
688 unsafe {
689 ffi::cairo_get_font_options(self.0.as_ptr(), out.to_raw_none());
690 }
691 Ok(out)
692 }
693
694 #[doc(alias = "cairo_set_font_face")]
695 pub fn set_font_face(&self, font_face: &FontFace) {
696 unsafe { ffi::cairo_set_font_face(self.0.as_ptr(), font_face.to_raw_none()) }
697 }
698
699 #[doc(alias = "get_font_face")]
700 #[doc(alias = "cairo_get_font_face")]
701 pub fn font_face(&self) -> FontFace {
702 unsafe { FontFace::from_raw_none(ffi::cairo_get_font_face(self.0.as_ptr())) }
703 }
704
705 #[doc(alias = "cairo_set_scaled_font")]
706 pub fn set_scaled_font(&self, scaled_font: &ScaledFont) {
707 unsafe { ffi::cairo_set_scaled_font(self.0.as_ptr(), scaled_font.to_raw_none()) }
708 }
709
710 #[doc(alias = "get_scaled_font")]
711 #[doc(alias = "cairo_get_scaled_font")]
712 pub fn scaled_font(&self) -> ScaledFont {
713 unsafe { ScaledFont::from_raw_none(ffi::cairo_get_scaled_font(self.0.as_ptr())) }
714 }
715
716 #[doc(alias = "cairo_show_text")]
717 pub fn show_text(&self, text: &str) -> Result<(), Error> {
718 unsafe {
719 let text = CString::new(text).unwrap();
720 ffi::cairo_show_text(self.0.as_ptr(), text.as_ptr())
721 };
722 self.status()
723 }
724
725 #[doc(alias = "cairo_show_glyphs")]
726 pub fn show_glyphs(&self, glyphs: &[Glyph]) -> Result<(), Error> {
727 unsafe {
728 ffi::cairo_show_glyphs(
729 self.0.as_ptr(),
730 glyphs.as_ptr() as *const _,
731 glyphs.len() as _,
732 )
733 };
734 self.status()
735 }
736
737 #[doc(alias = "cairo_show_text_glyphs")]
738 pub fn show_text_glyphs(
739 &self,
740 text: &str,
741 glyphs: &[Glyph],
742 clusters: &[TextCluster],
743 cluster_flags: TextClusterFlags,
744 ) -> Result<(), Error> {
745 unsafe {
746 let text = CString::new(text).unwrap();
747 ffi::cairo_show_text_glyphs(
748 self.0.as_ptr(),
749 text.as_ptr(),
750 -1_i32, glyphs.as_ptr() as *const _,
752 glyphs.len() as _,
753 clusters.as_ptr() as *const _,
754 clusters.len() as _,
755 cluster_flags.into(),
756 )
757 };
758 self.status()
759 }
760
761 #[doc(alias = "cairo_font_extents")]
762 pub fn font_extents(&self) -> Result<FontExtents, Error> {
763 let mut extents = MaybeUninit::<FontExtents>::uninit();
764
765 unsafe {
766 ffi::cairo_font_extents(self.0.as_ptr(), extents.as_mut_ptr() as *mut _);
767 self.status().map(|_| extents.assume_init() as _)
768 }
769 }
770
771 #[doc(alias = "cairo_text_extents")]
772 pub fn text_extents(&self, text: &str) -> Result<TextExtents, Error> {
773 let mut extents = MaybeUninit::<TextExtents>::uninit();
774
775 unsafe {
776 let text = CString::new(text).unwrap();
777 ffi::cairo_text_extents(
778 self.0.as_ptr(),
779 text.as_ptr(),
780 extents.as_mut_ptr() as *mut _,
781 );
782 self.status().map(|_| extents.assume_init())
783 }
784 }
785
786 #[doc(alias = "cairo_glyph_extents")]
787 pub fn glyph_extents(&self, glyphs: &[Glyph]) -> Result<TextExtents, Error> {
788 let mut extents = MaybeUninit::<TextExtents>::uninit();
789
790 unsafe {
791 ffi::cairo_glyph_extents(
792 self.0.as_ptr(),
793 glyphs.as_ptr() as *const _,
794 glyphs.len() as _,
795 extents.as_mut_ptr() as *mut _,
796 );
797 self.status().map(|_| extents.assume_init())
798 }
799 }
800
801 #[doc(alias = "cairo_copy_path")]
804 pub fn copy_path(&self) -> Result<Path, Error> {
805 let path = unsafe { Path::from_raw_full(ffi::cairo_copy_path(self.0.as_ptr())) };
806 self.status().map(|_| path)
807 }
808
809 #[doc(alias = "cairo_copy_path_flat")]
810 pub fn copy_path_flat(&self) -> Result<Path, Error> {
811 let path = unsafe { Path::from_raw_full(ffi::cairo_copy_path_flat(self.0.as_ptr())) };
812 self.status().map(|_| path)
813 }
814
815 #[doc(alias = "cairo_append_path")]
816 pub fn append_path(&self, path: &Path) {
817 unsafe { ffi::cairo_append_path(self.0.as_ptr(), path.as_ptr()) }
818 }
819
820 #[doc(alias = "cairo_has_current_point")]
821 pub fn has_current_point(&self) -> Result<bool, Error> {
822 let has_current_point = unsafe { ffi::cairo_has_current_point(self.0.as_ptr()).as_bool() };
823 self.status().map(|_| has_current_point)
824 }
825
826 #[doc(alias = "get_current_point")]
827 #[doc(alias = "cairo_get_current_point")]
828 pub fn current_point(&self) -> Result<(f64, f64), Error> {
829 unsafe {
830 let mut x = 0.0;
831 let mut y = 0.0;
832 ffi::cairo_get_current_point(self.0.as_ptr(), &mut x, &mut y);
833 self.status().map(|_| (x, y))
834 }
835 }
836
837 #[doc(alias = "cairo_new_path")]
838 pub fn new_path(&self) {
839 unsafe { ffi::cairo_new_path(self.0.as_ptr()) }
840 }
841
842 #[doc(alias = "cairo_new_sub_path")]
843 pub fn new_sub_path(&self) {
844 unsafe { ffi::cairo_new_sub_path(self.0.as_ptr()) }
845 }
846
847 #[doc(alias = "cairo_close_path")]
848 pub fn close_path(&self) {
849 unsafe { ffi::cairo_close_path(self.0.as_ptr()) }
850 }
851
852 #[doc(alias = "cairo_arc")]
853 pub fn arc(&self, xc: f64, yc: f64, radius: f64, angle1: f64, angle2: f64) {
854 unsafe { ffi::cairo_arc(self.0.as_ptr(), xc, yc, radius, angle1, angle2) }
855 }
856
857 #[doc(alias = "cairo_arc_negative")]
858 pub fn arc_negative(&self, xc: f64, yc: f64, radius: f64, angle1: f64, angle2: f64) {
859 unsafe { ffi::cairo_arc_negative(self.0.as_ptr(), xc, yc, radius, angle1, angle2) }
860 }
861
862 #[doc(alias = "cairo_curve_to")]
863 pub fn curve_to(&self, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) {
864 unsafe { ffi::cairo_curve_to(self.0.as_ptr(), x1, y1, x2, y2, x3, y3) }
865 }
866
867 #[doc(alias = "cairo_line_to")]
868 pub fn line_to(&self, x: f64, y: f64) {
869 unsafe { ffi::cairo_line_to(self.0.as_ptr(), x, y) }
870 }
871
872 #[doc(alias = "cairo_move_to")]
873 pub fn move_to(&self, x: f64, y: f64) {
874 unsafe { ffi::cairo_move_to(self.0.as_ptr(), x, y) }
875 }
876
877 #[doc(alias = "cairo_rectangle")]
878 pub fn rectangle(&self, x: f64, y: f64, width: f64, height: f64) {
879 unsafe { ffi::cairo_rectangle(self.0.as_ptr(), x, y, width, height) }
880 }
881
882 #[doc(alias = "cairo_text_path")]
883 pub fn text_path(&self, str_: &str) {
884 unsafe {
885 let str_ = CString::new(str_).unwrap();
886 ffi::cairo_text_path(self.0.as_ptr(), str_.as_ptr())
887 }
888 }
889
890 #[doc(alias = "cairo_glyph_path")]
891 pub fn glyph_path(&self, glyphs: &[Glyph]) {
892 unsafe {
893 ffi::cairo_glyph_path(
894 self.0.as_ptr(),
895 glyphs.as_ptr() as *const _,
896 glyphs.len() as _,
897 )
898 }
899 }
900
901 #[doc(alias = "cairo_rel_curve_to")]
902 pub fn rel_curve_to(&self, dx1: f64, dy1: f64, dx2: f64, dy2: f64, dx3: f64, dy3: f64) {
903 unsafe { ffi::cairo_rel_curve_to(self.0.as_ptr(), dx1, dy1, dx2, dy2, dx3, dy3) }
904 }
905
906 #[doc(alias = "cairo_rel_line_to")]
907 pub fn rel_line_to(&self, dx: f64, dy: f64) {
908 unsafe { ffi::cairo_rel_line_to(self.0.as_ptr(), dx, dy) }
909 }
910
911 #[doc(alias = "cairo_rel_move_to")]
912 pub fn rel_move_to(&self, dx: f64, dy: f64) {
913 unsafe { ffi::cairo_rel_move_to(self.0.as_ptr(), dx, dy) }
914 }
915
916 #[doc(alias = "cairo_path_extents")]
917 pub fn path_extents(&self) -> Result<(f64, f64, f64, f64), Error> {
918 let mut x1: f64 = 0.0;
919 let mut y1: f64 = 0.0;
920 let mut x2: f64 = 0.0;
921 let mut y2: f64 = 0.0;
922
923 unsafe {
924 ffi::cairo_path_extents(self.0.as_ptr(), &mut x1, &mut y1, &mut x2, &mut y2);
925 }
926 self.status().map(|_| (x1, y1, x2, y2))
927 }
928
929 #[cfg(feature = "v1_16")]
930 #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
931 #[doc(alias = "cairo_tag_begin")]
932 pub fn tag_begin(&self, tag_name: &str, attributes: &str) {
933 unsafe {
934 let tag_name = CString::new(tag_name).unwrap();
935 let attributes = CString::new(attributes).unwrap();
936 ffi::cairo_tag_begin(self.0.as_ptr(), tag_name.as_ptr(), attributes.as_ptr())
937 }
938 }
939
940 #[cfg(feature = "v1_16")]
941 #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
942 #[doc(alias = "cairo_tag_end")]
943 pub fn tag_end(&self, tag_name: &str) {
944 unsafe {
945 let tag_name = CString::new(tag_name).unwrap();
946 ffi::cairo_tag_end(self.0.as_ptr(), tag_name.as_ptr())
947 }
948 }
949}
950
951#[cfg(test)]
952mod tests {
953 use float_eq::float_eq;
954
955 use super::*;
956 use crate::{enums::Format, image_surface::ImageSurface, patterns::LinearGradient};
957
958 fn create_ctx() -> Context {
959 let surface = ImageSurface::create(Format::ARgb32, 10, 10).unwrap();
960 Context::new(&surface).expect("Can't create a Cairo context")
961 }
962
963 #[test]
964 fn invalid_surface_cant_create_context() {
965 unsafe {
966 let image_surf =
968 ffi::cairo_image_surface_create(Format::ARgb32.into(), 100_000, 100_000);
969
970 let wrapped = Surface::from_raw_none(image_surf);
973
974 assert!(Context::new(&wrapped).is_err());
975
976 ffi::cairo_surface_destroy(image_surf);
977 }
978 }
979
980 #[test]
981 fn drop_non_reference_pattern_from_ctx() {
982 let ctx = create_ctx();
983 ctx.source();
984 }
985
986 #[test]
987 fn drop_non_reference_pattern() {
988 let ctx = create_ctx();
989 let pattern = LinearGradient::new(1.0f64, 2.0f64, 3.0f64, 4.0f64);
990 ctx.set_source(&pattern).expect("Invalid surface state");
991 }
992
993 #[test]
994 fn clip_rectangle() {
995 let ctx = create_ctx();
996 let rect = ctx
997 .copy_clip_rectangle_list()
998 .expect("Failed to copy rectangle list");
999 let first_rect = rect[0];
1000 assert!(float_eq!(first_rect.x(), 0.0, abs <= 0.000_1));
1001 assert!(float_eq!(first_rect.y(), 0.0, abs <= 0.000_1));
1002 assert!(float_eq!(first_rect.width(), 10.0, abs <= 0.000_1));
1003 assert!(float_eq!(first_rect.height(), 10.0, abs <= 0.000_1));
1004 }
1005}