From 7bbe0da0c75fdb26954540a0827dfce7017c0cab Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 4 Jul 2020 17:51:45 +0200 Subject: [PATCH] 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 --- font/opentype/opentype.go | 13 ++++++++----- text/text.go | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/font/opentype/opentype.go b/font/opentype/opentype.go index 27afea4b..568faee8 100644 --- a/font/opentype/opentype.go +++ b/font/opentype/opentype.go @@ -19,10 +19,10 @@ import ( "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 { font *sfnt.Font - buf sfnt.Buffer } // 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 { 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 { - 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 { 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) { diff --git a/text/text.go b/text/text.go index 3ec7d1f2..3601e367 100644 --- a/text/text.go +++ b/text/text.go @@ -45,7 +45,8 @@ type Font struct { 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 { Layout(ppem fixed.Int26_6, maxWidth int, txt io.Reader) ([]Line, error) Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp