widget: replace segmentIterator with simpler functions

The replacement functions all use the single seeking function, seekPosition.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-16 22:26:02 +01:00
parent b7341672e3
commit 41489fb732
2 changed files with 140 additions and 177 deletions
+63 -129
View File
@@ -30,125 +30,57 @@ type Label struct {
// not pixels): Y = line number, X = rune column.
type screenPos image.Point
type segmentIterator struct {
Lines []text.Line
Clip image.Rectangle
Alignment text.Alignment
Width int
Offset image.Point
startSel screenPos
endSel screenPos
pos screenPos // current position
line text.Line // current line
layout text.Layout // current line's Layout
// pixel positions
off fixed.Point26_6
y, prevDesc fixed.Int26_6
}
const inf = 1e6
func (l *segmentIterator) Next() (text.Layout, image.Point, bool, int, image.Point, bool) {
for l.pos.Y < len(l.Lines) {
if l.pos.X == 0 {
l.line = l.Lines[l.pos.Y]
func posIsAbove(lines []text.Line, pos combinedPos, y int) bool {
line := lines[pos.lineCol.Y]
return pos.y+line.Bounds.Max.Y.Ceil() < y
}
// Calculate X & Y pixel coordinates of left edge of line. We need y
// for the next line, so it's in l, but we only need x here, so it's
// not.
x := align(l.Alignment, l.line.Width, l.Width) + fixed.I(l.Offset.X)
l.y += l.prevDesc + l.line.Ascent
l.prevDesc = l.line.Descent
// Align baseline and line start to the pixel grid.
l.off = fixed.Point26_6{X: fixed.I(x.Floor()), Y: fixed.I(l.y.Ceil())}
l.y = l.off.Y
l.off.Y += fixed.I(l.Offset.Y)
if (l.off.Y + l.line.Bounds.Min.Y).Floor() > l.Clip.Max.Y {
break
}
func posIsBelow(lines []text.Line, pos combinedPos, y int) bool {
line := lines[pos.lineCol.Y]
return pos.y+line.Bounds.Min.Y.Floor() > y
}
if (l.off.Y + l.line.Bounds.Max.Y).Ceil() < l.Clip.Min.Y {
// This line is outside/before the clip area; go on to the next line.
l.pos.Y++
continue
}
func clipLine(lines []text.Line, alignment text.Alignment, width int, clip image.Rectangle, linePos combinedPos) (start combinedPos, end combinedPos) {
// Seek to first (potentially) visible column.
lineIdx := linePos.lineCol.Y
line := lines[lineIdx]
// runeWidth is the width of the widest rune in line.
runeWidth := (line.Bounds.Max.X - line.Width).Ceil()
q := combinedPos{y: start.y, x: fixed.I(clip.Min.X - runeWidth)}
start, _ = seekPosition(lines, alignment, width, linePos, q, 0)
// Seek to first invisible column after start.
q = combinedPos{y: start.y, x: fixed.I(clip.Max.X + runeWidth)}
end, _ = seekPosition(lines, alignment, width, start, q, 0)
return start, end
}
// Copy the line's Layout, since we slice it up later.
l.layout = l.line.Layout
// Find the left edge of the text visible in the l.Clip clipping
// area.
for len(l.layout.Advances) > 0 {
_, n := utf8.DecodeRuneInString(l.layout.Text)
adv := l.layout.Advances[0]
if (l.off.X + adv + l.line.Bounds.Max.X - l.line.Width).Ceil() >= l.Clip.Min.X {
break
}
l.off.X += adv
l.layout.Text = l.layout.Text[n:]
l.layout.Advances = l.layout.Advances[1:]
l.pos.X++
}
}
selected := l.inSelection()
endx := l.off.X
rune := 0
nextLine := true
retLayout := l.layout
for n := range l.layout.Text {
selChanged := selected != l.inSelection()
beyondClipEdge := (endx + l.line.Bounds.Min.X).Floor() > l.Clip.Max.X
if selChanged || beyondClipEdge {
retLayout.Advances = l.layout.Advances[:rune]
retLayout.Text = l.layout.Text[:n]
if selChanged {
// Save the rest of the line
l.layout.Advances = l.layout.Advances[rune:]
l.layout.Text = l.layout.Text[n:]
nextLine = false
}
break
}
endx += l.layout.Advances[rune]
rune++
l.pos.X++
}
offFloor := image.Point{X: l.off.X.Floor(), Y: l.off.Y.Floor()}
// Calculate the width & height if the returned text.
//
// If there's a better way to do this, I'm all ears.
var d fixed.Int26_6
for _, adv := range retLayout.Advances {
d += adv
}
size := image.Point{
X: d.Ceil(),
Y: (l.line.Ascent + l.line.Descent).Ceil(),
}
if nextLine {
l.pos.Y++
l.pos.X = 0
} else {
l.off.X = endx
}
return retLayout, offFloor, selected, l.prevDesc.Ceil() - size.Y, size, true
func subLayout(line text.Line, startCol, endCol int) text.Layout {
adv := line.Layout.Advances
if startCol == len(adv) {
return text.Layout{}
}
return text.Layout{}, image.Point{}, false, 0, image.Point{}, false
adv = adv[startCol:endCol]
txt := line.Layout.Text
for i := 0; i < startCol; i++ {
_, s := utf8.DecodeRuneInString(txt)
txt = txt[s:]
}
n := 0
for i := startCol; i < endCol; i++ {
_, s := utf8.DecodeRuneInString(txt[n:])
n += s
}
txt = txt[:n]
return text.Layout{Text: txt, Advances: adv}
}
func (l *segmentIterator) inSelection() bool {
return l.startSel.LessOrEqual(l.pos) &&
l.pos.Less(l.endSel)
}
func (p1 screenPos) LessOrEqual(p2 screenPos) bool {
return p1.Y < p2.Y || (p1.Y == p2.Y && p1.X <= p2.X)
func firstPos(line text.Line, alignment text.Alignment, width int) combinedPos {
return combinedPos{
x: align(alignment, line.Width, width),
y: line.Ascent.Ceil(),
}
}
func (p1 screenPos) Less(p2 screenPos) bool {
@@ -164,29 +96,31 @@ func (l Label) Layout(gtx layout.Context, s text.Shaper, font text.Font, size un
}
dims := linesDimens(lines)
dims.Size = cs.Constrain(dims.Size)
if len(lines) == 0 {
return dims
}
cl := textPadding(lines)
cl.Max = cl.Max.Add(dims.Size)
it := segmentIterator{
Lines: lines,
Clip: cl,
Alignment: l.Alignment,
Width: dims.Size.X,
}
for {
l, off, _, _, _, ok := it.Next()
if !ok {
defer clip.Rect(cl).Push(gtx.Ops).Pop()
semantic.LabelOp(txt).Add(gtx.Ops)
pos := firstPos(lines[0], l.Alignment, dims.Size.X)
for !posIsBelow(lines, pos, cl.Max.Y) {
start, end := clipLine(lines, l.Alignment, dims.Size.X, cl, pos)
line := lines[start.lineCol.Y]
lt := subLayout(line, start.lineCol.X, end.lineCol.X)
off := image.Point{X: start.x.Floor(), Y: start.y}
t := op.Offset(layout.FPt(off)).Push(gtx.Ops)
op := clip.Outline{Path: s.Shape(font, textSize, lt)}.Op().Push(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
op.Pop()
t.Pop()
if pos.lineCol.Y == len(lines)-1 {
break
}
t := op.Offset(layout.FPt(off)).Push(gtx.Ops)
rcl := clip.Rect(cl.Sub(off)).Push(gtx.Ops)
cl := clip.Outline{Path: s.Shape(font, textSize, l)}.Op().Push(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
cl.Pop()
rcl.Pop()
t.Pop()
pos, _ = seekPosition(lines, l.Alignment, dims.Size.X, pos, combinedPos{lineCol: screenPos{Y: pos.lineCol.Y + 1}}, 0)
}
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
semantic.LabelOp(txt).Add(gtx.Ops)
return dims
}