@@ -20,8 +20,13 @@ use crate::{
20
20
PositionedGlyph , TextBounds , TextSection , YAxisOrientation ,
21
21
} ;
22
22
23
- /// A wrapper around a [`cosmic_text::FontSystem`]
24
- struct CosmicFontSystem ( cosmic_text:: FontSystem ) ;
23
+ /// A wrapper resource around a [`cosmic_text::FontSystem`]
24
+ ///
25
+ /// The font system is used to retrieve fonts and their information, including glyph outlines.
26
+ ///
27
+ /// This resource is updated by the [`TextPipeline`] resource.
28
+ #[ derive( Resource ) ]
29
+ pub struct CosmicFontSystem ( pub cosmic_text:: FontSystem ) ;
25
30
26
31
impl Default for CosmicFontSystem {
27
32
fn default ( ) -> Self {
@@ -32,8 +37,13 @@ impl Default for CosmicFontSystem {
32
37
}
33
38
}
34
39
35
- /// A wrapper around a [`cosmic_text::SwashCache`]
36
- struct SwashCache ( cosmic_text:: SwashCache ) ;
40
+ /// A wrapper resource around a [`cosmic_text::SwashCache`]
41
+ ///
42
+ /// The swash cache rasterizer is used to rasterize glyphs
43
+ ///
44
+ /// This resource is updated by the [`TextPipeline`] resource.
45
+ #[ derive( Resource ) ]
46
+ pub struct SwashCache ( pub cosmic_text:: SwashCache ) ;
37
47
38
48
impl Default for SwashCache {
39
49
fn default ( ) -> Self {
@@ -48,14 +58,6 @@ impl Default for SwashCache {
48
58
pub struct TextPipeline {
49
59
/// Identifies a font [`ID`](cosmic_text::fontdb::ID) by its [`Font`] [`Asset`](bevy_asset::Asset).
50
60
map_handle_to_font_id : HashMap < AssetId < Font > , ( cosmic_text:: fontdb:: ID , String ) > ,
51
- /// The font system is used to retrieve fonts and their information, including glyph outlines.
52
- ///
53
- /// See [`cosmic_text::FontSystem`] for more information.
54
- font_system : CosmicFontSystem ,
55
- /// The swash cache rasterizer is used to rasterize glyphs
56
- ///
57
- /// See [`cosmic_text::SwashCache`] for more information.
58
- swash_cache : SwashCache ,
59
61
/// Buffered vec for collecting spans.
60
62
///
61
63
/// See [this dark magic](https://users.rust-lang.org/t/how-to-cache-a-vectors-capacity/94478/10).
@@ -76,8 +78,9 @@ impl TextPipeline {
76
78
scale_factor : f64 ,
77
79
buffer : & mut CosmicBuffer ,
78
80
alignment : JustifyText ,
81
+ font_system : & mut CosmicFontSystem ,
79
82
) -> Result < ( ) , TextError > {
80
- let font_system = & mut self . font_system . 0 ;
83
+ let font_system = & mut font_system. 0 ;
81
84
82
85
// return early if the fonts are not loaded yet
83
86
let mut font_size = 0. ;
@@ -188,6 +191,8 @@ impl TextPipeline {
188
191
textures : & mut Assets < Image > ,
189
192
y_axis_orientation : YAxisOrientation ,
190
193
buffer : & mut CosmicBuffer ,
194
+ font_system : & mut CosmicFontSystem ,
195
+ swash_cache : & mut SwashCache ,
191
196
) -> Result < ( ) , TextError > {
192
197
layout_info. glyphs . clear ( ) ;
193
198
layout_info. size = Default :: default ( ) ;
@@ -204,11 +209,10 @@ impl TextPipeline {
204
209
scale_factor,
205
210
buffer,
206
211
text_alignment,
212
+ font_system,
207
213
) ?;
208
214
209
215
let box_size = buffer_dimensions ( buffer) ;
210
- let font_system = & mut self . font_system . 0 ;
211
- let swash_cache = & mut self . swash_cache . 0 ;
212
216
213
217
buffer
214
218
. layout_runs ( )
@@ -250,8 +254,8 @@ impl TextPipeline {
250
254
font_atlas_set. add_glyph_to_atlas (
251
255
texture_atlases,
252
256
textures,
253
- font_system,
254
- swash_cache,
257
+ & mut font_system. 0 ,
258
+ & mut swash_cache. 0 ,
255
259
layout_glyph,
256
260
font_smoothing,
257
261
)
@@ -300,6 +304,7 @@ impl TextPipeline {
300
304
linebreak_behavior : BreakLineOn ,
301
305
buffer : & mut CosmicBuffer ,
302
306
text_alignment : JustifyText ,
307
+ font_system : & mut CosmicFontSystem ,
303
308
) -> Result < TextMeasureInfo , TextError > {
304
309
const MIN_WIDTH_CONTENT_BOUNDS : TextBounds = TextBounds :: new_horizontal ( 0.0 ) ;
305
310
@@ -311,12 +316,13 @@ impl TextPipeline {
311
316
scale_factor,
312
317
buffer,
313
318
text_alignment,
319
+ font_system,
314
320
) ?;
315
321
316
322
let min_width_content_size = buffer_dimensions ( buffer) ;
317
323
318
324
let max_width_content_size = {
319
- let font_system = & mut self . font_system . 0 ;
325
+ let font_system = & mut font_system. 0 ;
320
326
buffer. set_size ( font_system, None , None ) ;
321
327
buffer_dimensions ( buffer)
322
328
} ;
@@ -328,11 +334,12 @@ impl TextPipeline {
328
334
} )
329
335
}
330
336
331
- /// Get a mutable reference to the [`cosmic_text::FontSystem`].
332
- ///
333
- /// Used internally.
334
- pub fn font_system_mut ( & mut self ) -> & mut cosmic_text:: FontSystem {
335
- & mut self . font_system . 0
337
+ /// Returns the [`cosmic_text::fontdb::ID`] for a given [`Font`] asset.
338
+ pub fn get_font_id ( & self , asset_id : AssetId < Font > ) -> Option < cosmic_text:: fontdb:: ID > {
339
+ self . map_handle_to_font_id
340
+ . get ( & asset_id)
341
+ . cloned ( )
342
+ . map ( |( id, _) | id)
336
343
}
337
344
}
338
345
@@ -442,11 +449,11 @@ fn buffer_dimensions(buffer: &Buffer) -> Vec2 {
442
449
}
443
450
444
451
/// Discards stale data cached in `FontSystem`.
445
- pub ( crate ) fn trim_cosmic_cache ( mut pipeline : ResMut < TextPipeline > ) {
452
+ pub ( crate ) fn trim_cosmic_cache ( mut font_system : ResMut < CosmicFontSystem > ) {
446
453
// A trim age of 2 was found to reduce frame time variance vs age of 1 when tested with dynamic text.
447
454
// See https://github.com/bevyengine/bevy/pull/15037
448
455
//
449
456
// We assume only text updated frequently benefits from the shape cache (e.g. animated text, or
450
457
// text that is dynamically measured for UI).
451
- pipeline . font_system_mut ( ) . shape_run_cache . trim ( 2 ) ;
458
+ font_system . 0 . shape_run_cache . trim ( 2 ) ;
452
459
}
0 commit comments