From fa981bf9bac1e91955402468644bb0107e4987c5 Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Sun, 22 Nov 2026 23:13:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(desktop):=20=E6=8F=92=E4=BB=B6=E7=94=BB?= =?UTF-8?q?=E5=B8=83=E5=A4=9A=E6=AE=B5=E6=96=87=E5=AD=97=E6=95=B4=E8=A1=8C?= =?UTF-8?q?=E6=94=BE=E5=BE=97=E4=B8=8B=E6=97=B6=E4=B8=8D=E5=86=8D=E6=88=AA?= =?UTF-8?q?=E6=96=AD=E6=9C=80=E5=90=8E=E4=B8=80=E6=AE=B5=20(#2309)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit painter.text 从各段宽度之和里逐段减去已画宽度,把剩余当作下一段的可用宽度。 float32 下剩余值可能比最后一段的宽度小一个舍入误差,整行放得下也会被截成省略号。 现在只有整行超过 maxWidth 才截断,并补回归测试。 --- desktop/internal/pluginview/canvas.go | 10 +++-- desktop/internal/pluginview/canvas_test.go | 47 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/desktop/internal/pluginview/canvas.go b/desktop/internal/pluginview/canvas.go index 568519229..d21dfbf94 100644 --- a/desktop/internal/pluginview/canvas.go +++ b/desktop/internal/pluginview/canvas.go @@ -534,9 +534,13 @@ func (pt *painter) text(sh *Shape, at place, opacity float32) { for _, run := range runs { total += canvas.TextWidthStyle(run.Text, style) } + // Only a line wider than maxWidth is cut. The room left after some runs is a float32 + // difference that can come out a hair under the next run's own width, so a line that + // fits must not fit its runs into that room. room := total - if sh.MaxWidth > 0 { - room = min(total, float32(sh.MaxWidth*at.k)) + cut := sh.MaxWidth > 0 && float32(sh.MaxWidth*at.k) < total + if cut { + room = float32(sh.MaxWidth * at.k) } x, baseline := at.point(sh.X, sh.Y) switch sh.Align { @@ -559,7 +563,7 @@ func (pt *painter) text(sh *Shape, at place, opacity float32) { } style.Color = fade(color, opacity) value, width := run.Text, canvas.TextWidthStyle(run.Text, style) - if width > left { + if cut && width > left { value = pt.fit(value, style, left) width = left } diff --git a/desktop/internal/pluginview/canvas_test.go b/desktop/internal/pluginview/canvas_test.go index 3a3693ef7..42ae5bd8a 100644 --- a/desktop/internal/pluginview/canvas_test.go +++ b/desktop/internal/pluginview/canvas_test.go @@ -2,9 +2,11 @@ package pluginview import ( "math" + "strings" "testing" "github.com/vivaui/viva/gfx" + "github.com/vivaui/viva/ui" ) // testCanvas is a canvas node of a 400 × 400 view box that pans, zooms from half to four @@ -204,3 +206,48 @@ func TestCanvasWheelZoomsOnlyWithCommandOrControl(t *testing.T) { t.Fatal("a canvas that does not ask for viewport sent one") } } + +// A line of runs that fits is drawn whole. The room the runs had left was the line's float32 +// width minus the runs before, which can come out a hair under the last run's own width, and +// fitting the run into that hair cut its last character for an ellipsis: a Python settings +// preview showed "bad-argument-type · 2 erro…" with plenty of room in its bar. +func TestCanvasTextDrawsRunsThatFitWhole(t *testing.T) { + widths := map[string]float32{"bad-argument-type": 101.3, " · 2 errors": 62.7} + canvas := new(gfx.Canvas) + canvas.SetMeasurer(func(value string, _, _ float32) float32 { + if width, ok := widths[value]; ok { + return width + } + return float32(len(value)) * 6 + }) + var total float32 + total += widths["bad-argument-type"] + total += widths[" · 2 errors"] + if total-widths["bad-argument-type"] >= widths[" · 2 errors"] { + t.Fatal("the widths no longer round below the last run; pick others") + } + for _, maxWidth := range []float64{0, 400} { + canvas.Reset() + pt := &painter{d: &ui.DrawContext{Canvas: canvas}, state: &canvasState{}, p: &Palette{}} + pt.text(&Shape{X: 10, Y: 20, Style: "caption", MaxWidth: maxWidth, Runs: []Run{ + {Text: "bad-argument-type"}, {Text: " · 2 errors"}, + }}, place{k: 1}, 1) + var drawn []string + for _, text := range canvas.Texts() { + drawn = append(drawn, text.Value) + } + if len(drawn) != 2 || drawn[0] != "bad-argument-type" || drawn[1] != " · 2 errors" { + t.Fatalf("maxWidth %v drew %q", maxWidth, drawn) + } + } + // A line wider than maxWidth is still cut, in the run that crosses it. + canvas.Reset() + pt := &painter{d: &ui.DrawContext{Canvas: canvas}, state: &canvasState{}, p: &Palette{}} + pt.text(&Shape{X: 10, Y: 20, Style: "caption", MaxWidth: 130, Runs: []Run{ + {Text: "bad-argument-type"}, {Text: " · 2 errors"}, + }}, place{k: 1}, 1) + texts := canvas.Texts() + if len(texts) != 2 || texts[0].Value != "bad-argument-type" || !strings.HasSuffix(texts[1].Value, "…") { + t.Fatalf("a line past maxWidth drew %+v", texts) + } +}