From 302fcad8fd5f8f6c324e966d81cd3adc990936b4 Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Fri, 20 Jan 2023 08:26:41 +1000 Subject: [PATCH 1/7] Define a new public enum 'TextLineBreakBehaviour' This enum simply adapts the glyph_brush_layout::BuiltInLineBreaker enum. --- crates/bevy_text/src/text.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 719b59106b700..fe77c95751d5c 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -170,3 +170,24 @@ impl Default for TextStyle { } } } + +/// Determines how lines will be broken when preventing text from running out of bounds. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] +#[reflect(Serialize, Deserialize)] +pub enum TextLineBreakBehaviour { + /// Lines will be broken up at the nearest word boundary, usually at a space.
+ /// This behaviour suits most cases, as it keeps words intact. Aims to implement the Unicode line breaking algorithm. + Unicode, + /// Lines will be broken without discrimination at the first character that runs out of bounds.
+ /// This is closer to the behaviour one might expect from a terminal. + AnyCharacter, +} + +impl From for glyph_brush_layout::BuiltInLineBreaker { + fn from(val: TextLineBreakBehaviour) -> Self { + match val { + TextLineBreakBehaviour::Unicode => glyph_brush_layout::BuiltInLineBreaker::UnicodeLineBreaker, + TextLineBreakBehaviour::AnyCharacter => glyph_brush_layout::BuiltInLineBreaker::AnyCharLineBreaker, + } + } +} From 790d1f8435514d1eb90429a36e7022f34623700b Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Fri, 20 Jan 2023 08:36:56 +1000 Subject: [PATCH 2/7] Use TextLineBreakBehaviour in GlyphBrush and expose it -Added a linebreak_behaviour member on Text -Added a with_linebreak_behaviour to both Text and TextBundle -Adjusted TextPipeline and GlyphBrush where necessary to make use of the new field --- crates/bevy_text/src/glyph_brush.rs | 9 +++++++-- crates/bevy_text/src/pipeline.rs | 5 +++-- crates/bevy_text/src/text.rs | 8 ++++++++ crates/bevy_text/src/text2d.rs | 1 + crates/bevy_ui/src/node_bundles.rs | 8 +++++++- crates/bevy_ui/src/widget/text.rs | 1 + 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index 8f88a33f56467..5a4fb2d3c0f38 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -5,12 +5,12 @@ use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; use bevy_utils::tracing::warn; use glyph_brush_layout::{ - FontId, GlyphPositioner, Layout, SectionGeometry, SectionGlyph, SectionText, ToSectionText, + FontId, GlyphPositioner, Layout, SectionGeometry, SectionGlyph, SectionText, ToSectionText, BuiltInLineBreaker, }; use crate::{ error::TextError, Font, FontAtlasSet, FontAtlasWarning, GlyphAtlasInfo, TextAlignment, - TextSettings, YAxisOrientation, + TextSettings, YAxisOrientation, TextLineBreakBehaviour, }; pub struct GlyphBrush { @@ -35,13 +35,18 @@ impl GlyphBrush { sections: &[S], bounds: Vec2, text_alignment: TextAlignment, + linebreak_behaviour: TextLineBreakBehaviour, ) -> Result, TextError> { let geom = SectionGeometry { bounds: (bounds.x, bounds.y), ..Default::default() }; + + let lbb: BuiltInLineBreaker = linebreak_behaviour.into(); + let section_glyphs = Layout::default() .h_align(text_alignment.into()) + .line_breaker(lbb) .calculate_glyphs(&self.fonts, &geom, sections); Ok(section_glyphs) } diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index 3726f57fd4038..b1a14d6e9f953 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -11,7 +11,7 @@ use glyph_brush_layout::{FontId, SectionText}; use crate::{ error::TextError, glyph_brush::GlyphBrush, scale_value, Font, FontAtlasSet, FontAtlasWarning, - PositionedGlyph, TextAlignment, TextSection, TextSettings, YAxisOrientation, + PositionedGlyph, TextAlignment, TextSection, TextSettings, YAxisOrientation, TextLineBreakBehaviour, }; #[derive(Default, Resource)] @@ -45,6 +45,7 @@ impl TextPipeline { sections: &[TextSection], scale_factor: f64, text_alignment: TextAlignment, + linebreak_behaviour: TextLineBreakBehaviour, bounds: Vec2, font_atlas_set_storage: &mut Assets, texture_atlases: &mut Assets, @@ -77,7 +78,7 @@ impl TextPipeline { let section_glyphs = self .brush - .compute_glyphs(§ions, bounds, text_alignment)?; + .compute_glyphs(§ions, bounds, text_alignment, linebreak_behaviour)?; if section_glyphs.is_empty() { return Ok(TextLayoutInfo::default()); diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index fe77c95751d5c..de679ad81e31a 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -14,6 +14,8 @@ pub struct Text { /// The text's internal alignment. /// Should not affect its position within a container. pub alignment: TextAlignment, + /// How the text should linebreak when running out of the bounds determined by max_size + pub linebreak_behaviour: TextLineBreakBehaviour, } impl Default for Text { @@ -21,6 +23,7 @@ impl Default for Text { Self { sections: Default::default(), alignment: TextAlignment::Left, + linebreak_behaviour: TextLineBreakBehaviour::Unicode, } } } @@ -103,6 +106,11 @@ impl Text { self.alignment = alignment; self } + + pub const fn with_linebreak_behaviour(mut self, linebreak_behaviour: TextLineBreakBehaviour) -> Self { + self.linebreak_behaviour = linebreak_behaviour; + self + } } #[derive(Debug, Default, Clone, FromReflect, Reflect)] diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 6a5ed74089486..56da32ade4e8b 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -186,6 +186,7 @@ pub fn update_text2d_layout( &text.sections, scale_factor, text.alignment, + text.linebreak_behaviour, text_bounds, &mut font_atlas_set_storage, &mut texture_atlases, diff --git a/crates/bevy_ui/src/node_bundles.rs b/crates/bevy_ui/src/node_bundles.rs index 8369b33bfe0b0..ebe19f693684d 100644 --- a/crates/bevy_ui/src/node_bundles.rs +++ b/crates/bevy_ui/src/node_bundles.rs @@ -9,7 +9,7 @@ use bevy_render::{ prelude::{Color, ComputedVisibility}, view::Visibility, }; -use bevy_text::{Text, TextAlignment, TextSection, TextStyle}; +use bevy_text::{Text, TextAlignment, TextSection, TextStyle, TextLineBreakBehaviour}; use bevy_transform::prelude::{GlobalTransform, Transform}; /// The basic UI node @@ -153,6 +153,12 @@ impl TextBundle { self } + /// Returns this [`TextBundle`] with a new [`TextLineBreakBehaviour`] on [`Text`]. + pub const fn with_linebreak_behaviour(mut self, linebreak_behaviour: TextLineBreakBehaviour) -> Self { + self.text.linebreak_behaviour = linebreak_behaviour; + self + } + /// Returns this [`TextBundle`] with a new [`Style`]. pub const fn with_style(mut self, style: Style) -> Self { self.style = style; diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index c2694f3e142a1..28a3984edbc58 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -120,6 +120,7 @@ pub fn text_system( &text.sections, scale_factor, text.alignment, + text.linebreak_behaviour, node_size, &mut font_atlas_set_storage, &mut texture_atlases, From 7004552fe2dceccf1c503a313133588229579663 Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Fri, 20 Jan 2023 08:39:19 +1000 Subject: [PATCH 3/7] Add TextLineBreakBehaviour to the 'test2d' example Duplicated the original "this text wraps in the box" but with AnyCharacter linebreak to show off the difference. --- examples/2d/text2d.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index 2f4f53df183ec..c043c22d6ed77 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -5,7 +5,7 @@ //! For an example on how to render text as part of a user interface, independent from the world //! viewport, you may want to look at `2d/contributors.rs` or `ui/text.rs`. -use bevy::{prelude::*, text::Text2dBounds}; +use bevy::{prelude::*, text::{Text2dBounds, TextLineBreakBehaviour}}; fn main() { App::new() @@ -29,7 +29,7 @@ struct AnimateScale; fn setup(mut commands: Commands, asset_server: Res) { let font = asset_server.load("fonts/FiraSans-Bold.ttf"); let text_style = TextStyle { - font, + font: font.clone(), font_size: 60.0, color: Color::WHITE, }; @@ -62,6 +62,11 @@ fn setup(mut commands: Commands, asset_server: Res) { AnimateScale, )); // Demonstrate text wrapping + let slightly_smaller_text_style = TextStyle { + font: font, + font_size: 42.0, + color: Color::WHITE, + }; let box_size = Vec2::new(300.0, 200.0); let box_position = Vec2::new(0.0, -250.0); commands @@ -76,8 +81,9 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|builder| { builder.spawn(Text2dBundle { - text: Text::from_section("this text wraps in the box", text_style) - .with_alignment(TextAlignment::Left), + text: Text::from_section("this text wraps in the box\n(Unicode linebreaks)", slightly_smaller_text_style.clone()) + .with_alignment(TextAlignment::Left) + .with_linebreak_behaviour(TextLineBreakBehaviour::Unicode), text_2d_bounds: Text2dBounds { // Wrap text in the rectangle size: box_size, @@ -87,6 +93,34 @@ fn setup(mut commands: Commands, asset_server: Res) { ..default() }); }); + + + let other_box_size = Vec2::new(300.0, 200.0); + let other_box_position = Vec2::new(320.0, -250.0); + commands + .spawn(SpriteBundle { + sprite: Sprite { + color: Color::rgb(0.20, 0.3, 0.70), + custom_size: Some(Vec2::new(other_box_size.x, other_box_size.y)), + ..default() + }, + transform: Transform::from_translation(other_box_position.extend(0.0)), + ..default() + }) + .with_children(|builder| { + builder.spawn(Text2dBundle { + text: Text::from_section("this text wraps in the box\n(AnyCharacter linebreaks)", slightly_smaller_text_style) + .with_alignment(TextAlignment::Left) + .with_linebreak_behaviour(TextLineBreakBehaviour::AnyCharacter), + text_2d_bounds: Text2dBounds { + // Wrap text in the rectangle + size: other_box_size, + }, + // ensure the text is drawn on top of the box + transform: Transform::from_translation(Vec3::Z), + ..default() + }); + }); } fn animate_translation( From 5fc01467b36c9b1cbd5d0998c146b2ebf1866c54 Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Fri, 20 Jan 2023 10:19:43 +1000 Subject: [PATCH 4/7] Fix CI issues for TextLineBreakBehaviour commits Ran cargo fmt and fixed redundant clone --- crates/bevy_text/src/glyph_brush.rs | 5 +++-- crates/bevy_text/src/pipeline.rs | 9 ++++---- crates/bevy_text/src/text.rs | 13 ++++++++--- crates/bevy_ui/src/node_bundles.rs | 7 ++++-- examples/2d/text2d.rs | 34 ++++++++++++++++++----------- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index 5a4fb2d3c0f38..2a00789904b38 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -5,12 +5,13 @@ use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; use bevy_utils::tracing::warn; use glyph_brush_layout::{ - FontId, GlyphPositioner, Layout, SectionGeometry, SectionGlyph, SectionText, ToSectionText, BuiltInLineBreaker, + BuiltInLineBreaker, FontId, GlyphPositioner, Layout, SectionGeometry, SectionGlyph, + SectionText, ToSectionText, }; use crate::{ error::TextError, Font, FontAtlasSet, FontAtlasWarning, GlyphAtlasInfo, TextAlignment, - TextSettings, YAxisOrientation, TextLineBreakBehaviour, + TextLineBreakBehaviour, TextSettings, YAxisOrientation, }; pub struct GlyphBrush { diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index b1a14d6e9f953..defe5c590750b 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -11,7 +11,8 @@ use glyph_brush_layout::{FontId, SectionText}; use crate::{ error::TextError, glyph_brush::GlyphBrush, scale_value, Font, FontAtlasSet, FontAtlasWarning, - PositionedGlyph, TextAlignment, TextSection, TextSettings, YAxisOrientation, TextLineBreakBehaviour, + PositionedGlyph, TextAlignment, TextLineBreakBehaviour, TextSection, TextSettings, + YAxisOrientation, }; #[derive(Default, Resource)] @@ -76,9 +77,9 @@ impl TextPipeline { }) .collect::, _>>()?; - let section_glyphs = self - .brush - .compute_glyphs(§ions, bounds, text_alignment, linebreak_behaviour)?; + let section_glyphs = + self.brush + .compute_glyphs(§ions, bounds, text_alignment, linebreak_behaviour)?; if section_glyphs.is_empty() { return Ok(TextLayoutInfo::default()); diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index de679ad81e31a..3f5251fc20e99 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -107,7 +107,10 @@ impl Text { self } - pub const fn with_linebreak_behaviour(mut self, linebreak_behaviour: TextLineBreakBehaviour) -> Self { + pub const fn with_linebreak_behaviour( + mut self, + linebreak_behaviour: TextLineBreakBehaviour, + ) -> Self { self.linebreak_behaviour = linebreak_behaviour; self } @@ -194,8 +197,12 @@ pub enum TextLineBreakBehaviour { impl From for glyph_brush_layout::BuiltInLineBreaker { fn from(val: TextLineBreakBehaviour) -> Self { match val { - TextLineBreakBehaviour::Unicode => glyph_brush_layout::BuiltInLineBreaker::UnicodeLineBreaker, - TextLineBreakBehaviour::AnyCharacter => glyph_brush_layout::BuiltInLineBreaker::AnyCharLineBreaker, + TextLineBreakBehaviour::Unicode => { + glyph_brush_layout::BuiltInLineBreaker::UnicodeLineBreaker + } + TextLineBreakBehaviour::AnyCharacter => { + glyph_brush_layout::BuiltInLineBreaker::AnyCharLineBreaker + } } } } diff --git a/crates/bevy_ui/src/node_bundles.rs b/crates/bevy_ui/src/node_bundles.rs index ebe19f693684d..06048de5a7d8e 100644 --- a/crates/bevy_ui/src/node_bundles.rs +++ b/crates/bevy_ui/src/node_bundles.rs @@ -9,7 +9,7 @@ use bevy_render::{ prelude::{Color, ComputedVisibility}, view::Visibility, }; -use bevy_text::{Text, TextAlignment, TextSection, TextStyle, TextLineBreakBehaviour}; +use bevy_text::{Text, TextAlignment, TextLineBreakBehaviour, TextSection, TextStyle}; use bevy_transform::prelude::{GlobalTransform, Transform}; /// The basic UI node @@ -154,7 +154,10 @@ impl TextBundle { } /// Returns this [`TextBundle`] with a new [`TextLineBreakBehaviour`] on [`Text`]. - pub const fn with_linebreak_behaviour(mut self, linebreak_behaviour: TextLineBreakBehaviour) -> Self { + pub const fn with_linebreak_behaviour( + mut self, + linebreak_behaviour: TextLineBreakBehaviour, + ) -> Self { self.text.linebreak_behaviour = linebreak_behaviour; self } diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index c043c22d6ed77..b6cb52c9571c5 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -5,7 +5,10 @@ //! For an example on how to render text as part of a user interface, independent from the world //! viewport, you may want to look at `2d/contributors.rs` or `ui/text.rs`. -use bevy::{prelude::*, text::{Text2dBounds, TextLineBreakBehaviour}}; +use bevy::{ + prelude::*, + text::{Text2dBounds, TextLineBreakBehaviour}, +}; fn main() { App::new() @@ -56,14 +59,14 @@ fn setup(mut commands: Commands, asset_server: Res) { // Demonstrate changing scale commands.spawn(( Text2dBundle { - text: Text::from_section("scale", text_style.clone()).with_alignment(text_alignment), + text: Text::from_section("scale", text_style).with_alignment(text_alignment), ..default() }, AnimateScale, )); // Demonstrate text wrapping let slightly_smaller_text_style = TextStyle { - font: font, + font, font_size: 42.0, color: Color::WHITE, }; @@ -81,9 +84,12 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|builder| { builder.spawn(Text2dBundle { - text: Text::from_section("this text wraps in the box\n(Unicode linebreaks)", slightly_smaller_text_style.clone()) - .with_alignment(TextAlignment::Left) - .with_linebreak_behaviour(TextLineBreakBehaviour::Unicode), + text: Text::from_section( + "this text wraps in the box\n(Unicode linebreaks)", + slightly_smaller_text_style.clone(), + ) + .with_alignment(TextAlignment::Left) + .with_linebreak_behaviour(TextLineBreakBehaviour::Unicode), text_2d_bounds: Text2dBounds { // Wrap text in the rectangle size: box_size, @@ -94,10 +100,9 @@ fn setup(mut commands: Commands, asset_server: Res) { }); }); - - let other_box_size = Vec2::new(300.0, 200.0); - let other_box_position = Vec2::new(320.0, -250.0); - commands + let other_box_size = Vec2::new(300.0, 200.0); + let other_box_position = Vec2::new(320.0, -250.0); + commands .spawn(SpriteBundle { sprite: Sprite { color: Color::rgb(0.20, 0.3, 0.70), @@ -109,9 +114,12 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|builder| { builder.spawn(Text2dBundle { - text: Text::from_section("this text wraps in the box\n(AnyCharacter linebreaks)", slightly_smaller_text_style) - .with_alignment(TextAlignment::Left) - .with_linebreak_behaviour(TextLineBreakBehaviour::AnyCharacter), + text: Text::from_section( + "this text wraps in the box\n(AnyCharacter linebreaks)", + slightly_smaller_text_style, + ) + .with_alignment(TextAlignment::Left) + .with_linebreak_behaviour(TextLineBreakBehaviour::AnyCharacter), text_2d_bounds: Text2dBounds { // Wrap text in the rectangle size: other_box_size, From 8a32172abe230d02b3b7050875c47b1e8769f9f8 Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:43:16 +1000 Subject: [PATCH 5/7] Removed the with_linebreak_behaviour methods Changed the text2d example code to suit --- crates/bevy_text/src/text.rs | 8 -------- crates/bevy_ui/src/node_bundles.rs | 11 +---------- examples/2d/text2d.rs | 28 ++++++++++++++++------------ 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 3f5251fc20e99..d81aea0685533 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -106,14 +106,6 @@ impl Text { self.alignment = alignment; self } - - pub const fn with_linebreak_behaviour( - mut self, - linebreak_behaviour: TextLineBreakBehaviour, - ) -> Self { - self.linebreak_behaviour = linebreak_behaviour; - self - } } #[derive(Debug, Default, Clone, FromReflect, Reflect)] diff --git a/crates/bevy_ui/src/node_bundles.rs b/crates/bevy_ui/src/node_bundles.rs index 06048de5a7d8e..8369b33bfe0b0 100644 --- a/crates/bevy_ui/src/node_bundles.rs +++ b/crates/bevy_ui/src/node_bundles.rs @@ -9,7 +9,7 @@ use bevy_render::{ prelude::{Color, ComputedVisibility}, view::Visibility, }; -use bevy_text::{Text, TextAlignment, TextLineBreakBehaviour, TextSection, TextStyle}; +use bevy_text::{Text, TextAlignment, TextSection, TextStyle}; use bevy_transform::prelude::{GlobalTransform, Transform}; /// The basic UI node @@ -153,15 +153,6 @@ impl TextBundle { self } - /// Returns this [`TextBundle`] with a new [`TextLineBreakBehaviour`] on [`Text`]. - pub const fn with_linebreak_behaviour( - mut self, - linebreak_behaviour: TextLineBreakBehaviour, - ) -> Self { - self.text.linebreak_behaviour = linebreak_behaviour; - self - } - /// Returns this [`TextBundle`] with a new [`Style`]. pub const fn with_style(mut self, style: Style) -> Self { self.style = style; diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index b6cb52c9571c5..94e822e70cc1a 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -84,12 +84,14 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|builder| { builder.spawn(Text2dBundle { - text: Text::from_section( - "this text wraps in the box\n(Unicode linebreaks)", - slightly_smaller_text_style.clone(), - ) - .with_alignment(TextAlignment::Left) - .with_linebreak_behaviour(TextLineBreakBehaviour::Unicode), + text: Text { + sections: vec![TextSection::new( + "this text wraps in the box\n(Unicode linebreaks)", + slightly_smaller_text_style.clone(), + )], + alignment: TextAlignment::Left, + linebreak_behaviour: TextLineBreakBehaviour::Unicode, + }, text_2d_bounds: Text2dBounds { // Wrap text in the rectangle size: box_size, @@ -114,12 +116,14 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|builder| { builder.spawn(Text2dBundle { - text: Text::from_section( - "this text wraps in the box\n(AnyCharacter linebreaks)", - slightly_smaller_text_style, - ) - .with_alignment(TextAlignment::Left) - .with_linebreak_behaviour(TextLineBreakBehaviour::AnyCharacter), + text: Text { + sections: vec![TextSection::new( + "this text wraps in the box\n(AnyCharacter linebreaks)", + slightly_smaller_text_style.clone(), + )], + alignment: TextAlignment::Left, + linebreak_behaviour: TextLineBreakBehaviour::AnyCharacter, + }, text_2d_bounds: Text2dBounds { // Wrap text in the rectangle size: other_box_size, From 901bfd5b20cf207e22a44f6832b0e9024309d0e8 Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:41:59 +1000 Subject: [PATCH 6/7] Renamed 'TextLineBreakBehaviour' to 'BreakLineOn' Adjusted the documentation to include a link to Unicode Standard Annex #14, Unicode Line Breaking Algorithm. --- crates/bevy_text/src/glyph_brush.rs | 6 +++--- crates/bevy_text/src/pipeline.rs | 7 +++---- crates/bevy_text/src/text.rs | 30 ++++++++++++++--------------- examples/2d/text2d.rs | 6 +++--- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/crates/bevy_text/src/glyph_brush.rs b/crates/bevy_text/src/glyph_brush.rs index 2a00789904b38..a96d25dd66923 100644 --- a/crates/bevy_text/src/glyph_brush.rs +++ b/crates/bevy_text/src/glyph_brush.rs @@ -10,8 +10,8 @@ use glyph_brush_layout::{ }; use crate::{ - error::TextError, Font, FontAtlasSet, FontAtlasWarning, GlyphAtlasInfo, TextAlignment, - TextLineBreakBehaviour, TextSettings, YAxisOrientation, + error::TextError, BreakLineOn, Font, FontAtlasSet, FontAtlasWarning, GlyphAtlasInfo, + TextAlignment, TextSettings, YAxisOrientation, }; pub struct GlyphBrush { @@ -36,7 +36,7 @@ impl GlyphBrush { sections: &[S], bounds: Vec2, text_alignment: TextAlignment, - linebreak_behaviour: TextLineBreakBehaviour, + linebreak_behaviour: BreakLineOn, ) -> Result, TextError> { let geom = SectionGeometry { bounds: (bounds.x, bounds.y), diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index defe5c590750b..ac29c6b7c9f87 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -10,9 +10,8 @@ use bevy_utils::HashMap; use glyph_brush_layout::{FontId, SectionText}; use crate::{ - error::TextError, glyph_brush::GlyphBrush, scale_value, Font, FontAtlasSet, FontAtlasWarning, - PositionedGlyph, TextAlignment, TextLineBreakBehaviour, TextSection, TextSettings, - YAxisOrientation, + error::TextError, glyph_brush::GlyphBrush, scale_value, BreakLineOn, Font, FontAtlasSet, + FontAtlasWarning, PositionedGlyph, TextAlignment, TextSection, TextSettings, YAxisOrientation, }; #[derive(Default, Resource)] @@ -46,7 +45,7 @@ impl TextPipeline { sections: &[TextSection], scale_factor: f64, text_alignment: TextAlignment, - linebreak_behaviour: TextLineBreakBehaviour, + linebreak_behaviour: BreakLineOn, bounds: Vec2, font_atlas_set_storage: &mut Assets, texture_atlases: &mut Assets, diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index d81aea0685533..e42548054f8b3 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -15,7 +15,7 @@ pub struct Text { /// Should not affect its position within a container. pub alignment: TextAlignment, /// How the text should linebreak when running out of the bounds determined by max_size - pub linebreak_behaviour: TextLineBreakBehaviour, + pub linebreak_behaviour: BreakLineOn, } impl Default for Text { @@ -23,7 +23,7 @@ impl Default for Text { Self { sections: Default::default(), alignment: TextAlignment::Left, - linebreak_behaviour: TextLineBreakBehaviour::Unicode, + linebreak_behaviour: BreakLineOn::WordBoundary, } } } @@ -177,24 +177,22 @@ impl Default for TextStyle { /// Determines how lines will be broken when preventing text from running out of bounds. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] #[reflect(Serialize, Deserialize)] -pub enum TextLineBreakBehaviour { - /// Lines will be broken up at the nearest word boundary, usually at a space.
- /// This behaviour suits most cases, as it keeps words intact. Aims to implement the Unicode line breaking algorithm. - Unicode, - /// Lines will be broken without discrimination at the first character that runs out of bounds.
- /// This is closer to the behaviour one might expect from a terminal. +pub enum BreakLineOn { + /// Uses the [Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/).
+ /// Lines will be broken up at the nearest suitable word boundary, usually a space.
+ /// This behaviour suits most cases, as it keeps words intact across linebreaks.
+ WordBoundary, + /// Lines will be broken without discrimination on any character that would leave bounds.
+ /// This is closer to the behaviour one might expect from text in a terminal.
+ /// However it may lead to words being broken up across linebreaks
AnyCharacter, } -impl From for glyph_brush_layout::BuiltInLineBreaker { - fn from(val: TextLineBreakBehaviour) -> Self { +impl From for glyph_brush_layout::BuiltInLineBreaker { + fn from(val: BreakLineOn) -> Self { match val { - TextLineBreakBehaviour::Unicode => { - glyph_brush_layout::BuiltInLineBreaker::UnicodeLineBreaker - } - TextLineBreakBehaviour::AnyCharacter => { - glyph_brush_layout::BuiltInLineBreaker::AnyCharLineBreaker - } + BreakLineOn::WordBoundary => glyph_brush_layout::BuiltInLineBreaker::UnicodeLineBreaker, + BreakLineOn::AnyCharacter => glyph_brush_layout::BuiltInLineBreaker::AnyCharLineBreaker, } } } diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index 94e822e70cc1a..e920ab5062557 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -7,7 +7,7 @@ use bevy::{ prelude::*, - text::{Text2dBounds, TextLineBreakBehaviour}, + text::{BreakLineOn, Text2dBounds}, }; fn main() { @@ -90,7 +90,7 @@ fn setup(mut commands: Commands, asset_server: Res) { slightly_smaller_text_style.clone(), )], alignment: TextAlignment::Left, - linebreak_behaviour: TextLineBreakBehaviour::Unicode, + linebreak_behaviour: BreakLineOn::WordBoundary, }, text_2d_bounds: Text2dBounds { // Wrap text in the rectangle @@ -122,7 +122,7 @@ fn setup(mut commands: Commands, asset_server: Res) { slightly_smaller_text_style.clone(), )], alignment: TextAlignment::Left, - linebreak_behaviour: TextLineBreakBehaviour::AnyCharacter, + linebreak_behaviour: BreakLineOn::AnyCharacter, }, text_2d_bounds: Text2dBounds { // Wrap text in the rectangle From ae58826fc3798bea5aa3238d2b8b08584f9c074b Mon Sep 17 00:00:00 2001 From: Molot2032 <117271367+Molot2032@users.noreply.github.com> Date: Sat, 21 Jan 2023 07:35:57 +1000 Subject: [PATCH 7/7] Removed hard linebreaks in doc comments for the BreakLineOn variants --- crates/bevy_text/src/text.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index e42548054f8b3..a6ca996f828a0 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -178,13 +178,13 @@ impl Default for TextStyle { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] #[reflect(Serialize, Deserialize)] pub enum BreakLineOn { - /// Uses the [Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/).
- /// Lines will be broken up at the nearest suitable word boundary, usually a space.
- /// This behaviour suits most cases, as it keeps words intact across linebreaks.
+ /// Uses the [Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/). + /// Lines will be broken up at the nearest suitable word boundary, usually a space. + /// This behaviour suits most cases, as it keeps words intact across linebreaks. WordBoundary, - /// Lines will be broken without discrimination on any character that would leave bounds.
- /// This is closer to the behaviour one might expect from text in a terminal.
- /// However it may lead to words being broken up across linebreaks
+ /// Lines will be broken without discrimination on any character that would leave bounds. + /// This is closer to the behaviour one might expect from text in a terminal. + /// However it may lead to words being broken up across linebreaks. AnyCharacter, }