-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfontdb.go
148 lines (135 loc) · 4.1 KB
/
fontdb.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
package resvg
import "github.com/kanrichan/resvg-go/internal"
// FontDB font database
type FontDB struct {
wk *Worker
ptr int32
}
// NewFontDBDefault new a empty `FontDB` object in wasm.
// `FontDB` are not goroutine-safe, don't forget to close!
func (wk *Worker) NewFontDBDefault() (*FontDB, error) {
if !wk.used.CompareAndSwap(false, true) {
return nil, ErrWorkerIsBeingUsed
}
defer wk.used.Store(false)
db, err := internal.FontdbDatabaseDefault(wk.ctx, wk.mod)
if err != nil {
return nil, err
}
return &FontDB{wk, db}, nil
}
// Close cloes the `FontDB` and recovers memory.
func (db *FontDB) Close() error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
err := internal.FontdbDatabaseDelete(db.wk.ctx, db.wk.mod, db.ptr)
if err != nil {
return err
}
db.ptr = 0
return nil
}
// LoadFontFile loads font file into the `FontDB`.
func (db *FontDB) LoadFontFile(file string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseLoadFontFile(db.wk.ctx, db.wk.mod, db.ptr, file)
}
// LoadFontsDir loads font files from the selected directory into the `FontDB`.
func (db *FontDB) LoadFontsDir(dir string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseLoadFontsDir(db.wk.ctx, db.wk.mod, db.ptr, dir)
}
// LoadFontData loads font data into the `FontDB`.
func (db *FontDB) LoadFontData(data []byte) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseLoadFontData(db.wk.ctx, db.wk.mod, db.ptr, data)
}
// SetSerifFamily sets the family that will be used by `Family::Serif`.
func (db *FontDB) SetSerifFamily(family string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseSetSerifFamily(db.wk.ctx, db.wk.mod, db.ptr, family)
}
// SetSansSerifFamily sets the family that will be used by `Family::SansSerif`.
func (db *FontDB) SetSansSerifFamily(family string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseSetSansSerifFamily(db.wk.ctx, db.wk.mod, db.ptr, family)
}
// SetCursiveFamily sets the family that will be used by `Family::Cursive`.
func (db *FontDB) SetCursiveFamily(family string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseSetCursiveFamily(db.wk.ctx, db.wk.mod, db.ptr, family)
}
// SetFantasyFamily sets the family that will be used by `Family::Fantasy`.
func (db *FontDB) SetFantasyFamily(family string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseSetFantasyFamily(db.wk.ctx, db.wk.mod, db.ptr, family)
}
// SetMonospaceFamily sets the family that will be used by `Family::Monospace`.
func (db *FontDB) SetMonospaceFamily(family string) error {
if !db.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return ErrPointerIsNil
}
return internal.FontdbDatabaseSetMonospaceFamily(db.wk.ctx, db.wk.mod, db.ptr, family)
}
// Len returns the number of font faces in the `FontDB`
func (db *FontDB) Len() (int32, error) {
if !db.wk.used.CompareAndSwap(false, true) {
return 0, ErrWorkerIsBeingUsed
}
defer db.wk.used.Store(false)
if db.ptr == 0 {
return 0, ErrPointerIsNil
}
return internal.FontdbDatabaseLen(db.wk.ctx, db.wk.mod, db.ptr)
}