-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
146 lines (114 loc) · 2.99 KB
/
convert.go
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
package main
import (
"fmt"
"image"
"image/draw"
"github.com/alacrity-engine/core/math/geometry"
codec "github.com/alacrity-engine/resource-codec"
"github.com/golang/freetype/truetype"
bolt "go.etcd.io/bbolt"
"golang.org/x/image/math/fixed"
)
func runeRangesToRunes(ranges [][2]string) ([]rune, error) {
runeSet := map[rune]struct{}{}
for _, runeRange := range ranges {
if len([]rune(runeRange[0])) > 1 || len([]rune(runeRange[1])) > 1 {
return nil, fmt.Errorf("incorrect range value: '%s'", runeRange)
}
if len([]rune(runeRange[0])) <= 0 || len([]rune(runeRange[1])) <= 0 {
return nil, fmt.Errorf("incorrect range value: '%s'", runeRange)
}
a := []rune(runeRange[0])[0]
b := []rune(runeRange[1])[0]
for i := a; i <= b; i++ {
runeSet[i] = struct{}{}
}
}
runeArr := make([]rune, 0, len(runeSet))
for run := range runeSet {
runeArr = append(runeArr, run)
}
return runeArr, nil
}
func (atlasMeta AtlasMeta) ToAtlasData(resourceFile *bolt.DB) (*codec.AtlasData, error) {
var fontData []byte
err := resourceFile.View(func(tx *bolt.Tx) error {
buck := tx.Bucket([]byte("fonts"))
if buck == nil {
return fmt.Errorf("fonts bucket not found")
}
fontData = buck.Get([]byte(atlasMeta.Font))
if fontData == nil {
return fmt.Errorf(
"the '%s' font not found", atlasMeta.Font)
}
return nil
})
if err != nil {
return nil, err
}
ttFont, err := truetype.Parse(fontData)
if err != nil {
return nil, err
}
face := truetype.NewFace(ttFont, &truetype.Options{
Size: float64(atlasMeta.Size),
GlyphCacheEntries: 1,
})
runes, err := runeRangesToRunes(atlasMeta.CharacterRanges)
if err != nil {
return nil, err
}
runes = append(runes, ' ')
fixedMapping, fixedBounds := makeSquareMapping(
face, runes, fixed.I(2))
atlasImg := image.NewRGBA(image.Rect(
fixedBounds.Min.X.Floor(),
fixedBounds.Min.Y.Floor(),
fixedBounds.Max.X.Ceil(),
fixedBounds.Max.Y.Ceil(),
))
for r, fg := range fixedMapping {
if dr, mask, maskp, _, ok := face.Glyph(fg.dot, r); ok {
draw.Draw(atlasImg, dr, mask, maskp, draw.Src)
}
}
bounds := geometry.R(
i2f(fixedBounds.Min.X),
i2f(fixedBounds.Min.Y),
i2f(fixedBounds.Max.X),
i2f(fixedBounds.Max.Y),
)
mapping := make(map[rune]codec.GlyphData)
var maxHeight float64
for r, fg := range fixedMapping {
glyph := codec.GlyphData{
Dot: geometry.V(
i2f(fg.dot.X),
bounds.Max.Y-(i2f(fg.dot.Y)-bounds.Min.Y),
),
Frame: geometry.R(
i2f(fg.frame.Min.X),
bounds.Max.Y-(i2f(fg.frame.Min.Y)-bounds.Min.Y),
i2f(fg.frame.Max.X),
bounds.Max.Y-(i2f(fg.frame.Max.Y)-bounds.Min.Y),
).Norm(),
Advance: i2f(fg.advance),
}
if glyph.Frame.H() > maxHeight {
maxHeight = glyph.Frame.H()
}
mapping[r] = glyph
}
atlasPic, err := codec.NewPictureFromImage(atlasImg)
if err != nil {
return nil, err
}
return &codec.AtlasData{
Glyphs: mapping,
SymbolSet: atlasPic,
Size: int32(atlasMeta.Size),
FontName: atlasMeta.Font,
MaxHeight: maxHeight,
}, nil
}