line_wrapper.rs

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