Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/platform/grid_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ func GridColumns(left, span float64, width int) (from, to int) {
return from, max(to, from)
}

// ShiftGridSpans moves the columns the runs of a grid line own, planned in the line's
// own device columns, onto a texture width columns wide that starts skip columns into
// the line: a line wider than a texture holds is rasterized from a window of it. A
// run's columns are cut to the texture, and a run outside it owns none.
func ShiftGridSpans(spans []GridSpan, skip, width int) {
for index, span := range spans {
from := min(max(span.From-skip, 0), width)
spans[index] = GridSpan{From: from, To: min(max(span.To-skip, from), width)}
}
}

func roundColumn(edge float64) int {
return int(math.Floor(edge + 0.5))
}
13 changes: 13 additions & 0 deletions internal/platform/grid_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ func TestGridColumnsOfAnEmptyRunAreEmpty(t *testing.T) {
t.Errorf("a run without cells = %d,%d", from, to)
}
}

// Columns planned along a whole line land on the window of it a texture holds: a run that
// straddles the window's edge keeps the part inside, and a run outside owns nothing.
func TestShiftGridSpansMovesTheColumnsOntoTheWindow(t *testing.T) {
spans := []GridSpan{{From: 0, To: 100}, {From: 100, To: 300}, {From: 300, To: 500}, {From: 500, To: 520}, {From: 700, To: 700}}
ShiftGridSpans(spans, 200, 300)
want := []GridSpan{{From: 0, To: 0}, {From: 0, To: 100}, {From: 100, To: 300}, {From: 300, To: 300}, {From: 300, To: 300}}
for index := range want {
if spans[index] != want[index] {
t.Errorf("span %d = %+v, want %+v", index, spans[index], want[index])
}
}
}
103 changes: 102 additions & 1 deletion internal/platform/label_pixels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,114 @@ func DeviceExtent(logical, length, scale float32) int {
return DevicePosition(logical+length, 0, scale) - DevicePosition(logical, 0, scale)
}

// LabelTextureLimit is the widest and tallest texture a label is rasterized into, in
// device pixels: the 2D texture limit of the Direct3D 11 and Vulkan devices Viva runs
// on, and the limit native.m keeps for Metal. LabelTexturePixels bounds the pixels of
// one such texture.
const (
LabelTextureLimit = 16384
LabelTexturePixels = 16 * 1024 * 1024
)

// LabelTextureWidth is the widest texture a label height device pixels high is
// rasterized into: LabelTextureLimit, narrowed for a tall label so that its texture
// stays within LabelTexturePixels. It is 0 for a label too tall for any texture,
// which is not drawn.
func LabelTextureWidth(height int) int {
if height <= 0 || height > LabelTextureLimit {
return 0
}
return min(LabelTextureLimit, LabelTexturePixels/height)
}

// LabelTextureFits reports whether a label texture width by height device pixels is
// one a backend allocates: width is at most LabelTextureWidth(height). It is tested
// without a division, as renderers ask it of every label every frame.
func LabelTextureFits(width, height int) bool {
return width > 0 && height > 0 && width <= LabelTextureLimit && height <= LabelTextureLimit && width*height <= LabelTexturePixels
}

// LabelVisibleColumns is the part of a label full device pixels wide, drawn at device
// column origin, that the device columns from left to right show: the columns from
// first to last, counted from the label's left edge. ok is false when they show none.
func LabelVisibleColumns(origin, full, left, right int) (first, last int, ok bool) {
first = min(max(left-origin, 0), full)
last = min(max(right-origin, 0), full)
return first, last, last > first
}

// WideLabelWindow chooses the device columns that the texture of a label full columns
// wide, more than one texture of limit columns holds, is rasterized from: the columns
// from skip to skip+width of the label. The columns from left to right, with left
// below right, are the ones that can be seen, and haveSkip and haveWidth the window of
// the texture the label has, or a width of 0 when it has none.
//
// A texture that still holds every column that can be seen is kept, so a scroll inside
// it does not rasterize the line again. Otherwise the visible columns are kept with as
// much margin on both sides as they are wide, so a scroll of up to their width keeps
// the new texture; at an end of the label the margin goes to the other side. This is
// the window viva_label_window in native.m chooses. Visible columns wider than limit
// keep their first limit columns: these backends draw one texture per label, and a
// viewport is wider than limit only for a label so tall that its limit is narrowed.
func WideLabelWindow(full, limit, left, right, haveSkip, haveWidth int) (skip, width int) {
if haveWidth > 0 && haveWidth <= limit && haveSkip <= left && right <= haveSkip+haveWidth && haveSkip+haveWidth <= full {
return haveSkip, haveWidth
}
span := right - left
if span >= limit {
return left, limit
}
margin := min(span, (limit-span)/2)
width = span + 2*margin
skip = max(left-margin, 0)
if skip+width > full {
skip = full - width
}
return skip, width
}

// WideTextLabelWindow is the window a text label's texture is rasterized from when one
// texture cannot hold the whole label. The label is full device columns wide and height
// high, drawn at the device column DevicePosition puts it at, on a target viewport device
// columns wide, or 0 when the caller has none. The columns that can be seen are the
// viewport's, cut to the label's clip; haveSkip and haveWidth are the window of the
// texture the label has, a width of 0 when it has none (see WideLabelWindow). wide is
// false for a label one texture holds whole, which is left to the caller. A wide label
// is not drawn, ok false, when none of it can be seen or it is too tall for any texture:
// an error for it would stop the window drawing anything.
func WideTextLabelWindow(label TextLabel, scale float32, full, height, viewport, haveSkip, haveWidth int) (skip, width int, wide, ok bool) {
limit := LabelTextureWidth(height)
if full <= limit {
return 0, full, false, full > 0
}
if limit <= 0 {
return 0, 0, true, false
}
origin := DevicePosition(label.X, label.OriginX, scale)
left, right := 0, origin+full
if viewport > 0 {
right = min(right, viewport)
}
if label.Clipped {
left = max(left, int(math.Floor(float64(label.Clip.X*scale))))
right = min(right, int(math.Ceil(float64((label.Clip.X+label.Clip.Width)*scale))))
}
first, last, visible := LabelVisibleColumns(origin, full, left, right)
if !visible {
return 0, 0, true, false
}
skip, width = WideLabelWindow(full, limit, first, last, haveSkip, haveWidth)
return skip, width, true, true
}

// LabelWindow is the part of a label's texture that a clip can show, in device
// pixels: the columns from skip to skip+width of a texture drawn at origin, whose
// whole width is full, when the clip covers left to right. A clip that starts left
// of the label keeps the whole texture. One that starts inside it skips the columns
// it cuts, so a long scrolled line costs its visible width, not its whole width,
// without moving the text by a fraction of a device pixel. ok is false when the
// clip shows none of those columns.
// clip shows none of those columns. It is for a label no wider than its texture
// limit; a wider one takes the window WideLabelWindow chooses.
func LabelWindow(origin, full, left, right int) (skip, width int, ok bool) {
if full <= 0 || right <= left {
return 0, 0, false
Expand Down
126 changes: 125 additions & 1 deletion internal/platform/label_pixels_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package platform

import "testing"
import (
"testing"

"github.com/vivaui/viva/gfx"
)

// A layout position half a device pixel either way snaps to the nearest pixel, and
// the remainder the painter took off is part of it.
Expand Down Expand Up @@ -97,3 +101,123 @@ func TestBitmapTextureFollowsTheDrawnSize(t *testing.T) {
t.Errorf("an image drawn larger than stored is %dx%d, want the stored size", width, height)
}
}

// A label texture is held to the device limit across and to the pixel budget overall, so a
// tall label gets a narrower texture, and one taller than the limit gets none.
func TestLabelTextureWidthKeepsTheTextureBounded(t *testing.T) {
cases := []struct{ height, width int }{
{0, 0},
{-3, 0},
{24, LabelTextureLimit},
{1024, LabelTextureLimit},
{2048, LabelTexturePixels / 2048},
{LabelTextureLimit, LabelTexturePixels / LabelTextureLimit},
{LabelTextureLimit + 1, 0},
}
for _, c := range cases {
if got := LabelTextureWidth(c.height); got != c.width {
t.Errorf("LabelTextureWidth(%d) = %d, want %d", c.height, got, c.width)
}
}
if !LabelTextureFits(LabelTextureLimit, 24) || LabelTextureFits(LabelTextureLimit+1, 24) || LabelTextureFits(0, 24) {
t.Error("a short label's texture is not bounded by the device limit")
}
if LabelTextureFits(LabelTexturePixels/2048+1, 2048) {
t.Error("a tall label's texture is not bounded by the pixel budget")
}
// LabelTextureFits tests without a division what LabelTextureWidth bounds, at each edge.
for _, height := range []int{-1, 0, 1, 24, 1023, 1024, 1025, 2047, 2048, 3000, LabelTextureLimit - 1, LabelTextureLimit, LabelTextureLimit + 1} {
limit := LabelTextureWidth(height)
for _, width := range []int{-1, 0, 1, limit - 1, limit, limit + 1, LabelTextureLimit, LabelTextureLimit + 1} {
if got, want := LabelTextureFits(width, height), width > 0 && width <= limit; got != want {
t.Errorf("LabelTextureFits(%d, %d) = %v; LabelTextureWidth is %d", width, height, got, limit)
}
}
}
}

// The columns a clip shows are counted from the label's left edge and cut to the label.
func TestLabelVisibleColumnsCutsTheClipToTheLabel(t *testing.T) {
cases := []struct {
name string
origin, full, left, right int
first, last int
ok bool
}{
{"label inside", 100, 200, 0, 1000, 0, 200, true},
{"label starts left of the clip", -5000, 40000, 0, 1920, 5000, 6920, true},
{"label ends inside the clip", -39000, 40000, 0, 1920, 39000, 40000, true},
{"clip left of the label", 2000, 100, 0, 1920, 100, 100, false},
{"clip right of the label", -500, 100, 0, 1920, 100, 100, false},
}
for _, c := range cases {
first, last, ok := LabelVisibleColumns(c.origin, c.full, c.left, c.right)
if ok != c.ok || ok && (first != c.first || last != c.last) {
t.Errorf("%s: LabelVisibleColumns(%d, %d, %d, %d) = %d, %d, %v; want %d, %d, %v",
c.name, c.origin, c.full, c.left, c.right, first, last, ok, c.first, c.last, c.ok)
}
}
}

// A line wider than a texture holds is rasterized around what can be seen, with a margin of
// the visible width on each side, shifted inward at the ends of the line; a texture that
// still holds what can be seen is kept, and one that does not is replaced.
func TestWideLabelWindowKeepsTheVisibleColumnsWithAMargin(t *testing.T) {
const full, limit = 60000, LabelTextureLimit
cases := []struct {
name string
left, right int
haveSkip, haveWidth int
skip, width int
}{
{"start of the line", 0, 2000, 0, 0, 0, 6000},
{"middle of the line", 20000, 22000, 0, 0, 18000, 6000},
{"end of the line", 58000, 60000, 0, 0, 54000, 6000},
{"wide span", 10000, 20000, 0, 0, 10000 - 3192, 10000 + 2*3192},
{"span wider than a texture", 1000, 1000 + limit + 10, 0, 0, 1000, limit},
{"scroll inside the texture", 19000, 21000, 18000, 6000, 18000, 6000},
{"scroll past the texture", 23000, 25000, 18000, 6000, 21000, 6000},
{"texture past the end of the line", 0, 2000, 59000, 6000, 0, 6000},
{"texture wider than the limit", 0, 2000, 0, limit + 1, 0, 6000},
}
for _, c := range cases {
skip, width := WideLabelWindow(full, limit, c.left, c.right, c.haveSkip, c.haveWidth)
if skip != c.skip || width != c.width {
t.Errorf("%s: WideLabelWindow(%d, %d, %d, %d, %d, %d) = %d, %d; want %d, %d",
c.name, full, limit, c.left, c.right, c.haveSkip, c.haveWidth, skip, width, c.skip, c.width)
continue
}
if width > limit || skip < 0 || skip+width > full || skip > c.left || skip+width < min(c.right, skip+limit) {
t.Errorf("%s: window %d+%d does not hold the visible columns %d..%d within the limit", c.name, skip, width, c.left, c.right)
}
}
}

// A text label wider than a texture is windowed to the columns the viewport shows, cut to
// its clip; one the viewport does not show, or too tall for any texture, is not drawn, and
// one a texture holds whole is left to its backend.
func TestWideTextLabelWindowFollowsTheViewportAndTheClip(t *testing.T) {
const scale, full, height, viewport = 1.5, 49164, 27, 600
label := TextLabel{X: -20000} // 30000 device columns off the left of the viewport
cases := []struct {
name string
label TextLabel
full, height int
haveSkip, haveWidth int
skip, width int
wide, ok bool
}{
{"viewport", label, full, height, 0, 0, 29400, 1800, true, true},
{"clip", TextLabel{X: -20000, Clipped: true, Clip: gfx.Rect{X: 100, Width: 200}}, full, height, 0, 0, 29850, 900, true, true},
{"kept texture", label, full, height, 29000, 2000, 29000, 2000, true, true},
{"scrolled out", TextLabel{X: -40000}, full, height, 0, 0, 0, 0, true, false},
{"too tall", label, full, LabelTextureLimit + 1, 0, 0, 0, 0, true, false},
{"one texture holds it", label, 2000, height, 0, 0, 0, 2000, false, true},
}
for _, c := range cases {
skip, width, wide, ok := WideTextLabelWindow(c.label, scale, c.full, c.height, viewport, c.haveSkip, c.haveWidth)
if skip != c.skip || width != c.width || wide != c.wide || ok != c.ok {
t.Errorf("%s: window = %d, %d, %v, %v; want %d, %d, %v, %v", c.name, skip, width, wide, ok, c.skip, c.width, c.wide, c.ok)
}
}
}
2 changes: 1 addition & 1 deletion internal/platform/linux/cpu_lcd_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCPUGridLCDLabelKeepsItsRunColors(t *testing.T) {
{Column: 1, Bytes: 1, Cells: 1, Color: gfx.RGB(0, 0, 255)},
},
}
image, ok, err := rasterizeLabel(services, label, 1, true)
image, ok, err := rasterizeLabel(services, label, 1, 0, labelImage{}, true)
if err != nil || !ok {
t.Fatalf("rasterize: ok=%v err=%v", ok, err)
}
Expand Down
49 changes: 38 additions & 11 deletions internal/platform/linux/cpu_offscreen_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (offscreen *CPUOffscreen) Close() {
// device pixel (platform.DevicePosition), a clip takes every device pixel it touches. ok is false
// when the label draws nothing.
func prepareLabel(services *textServices, l platform.TextLabel, scale float32, canvasWidth, canvasHeight int, allowLCD bool) (label raster.Label, ok bool, err error) {
image, ok, err := rasterizeLabel(services, l, scale, allowLCD)
image, ok, err := rasterizeLabel(services, l, scale, canvasWidth, labelImage{}, allowLCD)
if err != nil || !ok {
return raster.Label{}, false, err
}
Expand All @@ -114,16 +114,41 @@ func prepareLabel(services *textServices, l platform.TextLabel, scale float32, c
type labelImage struct {
pixels []byte
width, height int
lcd bool
spans []raster.Span
// skip is the device column of a text label the picture starts at: a line wider than a
// texture holds, wide, is drawn from the window of it the canvas shows (textWindow). It
// is 0 for every other label.
skip int
wide bool
lcd bool
spans []raster.Span
}

// textWindow is the part of a text label drawn into its picture at scale on a canvas
// canvasWidth device pixels wide: the device columns from skip to skip+width of the label,
// height high. A label one texture holds is drawn whole. A wider one, wide, is drawn from
// the columns of it the canvas and its clip show, keeping have, the picture it has, while
// that still holds them (platform.WideTextLabelWindow); the Vulkan backend draws the same
// window. ok is false when nothing is drawn: none of a wide line can be seen, or no
// texture holds the label.
func textWindow(l platform.TextLabel, scale float32, canvasWidth int, have labelImage) (skip, width, height int, wide, ok bool) {
full := int(math.Ceil(float64(l.Width * scale)))
height = int(math.Ceil(float64(l.Height * scale)))
haveSkip, haveWidth := 0, 0
if have.wide {
haveSkip, haveWidth = have.skip, have.width
}
skip, width, wide, ok = platform.WideTextLabelWindow(l, scale, full, height, canvasWidth, haveSkip, haveWidth)
return skip, width, height, wide, ok && platform.LabelTextureFits(width, height)
}

// rasterizeLabel draws a label's text, or resamples its image to the device pixels it covers. ok is
// false when it draws nothing. The picture does not depend on where the label is, except that an
// image smaller than its bitmap does, through the fraction of a pixel it starts at. With allowLCD,
// and the desktop asking for subpixel antialiasing, text comes back as per-channel coverage for the
// image smaller than its bitmap does, through the fraction of a pixel it starts at, and a line
// wider than a texture holds is drawn from the window of it a canvas canvasWidth device pixels
// wide shows, kept from have, the picture the label has (textWindow). With allowLCD, and the
// desktop asking for subpixel antialiasing, text comes back as per-channel coverage for the
// rasterizer to tint, the same way the Vulkan LCD shader reads it.
func rasterizeLabel(services *textServices, l platform.TextLabel, scale float32, allowLCD bool) (image labelImage, ok bool, err error) {
func rasterizeLabel(services *textServices, l platform.TextLabel, scale float32, canvasWidth int, have labelImage, allowLCD bool) (image labelImage, ok bool, err error) {
if l.Width <= 0 || l.Height <= 0 || (l.Value == "" && len(l.Bitmap) == 0) {
return labelImage{}, false, nil
}
Expand All @@ -138,13 +163,15 @@ func rasterizeLabel(services *textServices, l platform.TextLabel, scale float32,
}
return labelImage{pixels: tightBitmap(l.Bitmap, width, height, l.BitmapStride), width: width, height: height}, true, nil
}
width := int(math.Ceil(float64(l.Width * scale)))
height := int(math.Ceil(float64(l.Height * scale)))
pixels, lcd, err := services.rasterize(l, scale, width, height, allowLCD)
skip, width, height, wide, shown := textWindow(l, scale, canvasWidth, have)
if !shown {
return labelImage{}, false, nil
}
pixels, lcd, err := services.rasterizeWindow(l, scale, skip, width, height, allowLCD)
if err != nil {
return labelImage{}, false, err
}
image = labelImage{pixels: pixels, width: width, height: height, lcd: lcd}
image = labelImage{pixels: pixels, width: width, height: height, skip: skip, wide: wide, lcd: lcd}
if lcd && len(services.spans) > 0 {
image.spans = make([]raster.Span, len(services.spans))
for index, span := range services.spans {
Expand All @@ -161,7 +188,7 @@ func rasterizeLabel(services *textServices, l platform.TextLabel, scale float32,
// clip, if a canvas clip cuts it, on the device pixels the clip touches. ok is false when the clip
// leaves nothing.
func placeLabel(l platform.TextLabel, scale float32, image labelImage, canvasWidth, canvasHeight int) (label raster.Label, ok bool) {
x, y := platform.DevicePosition(l.X, l.OriginX, scale), platform.DevicePosition(l.Y, l.OriginY, scale)
x, y := platform.DevicePosition(l.X, l.OriginX, scale)+image.skip, platform.DevicePosition(l.Y, l.OriginY, scale)
if len(l.Bitmap) > 0 {
x, y = platform.DevicePosition(l.X, 0, scale), platform.DevicePosition(l.Y, 0, scale)
}
Expand Down
Loading
Loading