1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#[cfg(not(feature = "std"))]
use alloc::{
string::String,
vec::Vec,
};
use crate::{AttrsList, FontSystem, LayoutLine, ShapeLine};
pub struct BufferLine {
text: String,
attrs_list: AttrsList,
wrap_simple: bool,
shape_opt: Option<ShapeLine>,
layout_opt: Option<Vec<LayoutLine>>,
}
impl BufferLine {
pub fn new<T: Into<String>>(text: T, attrs_list: AttrsList) -> Self {
Self {
text: text.into(),
attrs_list,
wrap_simple: false,
shape_opt: None,
layout_opt: None,
}
}
pub fn text(&self) -> &str {
&self.text
}
pub fn set_text<T: AsRef<str> + Into<String>>(&mut self, text: T, attrs_list: AttrsList) -> bool {
if text.as_ref() != self.text || attrs_list != self.attrs_list {
self.text = text.into();
self.attrs_list = attrs_list;
self.reset();
true
} else {
false
}
}
pub fn attrs_list(&self) -> &AttrsList {
&self.attrs_list
}
pub fn set_attrs_list(&mut self, attrs_list: AttrsList) -> bool {
if attrs_list != self.attrs_list {
self.attrs_list = attrs_list;
self.reset();
true
} else {
false
}
}
pub fn wrap_simple(&self) -> bool {
self.wrap_simple
}
pub fn set_wrap_simple(&mut self, wrap_simple: bool) -> bool {
if wrap_simple != self.wrap_simple {
self.wrap_simple = wrap_simple;
self.reset();
true
} else {
false
}
}
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() {
self.attrs_list.add_span(len..len + other.text().len(), other.attrs_list.defaults());
}
for (other_range, attrs) in other.attrs_list.spans() {
let range = other_range.start + len..other_range.end + len;
self.attrs_list.add_span(range, attrs.as_attrs());
}
self.reset();
}
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);
new.wrap_simple = self.wrap_simple;
new
}
pub fn reset(&mut self) {
self.shape_opt = None;
self.layout_opt = None;
}
pub fn reset_layout(&mut self) {
self.layout_opt = None;
}
pub fn is_reset(&self) -> bool {
self.shape_opt.is_none()
}
pub fn shape(&mut self, font_system: &FontSystem) -> &ShapeLine {
if self.shape_opt.is_none() {
self.shape_opt = Some(ShapeLine::new(font_system, &self.text, &self.attrs_list));
self.layout_opt = None;
}
self.shape_opt.as_ref().expect("shape not found")
}
pub fn shape_opt(&self) -> &Option<ShapeLine> {
&self.shape_opt
}
pub fn layout(&mut self, font_system: &FontSystem, font_size: i32, width: i32) -> &[LayoutLine] {
if self.layout_opt.is_none() {
let wrap_simple = self.wrap_simple;
let shape = self.shape(font_system);
let layout = shape.layout(
font_size,
width,
wrap_simple
);
self.layout_opt = Some(layout);
}
self.layout_opt.as_ref().expect("layout not found")
}
pub fn layout_opt(&self) -> &Option<Vec<LayoutLine>> {
&self.layout_opt
}
}