canvist_layout 0.2.1

Incremental text and block layout for the canvist editor: line breaking, bidi, hit testing, display lists, and SVG/HTML renderers
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Greedy line breaking with CSS `white-space: pre-wrap` and
//! `overflow-wrap: break-word` semantics.
//!
//! - Lines break only at UAX #14 opportunities, filling each line greedily as
//!   browsers do for `text-wrap: wrap`.
//! - Trailing spaces and tabs *hang*: they stay on the line but never cause
//!   it to overflow.
//! - A word wider than the whole line is split at grapheme boundaries.
//! - Tabs advance to the next multiple of `tab-size` space widths, skipping a
//!   stop closer than half a space, per CSS Text 3.

use std::ops::Range;

use super::items::BreakAfter;
use super::items::Item;
use super::items::ItemKind;
use super::items::grapheme_boundaries;
use super::items::slice;

/// Widths within this tolerance of the available width still fit, so float
/// rounding in summed advances never wraps a line that fits exactly.
const EPSILON: f32 = 1.0 / 128.0;

/// One line before metrics and reordering.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct RawLine {
	/// Byte range including trailing spaces and any forced break character.
	pub(crate) range: Range<u32>,
	/// The last caret offset on the line: before a forced break character,
	/// otherwise the end of the range.
	pub(crate) caret_end: u32,
	/// Whether the line ended at a soft wrap, so `range.end` is also the
	/// first offset of the next line.
	pub(crate) soft: bool,
}

/// Where tab stops fall.
#[derive(Clone, Copy, Debug)]
pub(crate) struct TabStops {
	/// Distance between stops.
	pub(crate) interval: f32,
	/// The width of a space, for the half-space minimum.
	pub(crate) space: f32,
}

impl TabStops {
	/// The width of a tab starting at `x`.
	fn width_at(self, x: f32) -> f32 {
		if self.interval <= 0.0 {
			return 0.0;
		}

		let mut next = ((x / self.interval).floor() + 1.0) * self.interval;

		if next - x < self.space / 2.0 {
			next += self.interval;
		}

		next - x
	}
}

/// A position inside the item list.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Cursor {
	/// The item the position falls in.
	pub(crate) item: usize,
	/// The byte offset in the block text.
	pub(crate) offset: u32,
}

/// Break `items` into lines no wider than `available`.
pub(crate) fn break_lines(
	text: &str,
	items: &mut [Item],
	available: f32,
	tabs: TabStops,
) -> Vec<RawLine> {
	let mut breaker = Breaker {
		text,
		items,
		available,
		tabs,
		lines: Vec::new(),
		line_start: 0,
		x: 0.0,
	};
	breaker.run();
	breaker.lines
}

/// Break lines from `start`, the first offset of a line, until `resume`
/// accepts the start of a new line. Returns the lines broken and, if
/// breaking stopped early, the offset where the next line starts.
///
/// Greedy breaking only depends on where a line starts, so once a new line
/// starts where an old one did over unchanged items, every line after it is
/// unchanged too and the caller can reuse them.
pub(crate) fn break_from(
	text: &str,
	items: &mut [Item],
	start: Cursor,
	lines: Lines,
	mut resume: impl FnMut(u32) -> bool,
) -> (Vec<RawLine>, Option<u32>) {
	let mut breaker = Breaker {
		text,
		items,
		available: lines.available,
		tabs: lines.tabs,
		lines: Vec::new(),
		line_start: start.offset,
		x: 0.0,
	};
	let mut cursor = start;

	while cursor.item < breaker.items.len() {
		let finished = breaker.lines.len();
		cursor = breaker.place_unit(cursor);

		if breaker.lines.len() > finished && resume(breaker.line_start) {
			return (breaker.lines, Some(breaker.line_start));
		}
	}

	breaker.finish_text();

	(breaker.lines, None)
}

/// How lines are filled.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Lines {
	/// The width lines must fit.
	pub(crate) available: f32,
	/// Where tabs stop.
	pub(crate) tabs: TabStops,
}

struct Breaker<'a> {
	text: &'a str,
	items: &'a mut [Item],
	available: f32,
	tabs: TabStops,
	lines: Vec<RawLine>,
	line_start: u32,
	/// Width of the current line so far, including trailing spaces.
	x: f32,
}

impl Breaker<'_> {
	fn run(&mut self) {
		let mut cursor = Cursor { item: 0, offset: 0 };

		while cursor.item < self.items.len() {
			cursor = self.place_unit(cursor);
		}

		self.finish_text();
	}

	/// End the last line at the end of the text.
	fn finish_text(&mut self) {
		let end = self.text.len() as u32;
		self.lines.push(RawLine {
			range: self.line_start..end,
			caret_end: end,
			soft: false,
		});
	}

	/// Place the unbreakable unit at `cursor` and return where the next one
	/// starts. A unit that does not fit wraps to a new line first, and a
	/// unit wider than a whole line is cut at a grapheme boundary instead,
	/// leaving its rest to the next call.
	fn place_unit(&mut self, cursor: Cursor) -> Cursor {
		let last = self.unit_end(cursor.item);
		let (width, hang) = self.measure_unit(cursor, last, self.x);
		let has_content = self.x > 0.0 || cursor.offset > self.line_start;

		if has_content && self.x + width - hang > self.available + EPSILON {
			self.finish(cursor.offset, cursor.offset, true);

			return cursor;
		}

		let overflows = !has_content && width - hang > self.available + EPSILON;

		if let Some(cut) = overflows
			.then(|| self.emergency_cut(cursor, last))
			.flatten()
		{
			self.finish(cut, cut, true);

			return self.cursor_at(cursor.item, cut);
		}

		self.x += width;
		let item = &self.items[last];
		let end = item.range.end;

		if item.break_after == BreakAfter::Hard {
			self.finish(end, item.range.start, false);
		}

		Cursor {
			item: last + 1,
			offset: end,
		}
	}

	fn finish(&mut self, end: u32, caret_end: u32, soft: bool) {
		self.lines.push(RawLine {
			range: self.line_start..end,
			caret_end,
			soft,
		});
		self.line_start = end;
		self.x = 0.0;
	}

	/// The index of the last item of the unbreakable unit starting at `first`.
	fn unit_end(&self, first: usize) -> usize {
		self.items[first..]
			.iter()
			.position(|item| item.break_after != BreakAfter::None)
			.map_or(self.items.len() - 1, |index| first + index)
	}

	fn cursor_at(&self, from: usize, offset: u32) -> Cursor {
		let item = self.items[from..]
			.iter()
			.position(|item| item.range.end > offset)
			.map_or(self.items.len(), |index| from + index);

		Cursor { item, offset }
	}

	/// The width of the unit from `cursor` through item `last`, and how much
	/// of that is hanging white space. Lays out tabs starting at `x`.
	fn measure_unit(&mut self, cursor: Cursor, last: usize, x: f32) -> (f32, f32) {
		let mut width = 0.0;

		for item in &mut self.items[cursor.item..=last] {
			if item.kind == ItemKind::Tab {
				item.width = self.tabs.width_at(x + width);
			}

			width += item.span(self.text, cursor.offset, item.range.end);
		}

		let mut hang = 0.0;

		for item in self.items[cursor.item..=last].iter().rev() {
			if item.is_space(self.text) {
				hang += item.span(self.text, cursor.offset, item.range.end);
				continue;
			}

			hang += item.trailing_space(self.text, cursor.offset);
			break;
		}

		(width, hang)
	}

	/// The furthest grapheme boundary after `cursor` that keeps the line
	/// within the available width, taking at least one grapheme. `None` when
	/// the unit is a single grapheme.
	fn emergency_cut(&self, cursor: Cursor, last: usize) -> Option<u32> {
		let unit_end = self.items[last].range.end;
		let mut width = 0.0;
		let mut cut = None;
		let steps = self.items[cursor.item..=last]
			.iter()
			.flat_map(|item| self.cut_steps(item, cursor.offset));

		for (item, step) in steps {
			width += item.span(self.text, step.start, step.end);

			let fits = width <= self.available + EPSILON;

			if (fits || cut.is_none()) && step.end < unit_end {
				cut = Some(step.end);
			}

			if !fits {
				return cut;
			}
		}

		cut
	}

	/// The ranges between the places an emergency cut may fall in `item`
	/// from `offset` on: every grapheme boundary of text, but only the end
	/// of an atomic item.
	fn cut_steps<'item>(
		&self,
		item: &'item Item,
		offset: u32,
	) -> impl Iterator<Item = (&'item Item, Range<u32>)> {
		let start = item.range.start.max(offset);

		let boundaries = if matches!(item.kind, ItemKind::Text { .. }) {
			let local = slice(self.text, &(start..item.range.end));
			grapheme_boundaries(local)
				.into_iter()
				.skip(1)
				.map(|boundary| start + boundary)
				.collect()
		} else {
			vec![item.range.end]
		};

		boundaries
			.into_iter()
			.scan(start, move |previous, boundary| {
				let step = *previous..boundary;
				*previous = boundary;
				Some((item, step))
			})
	}
}

#[cfg(test)]
#[allow(
	clippy::float_cmp,
	reason = "the values compared are exactly representable"
)]
mod tests {
	use std::sync::Arc;

	use super::*;
	use crate::FontId;
	use crate::measure::Measured;

	/// Items for `text` split after spaces, every char 10 px wide.
	fn items(text: &str) -> Vec<Item> {
		let mut items = Vec::new();
		let mut start = 0;

		for (index, c) in text.char_indices() {
			let end = index + c.len_utf8();
			let boundary = c == ' ' && !text[end..].starts_with(' ');

			if boundary || end == text.len() {
				items.push(item(text, start..end));
				start = end;
			}
		}

		items
	}

	fn item(text: &str, range: Range<usize>) -> Item {
		let count = text[range.clone()].chars().count();
		let advances = vec![10.0; count].into_boxed_slice();
		Item {
			range: range.start as u32..range.end as u32,
			style: 0,
			level: 0,
			kind: ItemKind::Text {
				font: FontId(0),
				measured: Arc::new(Measured {
					width: 10.0 * count as f32,
					advances,
				}),
			},
			width: 10.0 * count as f32,
			kern: 0.0,
			break_after: BreakAfter::Soft,
		}
	}

	fn lines(text: &str, available: f32) -> Vec<&str> {
		let mut items = items(text);
		let tabs = TabStops {
			interval: 80.0,
			space: 10.0,
		};
		break_lines(text, &mut items, available, tabs)
			.into_iter()
			.map(|line| &text[line.range.start as usize..line.range.end as usize])
			.collect()
	}

	#[test]
	fn wraps_greedily_with_hanging_spaces() {
		assert_eq!(lines("aaa bbb ccc", 70.0), ["aaa bbb ", "ccc"]);
		assert_eq!(lines("aaa bbb   ccc", 70.0), ["aaa bbb   ", "ccc"]);
		assert_eq!(lines("aaa bbb", 1000.0), ["aaa bbb"]);
	}

	#[test]
	fn splits_words_wider_than_the_line() {
		assert_eq!(lines("abcdefgh ij", 30.0), ["abc", "def", "gh ", "ij"]);
		assert_eq!(lines("x", 1.0), ["x"]);
	}

	#[test]
	fn empty_text_has_one_line() {
		assert_eq!(lines("", 100.0), [""]);
	}

	#[test]
	fn tab_stops_skip_close_stops() {
		let tabs = TabStops {
			interval: 80.0,
			space: 10.0,
		};

		assert_eq!(tabs.width_at(0.0), 80.0);
		assert_eq!(tabs.width_at(30.0), 50.0);
		assert_eq!(tabs.width_at(76.0), 84.0);
	}
}