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