-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfonts.go
151 lines (122 loc) · 3.1 KB
/
fonts.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
package pdf
import (
"context"
"fmt"
"github.com/go-swiss/fonts"
"github.com/go-swiss/fonts/google"
)
type fontType string
const (
FontTypeInbuilt fontType = "inbuilt_font"
FontTypeCustom fontType = "custom_font"
FontTypeGoogle fontType = "google_font"
)
// A map of the inbuilt fonts that should be used for text (Headings, body)
var TextFontsInbuilt = map[string]Font{FontTimes.Family: FontTimes, FontHelvetica.Family: FontHelvetica}
// A map of the inbuilt monospace fonts. To be used for code blocks
var CodeFontsInbuilt = map[string]Font{FontCourier.Family: FontCourier}
// Represents a font.
type Font struct {
CanUseForText bool
CanUseForCode bool
Category string
Family string
FileRegular string
FileItalic string
FileBold string
FileBoldItalic string
Type fontType
}
// Returns a font from one of TextFontsInbuilt and TextFontsGoogle, or the backup
func GetTextFont(family string, fallback Font) Font {
f, ok := TextFontsInbuilt[family]
if ok {
return f
}
f, ok = TextFontsGoogle[family]
if ok {
return f
}
return fallback
}
// Returns a font from CodeFontsInbuilt and CodeFontsGoogle, or the backup
func GetCodeFont(family string, fallback Font) Font {
f, ok := CodeFontsInbuilt[family]
if ok {
return f
}
f, ok = CodeFontsGoogle[family]
if ok {
return f
}
return fallback
}
func addStyleFonts(ctx context.Context, pdf PDF, styles Styles, fontsCache fonts.Cache) error {
fontsToLoad := []Font{
styles.H1.Font,
styles.H2.Font,
styles.H3.Font,
styles.H4.Font,
styles.H5.Font,
styles.H6.Font,
styles.Normal.Font,
styles.Blockquote.Font,
styles.THeader.Font,
styles.TBody.Font,
styles.CodeFont,
}
return AddFonts(ctx, pdf, fontsToLoad, fontsCache)
}
func AddFonts(ctx context.Context, pdf PDF, fonts []Font, fontsCache fonts.Cache) error {
// Create a cache
if fontsCache == nil {
fontsCache = defaultCache
}
getFontBytes := func(family, variant string) ([]byte, error) {
fontBytes, err := google.GetFontBytes(ctx, family, variant, fontsCache)
if err != nil {
return nil, fmt.Errorf("could not get font bytes. %s-%s: %w", family, variant, err)
}
return fontBytes, nil
}
for _, f := range fonts {
if f.Type == FontTypeInbuilt || f.Type == FontTypeCustom {
continue
}
regular, err := getFontBytes(f.Family, f.FileRegular)
if err != nil {
return err
}
italic, err := getFontBytes(f.Family, f.FileItalic)
if err != nil {
return err
}
bold, err := getFontBytes(f.Family, f.FileBold)
if err != nil {
return err
}
boldItalic, err := getFontBytes(f.Family, f.FileBoldItalic)
if err != nil {
return err
}
if err := pdf.AddFont(f.Family, FontStyleRegular, regular); err != nil {
return err
}
if err := pdf.AddFont(f.Family, FontStyleItalic, italic); err != nil {
return err
}
if err := pdf.AddFont(f.Family, FontStyleBold, bold); err != nil {
return err
}
if err := pdf.AddFont(f.Family, FontStyleBoldItalic, boldItalic); err != nil {
return err
}
}
return nil
}
const (
FontStyleRegular = ""
FontStyleBold = "B"
FontStyleItalic = "I"
FontStyleBoldItalic = "BI"
)