ref: 628a97cd712e306c4748ed0ece977e7785e22421 gio/ui/text/label.go -rw-r--r-- 3.3 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: Unlicense OR MIT

package text

import (
	"image"
	"image/color"
	"unicode/utf8"

	"gioui.org/ui"
	"gioui.org/ui/f32"
	"gioui.org/ui/layout"
	"gioui.org/ui/paint"

	"golang.org/x/image/math/fixed"
)

type Label struct {
	Face      Face
	Material  ui.MacroOp
	Alignment Alignment
	Text      string
	MaxLines  int

	it lineIterator
}

type lineIterator struct {
	Lines     []Line
	Clip      image.Rectangle
	Alignment Alignment
	Width     int
	Offset    image.Point

	y, prevDesc fixed.Int26_6
}

const inf = 1e6

func (l *lineIterator) Next() (String, f32.Point, bool) {
	for len(l.Lines) > 0 {
		line := l.Lines[0]
		l.Lines = l.Lines[1:]
		x := align(l.Alignment, line.Width, l.Width) + fixed.I(l.Offset.X)
		l.y += l.prevDesc + line.Ascent
		l.prevDesc = line.Descent
		// Align baseline and line start to the pixel grid.
		off := fixed.Point26_6{X: fixed.I(x.Floor()), Y: fixed.I(l.y.Ceil())}
		x, l.y = off.X, off.Y
		off.Y += fixed.I(l.Offset.Y)
		if (off.Y + line.Bounds.Min.Y).Floor() > l.Clip.Max.Y {
			break
		}
		if (off.Y + line.Bounds.Max.Y).Ceil() < l.Clip.Min.Y {
			continue
		}
		str := line.Text
		for len(str.Advances) > 0 {
			adv := str.Advances[0]
			if (off.X + adv + line.Bounds.Max.X - line.Width).Ceil() >= l.Clip.Min.X {
				break
			}
			off.X += adv
			_, s := utf8.DecodeRuneInString(str.String)
			str.String = str.String[s:]
			str.Advances = str.Advances[1:]
		}
		n := 0
		endx := off.X
		for i, adv := range str.Advances {
			if (endx + line.Bounds.Min.X).Floor() > l.Clip.Max.X {
				str.String = str.String[:n]
				str.Advances = str.Advances[:i]
				break
			}
			_, s := utf8.DecodeRuneInString(str.String[n:])
			n += s
			endx += adv
		}
		offf := f32.Point{X: float32(off.X) / 64, Y: float32(off.Y) / 64}
		return str, offf, true
	}
	return String{}, f32.Point{}, false
}

func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
	textLayout := l.Face.Layout(l.Text, LayoutOptions{MaxWidth: cs.Width.Max})
	lines := textLayout.Lines
	if max := l.MaxLines; max > 0 && len(lines) > max {
		lines = lines[:max]
	}
	dims := linesDimens(lines)
	dims.Size = cs.Constrain(dims.Size)
	padTop, padBottom := textPadding(lines)
	clip := image.Rectangle{
		Min: image.Point{X: -inf, Y: -padTop},
		Max: image.Point{X: inf, Y: dims.Size.Y + padBottom},
	}
	l.it = lineIterator{
		Lines:     lines,
		Clip:      clip,
		Alignment: l.Alignment,
		Width:     dims.Size.X,
	}
	for {
		str, off, ok := l.it.Next()
		if !ok {
			break
		}
		lclip := toRectF(clip).Sub(off)
		var stack ui.StackOp
		stack.Push(ops)
		ui.TransformOp{}.Offset(off).Add(ops)
		l.Face.Path(str).Add(ops)
		// Set a default color in case the material is empty.
		paint.ColorOp{Color: color.RGBA{A: 0xff}}.Add(ops)
		l.Material.Add(ops)
		paint.PaintOp{Rect: lclip}.Add(ops)
		stack.Pop()
	}
	return dims
}

func toRectF(r image.Rectangle) f32.Rectangle {
	return f32.Rectangle{
		Min: f32.Point{X: float32(r.Min.X), Y: float32(r.Min.Y)},
		Max: f32.Point{X: float32(r.Max.X), Y: float32(r.Max.Y)},
	}
}

func textPadding(lines []Line) (padTop int, padBottom int) {
	if len(lines) > 0 {
		first := lines[0]
		if d := -first.Bounds.Min.Y - first.Ascent; d > 0 {
			padTop = d.Ceil()
		}
		last := lines[len(lines)-1]
		if d := last.Bounds.Max.Y - last.Descent; d > 0 {
			padBottom = d.Ceil()
		}
	}
	return
}