Align Left, Right, and Center works

This commit is contained in:
Hojjat 2023-02-22 18:31:49 -07:00
parent 9a4d067f9d
commit 00ff5b72f3
5 changed files with 106 additions and 31 deletions

View file

@ -1,7 +1,7 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use crate::{AttrsList, FontSystem, LayoutLine, ShapeLine, Wrap};
use crate::{Align, AttrsList, FontSystem, LayoutLine, ShapeLine, Wrap};
/// A line (or paragraph) of text that is shaped and laid out
pub struct BufferLine {
@ -9,6 +9,7 @@ pub struct BufferLine {
text: String,
attrs_list: AttrsList,
wrap: Wrap,
align: Align,
shape_opt: Option<ShapeLine>,
layout_opt: Option<Vec<LayoutLine>>,
}
@ -22,6 +23,7 @@ impl BufferLine {
text: text.into(),
attrs_list,
wrap: Wrap::Word,
align: Align::Left,
shape_opt: None,
layout_opt: None,
}
@ -94,6 +96,25 @@ impl BufferLine {
}
}
/// Get the Text alignment
pub fn align(&self) -> Align {
self.align
}
/// Set the text alignment
///
/// Will reset shape and layout if it differs from current alignment.
/// Returns true if the line was reset
pub fn set_align(&mut self, align: Align) -> bool {
if align != self.align {
self.align = align;
self.reset();
true
} else {
false
}
}
/// Append line at end of this line
///
/// The wrap setting of the appended line will be lost
@ -165,11 +186,13 @@ impl BufferLine {
font_size: i32,
width: i32,
wrap: Wrap,
align: Align,
) -> &[LayoutLine] {
if self.layout_opt.is_none() {
self.wrap = wrap;
self.align = align;
let shape = self.shape(font_system);
let layout = shape.layout(font_size, width, wrap);
let layout = shape.layout(font_size, width, wrap, align);
self.layout_opt = Some(layout);
}
self.layout_opt.as_ref().expect("layout not found")