pub struct LayoutJob {
pub text: String,
pub sections: Vec<LayoutSection>,
pub wrap: TextWrapping,
pub first_row_min_height: f32,
pub break_on_newline: bool,
pub halign: Align,
pub justify: bool,
pub round_output_to_gui: bool,
pub keep_trailing_whitespace: bool,
}Expand description
Describes the task of laying out text.
This supports mixing different fonts, color and formats (underline etc).
Pass this to crate::FontsView::layout_job or crate::text::layout.
§Example:
use epaint::{Color32, text::{LayoutJob, TextFormat}, FontFamily, FontId};
let mut job = LayoutJob::default();
job.append(
"Hello ",
0.0,
TextFormat {
font_id: FontId::new(14.0, FontFamily::Proportional),
color: Color32::WHITE,
..Default::default()
},
);
job.append(
"World!",
0.0,
TextFormat {
font_id: FontId::new(14.0, FontFamily::Monospace),
color: Color32::BLACK,
..Default::default()
},
);As you can see, constructing a LayoutJob is currently a lot of work.
It would be nice to have a helper macro for it!
Fields§
§text: StringThe complete text of this job, referenced by LayoutSection.
sections: Vec<LayoutSection>The different section, which can have different fonts, colors, etc.
Invariant: the sections are ordered by their byte_range,
and together cover the whole of Self::text with no gaps and no overlaps.
That is: the first section starts at byte 0, the last section ends at text.len(),
and each section starts exactly where the previous one ended.
This is checked by Self::debug_sanity_check.
wrap: TextWrappingControls the text wrapping and elision.
first_row_min_height: f32The first row must be at least this high.
This is in case we lay out text that is the continuation
of some earlier text (sharing the same row),
in which case this will be the height of the earlier text.
In other cases, set this to 0.0.
break_on_newline: boolIf true, all \n characters will result in a new paragraph,
starting on a new row.
If false, all \n characters will be ignored
and show up as the replacement character.
Default: true.
halign: AlignHow to horizontally align the text (Align::LEFT, Align::Center, Align::RIGHT).
justify: boolJustify text so that word-wrapped rows fill the whole TextWrapping::max_width.
round_output_to_gui: boolRound output sizes using emath::GuiRounding, to avoid rounding errors in layout code.
keep_trailing_whitespace: boolIf false (default), trailing whitespace is ignored when computing
horizontal alignment (Self::halign).
This is desirable for labels so that e.g. “Hello “ centers the same as “Hello”.
If true, trailing whitespace is included in the row width used for alignment.
This is desirable for text editors where the user expects to see their spaces.
Implementations§
Source§impl LayoutJob
impl LayoutJob
Sourcepub fn simple(
text: String,
font_id: FontId,
color: Color32,
wrap_width: f32,
) -> LayoutJob
pub fn simple( text: String, font_id: FontId, color: Color32, wrap_width: f32, ) -> LayoutJob
Break on \n and at the given wrap width.
Sourcepub fn simple_format(text: String, format: TextFormat) -> LayoutJob
pub fn simple_format(text: String, format: TextFormat) -> LayoutJob
Break on \n
Sourcepub fn simple_singleline(
text: String,
font_id: FontId,
color: Color32,
) -> LayoutJob
pub fn simple_singleline( text: String, font_id: FontId, color: Color32, ) -> LayoutJob
Does not break on \n, but shows the replacement character instead.
pub fn single_section(text: String, format: TextFormat) -> LayoutJob
pub fn is_empty(&self) -> bool
Sourcepub fn append(&mut self, text: &str, leading_space: f32, format: TextFormat)
pub fn append(&mut self, text: &str, leading_space: f32, format: TextFormat)
Helper for adding a new section when building a LayoutJob.
If the appended text has the same TextFormat as the last section and no
leading_space, it is merged into that section instead of adding a new one.
This keeps the section count down and lets text shaping (e.g. kerning) work
across the appended text, since shaping is done per LayoutSection.
Sourcepub fn format_at_byte(&self, byte_idx: ByteIndex) -> &TextFormat
pub fn format_at_byte(&self, byte_idx: ByteIndex) -> &TextFormat
The TextFormat of the section containing the character starting at the given byte index.
If the index is past the end, the format of the last section is returned.
Panics if the job has no sections.
Assumes LayoutJob::sections are ordered by increasing byte_range (as produced by Self::append).
Sourcepub fn debug_sanity_check(&self)
pub fn debug_sanity_check(&self)
Check the Self::sections invariant: the sections are ordered and together
cover the whole of Self::text with no gaps and no overlaps.
Only does anything in debug builds.
Sourcepub fn font_height(&self, fonts: &mut FontsView<'_>) -> f32
pub fn font_height(&self, fonts: &mut FontsView<'_>) -> f32
The height of the tallest font used in the job.
Returns a value rounded to emath::GUI_ROUNDING.
Sourcepub fn effective_wrap_width(&self) -> f32
pub fn effective_wrap_width(&self) -> f32
The wrap with, with a small margin in some cases.
Trait Implementations§
Source§impl From<LayoutJob> for WidgetText
impl From<LayoutJob> for WidgetText
impl StructuralPartialEq for LayoutJob
Auto Trait Implementations§
impl Freeze for LayoutJob
impl RefUnwindSafe for LayoutJob
impl Send for LayoutJob
impl Sync for LayoutJob
impl Unpin for LayoutJob
impl UnsafeUnpin for LayoutJob
impl UnwindSafe for LayoutJob
Blanket Implementations§
impl<T> AsId for T
impl<T> AsIdSalt for T
Source§impl<'a, T> AtomExt<'a> for T
impl<'a, T> AtomExt<'a> for T
Source§fn atom_shrink(self, shrink: bool) -> Atom<'a>
fn atom_shrink(self, shrink: bool) -> Atom<'a>
Source§fn atom_max_size(self, max_size: Vec2) -> Atom<'a>
fn atom_max_size(self, max_size: Vec2) -> Atom<'a>
Source§fn atom_max_width(self, max_width: f32) -> Atom<'a>
fn atom_max_width(self, max_width: f32) -> Atom<'a>
Source§fn atom_max_height(self, max_height: f32) -> Atom<'a>
fn atom_max_height(self, max_height: f32) -> Atom<'a>
Source§fn atom_align(self, align: Align2) -> Atom<'a>
fn atom_align(self, align: Align2) -> Atom<'a>
emath::Align2 of a single atom within its available space. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more