@@ -394,7 +394,6 @@ impl ViewportRepaintInfo {
394
394
#[ derive( Default ) ]
395
395
struct ContextImpl {
396
396
fonts : Option < FontStore > ,
397
- font_definitions : FontDefinitions ,
398
397
399
398
memory : Memory ,
400
399
animation_manager : AnimationManager ,
@@ -552,33 +551,29 @@ impl ContextImpl {
552
551
fn update_fonts_mut ( & mut self ) {
553
552
profiling:: function_scope!( ) ;
554
553
let input = & self . viewport ( ) . input ;
555
- let pixels_per_point = input. pixels_per_point ( ) ;
556
554
let max_texture_side = input. max_texture_side ;
557
555
558
- if let Some ( font_definitions) = self . memory . new_font_definitions . take ( ) {
559
- // New font definition loaded, so we need to reload all fonts.
560
- self . fonts = None ;
561
- self . font_definitions = font_definitions;
562
- #[ cfg( feature = "log" ) ]
563
- log:: trace!( "Loading new font definitions" ) ;
564
- }
556
+ let mut new_font_definitions =
557
+ if let Some ( font_definitions) = self . memory . new_font_definitions . take ( ) {
558
+ Cow :: Owned ( font_definitions)
559
+ } else if let Some ( fonts) = & self . fonts {
560
+ Cow :: Borrowed ( fonts. definitions ( ) )
561
+ } else {
562
+ Cow :: Owned ( FontDefinitions :: default ( ) )
563
+ } ;
565
564
566
565
if !self . memory . add_fonts . is_empty ( ) {
567
566
let fonts = self . memory . add_fonts . drain ( ..) ;
567
+ let font_definitions = new_font_definitions. to_mut ( ) ;
568
568
for font in fonts {
569
- self . fonts = None ; // recreate all the fonts
570
569
for family in font. families {
571
- let fam = self
572
- . font_definitions
573
- . families
574
- . entry ( family. family )
575
- . or_default ( ) ;
570
+ let fam = font_definitions. families . entry ( family. family ) . or_default ( ) ;
576
571
match family. priority {
577
572
FontPriority :: Highest => fam. insert ( 0 , font. name . clone ( ) ) ,
578
573
FontPriority :: Lowest => fam. push ( font. name . clone ( ) ) ,
579
574
}
580
575
}
581
- self . font_definitions
576
+ font_definitions
582
577
. font_data
583
578
. insert ( font. name , Arc :: new ( font. data ) ) ;
584
579
}
@@ -587,28 +582,43 @@ impl ContextImpl {
587
582
log:: trace!( "Adding new fonts" ) ;
588
583
}
589
584
590
- let mut is_new = false ;
585
+ let mut did_change_fonts = false ;
591
586
592
- let fonts = self . fonts . get_or_insert_with ( || {
593
- #[ cfg( feature = "log" ) ]
594
- log:: trace!( "Creating new Fonts for pixels_per_point={pixels_per_point}" ) ;
587
+ // If we changed the font definitions this frame, or self.fonts previously did not exist, new_font_definitions
588
+ // must be Cow::Owned. Set the font definitions.
589
+ let fonts = if let Cow :: Owned ( new_font_definitions) = new_font_definitions {
590
+ did_change_fonts = true ;
591
+ if let Some ( fonts) = self . fonts . as_mut ( ) {
592
+ fonts. set_definitions ( new_font_definitions) ;
593
+ did_change_fonts = true ;
594
+ fonts
595
+ } else {
596
+ self . fonts . get_or_insert_with ( || {
597
+ #[ cfg( feature = "log" ) ]
598
+ log:: trace!( "Creating new FontStore" ) ;
595
599
596
- is_new = true ;
597
- profiling:: scope!( "FontStore::new" ) ;
598
- FontStore :: new ( max_texture_side, self . font_definitions . clone ( ) )
599
- } ) ;
600
+ did_change_fonts = true ;
601
+ profiling:: scope!( "FontStore::new" ) ;
602
+ FontStore :: new ( max_texture_side, new_font_definitions)
603
+ } )
604
+ }
605
+ } else {
606
+ self . fonts
607
+ . as_mut ( )
608
+ . expect ( "new_font_definitions is borrowed, but we had nowhere to borrow it from" )
609
+ } ;
600
610
601
611
{
602
612
profiling:: scope!( "FontStore::begin_pass" ) ;
603
613
fonts. begin_pass ( max_texture_side) ;
604
614
}
605
615
606
- if is_new && self . memory . options . preload_font_glyphs {
616
+ if did_change_fonts && self . memory . options . preload_font_glyphs {
607
617
profiling:: scope!( "preload_font_glyphs" ) ;
608
618
// Preload the most common characters for the most common fonts.
609
619
// This is not very important to do, but may save a few GPU operations.
610
- for font_id in self . memory . options . style ( ) . text_styles . values ( ) {
611
- fonts. font ( font_id ) . preload_common_characters ( ) ;
620
+ for font_style in self . memory . options . style ( ) . text_styles . values ( ) {
621
+ fonts. preload_common_characters ( font_style ) ;
612
622
}
613
623
}
614
624
}
@@ -1495,11 +1505,10 @@ impl Context {
1495
1505
1496
1506
let font_id = TextStyle :: Body . resolve ( & self . style ( ) ) ;
1497
1507
self . fonts ( |f| {
1498
- let font = f. font ( & font_id) ;
1499
- font. has_glyphs ( alt)
1500
- && font. has_glyphs ( ctrl)
1501
- && font. has_glyphs ( shift)
1502
- && font. has_glyphs ( mac_cmd)
1508
+ f. has_glyphs_for ( & font_id, alt)
1509
+ && f. has_glyphs_for ( & font_id, ctrl)
1510
+ && f. has_glyphs_for ( & font_id, shift)
1511
+ && f. has_glyphs_for ( & font_id, mac_cmd)
1503
1512
} )
1504
1513
} ;
1505
1514
@@ -2909,7 +2918,7 @@ impl Context {
2909
2918
}
2910
2919
2911
2920
fn fonts_tweak_ui ( & self , ui : & mut Ui ) {
2912
- let mut font_definitions = self . write ( |ctx| ctx . font_definitions . clone ( ) ) ;
2921
+ let mut font_definitions = self . fonts ( |fonts| fonts . definitions ( ) . clone ( ) ) ;
2913
2922
let mut changed = false ;
2914
2923
2915
2924
for ( name, data) in & mut font_definitions. font_data {
0 commit comments