cairo/font/
text_extents.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use std::fmt;
5
6#[derive(Clone, Copy)]
7#[repr(transparent)]
8#[doc(alias = "cairo_text_extents_t")]
9pub struct TextExtents(ffi::cairo_text_extents_t);
10
11impl TextExtents {
12    pub fn new(
13        x_bearing: f64,
14        y_bearing: f64,
15        width: f64,
16        height: f64,
17        x_advance: f64,
18        y_advance: f64,
19    ) -> Self {
20        Self(ffi::cairo_text_extents_t {
21            x_bearing,
22            y_bearing,
23            width,
24            height,
25            x_advance,
26            y_advance,
27        })
28    }
29
30    pub fn x_bearing(&self) -> f64 {
31        self.0.x_bearing
32    }
33
34    pub fn y_bearing(&self) -> f64 {
35        self.0.y_bearing
36    }
37
38    pub fn width(&self) -> f64 {
39        self.0.width
40    }
41
42    pub fn height(&self) -> f64 {
43        self.0.height
44    }
45
46    pub fn x_advance(&self) -> f64 {
47        self.0.x_advance
48    }
49
50    pub fn y_advance(&self) -> f64 {
51        self.0.y_advance
52    }
53
54    pub fn set_x_bearing(&mut self, x_bearing: f64) {
55        self.0.x_bearing = x_bearing;
56    }
57
58    pub fn set_y_bearing(&mut self, y_bearing: f64) {
59        self.0.y_bearing = y_bearing;
60    }
61
62    pub fn set_width(&mut self, width: f64) {
63        self.0.width = width;
64    }
65
66    pub fn set_height(&mut self, height: f64) {
67        self.0.height = height;
68    }
69
70    pub fn set_x_advance(&mut self, x_advance: f64) {
71        self.0.x_advance = x_advance;
72    }
73
74    pub fn set_y_advance(&mut self, y_advance: f64) {
75        self.0.y_advance = y_advance;
76    }
77}
78
79impl fmt::Debug for TextExtents {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        f.debug_struct("TextExtents")
82            .field("x_bearing", &self.x_bearing())
83            .field("y_bearing", &self.y_bearing())
84            .field("width", &self.width())
85            .field("height", &self.height())
86            .field("x_advance", &self.x_advance())
87            .field("y_advance", &self.y_advance())
88            .finish()
89    }
90}
91
92#[doc(hidden)]
93impl From<TextExtents> for ffi::cairo_text_extents_t {
94    fn from(val: TextExtents) -> ffi::cairo_text_extents_t {
95        val.0
96    }
97}
98
99#[doc(hidden)]
100impl From<ffi::cairo_text_extents_t> for TextExtents {
101    fn from(value: ffi::cairo_text_extents_t) -> Self {
102        Self(value)
103    }
104}