Skip to content

Commit

Permalink
Revert "text/v2: remove faceWithCache's mutex"
Browse files Browse the repository at this point in the history
This reverts commit d5c6fd1.

Reason: CacheGlyphs and Measure should be concurrent safe.
  • Loading branch information
hajimehoshi committed Oct 25, 2024
1 parent d5c6fd1 commit 9449e0a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions text/v2/goxcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package text

import (
"image"
"sync"

"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
Expand Down Expand Up @@ -43,13 +44,18 @@ 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 {
if err := f.f.Close(); err != nil {
return err
}

f.m.Lock()
defer f.m.Unlock()

f.glyphBoundsCache = nil
f.glyphAdvanceCache = nil
f.kernCache = nil
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 9449e0a

Please sign in to comment.