1
1
use alloc:: sync:: Arc ;
2
2
3
3
use bevy_asset:: { AssetId , Assets } ;
4
+ use bevy_color:: Color ;
4
5
use bevy_ecs:: {
5
6
component:: Component ,
6
7
entity:: Entity ,
@@ -17,7 +18,7 @@ use cosmic_text::{Attrs, Buffer, Family, Metrics, Shaping, Wrap};
17
18
18
19
use crate :: {
19
20
error:: TextError , ComputedTextBlock , Font , FontAtlasSets , FontSmoothing , JustifyText ,
20
- LineBreak , PositionedGlyph , TextBounds , TextEntity , TextLayout , TextStyle , YAxisOrientation ,
21
+ LineBreak , PositionedGlyph , TextBounds , TextEntity , TextFont , TextLayout , YAxisOrientation ,
21
22
} ;
22
23
23
24
/// A wrapper resource around a [`cosmic_text::FontSystem`]
@@ -70,7 +71,7 @@ pub struct TextPipeline {
70
71
/// Buffered vec for collecting spans.
71
72
///
72
73
/// See [this dark magic](https://users.rust-lang.org/t/how-to-cache-a-vectors-capacity/94478/10).
73
- spans_buffer : Vec < ( usize , & ' static str , & ' static TextStyle , FontFaceInfo ) > ,
74
+ spans_buffer : Vec < ( usize , & ' static str , & ' static TextFont , FontFaceInfo ) > ,
74
75
/// Buffered vec for collecting info for glyph assembly.
75
76
glyph_info : Vec < ( AssetId < Font > , FontSmoothing ) > ,
76
77
}
@@ -83,7 +84,7 @@ impl TextPipeline {
83
84
pub fn update_buffer < ' a > (
84
85
& mut self ,
85
86
fonts : & Assets < Font > ,
86
- text_spans : impl Iterator < Item = ( Entity , usize , & ' a str , & ' a TextStyle ) > ,
87
+ text_spans : impl Iterator < Item = ( Entity , usize , & ' a str , & ' a TextFont , Color ) > ,
87
88
linebreak : LineBreak ,
88
89
justify : JustifyText ,
89
90
bounds : TextBounds ,
@@ -96,22 +97,22 @@ impl TextPipeline {
96
97
// Collect span information into a vec. This is necessary because font loading requires mut access
97
98
// to FontSystem, which the cosmic-text Buffer also needs.
98
99
let mut font_size: f32 = 0. ;
99
- let mut spans: Vec < ( usize , & str , & TextStyle , FontFaceInfo ) > =
100
+ let mut spans: Vec < ( usize , & str , & TextFont , FontFaceInfo , Color ) > =
100
101
core:: mem:: take ( & mut self . spans_buffer )
101
102
. into_iter ( )
102
- . map ( |_| -> ( usize , & str , & TextStyle , FontFaceInfo ) { unreachable ! ( ) } )
103
+ . map ( |_| -> ( usize , & str , & TextFont , FontFaceInfo , Color ) { unreachable ! ( ) } )
103
104
. collect ( ) ;
104
105
105
106
computed. entities . clear ( ) ;
106
107
107
- for ( span_index, ( entity, depth, span, style ) ) in text_spans. enumerate ( ) {
108
+ for ( span_index, ( entity, depth, span, text_font , color ) ) in text_spans. enumerate ( ) {
108
109
// Return early if a font is not loaded yet.
109
- if !fonts. contains ( style . font . id ( ) ) {
110
+ if !fonts. contains ( text_font . font . id ( ) ) {
110
111
spans. clear ( ) ;
111
112
self . spans_buffer = spans
112
113
. into_iter ( )
113
114
. map (
114
- |_| -> ( usize , & ' static str , & ' static TextStyle , FontFaceInfo ) {
115
+ |_| -> ( usize , & ' static str , & ' static TextFont , FontFaceInfo ) {
115
116
unreachable ! ( )
116
117
} ,
117
118
)
@@ -124,17 +125,21 @@ impl TextPipeline {
124
125
computed. entities . push ( TextEntity { entity, depth } ) ;
125
126
126
127
// Get max font size for use in cosmic Metrics.
127
- font_size = font_size. max ( style . font_size ) ;
128
+ font_size = font_size. max ( text_font . font_size ) ;
128
129
129
130
// Load Bevy fonts into cosmic-text's font system.
130
- let face_info =
131
- load_font_to_fontdb ( style, font_system, & mut self . map_handle_to_font_id , fonts) ;
131
+ let face_info = load_font_to_fontdb (
132
+ text_font,
133
+ font_system,
134
+ & mut self . map_handle_to_font_id ,
135
+ fonts,
136
+ ) ;
132
137
133
138
// Save spans that aren't zero-sized.
134
- if scale_factor <= 0.0 || style . font_size <= 0.0 {
139
+ if scale_factor <= 0.0 || text_font . font_size <= 0.0 {
135
140
continue ;
136
141
}
137
- spans. push ( ( span_index, span, style , face_info) ) ;
142
+ spans. push ( ( span_index, span, text_font , face_info, color ) ) ;
138
143
}
139
144
140
145
let line_height = font_size * 1.2 ;
@@ -151,12 +156,14 @@ impl TextPipeline {
151
156
// The section index is stored in the metadata of the spans, and could be used
152
157
// to look up the section the span came from and is not used internally
153
158
// in cosmic-text.
154
- let spans_iter = spans. iter ( ) . map ( |( span_index, span, style, font_info) | {
155
- (
156
- * span,
157
- get_attrs ( * span_index, style, font_info, scale_factor) ,
158
- )
159
- } ) ;
159
+ let spans_iter = spans
160
+ . iter ( )
161
+ . map ( |( span_index, span, text_font, font_info, color) | {
162
+ (
163
+ * span,
164
+ get_attrs ( * span_index, text_font, * color, font_info, scale_factor) ,
165
+ )
166
+ } ) ;
160
167
161
168
// Update the buffer.
162
169
let buffer = & mut computed. buffer ;
@@ -186,7 +193,7 @@ impl TextPipeline {
186
193
spans. clear ( ) ;
187
194
self . spans_buffer = spans
188
195
. into_iter ( )
189
- . map ( |_| -> ( usize , & ' static str , & ' static TextStyle , FontFaceInfo ) { unreachable ! ( ) } )
196
+ . map ( |_| -> ( usize , & ' static str , & ' static TextFont , FontFaceInfo ) { unreachable ! ( ) } )
190
197
. collect ( ) ;
191
198
192
199
Ok ( ( ) )
@@ -201,7 +208,7 @@ impl TextPipeline {
201
208
& mut self ,
202
209
layout_info : & mut TextLayoutInfo ,
203
210
fonts : & Assets < Font > ,
204
- text_spans : impl Iterator < Item = ( Entity , usize , & ' a str , & ' a TextStyle ) > ,
211
+ text_spans : impl Iterator < Item = ( Entity , usize , & ' a str , & ' a TextFont , Color ) > ,
205
212
scale_factor : f64 ,
206
213
layout : & TextLayout ,
207
214
bounds : TextBounds ,
@@ -222,8 +229,8 @@ impl TextPipeline {
222
229
// Extract font ids from the iterator while traversing it.
223
230
let mut glyph_info = core:: mem:: take ( & mut self . glyph_info ) ;
224
231
glyph_info. clear ( ) ;
225
- let text_spans = text_spans. inspect ( |( _, _, _, style ) | {
226
- glyph_info. push ( ( style . font . id ( ) , style . font_smoothing ) ) ;
232
+ let text_spans = text_spans. inspect ( |( _, _, _, text_font , _ ) | {
233
+ glyph_info. push ( ( text_font . font . id ( ) , text_font . font_smoothing ) ) ;
227
234
} ) ;
228
235
229
236
let update_result = self . update_buffer (
@@ -335,7 +342,7 @@ impl TextPipeline {
335
342
& mut self ,
336
343
entity : Entity ,
337
344
fonts : & Assets < Font > ,
338
- text_spans : impl Iterator < Item = ( Entity , usize , & ' a str , & ' a TextStyle ) > ,
345
+ text_spans : impl Iterator < Item = ( Entity , usize , & ' a str , & ' a TextFont , Color ) > ,
339
346
scale_factor : f64 ,
340
347
layout : & TextLayout ,
341
348
computed : & mut ComputedTextBlock ,
@@ -427,12 +434,12 @@ impl TextMeasureInfo {
427
434
}
428
435
429
436
fn load_font_to_fontdb (
430
- style : & TextStyle ,
437
+ text_font : & TextFont ,
431
438
font_system : & mut cosmic_text:: FontSystem ,
432
439
map_handle_to_font_id : & mut HashMap < AssetId < Font > , ( cosmic_text:: fontdb:: ID , Arc < str > ) > ,
433
440
fonts : & Assets < Font > ,
434
441
) -> FontFaceInfo {
435
- let font_handle = style . font . clone ( ) ;
442
+ let font_handle = text_font . font . clone ( ) ;
436
443
let ( face_id, family_name) = map_handle_to_font_id
437
444
. entry ( font_handle. id ( ) )
438
445
. or_insert_with ( || {
@@ -461,10 +468,11 @@ fn load_font_to_fontdb(
461
468
}
462
469
}
463
470
464
- /// Translates [`TextStyle `] to [`Attrs`].
471
+ /// Translates [`TextFont `] to [`Attrs`].
465
472
fn get_attrs < ' a > (
466
473
span_index : usize ,
467
- style : & TextStyle ,
474
+ text_font : & TextFont ,
475
+ color : Color ,
468
476
face_info : & ' a FontFaceInfo ,
469
477
scale_factor : f64 ,
470
478
) -> Attrs < ' a > {
@@ -474,8 +482,8 @@ fn get_attrs<'a>(
474
482
. stretch ( face_info. stretch )
475
483
. style ( face_info. style )
476
484
. weight ( face_info. weight )
477
- . metrics ( Metrics :: relative ( style . font_size , 1.2 ) . scale ( scale_factor as f32 ) )
478
- . color ( cosmic_text:: Color ( style . color . to_linear ( ) . as_u32 ( ) ) ) ;
485
+ . metrics ( Metrics :: relative ( text_font . font_size , 1.2 ) . scale ( scale_factor as f32 ) )
486
+ . color ( cosmic_text:: Color ( color. to_linear ( ) . as_u32 ( ) ) ) ;
479
487
attrs
480
488
}
481
489
0 commit comments