-
Notifications
You must be signed in to change notification settings - Fork 9
/
font.go
315 lines (289 loc) · 8.01 KB
/
font.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package font
import (
"fmt"
"image"
"image/color"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/gobold"
"golang.org/x/image/font/gofont/goitalic"
"golang.org/x/image/font/gofont/gomono"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/gofont/gosmallcaps"
"golang.org/x/image/math/fixed"
"github.com/divVerent/aaaaxy/internal/flag"
m "github.com/divVerent/aaaaxy/internal/math"
)
var (
pinFontsToCache = flag.Bool("pin_fonts_to_cache", true, "Pin all fonts to glyph cache.")
pinFontsToCacheHarder = flag.Bool("pin_fonts_to_cache_harder", false, "Do a dummy draw command to pin fonts to glyph cache harder.")
fontThreshold = flag.Int("font_threshold", 0x5E00, "Threshold for font rendering; lower values are bolder. 0 means antialias as usual; threshold range is 1 to 65535 inclusive.")
fontExtraSpacing = flag.Int("font_extra_spacing", 32, "Additional spacing for fonts in 64th pixels; should help with outline effect.")
)
// Face is an alias to font.Face so users do not need to import the font package.
type Face struct {
Face font.Face
Outline font.Face
}
func makeFace(f font.Face) Face {
effect := &fontEffects{f}
outline := &fontOutline{effect}
face := Face{
Face: effect,
Outline: outline,
}
all = append(all, face)
return face
}
// cacheChars are all characters the game uses. ASCII plus all Unicode our map file contains.
var cacheChars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~τπö¾©"
// We always keep the game character set in cache.
// This has to be repeated regularly as ebiten expires unused cache entries.
func KeepInCache(dst *ebiten.Image) {
if *pinFontsToCacheHarder {
for _, f := range all {
f.precache(dst, cacheChars)
}
}
if *pinFontsToCache {
for _, f := range all {
f.recache(cacheChars)
}
}
}
var (
all = []Face{}
ByName = map[string]Face{}
Centerprint Face
CenterprintBig Face
DebugSmall Face
MenuBig Face
Menu Face
MenuSmall Face
)
func Init() error {
// Load the fonts.
regular, err := truetype.Parse(goregular.TTF)
if err != nil {
return fmt.Errorf("could not load goitalic font: %v", err)
}
italic, err := truetype.Parse(goitalic.TTF)
if err != nil {
return fmt.Errorf("could not load goitalic font: %v", err)
}
bold, err := truetype.Parse(gobold.TTF)
if err != nil {
return fmt.Errorf("could not load gosmallcaps font: %v", err)
}
mono, err := truetype.Parse(gomono.TTF)
if err != nil {
return fmt.Errorf("could not load gomono font: %v", err)
}
smallcaps, err := truetype.Parse(gosmallcaps.TTF)
if err != nil {
return fmt.Errorf("could not load gosmallcaps font: %v", err)
}
ByName["Small"] = makeFace(truetype.NewFace(regular, &truetype.Options{
Size: 10,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
ByName["Regular"] = makeFace(truetype.NewFace(regular, &truetype.Options{
Size: 14,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
ByName["Italic"] = makeFace(truetype.NewFace(italic, &truetype.Options{
Size: 14,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
ByName["Bold"] = makeFace(truetype.NewFace(bold, &truetype.Options{
Size: 14,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
ByName["Mono"] = makeFace(truetype.NewFace(mono, &truetype.Options{
Size: 14,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
ByName["SmallCaps"] = makeFace(truetype.NewFace(smallcaps, &truetype.Options{
Size: 14,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
Centerprint = makeFace(truetype.NewFace(italic, &truetype.Options{
Size: 14,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
CenterprintBig = makeFace(truetype.NewFace(smallcaps, &truetype.Options{
Size: 24,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
DebugSmall = makeFace(truetype.NewFace(regular, &truetype.Options{
Size: 9,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
Menu = makeFace(truetype.NewFace(smallcaps, &truetype.Options{
Size: 18,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
MenuBig = makeFace(truetype.NewFace(smallcaps, &truetype.Options{
Size: 24,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
MenuSmall = makeFace(truetype.NewFace(smallcaps, &truetype.Options{
Size: 12,
Hinting: font.HintingFull,
SubPixelsX: 1,
SubPixelsY: 1,
}))
return nil
}
type fontEffects struct {
font.Face
}
func (e *fontEffects) Glyph(dot fixed.Point26_6, r rune) (
image.Rectangle, image.Image, image.Point, fixed.Int26_6, bool) {
dr, mask, maskp, advance, ok := e.Face.Glyph(dot, r)
return dr, &fontEffectsMask{mask}, maskp, advance, ok
}
func (e *fontEffects) Kern(r0, r1 rune) fixed.Int26_6 {
return e.Face.Kern(r0, r1) + fixed.Int26_6(*fontExtraSpacing)
}
type fontEffectsMask struct {
image.Image
}
func (e *fontEffectsMask) At(x, y int) color.Color {
// FYI: This yields a mask, and the draw function will only ever look at its alpha channel.
base := e.Image.At(x, y)
if *fontThreshold <= 0 {
return base
}
_, _, _, a := base.RGBA()
if int(a) < *fontThreshold {
return color.Transparent
}
return color.Opaque
}
type fontOutline struct {
font.Face
}
func (o *fontOutline) Glyph(dot fixed.Point26_6, r rune) (
image.Rectangle, image.Image, image.Point, fixed.Int26_6, bool) {
dr, mask, maskp, advance, ok := o.Face.Glyph(dot, r)
drExpanded := image.Rectangle{
Min: image.Point{
X: dr.Min.X - 1,
Y: dr.Min.Y - 1,
},
Max: image.Point{
X: dr.Max.X + 1,
Y: dr.Max.Y + 1,
},
}
maskpExpanded := image.Point{
X: maskp.X,
Y: maskp.Y,
}
return drExpanded, &fontOutlineMask{
Image: mask,
Rect: m.Rect{
Origin: m.Pos{
X: maskp.X,
Y: maskp.Y,
},
Size: m.Delta{
DX: dr.Max.X - dr.Min.X,
DY: dr.Max.Y - dr.Min.Y,
},
},
}, maskpExpanded, advance, ok
}
func (o *fontOutline) GlyphBounds(r rune) (fixed.Rectangle26_6, fixed.Int26_6, bool) {
bounds, advance, ok := o.Face.GlyphBounds(r)
bounds.Min.X -= 1 << 6
bounds.Min.Y -= 1 << 6
bounds.Max.X += 1 << 6
bounds.Max.Y += 1 << 6
return bounds, advance, ok
}
func (o *fontOutline) Metrics() font.Metrics {
m := o.Face.Metrics()
m.Height += 2 << 6
m.Ascent += 1 << 6
m.Descent += 1 << 6
return m
}
type fontOutlineMask struct {
image.Image
Rect m.Rect
}
func (o *fontOutlineMask) Bounds() image.Rectangle {
r := o.Image.Bounds()
r.Max.X += 2
r.Max.Y += 2
return r
}
func (o *fontOutlineMask) atRaw(x, y int) color.Color {
if o.Rect.DeltaPos(m.Pos{X: x, Y: y}).IsZero() {
return o.Image.At(x, y)
}
return color.Transparent
}
func (o *fontOutlineMask) At(x, y int) color.Color {
// The outline is:
// - Transparent where the font is fully opaque (only if antialiasing is off).
// This fixes alpha blending of "font atop outline".
if *fontThreshold > 0 {
_, _, _, a := o.atRaw(x-1, y-1).RGBA()
if a == 0xFFFF {
return color.Transparent
}
}
// - Otherwise it's the max of the bordering pixels.
var maxA uint32
for dy := -2; dy <= 0; dy++ {
for dx := -2; dx <= 0; dx++ {
if dx == -1 && dy == -1 {
continue
}
_, _, _, a := o.atRaw(x+dx, y+dy).RGBA()
if a > maxA {
maxA = a
}
}
}
return color.Alpha16{A: uint16(maxA)}
}