forked from fogleman/gg
/
reader.go
279 lines (223 loc) · 7.18 KB
/
reader.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
// Copyright 2023 The gg Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gg
import (
"bytes"
"image"
"image/jpeg"
"image/png"
"io"
"io/fs"
"os"
"strings"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
// LoadImage loads an image from the specified file path and returns it as an image.Image.
// It opens the file, decodes the image, and returns the decoded image.
func LoadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
im, err := LoadImageFromReader(file)
if err := file.Close(); err != nil {
return nil, err
}
return im, err
}
// LoadImageFromFS loads an image from the file system provided by a file system (fs.FS)
// and returns it as an image.Image. It opens the specified file within the file system,
// decodes the image, and returns the decoded image.
func LoadImageFromFS(fsys fs.FS, path string) (image.Image, error) {
var err error
if fsys == nil {
fsys, path, err = checkfsys(fsys, path)
if err != nil {
return nil, err
}
}
file, err := fsys.Open(path)
if err != nil {
return nil, err
}
im, err := LoadImageFromReader(file)
if err := file.Close(); err != nil {
return nil, err
}
return im, err
}
// LoadImageFromBytes decodes an image from a byte slice and returns it as an image.Image.
func LoadImageFromBytes(raw []byte) (image.Image, error) {
return LoadImageFromReader(bytes.NewReader(raw))
}
// LoadImageFromReader loads an image from an io.Reader and returns it as an image.Image.
// It decodes the image, and returns the decoded image.
func LoadImageFromReader(r io.Reader) (image.Image, error) {
im, _, err := image.Decode(r)
return im, err
}
// LoadPNG loads a PNG image from the specified file path and returns it as an image.Image.
// It opens the file, decodes the PNG image, and returns the decoded image.
func LoadPNG(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
im, err := LoadPNGFromReader(file)
if err := file.Close(); err != nil {
return nil, err
}
return im, err
}
// LoadPNGFromFS loads a PNG image from the specified file path within a file system (fs.FS)
// and returns it as an image.Image. It opens the specified file within the file system,
// decodes the PNG image, and returns the decoded image.
func LoadPNGFromFS(fsys fs.FS, path string) (image.Image, error) {
var err error
if fsys == nil {
fsys, path, err = checkfsys(fsys, path)
if err != nil {
return nil, err
}
}
file, err := fsys.Open(path)
if err != nil {
return nil, err
}
im, err := LoadPNGFromReader(file)
if err := file.Close(); err != nil {
return nil, err
}
return im, err
}
// LoadPNGFromBytes decodes a PNG image from a byte slice and returns it as an image.Image.
func LoadPNGFromBytes(raw []byte) (image.Image, error) {
return LoadPNGFromReader(bytes.NewReader(raw))
}
// LoadPNGFromReader loads a PNG image from an io.Reader and returns it as an image.Image.
// It decodes the PNG image, and returns the decoded image.
func LoadPNGFromReader(r io.Reader) (image.Image, error) {
im, err := png.Decode(r)
return im, err
}
// LoadJPG loads a JPEG image from the specified file path and returns it as an image.Image.
// It opens the file, decodes the JPEG image, and returns the decoded image.
func LoadJPG(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
im, err := LoadJPGFromReader(file)
if err := file.Close(); err != nil {
return nil, err
}
return im, err
}
// LoadJPGFromFS loads a JPEG image from the specified file path within a file system (fs.FS)
// and returns it as an image.Image. It opens the specified file within the file system,
// decodes the JPEG image, and returns the decoded image.
func LoadJPGFromFS(fsys fs.FS, path string) (image.Image, error) {
var err error
if fsys == nil {
fsys, path, err = checkfsys(fsys, path)
if err != nil {
return nil, err
}
}
file, err := fsys.Open(path)
if err != nil {
return nil, err
}
im, err := LoadJPGFromReader(file)
if err := file.Close(); err != nil {
return nil, err
}
return im, err
}
// LoadJPGFromBytes decodes a JPEG image from a byte slice and returns it as an image.Image.
func LoadJPGFromBytes(raw []byte) (image.Image, error) {
return LoadJPGFromReader(bytes.NewReader(raw))
}
// LoadJPGFromReader loads a JPEG image from an io.Reader and returns it as an image.Image.
// It decodes the JPEG image, and returns the decoded image.
func LoadJPGFromReader(r io.Reader) (image.Image, error) {
im, err := jpeg.Decode(r)
return im, err
}
// LoadFontFace loads a font face from a TrueType or OpenType font file at the specified file path
// and returns it as a font.Face with the specified point size.
func LoadFontFace(path string, points float64) (font.Face, error) {
fontBytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return LoadFontFaceFromBytes(fontBytes, points)
}
// LoadFontFaceFromReader loads a font face from a TrueType or OpenType font file at the specified io.Reader
// and returns it as a font.Face with the specified point size.
func LoadFontFaceFromReader(r io.Reader, points float64) (font.Face, error) {
fontBytes, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return LoadFontFaceFromBytes(fontBytes, points)
}
// LoadFontFaceFromFS loads a font face from a TrueType or OpenType font file at the specified file path within a file system (fs.FS)
// and returns it as a font.Face with the specified point size.
func LoadFontFaceFromFS(fsys fs.FS, path string, points float64) (font.Face, error) {
var err error
if fsys == nil {
fsys, path, err = checkfsys(fsys, path)
if err != nil {
return nil, err
}
}
fontBytes, err := fs.ReadFile(fsys, path)
if err != nil {
return nil, err
}
return LoadFontFaceFromBytes(fontBytes, points)
}
// LoadFontFaceFromBytes creates a font face from a byte slice containing TrueType or OpenType font data,
// and sets the specified point size.
func LoadFontFaceFromBytes(raw []byte, points float64) (font.Face, error) {
f, err := FontParse(raw)
if err != nil {
return nil, err
}
return FontNewFace(f, points)
}
// FontParse parses TrueType or OpenType font data from a byte slice and returns an *opentype.Font.
func FontParse(raw []byte) (*opentype.Font, error) {
f, err := opentype.Parse(raw)
if err != nil && strings.Contains(err.Error(), "font collection") {
return FontParseCollection(raw)
}
return f, err
}
// FontParseCollection parses a TrueType or OpenType font collection from a byte slice and returns an *opentype.Font.
func FontParseCollection(raw []byte, i ...int) (*opentype.Font, error) {
in := 0
if len(i) > 0 {
in = i[0]
}
c, err := opentype.ParseCollection(raw)
if err != nil {
return nil, err
}
return c.Font(in)
}
// FontNewFace creates a font face from a parsed *opentype.Font with the specified point size and hinting.
func FontNewFace(f *opentype.Font, points float64, hinting ...font.Hinting) (font.Face, error) {
hint := font.HintingNone
if len(hinting) > 0 {
hint = hinting[0]
}
return opentype.NewFace(f, &opentype.FaceOptions{
Size: points,
DPI: 72,
Hinting: hint,
})
}