@@ -13,7 +13,7 @@ use bevy_math::{Rect, UVec2, Vec2};
13
13
use bevy_platform:: collections:: HashMap ;
14
14
use bevy_reflect:: { std_traits:: ReflectDefault , Reflect } ;
15
15
16
- use cosmic_text:: { Attrs , Buffer , Family , Metrics , Shaping , Wrap } ;
16
+ use cosmic_text:: { Attrs , Buffer , Metrics , Shaping , Wrap } ;
17
17
18
18
use crate :: {
19
19
error:: TextError , ComputedTextBlock , Font , FontAtlasSets , FontSmoothing , Justify , LineBreak ,
@@ -44,6 +44,7 @@ impl CosmicFontSystem {
44
44
if load_system_fonts {
45
45
db. load_system_fonts ( ) ;
46
46
}
47
+
47
48
Self ( cosmic_text:: FontSystem :: new_with_locale_and_db ( locale, db) )
48
49
}
49
50
}
@@ -151,7 +152,8 @@ impl TextPipeline {
151
152
font_system,
152
153
& mut self . map_handle_to_font_id ,
153
154
fonts,
154
- ) ;
155
+ )
156
+ . ok_or ( TextError :: NoSuchFont ) ?;
155
157
156
158
// Save spans that aren't zero-sized.
157
159
if scale_factor <= 0.0 || text_font. font_size <= 0.0 {
@@ -507,34 +509,65 @@ pub fn load_font_to_fontdb(
507
509
font_system : & mut cosmic_text:: FontSystem ,
508
510
map_handle_to_font_id : & mut HashMap < AssetId < Font > , ( cosmic_text:: fontdb:: ID , Arc < str > ) > ,
509
511
fonts : & Assets < Font > ,
510
- ) -> FontFaceInfo {
512
+ ) -> Option < FontFaceInfo > {
511
513
let font_handle = text_font. font . clone ( ) ;
512
- let ( face_id, family_name) = map_handle_to_font_id
513
- . entry ( font_handle. id ( ) )
514
- . or_insert_with ( || {
514
+
515
+ let ( face_id, family_name) = match map_handle_to_font_id. get_mut ( & font_handle. id ( ) ) {
516
+ Some ( ( face_id, family_name) ) => ( face_id, family_name) ,
517
+ None => {
515
518
let font = fonts. get ( font_handle. id ( ) ) . expect (
516
519
"Tried getting a font that was not available, probably due to not being loaded yet" ,
517
520
) ;
518
- let data = Arc :: clone ( & font. data ) ;
519
- let ids = font_system
520
- . db_mut ( )
521
- . load_font_source ( cosmic_text:: fontdb:: Source :: Binary ( data) ) ;
522
521
523
- // TODO: it is assumed this is the right font face
524
- let face_id = * ids. last ( ) . unwrap ( ) ;
522
+ let face_id = match font {
523
+ Font :: Data ( data) => {
524
+ let data = Arc :: clone ( data) ;
525
+
526
+ let ids = font_system
527
+ . db_mut ( )
528
+ . load_font_source ( cosmic_text:: fontdb:: Source :: Binary ( data) ) ;
529
+
530
+ // TODO: it is assumed this is the right font face
531
+ * ids. last ( ) . unwrap ( )
532
+ }
533
+ Font :: System {
534
+ families,
535
+ weight,
536
+ stretch,
537
+ style,
538
+ } => {
539
+ let families = families
540
+ . iter ( )
541
+ . map ( |family| family. as_fontdb_family ( ) )
542
+ . collect :: < Vec < _ > > ( ) ;
543
+ let query = cosmic_text:: fontdb:: Query {
544
+ families : & families,
545
+ weight : * weight,
546
+ stretch : * stretch,
547
+ style : * style,
548
+ } ;
549
+
550
+ font_system. db ( ) . query ( & query) ?
551
+ }
552
+ } ;
553
+
525
554
let face = font_system. db ( ) . face ( face_id) . unwrap ( ) ;
526
555
let family_name = Arc :: from ( face. families [ 0 ] . 0 . as_str ( ) ) ;
527
556
557
+ map_handle_to_font_id. insert ( font_handle. id ( ) , ( face_id, family_name) ) ;
558
+ let ( face_id, family_name) = map_handle_to_font_id. get_mut ( & font_handle. id ( ) ) . unwrap ( ) ;
528
559
( face_id, family_name)
529
- } ) ;
560
+ }
561
+ } ;
562
+
530
563
let face = font_system. db ( ) . face ( * face_id) . unwrap ( ) ;
531
564
532
- FontFaceInfo {
565
+ Some ( FontFaceInfo {
533
566
stretch : face. stretch ,
534
567
style : face. style ,
535
568
weight : face. weight ,
536
569
family_name : family_name. clone ( ) ,
537
- }
570
+ } )
538
571
}
539
572
540
573
/// Translates [`TextFont`] to [`Attrs`].
@@ -547,7 +580,7 @@ fn get_attrs<'a>(
547
580
) -> Attrs < ' a > {
548
581
Attrs :: new ( )
549
582
. metadata ( span_index)
550
- . family ( Family :: Name ( & face_info. family_name ) )
583
+ . family ( cosmic_text :: Family :: Name ( & face_info. family_name ) )
551
584
. stretch ( face_info. stretch )
552
585
. style ( face_info. style )
553
586
. weight ( face_info. weight )
0 commit comments