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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use crate::{
alignment::TextAlignment,
parser::Token,
rendering::{EmptySpaceIterator, StateFactory, StyledCharacterIterator, StyledTextBoxIterator},
style::StyledTextBox,
utils::{font_ext::FontExt, rect_ext::RectExt},
};
use embedded_graphics::prelude::*;
use core::str::Chars;
#[derive(Copy, Clone, Debug)]
pub struct Justified;
impl TextAlignment for Justified {}
#[derive(Copy, Clone, Debug)]
pub struct SpaceInfo {
pub space_width: u32,
pub space_count: u32,
pub remaining_space_width: u32,
}
impl SpaceInfo {
#[inline]
#[must_use]
fn default<F: Font>() -> Self {
SpaceInfo::new(F::char_width(' '), 0)
}
#[inline]
#[must_use]
fn new(space_width: u32, extra_pixel_count: u32) -> Self {
SpaceInfo {
space_width: space_width + 1,
space_count: extra_pixel_count,
remaining_space_width: space_width,
}
}
#[inline]
fn space_width(&mut self) -> u32 {
if self.space_count == 0 {
self.remaining_space_width
} else {
self.space_count -= 1;
self.space_width
}
}
#[inline]
fn peek_space_width(&self, whitespace_count: u32) -> u32 {
let above_limit = whitespace_count.saturating_sub(self.space_count);
self.space_width * self.space_count + above_limit * self.remaining_space_width
}
}
#[derive(Debug)]
pub enum JustifiedState<'a, C, F>
where
C: PixelColor,
F: Font + Copy,
{
NextWord(SpaceInfo),
LineBreak(Chars<'a>),
MeasureLine(Chars<'a>),
DrawWord(Chars<'a>, SpaceInfo),
DrawCharacter(Chars<'a>, StyledCharacterIterator<C, F>, SpaceInfo),
DrawWhitespace(u32, EmptySpaceIterator<C, F>, SpaceInfo),
}
impl<'a, C, F> StateFactory for StyledTextBox<'a, C, F, Justified>
where
C: PixelColor,
F: Font + Copy,
{
type PixelIteratorState = JustifiedState<'a, C, F>;
#[inline]
#[must_use]
fn create_state() -> Self::PixelIteratorState {
JustifiedState::MeasureLine("".chars())
}
}
impl<C, F> Iterator for StyledTextBoxIterator<'_, C, F, Justified>
where
C: PixelColor,
F: Font + Copy,
{
type Item = Pixel<C>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
if !self.cursor.in_display_area() {
break None;
}
match self.state {
JustifiedState::LineBreak(ref remaining) => {
self.cursor.new_line();
self.state = JustifiedState::MeasureLine(remaining.clone());
}
JustifiedState::MeasureLine(ref remaining) => {
let max_line_width = RectExt::size(self.cursor.bounds).width;
let (mut total_width, fits) = F::max_fitting(remaining.clone(), max_line_width);
let mut total_whitespace_count = 0;
let mut stretch_line = true;
if fits {
let mut last_whitespace_width = 0;
let mut last_whitespace_count = 0;
let mut total_whitespace_width = 0;
for token in self.parser.clone() {
if total_width >= max_line_width {
break;
}
match token {
Token::NewLine => {
stretch_line = false;
break;
}
Token::Whitespace(_) if total_width == 0 => {
}
Token::Whitespace(n) => {
last_whitespace_count = n;
last_whitespace_width =
(n * F::char_width(' ')).min(max_line_width - total_width);
}
Token::Word(w) => {
let word_width = w.chars().map(F::char_width).sum::<u32>();
let new_total_width = total_width + word_width;
let new_whitespace_width =
total_whitespace_width + last_whitespace_width;
if new_whitespace_width + new_total_width > max_line_width {
break;
}
total_width = new_total_width;
total_whitespace_width = new_whitespace_width;
total_whitespace_count += last_whitespace_count;
last_whitespace_count = 0;
last_whitespace_width = 0;
}
}
}
}
let chars = remaining.clone();
if stretch_line && total_whitespace_count != 0 {
let total_space_width = max_line_width - total_width;
let space_width =
(total_space_width / total_whitespace_count).max(F::char_width(' '));
let extra_pixels = total_space_width - space_width * total_whitespace_count;
self.state = JustifiedState::DrawWord(
chars,
SpaceInfo::new(space_width, extra_pixels),
);
} else {
self.state = JustifiedState::DrawWord(chars, SpaceInfo::default::<F>());
}
}
JustifiedState::NextWord(space_info) => {
if let Some(token) = self.parser.next() {
match token {
Token::Word(w) => {
if self
.cursor
.fits_in_line(w.chars().map(F::char_width).sum::<u32>())
{
self.state = JustifiedState::DrawWord(w.chars(), space_info);
} else {
self.state = JustifiedState::LineBreak(w.chars());
}
}
Token::Whitespace(n) => {
let width = space_info.peek_space_width(n);
let mut lookahead = self.parser.clone();
if let Some(Token::Word(w)) = lookahead.next() {
let n_width = w.chars().map(F::char_width).sum::<u32>();
if !self.cursor.fits_in_line(width + n_width) {
self.state = JustifiedState::NextWord(space_info);
} else if n != 0 {
self.state = JustifiedState::DrawWhitespace(
n - 1,
EmptySpaceIterator::new(
self.cursor.position,
width,
self.style.text_style,
),
space_info,
);
}
} else {
}
}
Token::NewLine => {
self.state = JustifiedState::LineBreak("".chars());
}
}
} else {
break None;
}
}
JustifiedState::DrawWord(ref mut chars_iterator, space_info) => {
let mut copy = chars_iterator.clone();
self.state = if let Some(c) = copy.next() {
let width = F::char_width(c);
if self.cursor.fits_in_line(width) {
JustifiedState::DrawCharacter(
copy,
StyledCharacterIterator::new(
c,
self.cursor.position,
self.style.text_style,
),
space_info,
)
} else {
JustifiedState::LineBreak(chars_iterator.clone())
}
} else {
JustifiedState::NextWord(space_info)
}
}
JustifiedState::DrawWhitespace(n, ref mut iterator, mut space_info) => {
if let pixel @ Some(_) = iterator.next() {
break pixel;
}
let width = space_info.space_width();
self.state = if n == 0 {
self.cursor.advance(width);
JustifiedState::NextWord(space_info)
} else if self.cursor.fits_in_line(width) {
self.cursor.advance(width);
JustifiedState::DrawWhitespace(
n - 1,
EmptySpaceIterator::new(
self.cursor.position,
width,
self.style.text_style,
),
space_info,
)
} else {
JustifiedState::LineBreak("".chars())
}
}
JustifiedState::DrawCharacter(ref chars_iterator, ref mut iterator, space_info) => {
if let pixel @ Some(_) = iterator.next() {
break pixel;
}
self.cursor.advance_char(iterator.character);
self.state = JustifiedState::DrawWord(chars_iterator.clone(), space_info);
}
}
}
}
}