Skip to content

Commit f4eb3a2

Browse files
committed
Add documentation
1 parent 54debe5 commit f4eb3a2

5 files changed

Lines changed: 197 additions & 48 deletions

File tree

crates/bridge_freetype2/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ impl Drop for Face {
610610
}
611611
}
612612

613+
// SAFETY: Functions that are not safe to share across threads (such as FT_Done_Face) are protected
614+
// by mutex to ensure they are only called once per thread.
615+
unsafe impl Send for Face {}
616+
// SAFETY: Functions that are not safe to share across threads (such as FT_Done_Face) are protected
617+
// by mutex to ensure they are only called once per thread.
618+
unsafe impl Sync for Face {}
619+
613620
#[cfg(test)]
614621
mod tests {
615622
use super::*;

crates/bridge_harfbuzz/src/font_funcs.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ unsafe extern "C" fn glyph_contour_point<
145145
let font = FontRef::from_raw(NonNull::new(font).unwrap());
146146
let data = &*font_data.cast::<T>();
147147
let func = &*user_data.cast::<F>();
148-
match func(font, data, gid, index as u32) {
148+
#[allow(clippy::useless_conversion)]
149+
match func(font, data, gid, index.into()) {
149150
Some(val) => {
150151
*x = val.0;
151152
*y = val.1;
@@ -184,6 +185,7 @@ unsafe extern "C" fn glyph_name<T, F: Fn(FontRef<'_>, &T, Codepoint, &mut [u8])
184185
}
185186
}
186187

188+
/// A borrowed reference to a [`FontFuncs`]
187189
pub struct FontFuncsRef<'a, T>(
188190
NonNull<sys::hb_font_funcs_t>,
189191
PhantomData<(&'a sys::hb_font_funcs_t, T)>,
@@ -195,6 +197,7 @@ impl<T> FontFuncsRef<'_, T> {
195197
}
196198
}
197199

200+
/// A borrowed mutable reference to a [`FontFuncs`]
198201
pub struct FontFuncsMut<'a, T>(
199202
FontFuncsRef<'a, T>,
200203
PhantomData<&'a mut sys::hb_font_funcs_t>,
@@ -205,6 +208,7 @@ impl<T> FontFuncsMut<'_, T> {
205208
self.0.as_ptr()
206209
}
207210

211+
/// Set the nominal glyph function
208212
pub fn nominal_glyph_func<F>(&mut self, f: F)
209213
where
210214
F: Fn(FontRef<'_>, &T, Codepoint) -> Option<Codepoint> + 'static,
@@ -221,6 +225,7 @@ impl<T> FontFuncsMut<'_, T> {
221225
}
222226
}
223227

228+
/// Set the variation glyph function
224229
pub fn variation_glyph_func<F>(&mut self, f: F)
225230
where
226231
F: Fn(FontRef<'_>, &T, Codepoint, Codepoint) -> Option<Codepoint> + 'static,
@@ -237,6 +242,7 @@ impl<T> FontFuncsMut<'_, T> {
237242
}
238243
}
239244

245+
/// Set the horizontal glyph advance function
240246
pub fn glyph_h_advance<F>(&mut self, f: F)
241247
where
242248
F: Fn(FontRef<'_>, &T, Codepoint) -> Position + 'static,
@@ -253,6 +259,7 @@ impl<T> FontFuncsMut<'_, T> {
253259
}
254260
}
255261

262+
/// Set the vertical glyph advance function
256263
pub fn glyph_v_advance<F>(&mut self, f: F)
257264
where
258265
F: Fn(FontRef<'_>, &T, Codepoint) -> Position + 'static,
@@ -269,6 +276,7 @@ impl<T> FontFuncsMut<'_, T> {
269276
}
270277
}
271278

279+
/// Set the horizontal glyph origin function
272280
pub fn glyph_h_origin<F>(&mut self, f: F)
273281
where
274282
F: Fn(FontRef<'_>, &T, Codepoint) -> Option<(Position, Position)> + 'static,
@@ -285,6 +293,7 @@ impl<T> FontFuncsMut<'_, T> {
285293
}
286294
}
287295

296+
/// Set the vertical glyph origin function
288297
pub fn glyph_v_origin<F>(&mut self, f: F)
289298
where
290299
F: Fn(FontRef<'_>, &T, Codepoint) -> Option<(Position, Position)> + 'static,
@@ -301,6 +310,7 @@ impl<T> FontFuncsMut<'_, T> {
301310
}
302311
}
303312

313+
/// Set the horizontal glyph kerning function
304314
pub fn glyph_h_kerning<F>(&mut self, f: F)
305315
where
306316
F: Fn(FontRef<'_>, &T, Codepoint, Codepoint) -> Position + 'static,
@@ -317,6 +327,7 @@ impl<T> FontFuncsMut<'_, T> {
317327
}
318328
}
319329

330+
/// Set the vertical glyph kerning function
320331
pub fn glyph_v_kerning<F>(&mut self, f: F)
321332
where
322333
F: Fn(FontRef<'_>, &T, Codepoint, Codepoint) -> Position + 'static,
@@ -333,6 +344,7 @@ impl<T> FontFuncsMut<'_, T> {
333344
}
334345
}
335346

347+
/// Set the glyph extents function
336348
pub fn glyph_extents<F>(&mut self, f: F)
337349
where
338350
F: Fn(FontRef<'_>, &T, Codepoint) -> Option<GlyphExtents> + 'static,
@@ -349,6 +361,7 @@ impl<T> FontFuncsMut<'_, T> {
349361
}
350362
}
351363

364+
/// Set the contour point function
352365
pub fn glyph_contour_point<F>(&mut self, f: F)
353366
where
354367
F: Fn(FontRef<'_>, &T, Codepoint, u32) -> Option<(Position, Position)> + 'static,
@@ -365,6 +378,7 @@ impl<T> FontFuncsMut<'_, T> {
365378
}
366379
}
367380

381+
/// Set the glyph name function
368382
pub fn glyph_name<F>(&mut self, f: F)
369383
where
370384
F: Fn(FontRef<'_>, &T, Codepoint, &mut [u8]) -> usize + 'static,
@@ -390,23 +404,31 @@ impl<'a, T> Deref for FontFuncsMut<'a, T> {
390404
}
391405
}
392406

407+
/// A font function table. These functions allow in-depth customization of glyph shaping for a font.
408+
/// The `T` parameter represents user-provided data when the functions are linked to a font, that
409+
/// will be passed to each function and thus be used as part of generating the result value.
393410
pub struct FontFuncs<T>(NonNull<sys::hb_font_funcs_t>, PhantomData<T>);
394411

395412
impl<T> FontFuncs<T> {
413+
/// Create a new font function table with no functions set
396414
pub fn new() -> FontFuncs<T> {
397415
// SAFETY: This is always safe to call
398416
let ptr = unsafe { sys::hb_font_funcs_create() };
399417
FontFuncs(NonNull::new(ptr).unwrap(), PhantomData)
400418
}
401419

420+
/// Convert into a shared reference
402421
pub fn as_ref(&self) -> FontFuncsRef<'_, T> {
403422
FontFuncsRef(self.0, PhantomData)
404423
}
405424

425+
/// Convert into a mutable reference
406426
pub fn as_mut(&mut self) -> FontFuncsMut<'_, T> {
407427
FontFuncsMut(self.as_ref(), PhantomData)
408428
}
409429

430+
/// Convert this table into an [`ImmutFontFuncs`], rendering future attempts to alter it into
431+
/// no-ops. This makes the value safe to share between threads.
410432
pub fn make_immutable(self) -> ImmutFontFuncs<T> {
411433
let this = ManuallyDrop::new(self);
412434
// SAFETY: Internal pointer guaranteed valid. This cannot cause clones to exhibit UB -
@@ -437,9 +459,12 @@ impl<T> Drop for FontFuncs<T> {
437459
}
438460
}
439461

462+
/// An immutable [`FontFuncs`] variant. This allows the value to become [`Send`] and [`Sync`]
463+
/// (assuming T is [`Send`] and [`Sync`]), as it may no longer be mutated by any other caller.
440464
pub struct ImmutFontFuncs<T>(NonNull<sys::hb_font_funcs_t>, PhantomData<T>);
441465

442466
impl<T> ImmutFontFuncs<T> {
467+
/// Convert into a shared reference
443468
pub fn as_ref(&self) -> FontFuncsRef<'_, T> {
444469
FontFuncsRef(self.0, PhantomData)
445470
}
@@ -464,9 +489,9 @@ impl<T> Drop for ImmutFontFuncs<T> {
464489
// future attempts to change the value no-ops. This in turn means the object becomes safe to
465490
// send to other threads. The contained data isn't bound because it is tied to the font,
466491
// which is not Send or Sync and as such will not use the data across threads.
467-
unsafe impl<T> Send for ImmutFontFuncs<T> {}
492+
unsafe impl<T: Send> Send for ImmutFontFuncs<T> {}
468493
// SAFETY: ImmutFontFuncs is gained by calling `make_immutable` on a FontFuncs object, which renders
469494
// future attempts to change the value no-ops. This in turn means the object becomes safe to
470495
// reference from other threads. The contained data isn't bound because it is tied to the font,
471496
// which is not Send or Sync and as such will not use the data across threads.
472-
unsafe impl<T> Sync for ImmutFontFuncs<T> {}
497+
unsafe impl<T: Sync> Sync for ImmutFontFuncs<T> {}

0 commit comments

Comments
 (0)