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