font/opentype: report valid bounds from layoutText for the empty string

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-07-09 20:35:19 +02:00
parent 713770f808
commit 188bfa9a0b
2 changed files with 31 additions and 2 deletions
+3 -2
View File
@@ -176,9 +176,10 @@ func layoutText(sbuf *sfnt.Buffer, ppem fixed.Int26_6, maxWidth int, fonts []*op
} }
var prev, word state var prev, word state
endLine := func() { endLine := func() {
if prev.f != nil { if prev.f == nil && len(fonts) > 0 {
updateBounds(prev.f) prev.f = fonts[0]
} }
updateBounds(prev.f)
nextLine.Layout = glyphs[:prev.idx:prev.idx] nextLine.Layout = glyphs[:prev.idx:prev.idx]
nextLine.Len = prev.len nextLine.Len = prev.len
nextLine.Width = prev.x + prev.adv nextLine.Width = prev.x + prev.adv
+28
View File
@@ -13,6 +13,9 @@ import (
"gioui.org/internal/ops" "gioui.org/internal/ops"
"gioui.org/op" "gioui.org/op"
"gioui.org/text" "gioui.org/text"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/math/fixed" "golang.org/x/image/math/fixed"
) )
@@ -87,6 +90,31 @@ func TestCollectionAsFace(t *testing.T) {
} }
} }
func TestEmptyString(t *testing.T) {
face, err := Parse(goregular.TTF)
if err != nil {
t.Fatal(err)
}
ppem := fixed.I(200)
lines, err := face.Layout(ppem, 2000, strings.NewReader(""))
if err != nil {
t.Fatal(err)
}
if len(lines) == 0 {
t.Fatalf("Layout returned no lines for empty string; expected 1")
}
l := lines[0]
exp, err := face.font.Bounds(new(sfnt.Buffer), ppem, font.HintingFull)
if err != nil {
t.Fatal(err)
}
if got := l.Bounds; got != exp {
t.Errorf("got bounds %+v for empty string; expected %+v", got, exp)
}
}
func decompressFontFile(name string) (*Font, []byte, error) { func decompressFontFile(name string) (*Font, []byte, error) {
f, err := os.Open(name) f, err := os.Open(name)
if err != nil { if err != nil {