Skip to content

Commit 3fc7713

Browse files
authored
Merge branch 'main' into query_single
2 parents 801d37c + df23b93 commit 3fc7713

File tree

7 files changed

+75
-38
lines changed

7 files changed

+75
-38
lines changed

.github/ISSUE_TEMPLATE/docs_improvement.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ assignees: ''
1010

1111
Provide a link to the documentation and describe how it could be improved. In what ways is it incomplete, incorrect, or misleading?
1212

13-
If you have suggestions on exactly what the new docs should say, feel free to include them here. Or alternatively, make the changes yourself and [create a pull request](https://bevyengine.org/learn/book/contributing/code/) instead.
13+
If you have suggestions on exactly what the new docs should say, feel free to include them here. Alternatively, make the changes yourself and [create a pull request](https://bevyengine.org/learn/book/contributing/code/) instead.

.github/ISSUE_TEMPLATE/feature_request.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name: Feature Request
33
about: Propose a new feature!
44
title: ''
5-
labels: C-Enhancement, S-Needs-Triage
5+
labels: C-Feature, S-Needs-Triage
66
assignees: ''
77
---
88

crates/bevy_text/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ impl Plugin for TextPlugin {
109109
.register_type::<TextBounds>()
110110
.init_asset_loader::<FontLoader>()
111111
.init_resource::<FontAtlasSets>()
112-
.insert_resource(TextPipeline::default())
112+
.init_resource::<TextPipeline>()
113+
.init_resource::<CosmicFontSystem>()
114+
.init_resource::<SwashCache>()
113115
.add_systems(
114116
PostUpdate,
115117
(

crates/bevy_text/src/pipeline.rs

+32-25
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ use crate::{
2020
PositionedGlyph, TextBounds, TextSection, YAxisOrientation,
2121
};
2222

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);
2530

2631
impl Default for CosmicFontSystem {
2732
fn default() -> Self {
@@ -32,8 +37,13 @@ impl Default for CosmicFontSystem {
3237
}
3338
}
3439

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);
3747

3848
impl Default for SwashCache {
3949
fn default() -> Self {
@@ -48,14 +58,6 @@ impl Default for SwashCache {
4858
pub struct TextPipeline {
4959
/// Identifies a font [`ID`](cosmic_text::fontdb::ID) by its [`Font`] [`Asset`](bevy_asset::Asset).
5060
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,
5961
/// Buffered vec for collecting spans.
6062
///
6163
/// See [this dark magic](https://users.rust-lang.org/t/how-to-cache-a-vectors-capacity/94478/10).
@@ -76,8 +78,9 @@ impl TextPipeline {
7678
scale_factor: f64,
7779
buffer: &mut CosmicBuffer,
7880
alignment: JustifyText,
81+
font_system: &mut CosmicFontSystem,
7982
) -> Result<(), TextError> {
80-
let font_system = &mut self.font_system.0;
83+
let font_system = &mut font_system.0;
8184

8285
// return early if the fonts are not loaded yet
8386
let mut font_size = 0.;
@@ -188,6 +191,8 @@ impl TextPipeline {
188191
textures: &mut Assets<Image>,
189192
y_axis_orientation: YAxisOrientation,
190193
buffer: &mut CosmicBuffer,
194+
font_system: &mut CosmicFontSystem,
195+
swash_cache: &mut SwashCache,
191196
) -> Result<(), TextError> {
192197
layout_info.glyphs.clear();
193198
layout_info.size = Default::default();
@@ -204,11 +209,10 @@ impl TextPipeline {
204209
scale_factor,
205210
buffer,
206211
text_alignment,
212+
font_system,
207213
)?;
208214

209215
let box_size = buffer_dimensions(buffer);
210-
let font_system = &mut self.font_system.0;
211-
let swash_cache = &mut self.swash_cache.0;
212216

213217
buffer
214218
.layout_runs()
@@ -250,8 +254,8 @@ impl TextPipeline {
250254
font_atlas_set.add_glyph_to_atlas(
251255
texture_atlases,
252256
textures,
253-
font_system,
254-
swash_cache,
257+
&mut font_system.0,
258+
&mut swash_cache.0,
255259
layout_glyph,
256260
font_smoothing,
257261
)
@@ -300,6 +304,7 @@ impl TextPipeline {
300304
linebreak_behavior: BreakLineOn,
301305
buffer: &mut CosmicBuffer,
302306
text_alignment: JustifyText,
307+
font_system: &mut CosmicFontSystem,
303308
) -> Result<TextMeasureInfo, TextError> {
304309
const MIN_WIDTH_CONTENT_BOUNDS: TextBounds = TextBounds::new_horizontal(0.0);
305310

@@ -311,12 +316,13 @@ impl TextPipeline {
311316
scale_factor,
312317
buffer,
313318
text_alignment,
319+
font_system,
314320
)?;
315321

316322
let min_width_content_size = buffer_dimensions(buffer);
317323

318324
let max_width_content_size = {
319-
let font_system = &mut self.font_system.0;
325+
let font_system = &mut font_system.0;
320326
buffer.set_size(font_system, None, None);
321327
buffer_dimensions(buffer)
322328
};
@@ -328,11 +334,12 @@ impl TextPipeline {
328334
})
329335
}
330336

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)
336343
}
337344
}
338345

@@ -442,11 +449,11 @@ fn buffer_dimensions(buffer: &Buffer) -> Vec2 {
442449
}
443450

444451
/// 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>) {
446453
// A trim age of 2 was found to reduce frame time variance vs age of 1 when tested with dynamic text.
447454
// See https://github.com/bevyengine/bevy/pull/15037
448455
//
449456
// We assume only text updated frequently benefits from the shape cache (e.g. animated text, or
450457
// 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);
452459
}

crates/bevy_text/src/text2d.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use crate::pipeline::CosmicFontSystem;
12
use crate::{
2-
BreakLineOn, CosmicBuffer, Font, FontAtlasSets, PositionedGlyph, Text, TextBounds, TextError,
3-
TextLayoutInfo, TextPipeline, YAxisOrientation,
3+
BreakLineOn, CosmicBuffer, Font, FontAtlasSets, PositionedGlyph, SwashCache, Text, TextBounds,
4+
TextError, TextLayoutInfo, TextPipeline, YAxisOrientation,
45
};
56
use bevy_asset::Assets;
67
use bevy_color::LinearRgba;
@@ -158,6 +159,8 @@ pub fn update_text2d_layout(
158159
&mut TextLayoutInfo,
159160
&mut CosmicBuffer,
160161
)>,
162+
mut font_system: ResMut<CosmicFontSystem>,
163+
mut swash_cache: ResMut<SwashCache>,
161164
) {
162165
// We need to consume the entire iterator, hence `last`
163166
let factor_changed = scale_factor_changed.read().last().is_some();
@@ -198,6 +201,8 @@ pub fn update_text2d_layout(
198201
&mut textures,
199202
YAxisOrientation::BottomToTop,
200203
buffer.as_mut(),
204+
&mut font_system,
205+
&mut swash_cache,
201206
) {
202207
Err(TextError::NoSuchFont) => {
203208
// There was an error processing the text layout, let's add this entity to the
@@ -274,7 +279,9 @@ mod tests {
274279
.init_resource::<Assets<TextureAtlasLayout>>()
275280
.init_resource::<FontAtlasSets>()
276281
.init_resource::<Events<WindowScaleFactorChanged>>()
277-
.insert_resource(TextPipeline::default())
282+
.init_resource::<TextPipeline>()
283+
.init_resource::<CosmicFontSystem>()
284+
.init_resource::<SwashCache>()
278285
.add_systems(
279286
Update,
280287
(

crates/bevy_ui/src/layout/mod.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use thiserror::Error;
2222
use ui_surface::UiSurface;
2323

2424
#[cfg(feature = "bevy_text")]
25-
use bevy_text::{CosmicBuffer, TextPipeline};
25+
use bevy_text::CosmicBuffer;
26+
#[cfg(feature = "bevy_text")]
27+
use bevy_text::CosmicFontSystem;
2628

2729
mod convert;
2830
pub mod debug;
@@ -124,7 +126,7 @@ pub fn ui_layout_system(
124126
Option<&ScrollPosition>,
125127
)>,
126128
#[cfg(feature = "bevy_text")] mut buffer_query: Query<&mut CosmicBuffer>,
127-
#[cfg(feature = "bevy_text")] mut text_pipeline: ResMut<TextPipeline>,
129+
#[cfg(feature = "bevy_text")] mut font_system: ResMut<CosmicFontSystem>,
128130
) {
129131
let UiLayoutSystemBuffers {
130132
interned_root_nodes,
@@ -250,8 +252,6 @@ pub fn ui_layout_system(
250252

251253
#[cfg(feature = "bevy_text")]
252254
let text_buffers = &mut buffer_query;
253-
#[cfg(feature = "bevy_text")]
254-
let font_system = text_pipeline.font_system_mut();
255255
// clean up removed nodes after syncing children to avoid potential panic (invalid SlotMap key used)
256256
ui_surface.remove_entities(removed_components.removed_nodes.read());
257257

@@ -271,7 +271,7 @@ pub fn ui_layout_system(
271271
#[cfg(feature = "bevy_text")]
272272
text_buffers,
273273
#[cfg(feature = "bevy_text")]
274-
font_system,
274+
&mut font_system.0,
275275
);
276276

277277
for root in &camera.root_nodes {
@@ -523,6 +523,10 @@ mod tests {
523523
world.init_resource::<ManualTextureViews>();
524524
#[cfg(feature = "bevy_text")]
525525
world.init_resource::<bevy_text::TextPipeline>();
526+
#[cfg(feature = "bevy_text")]
527+
world.init_resource::<bevy_text::CosmicFontSystem>();
528+
#[cfg(feature = "bevy_text")]
529+
world.init_resource::<bevy_text::SwashCache>();
526530

527531
// spawn a dummy primary window and camera
528532
world.spawn((
@@ -1160,6 +1164,10 @@ mod tests {
11601164
world.init_resource::<ManualTextureViews>();
11611165
#[cfg(feature = "bevy_text")]
11621166
world.init_resource::<bevy_text::TextPipeline>();
1167+
#[cfg(feature = "bevy_text")]
1168+
world.init_resource::<bevy_text::CosmicFontSystem>();
1169+
#[cfg(feature = "bevy_text")]
1170+
world.init_resource::<bevy_text::SwashCache>();
11631171

11641172
// spawn a dummy primary window and camera
11651173
world.spawn((

crates/bevy_ui/src/widget/text.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};
1616
use bevy_render::{camera::Camera, texture::Image};
1717
use bevy_sprite::TextureAtlasLayout;
1818
use bevy_text::{
19-
scale_value, BreakLineOn, CosmicBuffer, Font, FontAtlasSets, JustifyText, Text, TextBounds,
20-
TextError, TextLayoutInfo, TextMeasureInfo, TextPipeline, YAxisOrientation,
19+
scale_value, BreakLineOn, CosmicBuffer, CosmicFontSystem, Font, FontAtlasSets, JustifyText,
20+
SwashCache, Text, TextBounds, TextError, TextLayoutInfo, TextMeasureInfo, TextPipeline,
21+
YAxisOrientation,
2122
};
2223
use bevy_utils::{tracing::error, Entry};
2324
use taffy::style::AvailableSpace;
@@ -112,6 +113,7 @@ fn create_text_measure(
112113
mut text_flags: Mut<TextFlags>,
113114
buffer: &mut CosmicBuffer,
114115
text_alignment: JustifyText,
116+
font_system: &mut CosmicFontSystem,
115117
) {
116118
match text_pipeline.create_text_measure(
117119
entity,
@@ -121,6 +123,7 @@ fn create_text_measure(
121123
text.linebreak_behavior,
122124
buffer,
123125
text_alignment,
126+
font_system,
124127
) {
125128
Ok(measure) => {
126129
if text.linebreak_behavior == BreakLineOn::NoWrap {
@@ -173,6 +176,7 @@ pub fn measure_text_system(
173176
With<Node>,
174177
>,
175178
mut text_pipeline: ResMut<TextPipeline>,
179+
mut font_system: ResMut<CosmicFontSystem>,
176180
) {
177181
scale_factors_buffer.clear();
178182

@@ -208,6 +212,7 @@ pub fn measure_text_system(
208212
text_flags,
209213
buffer.as_mut(),
210214
text_alignment,
215+
&mut font_system,
211216
);
212217
}
213218
}
@@ -229,6 +234,8 @@ fn queue_text(
229234
mut text_flags: Mut<TextFlags>,
230235
text_layout_info: Mut<TextLayoutInfo>,
231236
buffer: &mut CosmicBuffer,
237+
font_system: &mut CosmicFontSystem,
238+
swash_cache: &mut SwashCache,
232239
) {
233240
// Skip the text node if it is waiting for a new measure func
234241
if !text_flags.needs_new_measure_func {
@@ -258,6 +265,8 @@ fn queue_text(
258265
textures,
259266
YAxisOrientation::TopToBottom,
260267
buffer,
268+
font_system,
269+
swash_cache,
261270
) {
262271
Err(TextError::NoSuchFont) => {
263272
// There was an error processing the text layout, try again next frame
@@ -305,6 +314,8 @@ pub fn text_system(
305314
Option<&TargetCamera>,
306315
&mut CosmicBuffer,
307316
)>,
317+
mut font_system: ResMut<CosmicFontSystem>,
318+
mut swash_cache: ResMut<SwashCache>,
308319
) {
309320
scale_factors_buffer.clear();
310321

@@ -343,6 +354,8 @@ pub fn text_system(
343354
text_flags,
344355
text_layout_info,
345356
buffer.as_mut(),
357+
&mut font_system,
358+
&mut swash_cache,
346359
);
347360
}
348361
}

0 commit comments

Comments
 (0)