aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/llgcode/draw2d/font.go
blob: 90ef9a1c645035371504950fc302315d5ad7cba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff

package draw2d

import (
	"io/ioutil"
	"log"
	"path/filepath"

	"sync"
	"github.com/golang/freetype/truetype"
)

// FontStyle defines bold and italic styles for the font
// It is possible to combine values for mixed styles, eg.
//     FontData.Style = FontStyleBold | FontStyleItalic
type FontStyle byte

const (
	FontStyleNormal FontStyle = iota
	FontStyleBold
	FontStyleItalic
)

type FontFamily byte

const (
	FontFamilySans FontFamily = iota
	FontFamilySerif
	FontFamilyMono
)

type FontData struct {
	Name   string
	Family FontFamily
	Style  FontStyle
}

type FontFileNamer func(fontData FontData) string

func FontFileName(fontData FontData) string {
	fontFileName := fontData.Name
	switch fontData.Family {
	case FontFamilySans:
		fontFileName += "s"
	case FontFamilySerif:
		fontFileName += "r"
	case FontFamilyMono:
		fontFileName += "m"
	}
	if fontData.Style&FontStyleBold != 0 {
		fontFileName += "b"
	} else {
		fontFileName += "r"
	}

	if fontData.Style&FontStyleItalic != 0 {
		fontFileName += "i"
	}
	fontFileName += ".ttf"
	return fontFileName
}

func RegisterFont(fontData FontData, font *truetype.Font) {
	fontCache.Store(fontData, font)
}

func GetFont(fontData FontData) (font *truetype.Font) {
	var err error

	if font, err = fontCache.Load(fontData); err != nil {
		log.Println(err)
	}

	return
}

func GetFontFolder() string {
	return defaultFonts.folder
}

func SetFontFolder(folder string) {
	defaultFonts.setFolder(filepath.Clean(folder))
}

func GetGlobalFontCache() FontCache {
	return fontCache
}

func SetFontNamer(fn FontFileNamer) {
	defaultFonts.setNamer(fn)
}

// Types implementing this interface can be passed to SetFontCache to change the
// way fonts are being stored and retrieved.
type FontCache interface {
	// Loads a truetype font represented by the FontData object passed as
	// argument.
	// The method returns an error if the font could not be loaded, either
	// because it didn't exist or the resource it was loaded from was corrupted.
	Load(FontData) (*truetype.Font, error)

	// Sets the truetype font that will be returned by Load when given the font
	// data passed as first argument.
	Store(FontData, *truetype.Font)
}

// Changes the font cache backend used by the package. After calling this
// functionSetFontFolder and SetFontNamer will not affect anymore how fonts are
// loaded.
// To restore the default font cache, call this function passing nil as argument.
func SetFontCache(cache FontCache) {
	if cache == nil {
		fontCache = defaultFonts
	} else {
		fontCache = cache
	}
}

// FolderFontCache can Load font from folder
type FolderFontCache struct {
	fonts  map[string]*truetype.Font
	folder string
	namer  FontFileNamer
}

// 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
	}

	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.fonts[file] = font
	return
}

// Store a font to this cache
func (cache *FolderFontCache) Store(fontData FontData, font *truetype.Font) {
	cache.fonts[cache.namer(fontData)] = font
}

// 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: 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
)