make TextLayoutInfo a Component (#4460)

# Objective

Make `TextLayoutInfo` more accessible as a component, rather than internal to `TextPipeline`. I am working on a plugin that manipulates these and there is no (mutable) access to them right now.

## Solution

This changes `TextPipeline::queue_text` to return `TextLayoutInfo`'s rather than storing them in a map internally. `text2d_system` and `text_system` now take the returned `TextLayoutInfo` and store it as a component of the entity. I considered adding an accessor to `TextPipeline` (e.g. `get_glyphs_mut`) but this seems like it might be a little faster, and also has the added benefit of cleaning itself up when entities are removed. Right now nothing is ever removed from the glyphs map.

## Changelog

Removed `DefaultTextPipeline`. `TextPipeline` no longer has a generic key type. `TextPipeline::queue_text` returns `TextLayoutInfo` directly.

## Migration Guide

This might break a third-party crate? I could restore the orginal TextPipeline API as a wrapper around what's in this PR.
This commit is contained in:
Al M 2022-09-06 20:03:40 +00:00
parent 1914696a24
commit bd68ba1c3c
5 changed files with 137 additions and 149 deletions

View file

@ -28,13 +28,11 @@ pub mod prelude {
use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::{entity::Entity, schedule::ParallelSystemDescriptorCoercion};
use bevy_ecs::schedule::ParallelSystemDescriptorCoercion;
use bevy_render::{RenderApp, RenderStage};
use bevy_sprite::SpriteSystem;
use bevy_window::ModifiesWindows;
pub type DefaultTextPipeline = TextPipeline<Entity>;
#[derive(Default)]
pub struct TextPlugin;
@ -48,7 +46,7 @@ impl Plugin for TextPlugin {
.register_type::<VerticalAlign>()
.register_type::<HorizontalAlign>()
.init_asset_loader::<FontLoader>()
.insert_resource(DefaultTextPipeline::default())
.insert_resource(TextPipeline::default())
.add_system_to_stage(
CoreStage::PostUpdate,
update_text2d_layout.after(ModifiesWindows),

View file

@ -1,7 +1,6 @@
use std::hash::Hash;
use ab_glyph::{PxScale, ScaleFont};
use bevy_asset::{Assets, Handle, HandleId};
use bevy_ecs::component::Component;
use bevy_ecs::system::Resource;
use bevy_math::Vec2;
use bevy_render::texture::Image;
@ -15,29 +14,22 @@ use crate::{
TextAlignment, TextSection,
};
#[derive(Resource)]
pub struct TextPipeline<ID> {
#[derive(Default, Resource)]
pub struct TextPipeline {
brush: GlyphBrush,
glyph_map: HashMap<ID, TextLayoutInfo>,
map_font_id: HashMap<HandleId, FontId>,
}
impl<ID> Default for TextPipeline<ID> {
fn default() -> Self {
TextPipeline {
brush: GlyphBrush::default(),
glyph_map: Default::default(),
map_font_id: Default::default(),
}
}
}
/// Render information for a corresponding [`Text`](crate::Text) component.
///
/// Contains scaled glyphs and their size. Generated via [`TextPipeline::queue_text`].
#[derive(Component, Clone, Default, Debug)]
pub struct TextLayoutInfo {
pub glyphs: Vec<PositionedGlyph>,
pub size: Vec2,
}
impl<ID: Hash + Eq> TextPipeline<ID> {
impl TextPipeline {
pub fn get_or_insert_font_id(&mut self, handle: &Handle<Font>, font: &Font) -> FontId {
let brush = &mut self.brush;
*self
@ -46,14 +38,9 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
.or_insert_with(|| brush.add_font(handle.clone(), font.font.clone()))
}
pub fn get_glyphs(&self, id: &ID) -> Option<&TextLayoutInfo> {
self.glyph_map.get(id)
}
#[allow(clippy::too_many_arguments)]
pub fn queue_text(
&mut self,
id: ID,
fonts: &Assets<Font>,
sections: &[TextSection],
scale_factor: f64,
@ -62,7 +49,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
texture_atlases: &mut Assets<TextureAtlas>,
textures: &mut Assets<Image>,
) -> Result<(), TextError> {
) -> Result<TextLayoutInfo, TextError> {
let mut scaled_fonts = Vec::new();
let sections = sections
.iter()
@ -90,14 +77,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
.compute_glyphs(&sections, bounds, text_alignment)?;
if section_glyphs.is_empty() {
self.glyph_map.insert(
id,
TextLayoutInfo {
glyphs: Vec::new(),
size: Vec2::ZERO,
},
);
return Ok(());
return Ok(TextLayoutInfo::default());
}
let mut min_x: f32 = std::f32::MAX;
@ -125,8 +105,6 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
textures,
)?;
self.glyph_map.insert(id, TextLayoutInfo { glyphs, size });
Ok(())
Ok(TextLayoutInfo { glyphs, size })
}
}

View file

@ -6,7 +6,7 @@ use bevy_ecs::{
event::EventReader,
query::Changed,
reflect::ReflectComponent,
system::{Local, Query, Res, ResMut},
system::{Commands, Local, Query, Res, ResMut},
};
use bevy_math::{Vec2, Vec3};
use bevy_reflect::Reflect;
@ -22,7 +22,8 @@ use bevy_utils::HashSet;
use bevy_window::{WindowId, WindowScaleFactorChanged, Windows};
use crate::{
DefaultTextPipeline, Font, FontAtlasSet, HorizontalAlign, Text, TextError, VerticalAlign,
Font, FontAtlasSet, HorizontalAlign, Text, TextError, TextLayoutInfo, TextPipeline,
VerticalAlign,
};
/// The calculated size of text drawn in 2D scene.
@ -69,13 +70,13 @@ pub struct Text2dBundle {
pub fn extract_text2d_sprite(
mut extracted_sprites: ResMut<ExtractedSprites>,
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
text_pipeline: Extract<Res<DefaultTextPipeline>>,
windows: Extract<Res<Windows>>,
text2d_query: Extract<
Query<(
Entity,
&ComputedVisibility,
&Text,
&TextLayoutInfo,
&GlobalTransform,
&Text2dSize,
)>,
@ -83,15 +84,15 @@ pub fn extract_text2d_sprite(
) {
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
for (entity, computed_visibility, text, text_transform, calculated_size) in text2d_query.iter()
for (entity, computed_visibility, text, text_layout_info, text_transform, calculated_size) in
text2d_query.iter()
{
if !computed_visibility.is_visible() {
continue;
}
let (width, height) = (calculated_size.size.x, calculated_size.size.y);
if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
let text_glyphs = &text_layout.glyphs;
let text_glyphs = &text_layout_info.glyphs;
let alignment_offset = match text.alignment.vertical {
VerticalAlign::Top => Vec3::new(0.0, -height, 0.0),
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
@ -140,13 +141,13 @@ pub fn extract_text2d_sprite(
});
}
}
}
}
/// Updates the layout and size information whenever the text or style is changed.
/// This information is computed by the `TextPipeline` on insertion, then stored.
#[allow(clippy::too_many_arguments)]
pub fn update_text2d_layout(
mut commands: Commands,
// Text items which should be reprocessed again, generally when the font hasn't loaded yet.
mut queue: Local<HashSet<Entity>>,
mut textures: ResMut<Assets<Image>>,
@ -155,20 +156,23 @@ pub fn update_text2d_layout(
mut scale_factor_changed: EventReader<WindowScaleFactorChanged>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
mut text_pipeline: ResMut<DefaultTextPipeline>,
mut text_pipeline: ResMut<TextPipeline>,
mut text_query: Query<(
Entity,
Changed<Text>,
&Text,
Option<&Text2dBounds>,
&mut Text2dSize,
Option<&mut TextLayoutInfo>,
)>,
) {
// We need to consume the entire iterator, hence `last`
let factor_changed = scale_factor_changed.iter().last().is_some();
let scale_factor = windows.scale_factor(WindowId::primary());
for (entity, text_changed, text, maybe_bounds, mut calculated_size) in &mut text_query {
for (entity, text_changed, text, maybe_bounds, mut calculated_size, text_layout_info) in
&mut text_query
{
if factor_changed || text_changed || queue.remove(&entity) {
let text_bounds = match maybe_bounds {
Some(bounds) => Vec2::new(
@ -178,7 +182,6 @@ pub fn update_text2d_layout(
None => Vec2::new(f32::MAX, f32::MAX),
};
match text_pipeline.queue_text(
entity,
&fonts,
&text.sections,
scale_factor,
@ -196,14 +199,17 @@ pub fn update_text2d_layout(
Err(e @ TextError::FailedToAddGlyph(_)) => {
panic!("Fatal error when processing text: {}.", e);
}
Ok(()) => {
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
"Failed to get glyphs from the pipeline that have just been computed",
);
Ok(info) => {
calculated_size.size = Vec2::new(
scale_value(text_layout_info.size.x, 1. / scale_factor),
scale_value(text_layout_info.size.y, 1. / scale_factor),
scale_value(info.size.x, 1. / scale_factor),
scale_value(info.size.y, 1. / scale_factor),
);
match text_layout_info {
Some(mut t) => *t = info,
None => {
commands.entity(entity).insert(info);
}
}
}
}
}

View file

@ -24,7 +24,7 @@ use bevy_render::{
Extract, RenderApp, RenderStage,
};
use bevy_sprite::{SpriteAssetEvents, TextureAtlas};
use bevy_text::{DefaultTextPipeline, Text};
use bevy_text::{Text, TextLayoutInfo};
use bevy_transform::components::GlobalTransform;
use bevy_utils::FloatOrd;
use bevy_utils::HashMap;
@ -272,21 +272,21 @@ pub fn extract_default_ui_camera_view<T: Component>(
pub fn extract_text_uinodes(
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
text_pipeline: Extract<Res<DefaultTextPipeline>>,
windows: Extract<Res<Windows>>,
uinode_query: Extract<
Query<(
Entity,
&Node,
&GlobalTransform,
&Text,
&TextLayoutInfo,
&ComputedVisibility,
Option<&CalculatedClip>,
)>,
>,
) {
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
for (entity, uinode, global_transform, text, visibility, clip) in uinode_query.iter() {
for (uinode, global_transform, text, text_layout_info, visibility, clip) in uinode_query.iter()
{
if !visibility.is_visible() {
continue;
}
@ -294,8 +294,7 @@ pub fn extract_text_uinodes(
if uinode.size == Vec2::ZERO {
continue;
}
if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
let text_glyphs = &text_layout.glyphs;
let text_glyphs = &text_layout_info.glyphs;
let alignment_offset = (uinode.size / -2.0).extend(0.0);
let mut color = Color::WHITE;
@ -333,7 +332,6 @@ pub fn extract_text_uinodes(
});
}
}
}
}
#[repr(C)]

View file

@ -3,12 +3,12 @@ use bevy_asset::Assets;
use bevy_ecs::{
entity::Entity,
query::{Changed, Or, With},
system::{Local, ParamSet, Query, Res, ResMut},
system::{Commands, Local, ParamSet, Query, Res, ResMut},
};
use bevy_math::Vec2;
use bevy_render::texture::Image;
use bevy_sprite::TextureAtlas;
use bevy_text::{DefaultTextPipeline, Font, FontAtlasSet, Text, TextError};
use bevy_text::{Font, FontAtlasSet, Text, TextError, TextLayoutInfo, TextPipeline};
use bevy_window::Windows;
#[derive(Debug, Default)]
@ -38,6 +38,7 @@ pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f6
/// This information is computed by the `TextPipeline` on insertion, then stored.
#[allow(clippy::too_many_arguments)]
pub fn text_system(
mut commands: Commands,
mut queued_text: Local<QueuedText>,
mut last_scale_factor: Local<f64>,
mut textures: ResMut<Assets<Image>>,
@ -46,11 +47,16 @@ pub fn text_system(
ui_scale: Res<UiScale>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
mut text_pipeline: ResMut<DefaultTextPipeline>,
mut text_pipeline: ResMut<TextPipeline>,
mut text_queries: ParamSet<(
Query<Entity, Or<(Changed<Text>, Changed<Style>)>>,
Query<Entity, (With<Text>, With<Style>)>,
Query<(&Text, &Style, &mut CalculatedSize)>,
Query<(
&Text,
&Style,
&mut CalculatedSize,
Option<&mut TextLayoutInfo>,
)>,
)>,
) {
// TODO: This should support window-independent scale settings.
@ -85,7 +91,7 @@ pub fn text_system(
let mut new_queue = Vec::new();
let mut query = text_queries.p2();
for entity in queued_text.entities.drain(..) {
if let Ok((text, style, mut calculated_size)) = query.get_mut(entity) {
if let Ok((text, style, mut calculated_size, text_layout_info)) = query.get_mut(entity) {
let node_size = Vec2::new(
text_constraint(
style.min_size.width,
@ -102,7 +108,6 @@ pub fn text_system(
);
match text_pipeline.queue_text(
entity,
&fonts,
&text.sections,
scale_factor,
@ -120,14 +125,17 @@ pub fn text_system(
Err(e @ TextError::FailedToAddGlyph(_)) => {
panic!("Fatal error when processing text: {}.", e);
}
Ok(()) => {
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
"Failed to get glyphs from the pipeline that have just been computed",
);
Ok(info) => {
calculated_size.size = Size {
width: Val::Px(scale_value(text_layout_info.size.x, inv_scale_factor)),
height: Val::Px(scale_value(text_layout_info.size.y, inv_scale_factor)),
width: Val::Px(scale_value(info.size.x, inv_scale_factor)),
height: Val::Px(scale_value(info.size.y, inv_scale_factor)),
};
match text_layout_info {
Some(mut t) => *t = info,
None => {
commands.entity(entity).insert(info);
}
}
}
}
}