From 8da8dbe50feb05a6f9fffc163d0cc3c5d2a3ff64 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Sun, 22 Nov 2026 16:45:56 +0000 Subject: [PATCH] Add rowpaint, which paints a row of terminal cells into an image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each cell's background, then each character from its glyph drawing, blended in linear light as Gio's renderer does, so text keeps its weight. A character reaching into the next cell is painted over that cell's background. A full 1080p row of code takes about 33µs, with no allocations. --- internal/rowpaint/rowpaint.go | 161 +++++++++++++++++++++++++++++ internal/rowpaint/rowpaint_test.go | 143 +++++++++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 internal/rowpaint/rowpaint.go create mode 100644 internal/rowpaint/rowpaint_test.go diff --git a/internal/rowpaint/rowpaint.go b/internal/rowpaint/rowpaint.go new file mode 100644 index 0000000..3e7b768 --- /dev/null +++ b/internal/rowpaint/rowpaint.go @@ -0,0 +1,161 @@ +// Package rowpaint paints a row of terminal cells into an image: each +// cell's background, then its character, from drawings made once by the +// glyph package. A row painted this way is one image for the GPU to draw, +// however many characters and colours it has. +package rowpaint + +import ( + "image" + "image/color" + "math" + + "100gex/internal/glyph" +) + +// Cell is one cell to paint. +type Cell struct { + // Bg is the background, which must be opaque. + Bg color.NRGBA + // Fg is the colour an ordinary character is painted in. It may be + // translucent, as faint text is. + Fg color.NRGBA + // Glyph is the cell's character, or nil for none. Its drawing may + // reach into neighbouring cells, and is painted over their + // backgrounds. + Glyph *glyph.Glyph +} + +// Paint paints cells into dst, which is a row of cells cellW pixels wide, +// from x = 0, with the baseline at baseline pixels from the top. Cells +// past dst's width are left out, and so is any of a drawing outside it. +// dst ends up opaque. +func Paint(dst *image.RGBA, cells []Cell, cellW, baseline int) { + b := dst.Bounds() + if b.Empty() || cellW <= 0 { + return + } + // Backgrounds: the first line, then copies of it. + line := dst.Pix[:4*b.Dx()] + for i, c := range cells { + x0, x1 := i*cellW, min((i+1)*cellW, b.Dx()) + if x0 >= x1 { + break + } + px := line[4*x0 : 4*x1] + px[0], px[1], px[2], px[3] = c.Bg.R, c.Bg.G, c.Bg.B, 0xff + for j := 4; j < len(px); j *= 2 { + copy(px[j:], px[:j]) + } + } + if x := len(cells) * cellW; x < b.Dx() { + clear(line[4*x:]) // no cells: transparent, then opaque black + for i := 4*x + 3; i < len(line); i += 4 { + line[i] = 0xff + } + } + for y := 1; y < b.Dy(); y++ { + copy(dst.Pix[y*dst.Stride:], line) + } + + // Characters, over every background. + for i, c := range cells { + g := c.Glyph + if g == nil { + continue + } + pen := image.Pt(b.Min.X+i*cellW, b.Min.Y+baseline) + switch { + case g.Mask != nil: + paintMask(dst, g.Mask, pen.Add(g.Origin), c.Fg) + case g.Color != nil: + paintColor(dst, g.Color, pen.Add(g.Origin)) + } + } +} + +// paintMask blends fg into dst over the mask's coverage, at at, in linear +// light, as Gio's renderer does: blending sRGB values directly makes light +// text on a dark background look thin. +func paintMask(dst *image.RGBA, mask *image.Alpha, at image.Point, fg color.NRGBA) { + r := mask.Bounds().Add(at).Intersect(dst.Bounds()) + if r.Empty() || fg.A == 0 { + return + } + fr, fgG, fb := toLinear[fg.R], toLinear[fg.G], toLinear[fg.B] + fa := uint32(fg.A) + for y := r.Min.Y; y < r.Max.Y; y++ { + m := mask.Pix[mask.PixOffset(r.Min.X-at.X, y-at.Y):] + d := dst.Pix[dst.PixOffset(r.Min.X, y):] + for x := range r.Dx() { + cov := uint32(m[x]) + if cov == 0 { + continue + } + a := cov * fa // of 255*255 + p := d[4*x : 4*x+3] + if a == 255*255 { + p[0], p[1], p[2] = fg.R, fg.G, fg.B + continue + } + p[0] = blend(p[0], fr, a) + p[1] = blend(p[1], fgG, a) + p[2] = blend(p[2], fb, a) + } + } +} + +// blend mixes an sRGB value towards a linear one by a, of 255*255. +func blend(dst uint8, src uint16, a uint32) uint8 { + l := uint32(toLinear[dst]) + l = (l*(255*255-a) + uint32(src)*a) / (255 * 255) + return toSRGB[l] +} + +// paintColor paints a colour drawing, premultiplied, over dst at at. +func paintColor(dst *image.RGBA, img *image.RGBA, at image.Point) { + r := img.Bounds().Add(at).Intersect(dst.Bounds()) + for y := r.Min.Y; y < r.Max.Y; y++ { + s := img.Pix[img.PixOffset(r.Min.X-at.X, y-at.Y):] + d := dst.Pix[dst.PixOffset(r.Min.X, y):] + for x := range r.Dx() { + sa := uint32(s[4*x+3]) + if sa == 0 { + continue + } + for c := range 3 { + d[4*x+c] = uint8(uint32(s[4*x+c]) + uint32(d[4*x+c])*(255-sa)/255) + } + } + } +} + +// linearBits is the precision of linear values: enough that blending dark +// colours doesn't band. +const linearBits = 12 + +var ( + toLinear [256]uint16 // sRGB to linear, of 1< 1 || got.R != got.G || got.A != 0xff { + t.Errorf("half coverage: %v, want about %d grey", got, srgbOf(0x80/255.0)) + } + Paint(img2, []Cell{{Bg: black, Fg: white, Glyph: none}}, cellW, baseline) + if got := at(img2, 2, 0); got != rgba(black) { + t.Errorf("no coverage: %v, want the background", got) + } + // Faint text is half as strong at full coverage. + img3 := row(1) + Paint(img3, []Cell{{Bg: black, Fg: color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0x80}, Glyph: full}}, cellW, baseline) + if got := at(img3, 0, 0); absDiff(got.R, srgbOf(0x80/255.0)) > 1 { + t.Errorf("faint: %v, want about %d grey", got, srgbOf(0x80/255.0)) + } +} + +// TestOverhang paints a character that reaches into the next cell over +// that cell's background, and leaves out what falls outside the row. +func TestOverhang(t *testing.T) { + img := row(2) + // From the middle of the first cell to past the second, and above + // and below the row. + wide := block(image.Rect(cellW/2, -baseline-3, 3*cellW, cellH), 0xff) + Paint(img, []Cell{{Bg: black, Fg: white, Glyph: wide}, {Bg: blue}}, cellW, baseline) + if got := at(img, cellW+1, 0); got != rgba(white) { + t.Errorf("in the next cell: %v, want the character over its background", got) + } + if got := at(img, 0, 0); got != rgba(black) { + t.Errorf("before the character: %v, want the background", got) + } +} + +// TestColor paints a colour character as it is, over the background. +func TestColor(t *testing.T) { + img := row(1) + c := image.NewRGBA(image.Rect(0, 0, 2, 1)) + c.SetRGBA(0, 0, color.RGBA{G: 0xff, A: 0xff}) // opaque green + c.SetRGBA(1, 0, color.RGBA{G: 0x80, A: 0x80}) // half-transparent green, premultiplied + Paint(img, []Cell{{Bg: red, Fg: white, Glyph: &glyph.Glyph{Color: c, Origin: image.Pt(0, -baseline)}}}, cellW, baseline) + if got := at(img, 0, 0); got != (color.RGBA{G: 0xff, A: 0xff}) { + t.Errorf("opaque: %v, want green", got) + } + if got := at(img, 1, 0); absDiff(got.R, 0x7f) > 1 || absDiff(got.G, 0x80) > 1 { + t.Errorf("half: %v, want half red, half green", got) + } +} + +// BenchmarkPaint paints a full row of a 1080p pane: about 200 cells of +// code-like text. +func BenchmarkPaint(b *testing.B) { + r := glyph.New(glyph.Options{Families: "Go Mono", Faces: gofont.Collection(), NoSystemFonts: true}) + m := r.Metrics(15) + text := "func (v *View) Layout(gtx layout.Context, term *libghostty.Terminal) (layout.Dimensions, error) { " + cells := make([]Cell, 200) + for i := range cells { + cells[i] = Cell{Bg: black, Fg: white, Glyph: r.Glyph(string(text[i%len(text)]), glyph.Style{}, 15)} + } + img := image.NewRGBA(image.Rect(0, 0, len(cells)*m.Width, m.Height)) + b.ReportAllocs() + for b.Loop() { + Paint(img, cells, m.Width, m.Baseline) + } +} + +func absDiff(a, b uint8) int { return int(math.Abs(float64(a) - float64(b))) } + +func srgbOf(l float64) uint8 { + if l <= 0.0031308 { + return uint8(math.Round(255 * 12.92 * l)) + } + return uint8(math.Round(255 * (1.055*math.Pow(l, 1/2.4) - 0.055))) +} -- knotmirror 0.0.1