text: add io.Reader Layout method to Shaper

use them for Editor, which is no longer required to construct a string
for laying out its content.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-01-13 20:11:55 +01:00
parent 16d2a3ac0a
commit b331407e81
6 changed files with 123 additions and 53 deletions
+17 -5
View File
@@ -95,18 +95,30 @@ func (e *editBuffer) gapLen() int {
return e.gapend - e.gapstart
}
func (e *editBuffer) Reset() {
e.pos = 0
}
func (e *editBuffer) Read(p []byte) (int, error) {
if e.pos == e.len() {
return 0, io.EOF
}
var n int
var total int
if e.pos < e.gapstart {
n += copy(p, e.text[e.pos:e.gapstart])
n := copy(p, e.text[e.pos:e.gapstart])
p = p[n:]
total += n
e.pos += n
}
n += copy(p, e.text[e.gapend:])
e.pos += n
return n, nil
if e.pos >= e.gapstart {
n := copy(p, e.text[e.pos+e.gapLen():])
total += n
e.pos += n
}
if e.pos > e.len() {
panic("hey!")
}
return total, nil
}
func (e *editBuffer) ReadRune() (rune, int, error) {
+4 -6
View File
@@ -273,13 +273,11 @@ func (e *Editor) layout(gtx *layout.Context, sh text.Shaper) {
}
e.shapes = e.shapes[:0]
for {
start, end, layout, off, ok := it.Next()
_, _, layout, off, ok := it.Next()
if !ok {
break
}
// TODO: remove
str := e.rr.String()[start:end]
path := sh.Shape(gtx, e.font, str, layout)
path := sh.Shape(gtx, e.font, layout)
e.shapes = append(e.shapes, line{off, path})
}
@@ -433,9 +431,9 @@ 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()
e.rr.Reset()
opts := text.LayoutOptions{MaxWidth: e.maxWidth}
lines := s.Layout(c, font, txt, opts)
lines, _ := s.Layout(c, font, &e.rr, opts)
dims := linesDimens(lines)
for i := 0; i < len(lines)-1; i++ {
// To avoid layout flickering while editing, assume a soft newline takes
+2 -2
View File
@@ -85,7 +85,7 @@ func (l *lineIterator) Next() (int, int, []text.Glyph, f32.Point, bool) {
func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt string) {
cs := gtx.Constraints
lines := s.Layout(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
lines := s.LayoutString(gtx, font, txt, text.LayoutOptions{MaxWidth: cs.Width.Max})
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
}
@@ -109,7 +109,7 @@ func (l Label) Layout(gtx *layout.Context, s text.Shaper, font text.Font, txt st
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(off).Add(gtx.Ops)
str := txt[start:end]
s.Shape(gtx, font, str, layout).Add(gtx.Ops)
s.ShapeString(gtx, font, str, layout).Add(gtx.Ops)
paint.PaintOp{Rect: lclip}.Add(gtx.Ops)
stack.Pop()
}