2022-11-08 08:43:27 -07:00
|
|
|
#[cfg(not(feature = "std"))]
|
2023-01-04 20:03:03 -07:00
|
|
|
use alloc::{string::String, vec::Vec};
|
2022-11-08 08:43:27 -07:00
|
|
|
|
2023-02-22 18:31:49 -07:00
|
|
|
use crate::{Align, AttrsList, FontSystem, LayoutLine, ShapeLine, Wrap};
|
2022-10-27 14:51:46 -06:00
|
|
|
|
|
|
|
|
/// A line (or paragraph) of text that is shaped and laid out
|
2022-11-04 09:44:54 -06:00
|
|
|
pub struct BufferLine {
|
2022-10-27 14:51:46 -06:00
|
|
|
//TODO: make this not pub(crate)
|
|
|
|
|
text: String,
|
2022-11-04 09:44:54 -06:00
|
|
|
attrs_list: AttrsList,
|
2022-12-20 12:48:37 -07:00
|
|
|
wrap: Wrap,
|
2023-02-23 13:45:34 -07:00
|
|
|
align: Option<Align>,
|
2022-10-27 17:39:26 -06:00
|
|
|
shape_opt: Option<ShapeLine>,
|
|
|
|
|
layout_opt: Option<Vec<LayoutLine>>,
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
2022-11-04 09:44:54 -06:00
|
|
|
impl BufferLine {
|
2022-10-27 14:51:46 -06:00
|
|
|
/// Create a new line with the given text and attributes list
|
2022-11-15 12:26:59 -07:00
|
|
|
/// Cached shaping and layout can be done using the [`Self::shape`] and
|
|
|
|
|
/// [`Self::layout`] functions
|
2022-11-04 09:44:54 -06:00
|
|
|
pub fn new<T: Into<String>>(text: T, attrs_list: AttrsList) -> Self {
|
2022-10-27 14:51:46 -06:00
|
|
|
Self {
|
|
|
|
|
text: text.into(),
|
|
|
|
|
attrs_list,
|
2022-12-20 12:48:37 -07:00
|
|
|
wrap: Wrap::Word,
|
2023-02-23 13:45:34 -07:00
|
|
|
align: None,
|
2022-10-27 14:51:46 -06:00
|
|
|
shape_opt: None,
|
|
|
|
|
layout_opt: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get current text
|
|
|
|
|
pub fn text(&self) -> &str {
|
|
|
|
|
&self.text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set text and attributes list
|
|
|
|
|
///
|
|
|
|
|
/// Will reset shape and layout if it differs from current text and attributes list.
|
|
|
|
|
/// Returns true if the line was reset
|
2023-01-04 20:03:03 -07:00
|
|
|
pub fn set_text<T: AsRef<str> + Into<String>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
text: T,
|
|
|
|
|
attrs_list: AttrsList,
|
|
|
|
|
) -> bool {
|
2022-11-15 12:26:59 -07:00
|
|
|
if text.as_ref() != self.text || attrs_list != self.attrs_list {
|
2022-10-27 14:51:46 -06:00
|
|
|
self.text = text.into();
|
|
|
|
|
self.attrs_list = attrs_list;
|
|
|
|
|
self.reset();
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 15:33:13 -08:00
|
|
|
/// Consume this line, returning only its text contents as a String.
|
|
|
|
|
pub fn into_text(self) -> String {
|
|
|
|
|
self.text
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:51:46 -06:00
|
|
|
/// Get attributes list
|
2022-11-04 09:44:54 -06:00
|
|
|
pub fn attrs_list(&self) -> &AttrsList {
|
2022-10-27 14:51:46 -06:00
|
|
|
&self.attrs_list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set attributes list
|
|
|
|
|
///
|
|
|
|
|
/// Will reset shape and layout if it differs from current attributes list.
|
|
|
|
|
/// Returns true if the line was reset
|
2022-11-04 09:44:54 -06:00
|
|
|
pub fn set_attrs_list(&mut self, attrs_list: AttrsList) -> bool {
|
2022-10-27 14:51:46 -06:00
|
|
|
if attrs_list != self.attrs_list {
|
|
|
|
|
self.attrs_list = attrs_list;
|
|
|
|
|
self.reset();
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 12:48:37 -07:00
|
|
|
/// Get wrapping setting (wrap by characters/words or no wrapping)
|
|
|
|
|
pub fn wrap(&self) -> Wrap {
|
|
|
|
|
self.wrap
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-20 12:48:37 -07:00
|
|
|
/// Set wrapping setting (wrap by characters/words or no wrapping)
|
2022-10-27 14:51:46 -06:00
|
|
|
///
|
2022-12-20 12:48:37 -07:00
|
|
|
/// Will reset shape and layout if it differs from current wrapping setting.
|
2022-10-27 14:51:46 -06:00
|
|
|
/// Returns true if the line was reset
|
2022-12-20 12:48:37 -07:00
|
|
|
pub fn set_wrap(&mut self, wrap: Wrap) -> bool {
|
|
|
|
|
if wrap != self.wrap {
|
|
|
|
|
self.wrap = wrap;
|
2022-10-27 14:51:46 -06:00
|
|
|
self.reset();
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 18:31:49 -07:00
|
|
|
/// Get the Text alignment
|
2023-02-23 13:45:34 -07:00
|
|
|
pub fn align(&self) -> Option<Align> {
|
2023-02-22 18:31:49 -07:00
|
|
|
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 {
|
2023-02-23 13:45:34 -07:00
|
|
|
if Some(align) != self.align {
|
|
|
|
|
self.align = Some(align);
|
2023-02-22 18:31:49 -07:00
|
|
|
self.reset();
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:51:46 -06:00
|
|
|
/// Append line at end of this line
|
|
|
|
|
///
|
|
|
|
|
/// The wrap setting of the appended line will be lost
|
|
|
|
|
pub fn append(&mut self, other: Self) {
|
|
|
|
|
let len = self.text.len();
|
|
|
|
|
self.text.push_str(other.text());
|
|
|
|
|
|
|
|
|
|
if other.attrs_list.defaults() != self.attrs_list.defaults() {
|
|
|
|
|
// If default formatting does not match, make a new span for it
|
2023-01-04 20:03:03 -07:00
|
|
|
self.attrs_list
|
|
|
|
|
.add_span(len..len + other.text().len(), other.attrs_list.defaults());
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (other_range, attrs) in other.attrs_list.spans() {
|
|
|
|
|
// Add previous attrs spans
|
|
|
|
|
let range = other_range.start + len..other_range.end + len;
|
2022-11-04 09:44:54 -06:00
|
|
|
self.attrs_list.add_span(range, attrs.as_attrs());
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Split off new line at index
|
|
|
|
|
pub fn split_off(&mut self, index: usize) -> Self {
|
|
|
|
|
let text = self.text.split_off(index);
|
|
|
|
|
let attrs_list = self.attrs_list.split_off(index);
|
|
|
|
|
self.reset();
|
|
|
|
|
|
|
|
|
|
let mut new = Self::new(text, attrs_list);
|
2022-12-20 12:48:37 -07:00
|
|
|
new.wrap = self.wrap;
|
2022-10-27 14:51:46 -06:00
|
|
|
new
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reset shaping and layout information
|
|
|
|
|
//TODO: make this private
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
|
self.shape_opt = None;
|
|
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 17:39:26 -06:00
|
|
|
/// Reset only layout information
|
|
|
|
|
pub fn reset_layout(&mut self) {
|
|
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:51:46 -06:00
|
|
|
/// Check if shaping and layout information is cleared
|
|
|
|
|
pub fn is_reset(&self) -> bool {
|
|
|
|
|
self.shape_opt.is_none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Shape line, will cache results
|
2022-11-02 19:25:18 -07:00
|
|
|
pub fn shape(&mut self, font_system: &FontSystem) -> &ShapeLine {
|
2022-10-27 14:51:46 -06:00
|
|
|
if self.shape_opt.is_none() {
|
|
|
|
|
self.shape_opt = Some(ShapeLine::new(font_system, &self.text, &self.attrs_list));
|
|
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
2022-11-15 12:26:59 -07:00
|
|
|
self.shape_opt.as_ref().expect("shape not found")
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-27 17:39:26 -06:00
|
|
|
/// Get line shaping cache
|
|
|
|
|
pub fn shape_opt(&self) -> &Option<ShapeLine> {
|
|
|
|
|
&self.shape_opt
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:51:46 -06:00
|
|
|
/// Layout line, will cache results
|
2023-01-04 20:03:03 -07:00
|
|
|
pub fn layout(
|
|
|
|
|
&mut self,
|
|
|
|
|
font_system: &FontSystem,
|
|
|
|
|
font_size: i32,
|
|
|
|
|
width: i32,
|
|
|
|
|
wrap: Wrap,
|
2023-02-23 13:45:34 -07:00
|
|
|
align: Option<Align>,
|
2023-01-04 20:03:03 -07:00
|
|
|
) -> &[LayoutLine] {
|
2022-10-27 14:51:46 -06:00
|
|
|
if self.layout_opt.is_none() {
|
2022-12-20 12:48:37 -07:00
|
|
|
self.wrap = wrap;
|
2023-02-22 18:31:49 -07:00
|
|
|
self.align = align;
|
2022-10-27 14:51:46 -06:00
|
|
|
let shape = self.shape(font_system);
|
2023-02-22 18:31:49 -07:00
|
|
|
let layout = shape.layout(font_size, width, wrap, align);
|
2022-10-27 14:51:46 -06:00
|
|
|
self.layout_opt = Some(layout);
|
|
|
|
|
}
|
2022-11-15 12:26:59 -07:00
|
|
|
self.layout_opt.as_ref().expect("layout not found")
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|
2022-10-27 17:39:26 -06:00
|
|
|
|
|
|
|
|
/// Get line layout cache
|
|
|
|
|
pub fn layout_opt(&self) -> &Option<Vec<LayoutLine>> {
|
|
|
|
|
&self.layout_opt
|
|
|
|
|
}
|
2022-10-27 14:51:46 -06:00
|
|
|
}
|