harfbuzz_rs_now 2.3.2

A high-level interface to HarfBuzz, exposing its most important functionality in a safe manner using Rust.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2018 Manuel Reinhardt
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

//! Contains the `DrawFuncs` trait.

use crate::bindings::{
    hb_draw_funcs_create, hb_draw_funcs_destroy, hb_draw_funcs_get_empty, hb_draw_funcs_reference,
    hb_draw_funcs_set_close_path_func, hb_draw_funcs_set_cubic_to_func,
    hb_draw_funcs_set_line_to_func, hb_draw_funcs_set_move_to_func,
    hb_draw_funcs_set_quadratic_to_func, hb_draw_funcs_t, hb_draw_state_t,
};
use crate::common::{HarfbuzzObject, Owned, Shared};
use crate::font::destroy_box;

use std::os::raw::c_void;

use std::{self, fmt, marker::PhantomData, panic, ptr::NonNull};

#[derive(Copy, Clone, Debug)]
pub struct DrawState {
    pub path_open: bool,
    pub path_start_x: f32,
    pub path_start_y: f32,
    pub current_x: f32,
    pub current_y: f32,
}

/// This Trait specifies the font callbacks that harfbuzz uses when asked
/// to draw a glyph.
#[allow(unused_variables)]
pub trait DrawFuncs {
    fn move_to(&mut self, st: &DrawState, to_x: f32, to_y: f32);
    fn line_to(&mut self, st: &DrawState, to_x: f32, to_y: f32);
    fn quadratic_to(
        &mut self,
        st: &DrawState,
        control_x: f32,
        control_y: f32,
        to_x: f32,
        to_y: f32,
    );
    #[allow(clippy::too_many_arguments)]
    fn cubic_to(
        &mut self,
        st: &DrawState,
        control1_x: f32,
        control1_y: f32,
        control2_x: f32,
        control2_y: f32,
        to_x: f32,
        to_y: f32,
    );
    fn close_path(&mut self, st: &DrawState);
}

macro_rules! hb_callback {
    ($func_name:ident<$($arg:ident: $datatype:ty),*>{
        $(argument $closure_arg:ty => $expr:expr,)*
    }) => {
        #[allow(clippy::let_and_return)]
        extern "C" fn $func_name<T, F>(
            _dfuncs: *mut hb_draw_funcs_t,
            draw_data: *mut ::std::os::raw::c_void,
            $(
                $arg: $datatype,
            )*
            closure_data: *mut c_void,
        ) where F: Fn(&mut T, $($closure_arg),*) {
            let catch_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
                let draw_data = unsafe { &mut *(draw_data as *mut T) };
                let closure = unsafe { &mut *(closure_data as *mut F) };
                closure(draw_data, $($expr),*);
            }));
            match catch_result {
                Ok(val) => val,
                Err(_) => {
                    // TODO: Log error
                    Default::default()
                }
            }
        }
    };
}

hb_callback!(
    rust_move_to_closure<st: *mut hb_draw_state_t, to_x: f32, to_y: f32> {
        argument DrawState => DrawState {
            path_open: unsafe { (*st).path_open != 0 },
            path_start_x: unsafe { (*st).path_start_x },
            path_start_y: unsafe { (*st).path_start_y },
            current_x: unsafe { (*st).current_x },
            current_y: unsafe { (*st).current_y }
        },
        argument f32 => to_x,
        argument f32 => to_y,
    }
);

hb_callback!(
    rust_line_to_closure <st: *mut hb_draw_state_t, to_x: f32, to_y: f32> {
        argument DrawState => DrawState {
            path_open: unsafe { (*st).path_open != 0 },
            path_start_x: unsafe { (*st).path_start_x },
            path_start_y: unsafe { (*st).path_start_y },
            current_x: unsafe { (*st).current_x },
            current_y: unsafe { (*st).current_y }
        },
        argument f32 => to_x,
        argument f32 => to_y,
    }
);

hb_callback!(
    rust_quadratic_to_closure <st: *mut hb_draw_state_t, control_x: f32, control_y: f32, to_x: f32, to_y: f32> {
        argument DrawState => DrawState {
            path_open: unsafe { (*st).path_open != 0 },
            path_start_x: unsafe { (*st).path_start_x },
            path_start_y: unsafe { (*st).path_start_y },
            current_x: unsafe { (*st).current_x },
            current_y: unsafe { (*st).current_y }
        },
        argument f32 => control_x,
        argument f32 => control_y,
        argument f32 => to_x,
        argument f32 => to_y,
    }
);

hb_callback!(
    rust_cubic_to_closure <st: *mut hb_draw_state_t, control1_x: f32, control1_y: f32,  control2_x: f32, control2_y: f32, to_x: f32, to_y: f32> {
        argument DrawState => DrawState {
            path_open: unsafe { (*st).path_open != 0 },
            path_start_x: unsafe { (*st).path_start_x },
            path_start_y: unsafe { (*st).path_start_y },
            current_x: unsafe { (*st).current_x },
            current_y: unsafe { (*st).current_y }
        },
        argument f32 => control1_x,
        argument f32 => control1_y,
        argument f32 => control2_x,
        argument f32 => control2_y,
        argument f32 => to_x,
        argument f32 => to_y,
    }
);
hb_callback!(
    rust_close_path_closure <st: *mut hb_draw_state_t> {
        argument DrawState => DrawState {
            path_open: unsafe { (*st).path_open != 0 },
            path_start_x: unsafe { (*st).path_start_x },
            path_start_y: unsafe { (*st).path_start_y },
            current_x: unsafe { (*st).current_x },
            current_y: unsafe { (*st).current_y }
        },
    }
);

/// A `DrawFuncsImpl` contains implementations of the font callbacks that
/// harfbuzz uses to draw a glyph.
///
/// To use this, set the font funcs from a type that implements the `DrawFuncs`
/// trait using the `from_trait_impl` constructor.
///
/// # Example
///
/// ```ignore
/// use harfbuzz_rs::*;
/// use harfbuzz_rs::draw_funcs::DrawFuncsImpl;
///
/// // Dummy struct implementing DrawFuncs
/// struct MyDrawFuncs {
///    points: Vec<(f32,f32)>,
/// }
/// impl DrawFuncs for MyDrawFuncs {
///     fn move_to(&self, _: &DrawState, x: f32, y: f32) {
///         // ...
///     }
///     // implementations of other functions...
/// }
///
/// let draw_funcs: Owned<DrawFuncsImpl<MyFontData>> = DrawFuncsImpl::from_trait_impl();
/// ```
pub(crate) struct DrawFuncsImpl<T> {
    raw: NonNull<hb_draw_funcs_t>,
    marker: PhantomData<T>,
}

impl<T> DrawFuncsImpl<T> {
    /// Returns an empty `DrawFuncsImpl`. Every font callback of the returned
    /// `DrawFuncsImpl` gives a null value regardless of its input.
    #[allow(unused)]
    pub fn empty() -> Shared<DrawFuncsImpl<T>> {
        let raw = unsafe { hb_draw_funcs_get_empty() };
        unsafe { Shared::from_raw_ref(raw) }
    }
}

impl<T: DrawFuncs> DrawFuncsImpl<T> {
    /// Create a new `DrawFuncsImpl` from the `DrawFuncs` trait implementation
    /// of `T`.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use harfbuzz_rs::*;
    /// use harfbuzz_rs::draw_funcs::DrawFuncsImpl;
    ///
    /// // Dummy struct implementing DrawFuncs
    /// struct MyDrawFuncs {
    ///    points: Vec<(f32,f32)>,
    /// }
    /// impl DrawFuncs for MyDrawFuncs {
    ///     fn move_to(&self, _: &DrawState, x: f32, y: f32) {
    ///         // ...
    ///     }
    ///     // implementations of other functions...
    /// }
    ///
    /// let draw_funcs: Owned<DrawFuncsImpl<MyFontData>> = DrawFuncsImpl::from_trait_impl();
    /// ```
    ///
    pub fn from_trait_impl() -> Owned<DrawFuncsImpl<T>> {
        let mut ffuncs = DrawFuncsImpl::new();
        ffuncs.set_trait_impl();
        ffuncs
    }

    fn set_trait_impl(&mut self) {
        self.set_move_to_func(|data, st, x, y| data.move_to(&st, x, y));
        self.set_line_to_func(|data, st, x, y| data.line_to(&st, x, y));
        self.set_quadratic_to_func(|data, st, cx, cy, x, y| data.quadratic_to(&st, cx, cy, x, y));
        self.set_cubic_to_func(|data, st, c1x, c1y, c2x, c2y, x, y| {
            data.cubic_to(&st, c1x, c1y, c2x, c2y, x, y)
        });
        self.set_close_path_func(|data, st| data.close_path(&st));
    }
}

impl<T> DrawFuncsImpl<T> {
    pub fn new() -> Owned<DrawFuncsImpl<T>> {
        unsafe { Owned::from_raw(hb_draw_funcs_create()) }
    }

    pub fn set_move_to_func<F>(&mut self, func: F)
    where
        F: Fn(&mut T, DrawState, f32, f32),
    {
        let user_data = Box::new(func);
        unsafe {
            hb_draw_funcs_set_move_to_func(
                self.as_raw(),
                Some(rust_move_to_closure::<T, F>),
                Box::into_raw(user_data) as *mut _,
                Some(destroy_box::<F>),
            );
        }
    }

    pub fn set_quadratic_to_func<F>(&mut self, func: F)
    where
        F: Fn(&mut T, DrawState, f32, f32, f32, f32),
    {
        let user_data = Box::new(func);
        unsafe {
            hb_draw_funcs_set_quadratic_to_func(
                self.as_raw(),
                Some(rust_quadratic_to_closure::<T, F>),
                Box::into_raw(user_data) as *mut _,
                Some(destroy_box::<F>),
            );
        }
    }

    pub fn set_line_to_func<F>(&mut self, func: F)
    where
        F: Fn(&mut T, DrawState, f32, f32),
    {
        let user_data = Box::new(func);
        unsafe {
            hb_draw_funcs_set_line_to_func(
                self.as_raw(),
                Some(rust_line_to_closure::<T, F>),
                Box::into_raw(user_data) as *mut _,
                Some(destroy_box::<F>),
            );
        }
    }

    pub fn set_cubic_to_func<F>(&mut self, func: F)
    where
        F: Fn(&mut T, DrawState, f32, f32, f32, f32, f32, f32),
    {
        let user_data = Box::new(func);
        unsafe {
            hb_draw_funcs_set_cubic_to_func(
                self.as_raw(),
                Some(rust_cubic_to_closure::<T, F>),
                Box::into_raw(user_data) as *mut _,
                Some(destroy_box::<F>),
            );
        }
    }

    pub fn set_close_path_func<F>(&mut self, func: F)
    where
        F: Fn(&mut T, DrawState),
    {
        let user_data = Box::new(func);
        unsafe {
            hb_draw_funcs_set_close_path_func(
                self.as_raw(),
                Some(rust_close_path_closure::<T, F>),
                Box::into_raw(user_data) as *mut _,
                Some(destroy_box::<F>),
            );
        }
    }
}

impl<T> fmt::Debug for DrawFuncsImpl<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DrawFuncsImpl")
            .field("raw", &self.as_raw())
            .finish()
    }
}

unsafe impl<T> HarfbuzzObject for DrawFuncsImpl<T> {
    type Raw = hb_draw_funcs_t;

    unsafe fn from_raw(raw: *const Self::Raw) -> Self {
        DrawFuncsImpl {
            raw: NonNull::new(raw as *mut _).unwrap(),
            marker: PhantomData,
        }
    }

    fn as_raw(&self) -> *mut Self::Raw {
        self.raw.as_ptr()
    }

    unsafe fn reference(&self) {
        hb_draw_funcs_reference(self.as_raw());
    }

    unsafe fn dereference(&self) {
        hb_draw_funcs_destroy(self.as_raw())
    }
}

unsafe impl<T> Send for DrawFuncsImpl<T> {}
unsafe impl<T> Sync for DrawFuncsImpl<T> {}

#[cfg(test)]
mod tests {
    use crate::draw_funcs::DrawFuncs;

    use crate::{Face, Font, *};
    use std::path::PathBuf;

    #[repr(C)]
    #[derive(Debug)]
    struct TestDrawFuncs {
        output: String,
    }
    impl DrawFuncs for TestDrawFuncs {
        fn move_to(&mut self, _st: &draw_funcs::DrawState, to_x: f32, to_y: f32) {
            self.output.push_str(&format!("M {:} {:} ", to_x, to_y));
        }

        fn line_to(&mut self, _st: &draw_funcs::DrawState, to_x: f32, to_y: f32) {
            self.output.push_str(&format!("L {:} {:} ", to_x, to_y));
        }

        fn quadratic_to(
            &mut self,
            _st: &draw_funcs::DrawState,
            control_x: f32,
            control_y: f32,
            to_x: f32,
            to_y: f32,
        ) {
            self.output.push_str(&format!(
                "Q {:} {:}, {:} {:} ",
                control_x, control_y, to_x, to_y
            ));
        }

        fn cubic_to(
            &mut self,
            _st: &draw_funcs::DrawState,
            control1_x: f32,
            control1_y: f32,
            control2_x: f32,
            control2_y: f32,
            to_x: f32,
            to_y: f32,
        ) {
            self.output.push_str(&format!(
                "C {:} {:}, {:}, {:}, {:} {:} ",
                control1_x, control1_y, control2_x, control2_y, to_x, to_y
            ));
        }

        fn close_path(&mut self, _st: &draw_funcs::DrawState) {
            self.output.push('Z');
        }
    }

    #[test]
    fn test_move_to() {
        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        path.push("testfiles/SourceSansVariable-Roman.ttf");
        let face = Face::from_file(path, 0).expect("Error reading font file.");
        let font = Font::new(face);
        let shape = TestDrawFuncs {
            output: String::new(),
        };
        font.draw_glyph(2, &shape);
        println!("After");
        assert_eq!(shape.output, "M 10 0 L 246 660 L 274 660 L 510 0 L 476 0 L 338 396 Q 317 456, 298.5 510 Q 280 564, 262 626 L 258 626 Q 240 564, 221.5 510 Q 203 456, 182 396 L 42 0 L 10 0 ZM 112 236 L 112 264 L 405 264 L 405 236 L 112 236 Z");
    }
}