Revert "text/v2: remove faceWithCache's mutex"

This reverts commit d5c6fd174f.

Reason: CacheGlyphs and Measure should be concurrent safe.
This commit is contained in:
Hajime Hoshi 2024-10-25 14:52:02 +09:00
parent d5c6fd174f
commit 9449e0a8a6

View File

@ -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