1- use std:: hash:: Hash ;
2-
31use ab_glyph:: { PxScale , ScaleFont } ;
42use bevy_asset:: { Assets , Handle , HandleId } ;
3+ use bevy_ecs:: component:: Component ;
54use bevy_ecs:: system:: Resource ;
65use bevy_math:: Vec2 ;
76use bevy_render:: texture:: Image ;
@@ -15,29 +14,22 @@ use crate::{
1514 TextAlignment , TextSection ,
1615} ;
1716
18- #[ derive( Resource ) ]
19- pub struct TextPipeline < ID > {
17+ #[ derive( Default , Resource ) ]
18+ pub struct TextPipeline {
2019 brush : GlyphBrush ,
21- glyph_map : HashMap < ID , TextLayoutInfo > ,
2220 map_font_id : HashMap < HandleId , FontId > ,
2321}
2422
25- impl < ID > Default for TextPipeline < ID > {
26- fn default ( ) -> Self {
27- TextPipeline {
28- brush : GlyphBrush :: default ( ) ,
29- glyph_map : Default :: default ( ) ,
30- map_font_id : Default :: default ( ) ,
31- }
32- }
33- }
34-
23+ /// Render information for a corresponding [`Text`](crate::Text) component.
24+ ///
25+ /// Contains scaled glyphs and their size. Generated via [`TextPipeline::queue_text`].
26+ #[ derive( Component , Clone , Default , Debug ) ]
3527pub struct TextLayoutInfo {
3628 pub glyphs : Vec < PositionedGlyph > ,
3729 pub size : Vec2 ,
3830}
3931
40- impl < ID : Hash + Eq > TextPipeline < ID > {
32+ impl TextPipeline {
4133 pub fn get_or_insert_font_id ( & mut self , handle : & Handle < Font > , font : & Font ) -> FontId {
4234 let brush = & mut self . brush ;
4335 * self
@@ -46,14 +38,9 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
4638 . or_insert_with ( || brush. add_font ( handle. clone ( ) , font. font . clone ( ) ) )
4739 }
4840
49- pub fn get_glyphs ( & self , id : & ID ) -> Option < & TextLayoutInfo > {
50- self . glyph_map . get ( id)
51- }
52-
5341 #[ allow( clippy:: too_many_arguments) ]
5442 pub fn queue_text (
5543 & mut self ,
56- id : ID ,
5744 fonts : & Assets < Font > ,
5845 sections : & [ TextSection ] ,
5946 scale_factor : f64 ,
@@ -62,7 +49,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
6249 font_atlas_set_storage : & mut Assets < FontAtlasSet > ,
6350 texture_atlases : & mut Assets < TextureAtlas > ,
6451 textures : & mut Assets < Image > ,
65- ) -> Result < ( ) , TextError > {
52+ ) -> Result < TextLayoutInfo , TextError > {
6653 let mut scaled_fonts = Vec :: new ( ) ;
6754 let sections = sections
6855 . iter ( )
@@ -90,14 +77,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
9077 . compute_glyphs ( & sections, bounds, text_alignment) ?;
9178
9279 if section_glyphs. is_empty ( ) {
93- self . glyph_map . insert (
94- id,
95- TextLayoutInfo {
96- glyphs : Vec :: new ( ) ,
97- size : Vec2 :: ZERO ,
98- } ,
99- ) ;
100- return Ok ( ( ) ) ;
80+ return Ok ( TextLayoutInfo :: default ( ) ) ;
10181 }
10282
10383 let mut min_x: f32 = std:: f32:: MAX ;
@@ -125,8 +105,6 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
125105 textures,
126106 ) ?;
127107
128- self . glyph_map . insert ( id, TextLayoutInfo { glyphs, size } ) ;
129-
130- Ok ( ( ) )
108+ Ok ( TextLayoutInfo { glyphs, size } )
131109 }
132110}
0 commit comments