@@ -12,7 +12,19 @@ pub struct InvalidFont {
12
12
_priv : ( ) ,
13
13
}
14
14
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.
15
18
/// 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
+ /// ```
16
28
pub fn register_font ( name : & str , bytes : & ' static [ u8 ] ) -> Result < ( ) , InvalidFont > {
17
29
let font = FontRef :: try_from_slice ( bytes) . map_err ( |_| InvalidFont { _priv : ( ) } ) ?;
18
30
let mut lock = FONTS . write ( ) . unwrap ( ) ;
@@ -100,15 +112,14 @@ impl FontData for FontDataInternal {
100
112
let rect = q. px_bounds ( ) ;
101
113
let y_shift = ( ( size as f32 ) / 2.0 + rect. min . y ) as i32 ;
102
114
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
+ } ) ?;
112
123
}
113
124
}
114
125
x_shift += font. h_advance ( font. glyph_id ( c) ) ;
0 commit comments