Documentation
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use libc;
use std;

pub mod cluster_flags;
pub mod font_type;
pub mod slant;
pub mod weight;
pub mod subpixel_order;
pub mod hint_style;
pub mod hint_metrics;

pub struct Options {
  pub opaque: *mut libc::c_void
}

impl Options {
  pub fn create() -> Options {
    unsafe {
      let foreign_result = cairo_font_options_create();
      return Options { opaque: foreign_result as *mut libc::c_void };
    }
  }

  pub fn status(&mut self) -> super::Status {
    unsafe {
      let foreign_result = cairo_font_options_status(self.opaque);
      return foreign_result;
    }
  }

  pub fn merge(&mut self, other: &mut Options) {
    unsafe {
      cairo_font_options_merge(self.opaque, other.opaque);
    }
  }

  pub fn hash(&mut self) -> libc::c_ulong {
    unsafe {
      let foreign_result = cairo_font_options_hash(self.opaque);
      return foreign_result;
    }
  }

  pub fn equal(&mut self, other: &mut Options) -> bool {
    unsafe {
      let foreign_result = cairo_font_options_equal(self.opaque, other.opaque);
      return foreign_result;
    }
  }

  pub fn set_antialias(&mut self, antialias: super::antialias::Antialias) {
    unsafe {
      cairo_font_options_set_antialias(self.opaque, antialias);
    }
  }

  pub fn get_antialias(&mut self) -> super::antialias::Antialias {
    unsafe {
      let foreign_result = cairo_font_options_get_antialias(self.opaque);
      return foreign_result;
    }
  }

  pub fn set_subpixel_order(&mut self, subpixel_order: subpixel_order::SubpixelOrder) {
    unsafe {
      cairo_font_options_set_subpixel_order(self.opaque, subpixel_order);
    }
  }

  pub fn get_subpixel_order(&mut self) -> subpixel_order::SubpixelOrder {
    unsafe {
      let foreign_result = cairo_font_options_get_subpixel_order(self.opaque);
      return foreign_result;
    }
  }

  pub fn set_hint_style(&mut self, hint_style: hint_style::HintStyle) {
    unsafe {
      cairo_font_options_set_hint_style(self.opaque, hint_style);
    }
  }

  pub fn get_hint_style(&mut self) -> hint_style::HintStyle {
    unsafe {
      let foreign_result = cairo_font_options_get_hint_style(self.opaque);
      return foreign_result;
    }
  }

  pub fn set_hint_metrics(&mut self, hint_metrics: hint_metrics::HintMetrics) {
    unsafe {
      cairo_font_options_set_hint_metrics(self.opaque, hint_metrics);
    }
  }

  pub fn get_hint_metrics(&mut self) -> hint_metrics::HintMetrics {
    unsafe {
      let foreign_result = cairo_font_options_get_hint_metrics(self.opaque);
      return foreign_result;
    }
  }
}

extern {
  fn cairo_font_options_create() -> *mut libc::c_void;
  fn cairo_font_options_status(self_arg: *mut libc::c_void) -> super::Status;
  fn cairo_font_options_merge(self_arg: *mut libc::c_void, other: *mut libc::c_void);
  fn cairo_font_options_hash(self_arg: *mut libc::c_void) -> libc::c_ulong;
  fn cairo_font_options_equal(self_arg: *mut libc::c_void, other: *mut libc::c_void) -> bool;
  fn cairo_font_options_set_antialias(self_arg: *mut libc::c_void, antialias: super::antialias::Antialias);
  fn cairo_font_options_get_antialias(self_arg: *mut libc::c_void) -> super::antialias::Antialias;
  fn cairo_font_options_set_subpixel_order(self_arg: *mut libc::c_void, subpixel_order: subpixel_order::SubpixelOrder);
  fn cairo_font_options_get_subpixel_order(self_arg: *mut libc::c_void) -> subpixel_order::SubpixelOrder;
  fn cairo_font_options_set_hint_style(self_arg: *mut libc::c_void, hint_style: hint_style::HintStyle);
  fn cairo_font_options_get_hint_style(self_arg: *mut libc::c_void) -> hint_style::HintStyle;
  fn cairo_font_options_set_hint_metrics(self_arg: *mut libc::c_void, hint_metrics: hint_metrics::HintMetrics);
  fn cairo_font_options_get_hint_metrics(self_arg: *mut libc::c_void) -> hint_metrics::HintMetrics;
}

impl std::clone::Clone for Options {
  fn clone(&self) -> Options {
    unsafe {
      let foreign_result = cairo_font_options_copy(self.opaque);
      return Options { opaque: foreign_result as *mut libc::c_void };
    }
  }
}

extern {
  fn cairo_font_options_copy(self_arg: *mut libc::c_void) -> *mut libc::c_void;
}

impl std::ops::Drop for Options {
  fn drop(&mut self) {
    unsafe {
      cairo_font_options_destroy(self.opaque);
    }
  }
}

extern {
  fn cairo_font_options_destroy(self_arg: *mut libc::c_void);
}


pub struct FontFace {
  pub opaque: *mut libc::c_void
}

impl FontFace {
  pub fn create_toy(family: &str, slant: slant::Slant, weight: weight::Weight) -> FontFace {
    use std::ffi::CString;
    // CString::new will return an error if the bytes yielded contain an internal 0 byte.
    let cstr_family = CString::new(family.as_bytes()).unwrap(); // TODO!
    unsafe {
      let foreign_result = cairo_toy_font_face_create(cstr_family.as_ptr(), slant, weight);
      return FontFace { opaque: foreign_result as *mut libc::c_void };
    }
  }

  pub fn toy_get_family(&mut self) -> std::string::String {
    unsafe {
      let foreign_result = cairo_toy_font_face_get_family(self.opaque);
      return std::string::String::from_utf8_lossy(std::ffi::CStr::from_ptr(foreign_result).to_bytes()).into_owned()
    }
  }

  pub fn toy_get_slant(&mut self) -> slant::Slant {
    unsafe {
      let foreign_result = cairo_toy_font_face_get_slant(self.opaque);
      return foreign_result;
    }
  }

  pub fn toy_get_weight(&mut self) -> slant::Slant {
    unsafe {
      let foreign_result = cairo_toy_font_face_get_weight(self.opaque);
      return foreign_result;
    }
  }

  pub fn status(&mut self) -> super::Status {
    unsafe {
      let foreign_result = cairo_font_face_status(self.opaque);
      return foreign_result;
    }
  }

  pub fn get_type(&mut self) -> font_type::FontType {
    unsafe {
      let foreign_result = cairo_font_face_get_type(self.opaque);
      return foreign_result;
    }
  }

  pub fn get_reference_count(&mut self) -> libc::c_uint {
    unsafe {
      let foreign_result = cairo_font_face_get_reference_count(self.opaque);
      return foreign_result;
    }
  }
}

extern {
  fn cairo_toy_font_face_create(family: *const libc::c_char, slant: slant::Slant, weight: weight::Weight) -> *mut libc::c_void;
  fn cairo_toy_font_face_get_family(self_arg: *mut libc::c_void) -> *const libc::c_char;
  fn cairo_toy_font_face_get_slant(self_arg: *mut libc::c_void) -> slant::Slant;
  fn cairo_toy_font_face_get_weight(self_arg: *mut libc::c_void) -> slant::Slant;
  fn cairo_font_face_status(self_arg: *mut libc::c_void) -> super::Status;
  fn cairo_font_face_get_type(self_arg: *mut libc::c_void) -> font_type::FontType;
  fn cairo_font_face_get_reference_count(self_arg: *mut libc::c_void) -> libc::c_uint;
}

impl std::clone::Clone for FontFace {
  fn clone(&self) -> FontFace {
    unsafe {
      let foreign_result = cairo_font_face_reference(self.opaque);
      return FontFace { opaque: foreign_result as *mut libc::c_void };
    }
  }
}

extern {
  fn cairo_font_face_reference(self_arg: *mut libc::c_void) -> *mut libc::c_void;
}

impl std::ops::Drop for FontFace {
  fn drop(&mut self) {
    unsafe {
      cairo_font_face_destroy(self.opaque);
    }
  }
}

extern {
  fn cairo_font_face_destroy(self_arg: *mut libc::c_void);
}


pub struct ScaledFont {
  pub opaque: *mut libc::c_void
}

impl ScaledFont {
  pub fn create(font_face: &mut FontFace, font_matrix: &mut super::matrix::Matrix, ctm: &mut super::matrix::Matrix, options: &mut Options) -> ScaledFont {
    unsafe {
      let foreign_result = cairo_scaled_font_create(font_face.opaque, font_matrix, ctm, options.opaque);
      return ScaledFont { opaque: foreign_result as *mut libc::c_void };
    }
  }

  pub fn status(&mut self) -> super::Status {
    unsafe {
      let foreign_result = cairo_scaled_font_status(self.opaque);
      return foreign_result;
    }
  }

  pub fn extents(&mut self) -> FontExtents {
    unsafe {
      let mut extents:FontExtents = std::mem::zeroed();
      cairo_scaled_font_extents(self.opaque, &mut extents);
      return extents;
    }
  }

  pub fn text_extents(&mut self, utf8: &str) -> TextExtents {
    use std::ffi::CString;
    let cstr_utf8 = CString::new(utf8.as_bytes()).unwrap(); // TODO!
    // CString::new will return an error if the bytes yielded contain an internal 0 byte.
    unsafe {
      let mut extents:TextExtents = std::mem::zeroed();
      cairo_scaled_font_text_extents(self.opaque, cstr_utf8.as_ptr(), &mut extents);
      return extents;
    }
  }

  pub fn glyph_extents(&mut self, glyphs: &[Glyph]) -> TextExtents {
    unsafe {
      let mut extents:TextExtents = std::mem::zeroed();
      cairo_scaled_font_glyph_extents(self.opaque, glyphs.as_ptr() as *mut Glyph, glyphs.len() as libc::c_int, &mut extents);
      return extents;
    }
  }

  pub fn get_font_face(&mut self) -> FontFace {
    unsafe {
      let foreign_result = cairo_scaled_font_get_font_face(self.opaque);
      return FontFace { opaque: foreign_result as *mut libc::c_void };
    }
  }

  pub fn get_font_options(&mut self, options: &mut FontExtents) {
    unsafe {
      cairo_scaled_font_get_font_options(self.opaque, options);
    }
  }

  pub fn get_font_matrix(&mut self) -> super::matrix::Matrix {
    unsafe {
      let mut font_matrix:super::matrix::Matrix = std::mem::zeroed();
      cairo_scaled_font_get_font_matrix(self.opaque, &mut font_matrix);
      return font_matrix;
    }
  }

  pub fn get_ctm(&mut self) -> super::matrix::Matrix {
    unsafe {
      let mut ctm:super::matrix::Matrix = std::mem::zeroed();
      cairo_scaled_font_get_ctm(self.opaque, &mut ctm);
      return ctm;
    }
  }

  pub fn get_scale_matrix(&mut self) -> super::matrix::Matrix {
    unsafe {
      let mut scale_matrix:super::matrix::Matrix = std::mem::zeroed();
      cairo_scaled_font_get_scale_matrix(self.opaque, &mut scale_matrix);
      return scale_matrix;
    }
  }

  pub fn get_type(&mut self) -> font_type::FontType {
    unsafe {
      let foreign_result = cairo_scaled_font_get_type(self.opaque);
      return foreign_result;
    }
  }

  pub fn get_reference_count(&mut self) -> libc::c_uint {
    unsafe {
      let foreign_result = cairo_scaled_font_get_reference_count(self.opaque);
      return foreign_result;
    }
  }
}

extern {
  fn cairo_scaled_font_create(font_face: *mut libc::c_void, font_matrix: *mut super::matrix::Matrix, ctm: *mut super::matrix::Matrix, options: *mut libc::c_void) -> *mut libc::c_void;
  fn cairo_scaled_font_status(self_arg: *mut libc::c_void) -> super::Status;
  fn cairo_scaled_font_extents(self_arg: *mut libc::c_void, extents: *mut FontExtents);
  fn cairo_scaled_font_text_extents(self_arg: *mut libc::c_void, utf8: *const libc::c_char, extents: *mut TextExtents);
  fn cairo_scaled_font_glyph_extents(self_arg: *mut libc::c_void, glyphs: *mut Glyph, glyphs: libc::c_int, extents: *mut TextExtents);
  fn cairo_scaled_font_get_font_face(self_arg: *mut libc::c_void) -> *mut libc::c_void;
  fn cairo_scaled_font_get_font_options(self_arg: *mut libc::c_void, options: *mut FontExtents);
  fn cairo_scaled_font_get_font_matrix(self_arg: *mut libc::c_void, font_matrix: *mut super::matrix::Matrix);
  fn cairo_scaled_font_get_ctm(self_arg: *mut libc::c_void, ctm: *mut super::matrix::Matrix);
  fn cairo_scaled_font_get_scale_matrix(self_arg: *mut libc::c_void, scale_matrix: *mut super::matrix::Matrix);
  fn cairo_scaled_font_get_type(self_arg: *mut libc::c_void) -> font_type::FontType;
  fn cairo_scaled_font_get_reference_count(self_arg: *mut libc::c_void) -> libc::c_uint;
}

impl std::clone::Clone for ScaledFont {
  fn clone(&self) -> ScaledFont {
    unsafe {
      let foreign_result = cairo_scaled_font_reference(self.opaque);
      return ScaledFont { opaque: foreign_result as *mut libc::c_void };
    }
  }
}

extern {
  fn cairo_scaled_font_reference(self_arg: *mut libc::c_void) -> *mut libc::c_void;
}

impl std::ops::Drop for ScaledFont {
  fn drop(&mut self) {
    unsafe {
      cairo_scaled_font_destroy(self.opaque);
    }
  }
}

extern {
  fn cairo_scaled_font_destroy(self_arg: *mut libc::c_void);
}

#[repr(C)]
pub struct Glyph {
  index: libc::c_ulong,
  x: f64,
  y: f64
}

#[repr(C)]
pub struct Cluster {
  num_bytes: libc::c_int,
  num_glyphs: libc::c_int
}

#[repr(C)]
pub struct FontExtents {
  ascent: f64,
  descent: f64,
  height: f64,
  max_x_advance: f64,
  max_y_advance: f64
}

#[repr(C)]
pub struct TextExtents {
  x_bearing: f64,
  y_bearing: f64,
  width: f64,
  height: f64,
  max_x_advance: f64,
  max_y_advance: f64
}