From 9449e0a8a676554e7b6bf2329ed2e431fc09adbf Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 25 Oct 2024 14:52:02 +0900 Subject: [PATCH] Revert "text/v2: remove faceWithCache's mutex" This reverts commit d5c6fd174f6834166137a7a8611bf6c451ea6f56. Reason: CacheGlyphs and Measure should be concurrent safe. --- text/v2/goxcache.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/text/v2/goxcache.go b/text/v2/goxcache.go index bcc4f6d10..b871b78c0 100644 --- a/text/v2/goxcache.go +++ b/text/v2/goxcache.go @@ -16,6 +16,7 @@ package text import ( "image" + "sync" "golang.org/x/image/font" "golang.org/x/image/math/fixed" @@ -43,6 +44,8 @@ type faceWithCache struct { glyphBoundsCache map[rune]glyphBoundsCacheValue glyphAdvanceCache map[rune]glyphAdvanceCacheValue kernCache map[kernCacheKey]fixed.Int26_6 + + m sync.Mutex } func (f *faceWithCache) Close() error { @@ -50,6 +53,9 @@ func (f *faceWithCache) Close() error { return err } + f.m.Lock() + defer f.m.Unlock() + f.glyphBoundsCache = nil f.glyphAdvanceCache = nil f.kernCache = nil @@ -61,6 +67,9 @@ func (f *faceWithCache) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, } func (f *faceWithCache) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) { + f.m.Lock() + defer f.m.Unlock() + if v, ok := f.glyphBoundsCache[r]; ok { return v.bounds, v.advance, v.ok } @@ -78,6 +87,9 @@ func (f *faceWithCache) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance } func (f *faceWithCache) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) { + f.m.Lock() + defer f.m.Unlock() + if v, ok := f.glyphAdvanceCache[r]; ok { return v.advance, v.ok } @@ -94,6 +106,9 @@ func (f *faceWithCache) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) { } func (f *faceWithCache) Kern(r0, r1 rune) fixed.Int26_6 { + f.m.Lock() + defer f.m.Unlock() + key := kernCacheKey{r0: r0, r1: r1} if v, ok := f.kernCache[key]; ok { return v