text,font/opentype: make text layout and shaping safe for concurrent use

Implementations of text.Face are reused across multiple windows for efficiency.
Make the opentype implementation safe for concurrent use and document it.

Updates gio#104

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-07-04 17:51:45 +02:00
parent e2278b64c1
commit 7bbe0da0c7
2 changed files with 10 additions and 6 deletions
+8 -5
View File
@@ -19,10 +19,10 @@ import (
"golang.org/x/image/math/fixed" "golang.org/x/image/math/fixed"
) )
// Font implements text.Face. // Font implements text.Face. It's methods are safe to use
// concurrently.
type Font struct { type Font struct {
font *sfnt.Font font *sfnt.Font
buf sfnt.Buffer
} }
// Collection is a collection of one or more fonts. // Collection is a collection of one or more fonts.
@@ -90,16 +90,19 @@ func (f *Font) Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]text.L
if err != nil { if err != nil {
return nil, err return nil, err
} }
return layoutText(&f.buf, ppem, maxWidth, &opentype{Font: f.font, Hinting: font.HintingFull}, glyphs) var buf sfnt.Buffer
return layoutText(&buf, ppem, maxWidth, &opentype{Font: f.font, Hinting: font.HintingFull}, glyphs)
} }
func (f *Font) Shape(ppem fixed.Int26_6, str []text.Glyph) op.CallOp { func (f *Font) Shape(ppem fixed.Int26_6, str []text.Glyph) op.CallOp {
return textPath(&f.buf, ppem, &opentype{Font: f.font, Hinting: font.HintingFull}, str) var buf sfnt.Buffer
return textPath(&buf, ppem, &opentype{Font: f.font, Hinting: font.HintingFull}, str)
} }
func (f *Font) Metrics(ppem fixed.Int26_6) font.Metrics { func (f *Font) Metrics(ppem fixed.Int26_6) font.Metrics {
o := &opentype{Font: f.font, Hinting: font.HintingFull} o := &opentype{Font: f.font, Hinting: font.HintingFull}
return o.Metrics(&f.buf, ppem) var buf sfnt.Buffer
return o.Metrics(&buf, ppem)
} }
func layoutText(sbuf *sfnt.Buffer, ppem fixed.Int26_6, maxWidth int, f *opentype, glyphs []text.Glyph) ([]text.Line, error) { func layoutText(sbuf *sfnt.Buffer, ppem fixed.Int26_6, maxWidth int, f *opentype, glyphs []text.Glyph) ([]text.Line, error) {
+2 -1
View File
@@ -45,7 +45,8 @@ type Font struct {
Weight Weight Weight Weight
} }
// Face implements text layout and shaping for a particular font. // Face implements text layout and shaping for a particular font. All
// methods must be safe for concurrent use.
type Face interface { type Face interface {
Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error) Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error)
Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp