1use crate::{FontId, FontRun, Pixels, PlatformTextSystem, SharedString, TextRun, px};
2use collections::HashMap;
3use std::{borrow::Cow, iter, sync::Arc};
4
5/// The GPUI line wrapper, used to wrap lines of text to a given width.
6pub struct LineWrapper {
7 platform_text_system: Arc<dyn PlatformTextSystem>,
8 pub(crate) font_id: FontId,
9 pub(crate) font_size: Pixels,
10 cached_ascii_char_widths: [Option<Pixels>; 128],
11 cached_other_char_widths: HashMap<char, Pixels>,
12}
13
14impl LineWrapper {
15 /// The maximum indent that can be applied to a line.
16 pub const MAX_INDENT: u32 = 256;
17
18 pub(crate) fn new(
19 font_id: FontId,
20 font_size: Pixels,
21 text_system: Arc<dyn PlatformTextSystem>,
22 ) -> Self {
23 Self {
24 platform_text_system: text_system,
25 font_id,
26 font_size,
27 cached_ascii_char_widths: [None; 128],
28 cached_other_char_widths: HashMap::default(),
29 }
30 }
31
32 /// Wrap a line of text to the given width with this wrapper's font and font size.
33 pub fn wrap_line<'a>(
34 &'a mut self,
35 fragments: &'a [LineFragment],
36 wrap_width: Pixels,
37 ) -> impl Iterator<Item = Boundary> + 'a {
38 let mut width = px(0.);
39 let mut first_non_whitespace_ix = None;
40 let mut indent = None;
41 let mut last_candidate_ix = 0;
42 let mut last_candidate_width = px(0.);
43 let mut last_wrap_ix = 0;
44 let mut prev_c = '\0';
45 let mut index = 0;
46 let mut candidates = fragments
47 .iter()
48 .flat_map(move |fragment| fragment.wrap_boundary_candidates())
49 .peekable();
50 iter::from_fn(move || {
51 for candidate in candidates.by_ref() {
52 let ix = index;
53 index += candidate.len_utf8();
54 let mut new_prev_c = prev_c;
55 let item_width = match candidate {
56 WrapBoundaryCandidate::Char { character: c } => {
57 if c == '\n' {
58 continue;
59 }
60
61 if Self::is_word_char(c) {
62 if prev_c == ' ' && c != ' ' && first_non_whitespace_ix.is_some() {
63 last_candidate_ix = ix;
64 last_candidate_width = width;
65 }
66 } else {
67 // CJK may not be space separated, e.g.: `Hello world你好世界`
68 if c != ' ' && first_non_whitespace_ix.is_some() {
69 last_candidate_ix = ix;
70 last_candidate_width = width;
71 }
72 }
73
74 if c != ' ' && first_non_whitespace_ix.is_none() {
75 first_non_whitespace_ix = Some(ix);
76 }
77
78 new_prev_c = c;
79
80 self.width_for_char(c)
81 }
82 WrapBoundaryCandidate::Element {
83 width: element_width,
84 ..
85 } => {
86 if prev_c == ' ' && first_non_whitespace_ix.is_some() {
87 last_candidate_ix = ix;
88 last_candidate_width = width;
89 }
90
91 if first_non_whitespace_ix.is_none() {
92 first_non_whitespace_ix = Some(ix);
93 }
94
95 element_width
96 }
97 };
98
99 width += item_width;
100 if width > wrap_width && ix > last_wrap_ix {
101 if let (None, Some(first_non_whitespace_ix)) = (indent, first_non_whitespace_ix)
102 {
103 indent = Some(
104 Self::MAX_INDENT.min((first_non_whitespace_ix - last_wrap_ix) as u32),
105 );
106 }
107
108 if last_candidate_ix > 0 {
109 last_wrap_ix = last_candidate_ix;
110 width -= last_candidate_width;
111 last_candidate_ix = 0;
112 } else {
113 last_wrap_ix = ix;
114 width = item_width;
115 }
116
117 if let Some(indent) = indent {
118 width += self.width_for_char(' ') * indent as f32;
119 }
120
121 return Some(Boundary::new(last_wrap_ix, indent.unwrap_or(0)));
122 }
123
124 prev_c = new_prev_c;
125 }
126
127 None
128 })
129 }
130
131 /// Truncate a line of text to the given width with this wrapper's font and font size.
132 pub fn truncate_line<'a>(
133 &mut self,
134 line: SharedString,
135 truncate_width: Pixels,
136 truncation_suffix: &str,
137 runs: &'a [TextRun],
138 ) -> (SharedString, Cow<'a, [TextRun]>) {
139 let mut width = px(0.);
140 let mut suffix_width = truncation_suffix
141 .chars()
142 .map(|c| self.width_for_char(c))
143 .fold(px(0.0), |a, x| a + x);
144 let mut char_indices = line.char_indices();
145 let mut truncate_ix = 0;
146 for (ix, c) in char_indices {
147 if width + suffix_width < truncate_width {
148 truncate_ix = ix;
149 }
150
151 let char_width = self.width_for_char(c);
152 width += char_width;
153
154 if width.floor() > truncate_width {
155 let result =
156 SharedString::from(format!("{}{}", &line[..truncate_ix], truncation_suffix));
157 let mut runs = runs.to_vec();
158 update_runs_after_truncation(&result, truncation_suffix, &mut runs);
159
160 return (result, Cow::Owned(runs));
161 }
162 }
163
164 (line, Cow::Borrowed(runs))
165 }
166
167 /// Any character in this list should be treated as a word character,
168 /// meaning it can be part of a word that should not be wrapped.
169 pub(crate) fn is_word_char(c: char) -> bool {
170 // ASCII alphanumeric characters, for English, numbers: `Hello123`, etc.
171 c.is_ascii_alphanumeric() ||
172 // Latin script in Unicode for French, German, Spanish, etc.
173 // Latin-1 Supplement
174 // https://en.wikipedia.org/wiki/Latin-1_Supplement
175 matches!(c, '\u{00C0}'..='\u{00FF}') ||
176 // Latin Extended-A
177 // https://en.wikipedia.org/wiki/Latin_Extended-A
178 matches!(c, '\u{0100}'..='\u{017F}') ||
179 // Latin Extended-B
180 // https://en.wikipedia.org/wiki/Latin_Extended-B
181 matches!(c, '\u{0180}'..='\u{024F}') ||
182 // Cyrillic for Russian, Ukrainian, etc.
183 // https://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode
184 matches!(c, '\u{0400}'..='\u{04FF}') ||
185 // Some other known special characters that should be treated as word characters,
186 // e.g. `a-b`, `var_name`, `I'm`, '@mention`, `#hashtag`, `100%`, `3.1415`,
187 // `2^3`, `a~b`, `a=1`, `Self::new`, etc.
188 matches!(c, '-' | '_' | '.' | '\'' | '$' | '%' | '@' | '#' | '^' | '~' | ',' | '=' | ':') ||
189 // `⋯` character is special used in Zed, to keep this at the end of the line.
190 matches!(c, '⋯')
191 }
192
193 #[inline(always)]
194 fn width_for_char(&mut self, c: char) -> Pixels {
195 if (c as u32) < 128 {
196 if let Some(cached_width) = self.cached_ascii_char_widths[c as usize] {
197 cached_width
198 } else {
199 let width = self.compute_width_for_char(c);
200 self.cached_ascii_char_widths[c as usize] = Some(width);
201 width
202 }
203 } else if let Some(cached_width) = self.cached_other_char_widths.get(&c) {
204 *cached_width
205 } else {
206 let width = self.compute_width_for_char(c);
207 self.cached_other_char_widths.insert(c, width);
208 width
209 }
210 }
211
212 fn compute_width_for_char(&self, c: char) -> Pixels {
213 let mut buffer = [0; 4];
214 let buffer = c.encode_utf8(&mut buffer);
215 self.platform_text_system
216 .layout_line(
217 buffer,
218 self.font_size,
219 &[FontRun {
220 len: buffer.len(),
221 font_id: self.font_id,
222 }],
223 )
224 .width
225 }
226}
227
228fn update_runs_after_truncation(result: &str, ellipsis: &str, runs: &mut Vec<TextRun>) {
229 let mut truncate_at = result.len() - ellipsis.len();
230 for (run_index, run) in runs.iter_mut().enumerate() {
231 if run.len <= truncate_at {
232 truncate_at -= run.len;
233 } else {
234 run.len = truncate_at + ellipsis.len();
235 runs.truncate(run_index + 1);
236 break;
237 }
238 }
239}
240
241/// A fragment of a line that can be wrapped.
242pub enum LineFragment<'a> {
243 /// A text fragment consisting of characters.
244 Text {
245 /// The text content of the fragment.
246 text: &'a str,
247 },
248 /// A non-text element with a fixed width.
249 Element {
250 /// The width of the element in pixels.
251 width: Pixels,
252 /// The UTF-8 encoded length of the element.
253 len_utf8: usize,
254 },
255}
256
257impl<'a> LineFragment<'a> {
258 /// Creates a new text fragment from the given text.
259 pub fn text(text: &'a str) -> Self {
260 LineFragment::Text { text }
261 }
262
263 /// Creates a new non-text element with the given width and UTF-8 encoded length.
264 pub fn element(width: Pixels, len_utf8: usize) -> Self {
265 LineFragment::Element { width, len_utf8 }
266 }
267
268 fn wrap_boundary_candidates(&self) -> impl Iterator<Item = WrapBoundaryCandidate> {
269 let text = match self {
270 LineFragment::Text { text } => text,
271 LineFragment::Element { .. } => "\0",
272 };
273 text.chars().map(move |character| {
274 if let LineFragment::Element { width, len_utf8 } = self {
275 WrapBoundaryCandidate::Element {
276 width: *width,
277 len_utf8: *len_utf8,
278 }
279 } else {
280 WrapBoundaryCandidate::Char { character }
281 }
282 })
283 }
284}
285
286enum WrapBoundaryCandidate {
287 Char { character: char },
288 Element { width: Pixels, len_utf8: usize },
289}
290
291impl WrapBoundaryCandidate {
292 pub fn len_utf8(&self) -> usize {
293 match self {
294 WrapBoundaryCandidate::Char { character } => character.len_utf8(),
295 WrapBoundaryCandidate::Element { len_utf8: len, .. } => *len,
296 }
297 }
298}
299
300/// A boundary between two lines of text.
301#[derive(Copy, Clone, Debug, PartialEq, Eq)]
302pub struct Boundary {
303 /// The index of the last character in a line
304 pub ix: usize,
305 /// The indent of the next line.
306 pub next_indent: u32,
307}
308
309impl Boundary {
310 fn new(ix: usize, next_indent: u32) -> Self {
311 Self { ix, next_indent }
312 }
313}
314
315#[cfg(test)]
316mod tests {
317 use super::*;
318 use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font};
319 #[cfg(target_os = "macos")]
320 use crate::{TextRun, WindowTextSystem, WrapBoundary};
321 use rand::prelude::*;
322
323 fn build_wrapper() -> LineWrapper {
324 let dispatcher = TestDispatcher::new(StdRng::seed_from_u64(0));
325 let cx = TestAppContext::build(dispatcher, None);
326 let id = cx.text_system().resolve_font(&font(".ZedMono"));
327 LineWrapper::new(id, px(16.), cx.text_system().platform_text_system.clone())
328 }
329
330 fn generate_test_runs(input_run_len: &[usize]) -> Vec<TextRun> {
331 input_run_len
332 .iter()
333 .map(|run_len| TextRun {
334 len: *run_len,
335 font: Font {
336 family: "Dummy".into(),
337 features: FontFeatures::default(),
338 fallbacks: None,
339 weight: FontWeight::default(),
340 style: FontStyle::Normal,
341 },
342 ..Default::default()
343 })
344 .collect()
345 }
346
347 #[test]
348 fn test_wrap_line() {
349 let mut wrapper = build_wrapper();
350
351 assert_eq!(
352 wrapper
353 .wrap_line(&[LineFragment::text("aa bbb cccc ddddd eeee")], px(72.))
354 .collect::<Vec<_>>(),
355 &[
356 Boundary::new(7, 0),
357 Boundary::new(12, 0),
358 Boundary::new(18, 0)
359 ],
360 );
361 assert_eq!(
362 wrapper
363 .wrap_line(&[LineFragment::text("aaa aaaaaaaaaaaaaaaaaa")], px(72.0))
364 .collect::<Vec<_>>(),
365 &[
366 Boundary::new(4, 0),
367 Boundary::new(11, 0),
368 Boundary::new(18, 0)
369 ],
370 );
371 assert_eq!(
372 wrapper
373 .wrap_line(&[LineFragment::text(" aaaaaaa")], px(72.))
374 .collect::<Vec<_>>(),
375 &[
376 Boundary::new(7, 5),
377 Boundary::new(9, 5),
378 Boundary::new(11, 5),
379 ]
380 );
381 assert_eq!(
382 wrapper
383 .wrap_line(
384 &[LineFragment::text(" ")],
385 px(72.)
386 )
387 .collect::<Vec<_>>(),
388 &[
389 Boundary::new(7, 0),
390 Boundary::new(14, 0),
391 Boundary::new(21, 0)
392 ]
393 );
394 assert_eq!(
395 wrapper
396 .wrap_line(&[LineFragment::text(" aaaaaaaaaaaaaa")], px(72.))
397 .collect::<Vec<_>>(),
398 &[
399 Boundary::new(7, 0),
400 Boundary::new(14, 3),
401 Boundary::new(18, 3),
402 Boundary::new(22, 3),
403 ]
404 );
405
406 // Test wrapping multiple text fragments
407 assert_eq!(
408 wrapper
409 .wrap_line(
410 &[
411 LineFragment::text("aa bbb "),
412 LineFragment::text("cccc ddddd eeee")
413 ],
414 px(72.)
415 )
416 .collect::<Vec<_>>(),
417 &[
418 Boundary::new(7, 0),
419 Boundary::new(12, 0),
420 Boundary::new(18, 0)
421 ],
422 );
423
424 // Test wrapping with a mix of text and element fragments
425 assert_eq!(
426 wrapper
427 .wrap_line(
428 &[
429 LineFragment::text("aa "),
430 LineFragment::element(px(20.), 1),
431 LineFragment::text(" bbb "),
432 LineFragment::element(px(30.), 1),
433 LineFragment::text(" cccc")
434 ],
435 px(72.)
436 )
437 .collect::<Vec<_>>(),
438 &[
439 Boundary::new(5, 0),
440 Boundary::new(9, 0),
441 Boundary::new(11, 0)
442 ],
443 );
444
445 // Test with element at the beginning and text afterward
446 assert_eq!(
447 wrapper
448 .wrap_line(
449 &[
450 LineFragment::element(px(50.), 1),
451 LineFragment::text(" aaaa bbbb cccc dddd")
452 ],
453 px(72.)
454 )
455 .collect::<Vec<_>>(),
456 &[
457 Boundary::new(2, 0),
458 Boundary::new(7, 0),
459 Boundary::new(12, 0),
460 Boundary::new(17, 0)
461 ],
462 );
463
464 // Test with a large element that forces wrapping by itself
465 assert_eq!(
466 wrapper
467 .wrap_line(
468 &[
469 LineFragment::text("short text "),
470 LineFragment::element(px(100.), 1),
471 LineFragment::text(" more text")
472 ],
473 px(72.)
474 )
475 .collect::<Vec<_>>(),
476 &[
477 Boundary::new(6, 0),
478 Boundary::new(11, 0),
479 Boundary::new(12, 0),
480 Boundary::new(18, 0)
481 ],
482 );
483 }
484
485 #[test]
486 fn test_truncate_line() {
487 let mut wrapper = build_wrapper();
488
489 fn perform_test(
490 wrapper: &mut LineWrapper,
491 text: &'static str,
492 expected: &'static str,
493 ellipsis: &str,
494 ) {
495 let dummy_run_lens = vec![text.len()];
496 let dummy_runs = generate_test_runs(&dummy_run_lens);
497 let (result, dummy_runs) =
498 wrapper.truncate_line(text.into(), px(220.), ellipsis, &dummy_runs);
499 assert_eq!(result, expected);
500 assert_eq!(dummy_runs.first().unwrap().len, result.len());
501 }
502
503 perform_test(
504 &mut wrapper,
505 "aa bbb cccc ddddd eeee ffff gggg",
506 "aa bbb cccc ddddd eeee",
507 "",
508 );
509 perform_test(
510 &mut wrapper,
511 "aa bbb cccc ddddd eeee ffff gggg",
512 "aa bbb cccc ddddd eee…",
513 "…",
514 );
515 perform_test(
516 &mut wrapper,
517 "aa bbb cccc ddddd eeee ffff gggg",
518 "aa bbb cccc dddd......",
519 "......",
520 );
521 }
522
523 #[test]
524 fn test_truncate_multiple_runs() {
525 let mut wrapper = build_wrapper();
526
527 fn perform_test(
528 wrapper: &mut LineWrapper,
529 text: &'static str,
530 expected: &str,
531 run_lens: &[usize],
532 result_run_len: &[usize],
533 line_width: Pixels,
534 ) {
535 let dummy_runs = generate_test_runs(run_lens);
536 let (result, dummy_runs) =
537 wrapper.truncate_line(text.into(), line_width, "…", &dummy_runs);
538 assert_eq!(result, expected);
539 for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
540 assert_eq!(run.len, *result_len);
541 }
542 }
543 // Case 0: Normal
544 // Text: abcdefghijkl
545 // Runs: Run0 { len: 12, ... }
546 //
547 // Truncate res: abcd… (truncate_at = 4)
548 // Run res: Run0 { string: abcd…, len: 7, ... }
549 perform_test(&mut wrapper, "abcdefghijkl", "abcd…", &[12], &[7], px(50.));
550 // Case 1: Drop some runs
551 // Text: abcdefghijkl
552 // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
553 //
554 // Truncate res: abcdef… (truncate_at = 6)
555 // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: ef…, len:
556 // 5, ... }
557 perform_test(
558 &mut wrapper,
559 "abcdefghijkl",
560 "abcdef…",
561 &[4, 4, 4],
562 &[4, 5],
563 px(70.),
564 );
565 // Case 2: Truncate at start of some run
566 // Text: abcdefghijkl
567 // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
568 //
569 // Truncate res: abcdefgh… (truncate_at = 8)
570 // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: efgh, len:
571 // 4, ... }, Run2 { string: …, len: 3, ... }
572 perform_test(
573 &mut wrapper,
574 "abcdefghijkl",
575 "abcdefgh…",
576 &[4, 4, 4],
577 &[4, 4, 3],
578 px(90.),
579 );
580 }
581
582 #[test]
583 fn test_update_run_after_truncation() {
584 fn perform_test(result: &str, run_lens: &[usize], result_run_lens: &[usize]) {
585 let mut dummy_runs = generate_test_runs(run_lens);
586 update_runs_after_truncation(result, "…", &mut dummy_runs);
587 for (run, result_len) in dummy_runs.iter().zip(result_run_lens) {
588 assert_eq!(run.len, *result_len);
589 }
590 }
591 // Case 0: Normal
592 // Text: abcdefghijkl
593 // Runs: Run0 { len: 12, ... }
594 //
595 // Truncate res: abcd… (truncate_at = 4)
596 // Run res: Run0 { string: abcd…, len: 7, ... }
597 perform_test("abcd…", &[12], &[7]);
598 // Case 1: Drop some runs
599 // Text: abcdefghijkl
600 // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
601 //
602 // Truncate res: abcdef… (truncate_at = 6)
603 // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: ef…, len:
604 // 5, ... }
605 perform_test("abcdef…", &[4, 4, 4], &[4, 5]);
606 // Case 2: Truncate at start of some run
607 // Text: abcdefghijkl
608 // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
609 //
610 // Truncate res: abcdefgh… (truncate_at = 8)
611 // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: efgh, len:
612 // 4, ... }, Run2 { string: …, len: 3, ... }
613 perform_test("abcdefgh…", &[4, 4, 4], &[4, 4, 3]);
614 }
615
616 #[test]
617 fn test_is_word_char() {
618 #[track_caller]
619 fn assert_word(word: &str) {
620 for c in word.chars() {
621 assert!(LineWrapper::is_word_char(c), "assertion failed for '{}'", c);
622 }
623 }
624
625 #[track_caller]
626 fn assert_not_word(word: &str) {
627 let found = word.chars().any(|c| !LineWrapper::is_word_char(c));
628 assert!(found, "assertion failed for '{}'", word);
629 }
630
631 assert_word("Hello123");
632 assert_word("non-English");
633 assert_word("var_name");
634 assert_word("123456");
635 assert_word("3.1415");
636 assert_word("10^2");
637 assert_word("1~2");
638 assert_word("100%");
639 assert_word("@mention");
640 assert_word("#hashtag");
641 assert_word("$variable");
642 assert_word("a=1");
643 assert_word("Self::is_word_char");
644 assert_word("more⋯");
645
646 // Space
647 assert_not_word("foo bar");
648
649 // URL case
650 assert_word("github.com");
651 assert_not_word("zed-industries/zed");
652 assert_not_word("zed-industries\\zed");
653 assert_not_word("a=1&b=2");
654 assert_not_word("foo?b=2");
655
656 // Latin-1 Supplement
657 assert_word("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ");
658 // Latin Extended-A
659 assert_word("ĀāĂ㥹ĆćĈĉĊċČčĎď");
660 // Latin Extended-B
661 assert_word("ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ");
662 // Cyrillic
663 assert_word("АБВГДЕЖЗИЙКЛМНОП");
664
665 // non-word characters
666 assert_not_word("你好");
667 assert_not_word("안녕하세요");
668 assert_not_word("こんにちは");
669 assert_not_word("😀😁😂");
670 assert_not_word("()[]{}<>");
671 }
672
673 // For compatibility with the test macro
674 #[cfg(target_os = "macos")]
675 use crate as gpui;
676
677 // These seem to vary wildly based on the text system.
678 #[cfg(target_os = "macos")]
679 #[crate::test]
680 fn test_wrap_shaped_line(cx: &mut TestAppContext) {
681 cx.update(|cx| {
682 let text_system = WindowTextSystem::new(cx.text_system().clone());
683
684 let normal = TextRun {
685 len: 0,
686 font: font("Helvetica"),
687 color: Default::default(),
688 underline: Default::default(),
689 ..Default::default()
690 };
691 let bold = TextRun {
692 len: 0,
693 font: font("Helvetica").bold(),
694 ..Default::default()
695 };
696
697 let text = "aa bbb cccc ddddd eeee".into();
698 let lines = text_system
699 .shape_text(
700 text,
701 px(16.),
702 &[
703 normal.with_len(4),
704 bold.with_len(5),
705 normal.with_len(6),
706 bold.with_len(1),
707 normal.with_len(7),
708 ],
709 Some(px(72.)),
710 None,
711 )
712 .unwrap();
713
714 assert_eq!(
715 lines[0].layout.wrap_boundaries(),
716 &[
717 WrapBoundary {
718 run_ix: 0,
719 glyph_ix: 7
720 },
721 WrapBoundary {
722 run_ix: 0,
723 glyph_ix: 12
724 },
725 WrapBoundary {
726 run_ix: 0,
727 glyph_ix: 18
728 }
729 ],
730 );
731 });
732 }
733}