From a8873a0536d03832683068367088cb49b64dec39 Mon Sep 17 00:00:00 2001 From: Hojjat Date: Fri, 20 Feb 2026 15:25:02 -0700 Subject: [PATCH] improv: show ellipsis if nothing else fits If buffer is small enough that char+ellipsis doesn't fit, then show ellipsis alone. If buffer is smaller than ellipsis, show nothing. --- src/shape.rs | 15 +++++++++++++++ tests/ellipsize_rendering.rs | 13 +++++++++++++ .../images/ellipsize_ltr_middle_small_buffer.png | 3 +++ 3 files changed, 31 insertions(+) create mode 100644 tests/images/ellipsize_ltr_middle_small_buffer.png diff --git a/src/shape.rs b/src/shape.rs index 3da6682..9b72457 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -1860,11 +1860,26 @@ impl ShapeLine { current_visual_line.w = starting_line.w + ending_line.w + ellipsis_w; current_visual_line.spaces = starting_line.spaces + ending_line.spaces; } + None if forward_pass_overflowed && width > ellipsis_w => { + // buffer is small enough that the forward pass didn't fit + // only show the ellipsis + current_visual_line + .ranges + .push(self.ellipsis_vlrange(if self.rtl { + unicode_bidi::Level::rtl() + } else { + unicode_bidi::Level::ltr() + })); + current_visual_line.ellipsized = true; + current_visual_line.w = ellipsis_w; + current_visual_line.spaces = 0; + } _ => { // everything fit in the forward pass current_visual_line.ranges = starting_line.ranges; current_visual_line.w = starting_line.w; current_visual_line.spaces = starting_line.spaces; + current_visual_line.ellipsized = false; } } } diff --git a/tests/ellipsize_rendering.rs b/tests/ellipsize_rendering.rs index 2e75107..0a45200 100644 --- a/tests/ellipsize_rendering.rs +++ b/tests/ellipsize_rendering.rs @@ -183,3 +183,16 @@ fn test_ellipsize_bidi_isolates_middle_bug() { .canvas(220, 50) .validate_text_rendering(); } + +#[test] +fn test_ellipsize_ltr_middle_small_buffer() { + let attrs = Attrs::new().family(Family::Name("Inter")); + DrawTestCfg::new("ellipsize_ltr_middle_small_buffer") + .font_size(20., 26.) + .font_attrs(attrs) + .text("A/B Tester x8 Mono") + .wrap(Wrap::None) + .ellipsize(Ellipsize::Middle(EllipsizeHeightLimit::Lines(1))) + .canvas(30, 100) + .validate_text_rendering(); +} diff --git a/tests/images/ellipsize_ltr_middle_small_buffer.png b/tests/images/ellipsize_ltr_middle_small_buffer.png new file mode 100644 index 0000000..1bf7e3d --- /dev/null +++ b/tests/images/ellipsize_ltr_middle_small_buffer.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4cfbb42f584b56040a99ddb82eebdecc967a89ce340d393aa651c2a441fd7fcf +size 764