aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/llgcode/draw2d/font.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-01 22:45:26 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-01 22:45:26 +0100
commit16df0bea5fe8ce0a4ea871d409f589b298567a97 (patch)
treef2f18a2c6773d86a895d075bf8f1a374d48406c4 /vendor/github.com/llgcode/draw2d/font.go
parent500caaeda74dd9c660279036293f4b2997cf0b03 (diff)
Update vendor
Diffstat (limited to 'vendor/github.com/llgcode/draw2d/font.go')
-rw-r--r--vendor/github.com/llgcode/draw2d/font.go93
1 files changed, 85 insertions, 8 deletions
diff --git a/vendor/github.com/llgcode/draw2d/font.go b/vendor/github.com/llgcode/draw2d/font.go
index cd0ec1b..90ef9a1 100644
--- a/vendor/github.com/llgcode/draw2d/font.go
+++ b/vendor/github.com/llgcode/draw2d/font.go
@@ -8,6 +8,7 @@ import (
"log"
"path/filepath"
+ "sync"
"github.com/golang/freetype/truetype"
)
@@ -80,11 +81,15 @@ func GetFontFolder() string {
}
func SetFontFolder(folder string) {
- defaultFonts.folder = filepath.Clean(folder)
+ defaultFonts.setFolder(filepath.Clean(folder))
+}
+
+func GetGlobalFontCache() FontCache {
+ return fontCache
}
func SetFontNamer(fn FontFileNamer) {
- defaultFonts.namer = fn
+ defaultFonts.setNamer(fn)
}
// Types implementing this interface can be passed to SetFontCache to change the
@@ -113,13 +118,24 @@ func SetFontCache(cache FontCache) {
}
}
-type defaultFontCache struct {
+// FolderFontCache can Load font from folder
+type FolderFontCache struct {
fonts map[string]*truetype.Font
folder string
namer FontFileNamer
}
-func (cache *defaultFontCache) Load(fontData FontData) (font *truetype.Font, err error) {
+// NewFolderFontCache creates FolderFontCache
+func NewFolderFontCache(folder string) *FolderFontCache {
+ return &FolderFontCache{
+ fonts: make(map[string]*truetype.Font),
+ folder: folder,
+ namer: FontFileName,
+ }
+}
+
+// Load a font from cache if exists otherwise it will load the font from file
+func (cache *FolderFontCache) Load(fontData FontData) (font *truetype.Font, err error) {
if font = cache.fonts[cache.namer(fontData)]; font != nil {
return font, nil
}
@@ -139,16 +155,77 @@ func (cache *defaultFontCache) Load(fontData FontData) (font *truetype.Font, err
return
}
-func (cache *defaultFontCache) Store(fontData FontData, font *truetype.Font) {
+// Store a font to this cache
+func (cache *FolderFontCache) Store(fontData FontData, font *truetype.Font) {
cache.fonts[cache.namer(fontData)] = font
}
-var (
- defaultFonts = &defaultFontCache{
+// SyncFolderFontCache can Load font from folder
+type SyncFolderFontCache struct {
+ sync.RWMutex
+ fonts map[string]*truetype.Font
+ folder string
+ namer FontFileNamer
+}
+
+
+
+// NewSyncFolderFontCache creates SyncFolderFontCache
+func NewSyncFolderFontCache(folder string) *SyncFolderFontCache {
+ return &SyncFolderFontCache{
fonts: make(map[string]*truetype.Font),
- folder: "../resource/font",
+ folder: folder,
namer: FontFileName,
}
+}
+
+func (cache *SyncFolderFontCache) setFolder(folder string) {
+ cache.Lock()
+ cache.folder = folder
+ cache.Unlock()
+}
+
+func (cache *SyncFolderFontCache) setNamer(namer FontFileNamer) {
+ cache.Lock()
+ cache.namer = namer
+ cache.Unlock()
+}
+
+// Load a font from cache if exists otherwise it will load the font from file
+func (cache *SyncFolderFontCache) Load(fontData FontData) (font *truetype.Font, err error) {
+ cache.RLock()
+ font = cache.fonts[cache.namer(fontData)]
+ cache.RUnlock()
+
+ if font != nil {
+ return font, nil
+ }
+
+ var data []byte
+ var file = cache.namer(fontData)
+
+ if data, err = ioutil.ReadFile(filepath.Join(cache.folder, file)); err != nil {
+ return
+ }
+
+ if font, err = truetype.Parse(data); err != nil {
+ return
+ }
+ cache.Lock()
+ cache.fonts[file] = font
+ cache.Unlock()
+ return
+}
+
+// Store a font to this cache
+func (cache *SyncFolderFontCache) Store(fontData FontData, font *truetype.Font) {
+ cache.Lock()
+ cache.fonts[cache.namer(fontData)] = font
+ cache.Unlock()
+}
+
+var (
+ defaultFonts = NewSyncFolderFontCache("../resource/font")
fontCache FontCache = defaultFonts
)