Skip to content

Commit

Permalink
converted earcut.hpp library to Go and used it to triangulate fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
tfriedel6 committed Mar 28, 2020
1 parent 1d5a02b commit c4d3130
Show file tree
Hide file tree
Showing 3 changed files with 966 additions and 26 deletions.
39 changes: 27 additions & 12 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type Canvas struct {
state drawState
stateStack []drawState

images map[interface{}]*Image
fonts map[interface{}]*Font
fontCtxs map[fontKey]*frCache
fontTriCache map[*Font]*fontTriCache
images map[interface{}]*Image
fonts map[interface{}]*Font
fontCtxs map[fontKey]*frCache
fontPathCache map[*Font]*fontPathCache
fontTriCache map[*Font]*fontTriCache

shadowBuf []backendbase.Vec
}
Expand Down Expand Up @@ -139,12 +140,13 @@ var Performance = struct {
// coordinates given here also use the bottom left as origin
func New(backend backendbase.Backend) *Canvas {
cv := &Canvas{
b: backend,
stateStack: make([]drawState, 0, 20),
images: make(map[interface{}]*Image),
fonts: make(map[interface{}]*Font),
fontCtxs: make(map[fontKey]*frCache),
fontTriCache: make(map[*Font]*fontTriCache),
b: backend,
stateStack: make([]drawState, 0, 20),
images: make(map[interface{}]*Image),
fonts: make(map[interface{}]*Font),
fontCtxs: make(map[fontKey]*frCache),
fontPathCache: make(map[*Font]*fontPathCache),
fontTriCache: make(map[*Font]*fontTriCache),
}
cv.state.lineWidth = 1
cv.state.lineAlpha = 1
Expand Down Expand Up @@ -482,6 +484,7 @@ func (cv *Canvas) reduceCache(keepSize, rec int) {
oldest := time.Now()
var oldestFontKey fontKey
var oldestFontKey2 *Font
var oldestFontKey3 *Font
var oldestImageKey interface{}
for src, img := range cv.images {
w, h := img.img.Size()
Expand All @@ -499,7 +502,7 @@ func (cv *Canvas) reduceCache(keepSize, rec int) {
oldestImageKey = nil
}
}
for fnt, cache := range cv.fontTriCache {
for fnt, cache := range cv.fontPathCache {
total += cache.size()
if cache.lastUsed.Before(oldest) {
oldest = cache.lastUsed
Expand All @@ -508,6 +511,16 @@ func (cv *Canvas) reduceCache(keepSize, rec int) {
oldestImageKey = nil
}
}
for fnt, cache := range cv.fontTriCache {
total += cache.size()
if cache.lastUsed.Before(oldest) {
oldest = cache.lastUsed
oldestFontKey3 = fnt
oldestFontKey2 = nil
oldestFontKey = fontKey{}
oldestImageKey = nil
}
}
if total <= keepSize {
return
}
Expand All @@ -516,7 +529,9 @@ func (cv *Canvas) reduceCache(keepSize, rec int) {
cv.images[oldestImageKey].Delete()
delete(cv.images, oldestImageKey)
} else if oldestFontKey2 != nil {
delete(cv.fontTriCache, oldestFontKey2)
delete(cv.fontPathCache, oldestFontKey2)
} else if oldestFontKey3 != nil {
delete(cv.fontTriCache, oldestFontKey3)
} else {
cv.fontCtxs[oldestFontKey].ctx = nil
delete(cv.fontCtxs, oldestFontKey)
Expand Down
Loading

0 comments on commit c4d3130

Please sign in to comment.