-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfont.go
78 lines (64 loc) · 1.94 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
package main
import (
"bytes"
"fmt"
"path"
"strings"
"github.com/ConradIrwin/font/sfnt"
log "github.com/Crosse/gosimplelogger"
)
// FontData describes a font file and the various metadata associated with it.
type FontData struct {
Name string
Family string
FileName string
Metadata map[sfnt.NameID]string
Data []byte
}
// fontExtensions is a list of file extensions that denote fonts.
// Only files ending with these extensions will be installed.
var fontExtensions = map[string]bool{
".otf": true,
".ttf": true,
}
// NewFontData creates a new FontData struct.
// fileName is the font's file name, and data is a byte slice containing the font file data.
// It returns a FontData struct describing the font, or an error.
func NewFontData(fileName string, data []byte) (*FontData, error) {
if _, ok := fontExtensions[strings.ToLower(path.Ext(fileName))]; !ok {
return nil, fmt.Errorf("not a font: %v", fileName)
}
fontData := &FontData{
FileName: fileName,
Metadata: make(map[sfnt.NameID]string),
Data: data,
}
font, err := sfnt.Parse(bytes.NewReader(fontData.Data))
if err != nil {
return nil, fmt.Errorf("cannot parse font: %w", err)
}
if !font.HasTable(sfnt.TagName) {
return nil, fmt.Errorf("font has no name table: %s", fileName)
}
nameTable, err := font.NameTable()
if err != nil {
return nil, fmt.Errorf("cannot get font table for %s: %w", fileName, err)
}
for _, nameEntry := range nameTable.List() {
fontData.Metadata[nameEntry.NameID] = nameEntry.String()
}
fontData.Name = fontData.Metadata[sfnt.NameFull]
fontData.Family = fontData.Metadata[sfnt.NamePreferredFamily]
if fontData.Family == "" {
if v, ok := fontData.Metadata[sfnt.NameFontFamily]; ok {
fontData.Family = v
} else {
log.Errorf("Font %v has no font family!", fontData.Name)
}
}
if fontData.Name == "" {
log.Errorf("Font %v has no name! Using file name instead.", fileName)
fontData.Name = fileName
}
return fontData, nil
}