text: remove String, Layout and add Glyph

In preparation for using Shaper with an io.Reader, rework the API to not refer
to strings. In particular, introduce Glyph for holding the rune in addition to
the advance. For fast traversing of the underlying text, add Len to Line with
the UTF8 length.

Layout is a useless wrapper around []Line; remove it while we're
here.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-01-13 19:54:11 +01:00
parent e25b1639b9
commit 16d2a3ac0a
6 changed files with 95 additions and 116 deletions
+23 -25
View File
@@ -273,11 +273,13 @@ func (e *Editor) layout(gtx *layout.Context, sh text.Shaper) {
}
e.shapes = e.shapes[:0]
for {
str, off, ok := it.Next()
start, end, layout, off, ok := it.Next()
if !ok {
break
}
path := sh.Shape(gtx, e.font, str)
// TODO: remove
str := e.rr.String()[start:end]
path := sh.Shape(gtx, e.font, str, layout)
e.shapes = append(e.shapes, line{off, path})
}
@@ -433,15 +435,13 @@ func (e *Editor) moveCoord(c unit.Converter, pos image.Point) {
func (e *Editor) layoutText(c unit.Converter, s text.Shaper, font text.Font) ([]text.Line, layout.Dimensions) {
txt := e.rr.String()
opts := text.LayoutOptions{MaxWidth: e.maxWidth}
textLayout := s.Layout(c, font, txt, opts)
lines := textLayout.Lines
lines := s.Layout(c, font, txt, opts)
dims := linesDimens(lines)
for i := 0; i < len(lines)-1; i++ {
s := lines[i].Text.String
// To avoid layout flickering while editing, assume a soft newline takes
// up all available space.
if len(s) > 0 {
r, _ := utf8.DecodeLastRuneInString(s)
if layout := lines[i].Layout; len(layout) > 0 {
r := layout[len(layout)-1].Rune
if r != '\n' {
dims.Size.X = e.maxWidth
break
@@ -459,21 +459,18 @@ loop:
l := e.lines[carLine]
y += (prevDesc + l.Ascent).Ceil()
prevDesc = l.Descent
if carLine == len(e.lines)-1 || idx+len(l.Text.String) > e.rr.caret {
str := l.Text.String
for _, adv := range l.Text.Advances {
if carLine == len(e.lines)-1 || idx+len(l.Layout) > e.rr.caret {
for _, g := range l.Layout {
if idx == e.rr.caret {
break loop
}
x += adv
_, s := utf8.DecodeRuneInString(str)
idx += s
str = str[s:]
x += g.Advance
idx += utf8.RuneLen(g.Rune)
carCol++
}
break
}
idx += len(l.Text.String)
idx += l.Len
}
x += align(e.Alignment, e.lines[carLine].Width, e.viewSize.X)
return
@@ -553,11 +550,11 @@ func (e *Editor) moveToLine(carX fixed.Int26_6, carLine2 int) fixed.Int26_6 {
// Move to start of line2.
if carLine2 > carLine {
for i := carLine; i < carLine2; i++ {
e.rr.caret += len(e.lines[i].Text.String)
e.rr.caret += e.lines[i].Len
}
} else {
for i := carLine - 1; i >= carLine2; i-- {
e.rr.caret -= len(e.lines[i].Text.String)
e.rr.caret -= e.lines[i].Len
}
}
}
@@ -569,15 +566,15 @@ func (e *Editor) moveToLine(carX fixed.Int26_6, carLine2 int) fixed.Int26_6 {
end = 1
}
// Move to rune closest to previous horizontal position.
for i := 0; i < len(l2.Text.Advances)-end; i++ {
adv := l2.Text.Advances[i]
for i := 0; i < len(l2.Layout)-end; i++ {
g := l2.Layout[i]
if carX2 >= carX {
break
}
if carX2+adv-carX >= carX-carX2 {
if carX2+g.Advance-carX >= carX-carX2 {
break
}
carX2 += adv
carX2 += g.Advance
_, s := e.rr.runeAt(e.rr.caret)
e.rr.caret += s
}
@@ -593,11 +590,11 @@ func (e *Editor) Move(distance int) {
func (e *Editor) moveStart() {
carLine, carCol, x, _ := e.layoutCaret()
advances := e.lines[carLine].Text.Advances
layout := e.lines[carLine].Layout
for i := carCol - 1; i >= 0; i-- {
_, s := e.rr.runeBefore(e.rr.caret)
e.rr.caret -= s
x -= advances[i]
x -= layout[i].Advance
}
e.carXOff = -x
}
@@ -610,8 +607,9 @@ func (e *Editor) moveEnd() {
if carLine < len(e.lines)-1 {
end = 1
}
for i := carCol; i < len(l.Text.Advances)-end; i++ {
adv := l.Text.Advances[i]
layout := l.Layout
for i := carCol; i < len(layout)-end; i++ {
adv := layout[i].Advance
_, s := e.rr.runeAt(e.rr.caret)
e.rr.caret += s
x += adv
+21 -20
View File
@@ -32,11 +32,12 @@ type lineIterator struct {
Offset image.Point
y, prevDesc fixed.Int26_6
txtOff int
}
const inf = 1e6
func (l *lineIterator) Next() (text.String, f32.Point, bool) {
func (l *lineIterator) Next() (int, int, []text.Glyph, f32.Point, bool) {
for len(l.Lines) > 0 {
line := l.Lines[0]
l.Lines = l.Lines[1:]
@@ -50,42 +51,41 @@ func (l *lineIterator) Next() (text.String, f32.Point, bool) {
if (off.Y + line.Bounds.Min.Y).Floor() > l.Clip.Max.Y {
break
}
layout := line.Layout
start := l.txtOff
l.txtOff += line.Len
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]
for len(layout) > 0 {
g := layout[0]
adv := g.Advance
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:]
layout = layout[1:]
start += utf8.RuneLen(g.Rune)
}
n := 0
end := start
endx := off.X
for i, adv := range str.Advances {
for i, g := range layout {
if (endx + line.Bounds.Min.X).Floor() > l.Clip.Max.X {
str.String = str.String[:n]
str.Advances = str.Advances[:i]
layout = layout[:i]
break
}
_, s := utf8.DecodeRuneInString(str.String[n:])
n += s
endx += adv
end += utf8.RuneLen(g.Rune)
endx += g.Advance
}
offf := f32.Point{X: float32(off.X) / 64, Y: float32(off.Y) / 64}
return str, offf, true
return start, end, layout, offf, true
}
return text.String{}, f32.Point{}, false
return 0, 0, nil, f32.Point{}, false
}
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt string) {
cs := gtx.Constraints
textLayout := s.Layout(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
lines := textLayout.Lines
lines := s.Layout(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
}
@@ -100,7 +100,7 @@ func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt st
Width: dims.Size.X,
}
for {
str, off, ok := it.Next()
start, end, layout, off, ok := it.Next()
if !ok {
break
}
@@ -108,7 +108,8 @@ func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt st
var stack op.StackOp
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(off).Add(gtx.Ops)
s.Shape(gtx, font, str).Add(gtx.Ops)
str := txt[start:end]
s.Shape(gtx, font, str, layout).Add(gtx.Ops)
paint.PaintOp{Rect: lclip}.Add(gtx.Ops)
stack.Pop()
}