Skip to content

Commit 6e5b04e

Browse files
committed
improve register_font docs, buffer to avoid panic unwind reliance
1 parent ceb179b commit 6e5b04e

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

plotters/src/style/font/ab_glyph.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ pub struct InvalidFont {
1212
_priv: (),
1313
}
1414

15+
// Note for future contributors: There is nothing fundamental about the static reference requirement here.
16+
// It would be reasonably easy to add a function which accepts an owned buffer,
17+
// or even a reference counted buffer, instead.
1518
/// Register a font in the fonts table.
19+
///
20+
/// The `name` parameter gives the name this font shall be referred to
21+
/// in the other APIs, like `"sans-serif"`.
22+
///
23+
/// The `bytes` parameter should be the complete contents
24+
/// of an OpenType font file, like:
25+
/// ```
26+
/// include_bytes!("FiraGO-Regular.otf")
27+
/// ```
1628
pub fn register_font(name: &str, bytes: &'static [u8]) -> Result<(), InvalidFont> {
1729
let font = FontRef::try_from_slice(bytes).map_err(|_| InvalidFont { _priv: () })?;
1830
let mut lock = FONTS.write().unwrap();
@@ -100,15 +112,14 @@ impl FontData for FontDataInternal {
100112
let rect = q.px_bounds();
101113
let y_shift = ((size as f32) / 2.0 + rect.min.y) as i32;
102114
let x_shift = x_shift as i32;
103-
let res = panic::catch_unwind(AssertUnwindSafe(|| {
104-
q.draw(|x, y, c| {
105-
if let Err(_) = draw(x as i32 + x_shift, y as i32 + y_shift, c) {
106-
panic!("fail")
107-
}
108-
});
109-
}));
110-
if let Err(_) = res {
111-
return Err(FontError::Unknown);
115+
let mut buf = vec![];
116+
q.draw(|x, y, c| buf.push((x, y, c)));
117+
for (x, y, c) in buf {
118+
draw(x as i32 + x_shift, y as i32 + y_shift, c).map_err(|e| {
119+
// Note: If ever `plotters` adds a tracing or logging crate,
120+
// this would be a good place to use it.
121+
FontError::Unknown
122+
})?;
112123
}
113124
}
114125
x_shift += font.h_advance(font.glyph_id(c));

0 commit comments

Comments
 (0)