-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.go
270 lines (209 loc) · 5.96 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
package fontforge
import (
"regexp"
"strings"
"github.com/Masterminds/semver/v3"
python3 "go.nhat.io/python/v3"
)
// Font is a wrapper around a Python object.
type Font struct {
obj *python3.Object
}
// callMethodArgs calls a method with args.
func (f *Font) callMethodArgs(name string, args ...any) *python3.Object { //nolint: unparam
return f.obj.CallMethodArgs(name, args...)
}
// Close closes the font.
func (f *Font) Close() error {
f.obj.DecRef()
f.obj.CallMethodArgs("close")
return python3.LastError() //nolint: wrapcheck
}
// DecRef decreases the reference count of the object.
func (f *Font) DecRef() {
f.obj.DecRef()
}
// AsObject returns the underlying Object.
func (f *Font) AsObject() *python3.Object {
return f.obj
}
// FontName returns the font name of the font.
func (f *Font) FontName() string {
attr := f.obj.GetAttr("fontname")
defer attr.DecRef()
return python3.AsString(attr)
}
// SetFontName sets the font name of the font.
func (f *Font) SetFontName(name string) {
f.obj.SetAttr("fontname", name)
}
// FullName returns the full name of the font.
func (f *Font) FullName() string {
attr := f.obj.GetAttr("fullname")
defer attr.DecRef()
return python3.AsString(attr)
}
// SetFullName sets the full name of the font.
func (f *Font) SetFullName(name string) {
f.obj.SetAttr("fullname", name)
}
// FamilyName returns the family name of the font.
func (f *Font) FamilyName() string {
attr := f.obj.GetAttr("familyname")
defer attr.DecRef()
return python3.AsString(attr)
}
// SetFamilyName sets the family name of the font.
func (f *Font) SetFamilyName(name string) {
f.obj.SetAttr("familyname", name)
}
// Em returns the em size of the font.
func (f *Font) Em() int {
attr := f.obj.GetAttr("em")
defer attr.DecRef()
return python3.AsInt(attr)
}
// SetEm sets the em size of the font.
func (f *Font) SetEm(em int) {
f.obj.SetAttr("em", em)
}
// UnderlinePosition returns the underline position of the font.
func (f *Font) UnderlinePosition() float64 {
attr := f.obj.GetAttr("upos")
defer attr.DecRef()
return python3.AsFloat64(attr)
}
// SetUnderlinePosition sets the underline position of the font.
func (f *Font) SetUnderlinePosition(pos float64) {
f.obj.SetAttr("upos", pos)
}
// UnderlineWith returns the underline with of the font.
func (f *Font) UnderlineWith() float64 {
attr := f.obj.GetAttr("uwidth")
defer attr.DecRef()
return python3.AsFloat64(attr)
}
// SetUnderlineWith sets the underline with of the font.
func (f *Font) SetUnderlineWith(with float64) {
f.obj.SetAttr("uwidth", with)
}
// Path returns the path of the font.
func (f *Font) Path() string {
attr := f.obj.GetAttr("path")
defer attr.DecRef()
return python3.AsString(attr)
}
// Copyright returns the copyright.
func (f *Font) Copyright() string {
attr := f.obj.GetAttr("copyright")
defer attr.DecRef()
return python3.AsString(attr)
}
// SetCopyright sets the copyright.
func (f *Font) SetCopyright(copyright string) {
f.obj.SetAttr("copyright", copyright)
}
// SFNTNames returns the SFNT names.
func (f *Font) SFNTNames() SFNTNames {
attr := f.obj.GetAttr("sfnt_names")
defer attr.DecRef()
return python3.MustUnmarshalAs[SFNTNames](attr)
}
// SetSFNTNames sets the SFNT names.
func (f *Font) SetSFNTNames(keyAndValues ...string) {
if len(keyAndValues)%2 != 0 {
panic("key and value must be provided in pair")
}
names := f.SFNTNames()
values := make(map[string]string, len(keyAndValues)%2)
for i := 0; i < len(keyAndValues); i += 2 {
values[keyAndValues[i]] = keyAndValues[i+1]
}
for j := range names {
if value, ok := values[names[j].Key]; ok {
names[j].Value = value
}
}
f.obj.SetAttr("sfnt_names", names)
}
// Version returns the version of the font.
func (f *Font) Version() *semver.Version {
attr := f.obj.GetAttr("version")
defer attr.DecRef()
return parseVersion(python3.AsString(attr))
}
// SetVersion sets the version of the font.
func (f *Font) SetVersion(version semver.Version) {
f.obj.SetAttr("version", version.String())
}
// HasGlyph returns true if the font has the glyph.
func (f *Font) HasGlyph(glyph any) bool {
return f.obj.HasItem(glyph)
}
// Glyph returns the glyph of the font.
func (f *Font) Glyph(glyph any) *Glyph {
o := f.obj.GetItem(glyph)
if o == nil {
return nil
}
return newGlyph(o)
}
// CreateGlyph creates a new glyph.
func (f *Font) CreateGlyph(glyph string) error {
f.obj.CallMethodArgs("createChar", -1, glyph)
return python3.LastError() //nolint: wrapcheck
}
// newFont creates a new Font.
func newFont(obj *python3.Object) *Font {
return &Font{obj: obj}
}
// SFNTNames is a list of SFNTName.
type SFNTNames []SFNTName
// MarshalPyObject marshals a SFNTName to a python3.Object.
func (n SFNTNames) MarshalPyObject() *python3.Object {
return python3.NewTupleFromValues(([]SFNTName)(n)...).AsObject()
}
// Find finds a SFNTName by key.
func (n SFNTNames) Find(key string) string {
for _, name := range n {
if name.Key == key {
return name.Value
}
}
return ""
}
// SFNTName is a SFNT name.
type SFNTName struct {
Locale string
Key string
Value string
}
// MarshalPyObject marshals a SFNTName to a python3.Object.
func (n SFNTName) MarshalPyObject() *python3.Object {
return python3.NewTupleFromValues(n.Locale, n.Key, n.Value).AsObject()
}
// UnmarshalPyObject unmarshals a python3.Object to a SFNTName.
func (n *SFNTName) UnmarshalPyObject(o *python3.Object) error { //nolint: unparam
locale := o.GetItem(0)
key := o.GetItem(1)
value := o.GetItem(2)
defer locale.DecRef()
defer key.DecRef()
defer value.DecRef()
*n = SFNTName{
Locale: locale.String(),
Key: key.String(),
Value: value.String(),
}
return nil
}
// buildPattern is a regex pattern to detect build id in semver, such as `1.2 build 110`.
var buildPattern = regexp.MustCompile(`\s+build\s+(\d+)$`)
func parseVersion(v string) *semver.Version {
// Sanitize the version.
v, _, _ = strings.Cut(v, ";")
v = strings.TrimSpace(v)
v = buildPattern.ReplaceAllString(v, "+$1")
r, _ := semver.NewVersion(v) //nolint: errcheck
return r
}