Repository navigation
Expand file tree
/
Copy pathlru.go
More file actions
141 lines (123 loc) · 2.54 KB
/
Copy pathlru.go
File metadata and controls
141 lines (123 loc) · 2.54 KB
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
// SPDX-License-Identifier: Unlicense OR MIT
package text
import (
"gioui.org/io/system"
"gioui.org/op/clip"
"github.com/benoitkugler/textlayout/fonts"
"golang.org/x/image/math/fixed"
)
type layoutCache struct {
m map[layoutKey]*layoutElem
head, tail *layoutElem
}
type pathCache struct {
m map[pathKey]*path
head, tail *path
}
type layoutElem struct {
next, prev *layoutElem
key layoutKey
layout []Line
}
type path struct {
next, prev *path
key pathKey
val clip.PathSpec
gids []fonts.GID
}
type layoutKey struct {
ppem fixed.Int26_6
maxWidth int
str string
locale system.Locale
}
type pathKey struct {
ppem fixed.Int26_6
gidHash uint64
}
const maxSize = 1000
func (l *layoutCache) Get(k layoutKey) ([]Line, bool) {
if lt, ok := l.m[k]; ok {
l.remove(lt)
l.insert(lt)
return lt.layout, true
}
return nil, false
}
func (l *layoutCache) Put(k layoutKey, lt []Line) {
if l.m == nil {
l.m = make(map[layoutKey]*layoutElem)
l.head = new(layoutElem)
l.tail = new(layoutElem)
l.head.prev = l.tail
l.tail.next = l.head
}
val := &layoutElem{key: k, layout: lt}
l.m[k] = val
l.insert(val)
if len(l.m) > maxSize {
oldest := l.tail.next
l.remove(oldest)
delete(l.m, oldest.key)
}
}
func (l *layoutCache) remove(lt *layoutElem) {
lt.next.prev = lt.prev
lt.prev.next = lt.next
}
func (l *layoutCache) insert(lt *layoutElem) {
lt.next = l.head
lt.prev = l.head.prev
lt.prev.next = lt
lt.next.prev = lt
}
func gidsMatch(gids []fonts.GID, l Layout) bool {
if len(gids) != len(l.Glyphs) {
return false
}
for i := range gids {
if gids[i] != l.Glyphs[i].ID {
return false
}
}
return true
}
func (c *pathCache) Get(k pathKey, l Layout) (clip.PathSpec, bool) {
if v, ok := c.m[k]; ok && gidsMatch(v.gids, l) {
c.remove(v)
c.insert(v)
return v.val, true
}
return clip.PathSpec{}, false
}
func (c *pathCache) Put(k pathKey, l Layout, v clip.PathSpec) {
if c.m == nil {
c.m = make(map[pathKey]*path)
c.head = new(path)
c.tail = new(path)
c.head.prev = c.tail
c.tail.next = c.head
}
gids := make([]fonts.GID, len(l.Glyphs))
for i := range l.Glyphs {
gids[i] = l.Glyphs[i].ID
}
val := &path{key: k, val: v, gids: gids}
c.m[k] = val
c.insert(val)
if len(c.m) > maxSize {
oldest := c.tail.next
c.remove(oldest)
delete(c.m, oldest.key)
}
}
func (c *pathCache) remove(v *path) {
v.next.prev = v.prev
v.prev.next = v.next
}
func (c *pathCache) insert(v *path) {
v.next = c.head
v.prev = c.head.prev
v.prev.next = v
v.next.prev = v
}