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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#[cfg(feature = "glib")]
use glib::translate::*;
use std::ptr;
use ffi;
use std::ffi::CString;
use ffi::enums::{
FontType,
TextClusterFlags,
};
use ::matrices::{
Matrix,
MatrixTrait
};
use ffi::{
FontExtents,
Glyph,
TextCluster,
TextExtents
};
use super::{FontFace, FontOptions};
#[cfg(feature = "glib")]
glib_wrapper! {
pub struct ScaledFont(Shared<ffi::cairo_scaled_font_t>);
match fn {
ref => |ptr| ffi::cairo_scaled_font_reference(ptr),
unref => |ptr| ffi::cairo_scaled_font_destroy(ptr),
}
}
#[cfg(not(feature = "glib"))]
pub struct ScaledFont(*mut ffi::cairo_scaled_font_t);
impl ScaledFont {
pub fn new(font_face: FontFace, font_matrix: &Matrix, ctm: &Matrix, options: &FontOptions) -> ScaledFont {
let scaled_font: ScaledFont = unsafe {
ScaledFont::from_raw_full(ffi::cairo_scaled_font_create(font_face.to_raw_none(), font_matrix, ctm, options.to_raw_none()))
};
scaled_font.ensure_status();
scaled_font
}
#[cfg(feature = "glib")]
#[doc(hidden)]
pub fn to_raw_none(&self) -> *mut ffi::cairo_scaled_font_t {
self.to_glib_none().0
}
#[cfg(not(feature = "glib"))]
#[doc(hidden)]
pub fn to_raw_none(&self) -> *mut ffi::cairo_scaled_font_t {
self.0
}
#[cfg(not(feature = "glib"))]
#[doc(hidden)]
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_scaled_font_t) -> ScaledFont {
assert!(!ptr.is_null());
ScaledFont(ptr)
}
#[cfg(feature = "glib")]
#[doc(hidden)]
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_scaled_font_t) -> ScaledFont {
from_glib_full(ptr)
}
#[cfg(feature = "glib")]
#[doc(hidden)]
pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_scaled_font_t) -> ScaledFont {
from_glib_none(ptr)
}
#[cfg(not(feature = "glib"))]
#[doc(hidden)]
pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_scaled_font_t) -> ScaledFont {
assert!(!ptr.is_null());
ffi::cairo_scaled_font_reference(ptr);
ScaledFont(ptr)
}
pub fn ensure_status(&self) {
let status = unsafe {
ffi::cairo_scaled_font_status(self.to_raw_none())
};
status.ensure_valid()
}
pub fn get_type(&self) -> FontType {
unsafe {
ffi::cairo_scaled_font_get_type(self.to_raw_none())
}
}
pub fn get_reference_count(&self) -> usize {
unsafe {
ffi::cairo_scaled_font_get_reference_count(self.to_raw_none()) as usize
}
}
pub fn extents(&self) -> FontExtents {
let mut extents = FontExtents {
ascent: 0.0,
descent: 0.0,
height: 0.0,
max_x_advance: 0.0,
max_y_advance: 0.0,
};
unsafe {
ffi::cairo_scaled_font_extents(self.to_raw_none(), &mut extents)
}
extents
}
pub fn text_extents(&self, text: &str) -> TextExtents {
let mut extents = TextExtents {
x_bearing: 0.0,
y_bearing: 0.0,
width: 0.0,
height: 0.0,
x_advance: 0.0,
y_advance: 0.0,
};
let text = CString::new(text).unwrap();
unsafe {
ffi::cairo_scaled_font_text_extents(self.to_raw_none(), text.as_ptr(), &mut extents)
}
extents
}
pub fn glyph_extents(&self, glyphs: &[Glyph]) -> TextExtents {
let mut extents = TextExtents {
x_bearing: 0.0,
y_bearing: 0.0,
width: 0.0,
height: 0.0,
x_advance: 0.0,
y_advance: 0.0,
};
unsafe {
ffi::cairo_scaled_font_glyph_extents(self.to_raw_none(), glyphs.as_ptr(), glyphs.len() as i32, &mut extents)
}
extents
}
pub fn text_to_glyphs(&self, x: f64, y: f64, text: &str) -> (Vec<Glyph>, Vec<TextCluster>) {
unsafe {
let mut glyphs_ptr: *mut Glyph = ptr::null_mut();
let mut glyph_count = 0i32;
let mut clusters_ptr: *mut TextCluster = ptr::null_mut();
let mut cluster_count = 0i32;
let mut cluster_flags = TextClusterFlags::None;
let text_length = text.len() as i32;
let text = CString::new(text).unwrap();
let status = ffi::cairo_scaled_font_text_to_glyphs(
self.to_raw_none(),
x,
y,
text.as_ptr(),
text_length,
&mut glyphs_ptr,
&mut glyph_count,
&mut clusters_ptr,
&mut cluster_count,
&mut cluster_flags);
status.ensure_valid();
let glyph_count = glyph_count as usize;
let glyphs: Vec<Glyph> = {
let mut glyphs: Vec<Glyph> = Vec::with_capacity(glyph_count);
glyphs.set_len(glyph_count);
ptr::copy(glyphs_ptr, glyphs.as_mut_ptr(), glyph_count);
glyphs
};
let cluster_count = cluster_count as usize;
let clusters: Vec<TextCluster> = {
let mut clusters = Vec::with_capacity(cluster_count);
clusters.set_len(cluster_count);
ptr::copy(clusters_ptr, clusters.as_mut_ptr(), cluster_count);
clusters
};
ffi::cairo_glyph_free(glyphs_ptr);
ffi::cairo_text_cluster_free(clusters_ptr);
(glyphs, clusters)
}
}
pub fn get_font_face(&self) -> FontFace {
unsafe {
FontFace::from_raw_none(ffi::cairo_scaled_font_get_font_face(self.to_raw_none()))
}
}
pub fn get_font_options(&self) -> FontOptions {
let options = FontOptions::new();
unsafe {
ffi::cairo_scaled_font_get_font_options(self.to_raw_none(), options.to_raw_none())
}
options
}
pub fn get_font_matrix(&self) -> Matrix {
let mut matrix = Matrix::null();
unsafe {
ffi::cairo_scaled_font_get_font_matrix(self.to_raw_none(), &mut matrix)
}
matrix
}
pub fn get_ctm(&self) -> Matrix {
let mut matrix = Matrix::null();
unsafe {
ffi::cairo_scaled_font_get_ctm(self.to_raw_none(), &mut matrix)
}
matrix
}
pub fn get_scale_matrix(&self) -> Matrix {
let mut matrix = Matrix::null();
unsafe {
ffi::cairo_scaled_font_get_scale_matrix(self.to_raw_none(), &mut matrix)
}
matrix
}
}