-
Notifications
You must be signed in to change notification settings - Fork 8
/
fonttc.go
113 lines (92 loc) · 2.63 KB
/
fonttc.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
// Copyright 2024 Hajime Hoshi
//
// 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 bitmapfont
import (
"compress/gzip"
"image"
"io"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/bitmapfont/v3/internal/bitmap"
)
func init() {
f, err := data.Open("data/face_zhhant.bin")
if err != nil {
panic(err)
}
defer f.Close()
s, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
defer s.Close()
bits, err := io.ReadAll(s)
if err != nil {
panic(err)
}
FaceTC = &tcFace{
face: bitmap.NewFace(bitmap.NewBinaryImage(bits, imageWidth, imageHeight), fixed.I(dotX), fixed.I(dotY), false),
}
}
func init() {
f, err := data.Open("data/face_zhhant_ea.bin")
if err != nil {
panic(err)
}
defer f.Close()
s, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
defer s.Close()
bits, err := io.ReadAll(s)
if err != nil {
panic(err)
}
FaceTCEA = &tcFace{
face: bitmap.NewFace(bitmap.NewBinaryImage(bits, imageWidth, imageHeight), fixed.I(dotX), fixed.I(dotY), true),
}
}
var _ font.Face = (*tcFace)(nil)
type tcFace struct {
face font.Face
}
func (t *tcFace) Close() error {
return t.face.Close()
}
func (t *tcFace) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
dr, mask, maskp, advance, ok = t.face.Glyph(dot, r)
if r == '、' || r == ',' || r == '。' || r == '.' {
dr = dr.Add(image.Pt(3, -3))
}
return dr, mask, maskp, advance, ok
}
func (t *tcFace) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
return t.face.GlyphBounds(r)
}
func (t *tcFace) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
return t.face.GlyphAdvance(r)
}
func (t *tcFace) Kern(r0, r1 rune) fixed.Int26_6 {
return t.face.Kern(r0, r1)
}
func (t *tcFace) Metrics() font.Metrics {
return t.face.Metrics()
}
var (
// FaceTC is a font.Face of the bitmap font (12px regular, prefer traditional Chinese characters).
FaceTC font.Face
// FaceTCEA is a font.Face of the bitmap font (12px regular, prefer traditional Chinese characters and East Asia wide characters).
FaceTCEA font.Face
)