text: simplify text layout and shaping API

First, replace LayoutOptions with an explicit maximum width parameter.  The
single-field option struct doesn't carry its weight, and I don't think we'll
see more global layout options in the future. Rather, I expect options to cover
spans of text or be part of a Font.

Second, replace the unit.Converter with an scaled text size. It's simpler and
allow the Editor and similar widgets to easily detect whether their cached
layouts are stale. Package text no longer depends on package unit, which is
now dealt with at the widget-level only.

Finally, remove the Size field from Font. It was a design mistake: a Font is
assumed to cover all sizes, as evidenced by the FontRegistry disregarding
Size when looking up fonts.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-02-03 23:19:28 +01:00
parent dcacca3442
commit 4c220f4554
12 changed files with 68 additions and 85 deletions
+9 -7
View File
@@ -36,6 +36,7 @@ type Editor struct {
eventKey int
scale int
font text.Font
textSize fixed.Int26_6
blinkStart time.Time
focused bool
rr editBuffer
@@ -213,14 +214,16 @@ func (e *Editor) Focus() {
}
// Layout lays out the editor.
func (e *Editor) Layout(gtx *layout.Context, sh text.Shaper, font text.Font) {
func (e *Editor) Layout(gtx *layout.Context, sh text.Shaper, font text.Font, size unit.Value) {
// Flush events from before the previous frame.
copy(e.events, e.events[e.prevEvents:])
e.events = e.events[:len(e.events)-e.prevEvents]
e.prevEvents = len(e.events)
if e.font != font {
textSize := fixed.I(gtx.Px(size))
if e.font != font || e.textSize != textSize {
e.invalidate()
e.font = font
e.textSize = textSize
}
e.processEvents(gtx)
e.layout(gtx, sh)
@@ -245,7 +248,7 @@ func (e *Editor) layout(gtx *layout.Context, sh text.Shaper) {
}
if !e.valid {
e.lines, e.dims = e.layoutText(gtx, sh, e.font)
e.lines, e.dims = e.layoutText(sh)
e.valid = true
}
@@ -277,7 +280,7 @@ func (e *Editor) layout(gtx *layout.Context, sh text.Shaper) {
if !ok {
break
}
path := sh.Shape(gtx, e.font, layout)
path := sh.Shape(e.font, e.textSize, layout)
e.shapes = append(e.shapes, line{off, path})
}
@@ -430,10 +433,9 @@ func (e *Editor) moveCoord(c unit.Converter, pos image.Point) {
e.moveToLine(x, carLine)
}
func (e *Editor) layoutText(c unit.Converter, s text.Shaper, font text.Font) ([]text.Line, layout.Dimensions) {
func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
e.rr.Reset()
opts := text.LayoutOptions{MaxWidth: e.maxWidth}
lines, _ := s.Layout(c, font, &e.rr, opts)
lines, _ := s.Layout(e.font, e.textSize, e.maxWidth, &e.rr)
dims := linesDimens(lines)
for i := 0; i < len(lines)-1; i++ {
// To avoid layout flickering while editing, assume a soft newline takes