10
10
11
11
use crate :: { CanvasRenderingContext2D , State , TextAlign , TextBaseline } ;
12
12
use font_kit:: canvas:: RasterizationOptions ;
13
+ use font_kit:: error:: { FontLoadingError , SelectionError } ;
13
14
use font_kit:: family_name:: FamilyName ;
14
15
use font_kit:: handle:: Handle ;
15
16
use font_kit:: hinting:: HintingOptions ;
@@ -103,9 +104,10 @@ impl CanvasRenderingContext2D {
103
104
}
104
105
105
106
#[ inline]
106
- pub fn set_font < FC > ( & mut self , font_collection : FC ) where FC : IntoFontCollection {
107
- let font_collection = font_collection. into_font_collection ( & self . canvas_font_context ) ;
107
+ pub fn set_font < FC > ( & mut self , font_collection : FC ) -> Result < ( ) , FontError > where FC : IntoFontCollection {
108
+ let font_collection = font_collection. into_font_collection ( & self . canvas_font_context ) ? ;
108
109
self . current_state . font_collection = font_collection;
110
+ Ok ( ( ) )
109
111
}
110
112
111
113
#[ inline]
@@ -188,6 +190,13 @@ impl ToTextLayout for TextMetrics {
188
190
#[ derive( Clone ) ]
189
191
pub struct CanvasFontContext ( pub ( crate ) Rc < RefCell < CanvasFontContextData > > ) ;
190
192
193
+ /// The reason a font could not be loaded
194
+ #[ derive( Debug ) ]
195
+ pub enum FontError {
196
+ NotFound ( SelectionError ) ,
197
+ LoadError ( FontLoadingError ) ,
198
+ }
199
+
191
200
pub ( super ) struct CanvasFontContextData {
192
201
pub ( super ) font_context : FontContext < Font > ,
193
202
#[ allow( dead_code) ]
@@ -224,16 +233,15 @@ impl CanvasFontContext {
224
233
CanvasFontContext :: new ( Arc :: new ( MemSource :: from_fonts ( fonts) . unwrap ( ) ) )
225
234
}
226
235
227
- fn get_font_by_postscript_name ( & self , postscript_name : & str ) -> Font {
236
+ fn get_font_by_postscript_name ( & self , postscript_name : & str ) -> Result < Font , FontError > {
228
237
let this = self . 0 . borrow ( ) ;
229
238
if let Some ( cached_font) = this. font_context . get_cached_font ( postscript_name) {
230
- return ( * cached_font) . clone ( ) ;
239
+ return Ok ( ( * cached_font) . clone ( ) ) ;
231
240
}
232
241
this. font_source
233
242
. select_by_postscript_name ( postscript_name)
234
- . expect ( "Couldn't find a font with that PostScript name!" )
235
- . load ( )
236
- . expect ( "Failed to load the font!" )
243
+ . map_err ( FontError :: NotFound ) ?
244
+ . load ( ) . map_err ( FontError :: LoadError )
237
245
}
238
246
}
239
247
@@ -523,46 +531,46 @@ impl VerticalMetrics {
523
531
/// Various things that can be conveniently converted into font collections for use with
524
532
/// `CanvasRenderingContext2D::set_font()`.
525
533
pub trait IntoFontCollection {
526
- fn into_font_collection ( self , font_context : & CanvasFontContext ) -> Arc < FontCollection > ;
534
+ fn into_font_collection ( self , font_context : & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > ;
527
535
}
528
536
529
537
impl IntoFontCollection for Arc < FontCollection > {
530
538
#[ inline]
531
- fn into_font_collection ( self , _: & CanvasFontContext ) -> Arc < FontCollection > {
532
- self
539
+ fn into_font_collection ( self , _: & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
540
+ Ok ( self )
533
541
}
534
542
}
535
543
536
544
impl IntoFontCollection for FontFamily {
537
545
#[ inline]
538
- fn into_font_collection ( self , _: & CanvasFontContext ) -> Arc < FontCollection > {
546
+ fn into_font_collection ( self , _: & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
539
547
let mut font_collection = FontCollection :: new ( ) ;
540
548
font_collection. add_family ( self ) ;
541
- Arc :: new ( font_collection)
549
+ Ok ( Arc :: new ( font_collection) )
542
550
}
543
551
}
544
552
545
553
impl IntoFontCollection for Vec < FontFamily > {
546
554
#[ inline]
547
- fn into_font_collection ( self , _: & CanvasFontContext ) -> Arc < FontCollection > {
555
+ fn into_font_collection ( self , _: & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
548
556
let mut font_collection = FontCollection :: new ( ) ;
549
557
for family in self {
550
558
font_collection. add_family ( family) ;
551
559
}
552
- Arc :: new ( font_collection)
560
+ Ok ( Arc :: new ( font_collection) )
553
561
}
554
562
}
555
563
556
564
impl IntoFontCollection for Font {
557
565
#[ inline]
558
- fn into_font_collection ( self , context : & CanvasFontContext ) -> Arc < FontCollection > {
559
- FontFamily :: new_from_font ( self ) . into_font_collection ( context)
566
+ fn into_font_collection ( self , context : & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
567
+ Ok ( FontFamily :: new_from_font ( self ) . into_font_collection ( context) ? )
560
568
}
561
569
}
562
570
563
571
impl < ' a > IntoFontCollection for & ' a [ Font ] {
564
572
#[ inline]
565
- fn into_font_collection ( self , context : & CanvasFontContext ) -> Arc < FontCollection > {
573
+ fn into_font_collection ( self , context : & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
566
574
let mut family = FontFamily :: new ( ) ;
567
575
for font in self {
568
576
family. add_font ( FontRef :: new ( ( * font) . clone ( ) ) )
@@ -573,19 +581,19 @@ impl<'a> IntoFontCollection for &'a [Font] {
573
581
574
582
impl < ' a > IntoFontCollection for & ' a str {
575
583
#[ inline]
576
- fn into_font_collection ( self , context : & CanvasFontContext ) -> Arc < FontCollection > {
577
- context. get_font_by_postscript_name ( self ) . into_font_collection ( context)
584
+ fn into_font_collection ( self , context : & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
585
+ context. get_font_by_postscript_name ( self ) ? . into_font_collection ( context)
578
586
}
579
587
}
580
588
581
589
impl < ' a , ' b > IntoFontCollection for & ' a [ & ' b str ] {
582
590
#[ inline]
583
- fn into_font_collection ( self , context : & CanvasFontContext ) -> Arc < FontCollection > {
591
+ fn into_font_collection ( self , context : & CanvasFontContext ) -> Result < Arc < FontCollection > , FontError > {
584
592
let mut font_collection = FontCollection :: new ( ) ;
585
593
for postscript_name in self {
586
- let font = context. get_font_by_postscript_name ( postscript_name) ;
594
+ let font = context. get_font_by_postscript_name ( postscript_name) ? ;
587
595
font_collection. add_family ( FontFamily :: new_from_font ( font) ) ;
588
596
}
589
- Arc :: new ( font_collection)
597
+ Ok ( Arc :: new ( font_collection) )
590
598
}
591
599
}
0 commit comments