From 3a440c07c6bb7377a86fe23f8e0f36ecb228a175 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 15 Oct 2019 10:07:10 +0200 Subject: [PATCH] text: don't panic on missing default font Signed-off-by: Elias Naur --- text/shaper.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/text/shaper.go b/text/shaper.go index 079ae9fe..1b1d1bd0 100644 --- a/text/shaper.go +++ b/text/shaper.go @@ -3,6 +3,8 @@ package text import ( + "unicode/utf8" + "gioui.org/op/paint" "gioui.org/unit" "golang.org/x/image/math/fixed" @@ -77,13 +79,13 @@ func (s *Shaper) faceForFont(font Font) *face { font.Typeface = s.def tf = s.faceForStyle(font) } - if tf == nil { - panic("no default typeface defined") - } return tf } func (t *face) layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layout { + if t == nil { + return fallbackLayout(str) + } lk := layoutKey{ ppem: ppem, str: str, @@ -98,6 +100,9 @@ func (t *face) layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layou } func (t *face) shape(ppem fixed.Int26_6, str String) paint.ClipOp { + if t == nil { + return paint.ClipOp{} + } pk := pathKey{ ppem: ppem, str: str.String, @@ -109,3 +114,16 @@ func (t *face) shape(ppem fixed.Int26_6, str String) paint.ClipOp { t.pathCache.Put(pk, clip) return clip } + +func fallbackLayout(str string) *Layout { + l := &Layout{ + Lines: []Line{ + {Text: String{ + String: str, + }}, + }, + } + strlen := utf8.RuneCountInString(str) + l.Lines[0].Text.Advances = make([]fixed.Int26_6, strlen) + return l +}