forked from stephenafamo/goldmark-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (106 loc) · 3.48 KB
/
main.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
package main
import (
"context"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/dave/jennifer/jen"
"google.golang.org/api/option"
"google.golang.org/api/webfonts/v1"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
apikey := os.Getenv("GOOGLE_FONTS_API_KEY")
if apikey == "" {
log.Fatalf("env var GOOGLE_FONTS_API_KEY is needed to generate files")
}
ws, err := webfonts.NewService(ctx, option.WithAPIKey(apikey))
if err != nil {
log.Fatalf("could not get webfonts service: %v", err)
}
fonts, err := webfonts.NewWebfontsService(ws).List().Context(ctx).Do()
if err != nil {
log.Fatalf("could not get webfonts: %v", err)
}
err = generateConstants(fonts)
if err != nil {
log.Fatalf("could not generate constants: %v", err)
}
}
func generateConstants(fonts *webfonts.WebfontList) error {
// Generate our fonts.go file
f := jen.NewFilePathName("github.com/abdullahiqbal1996/goldmark-pdf", "pdf")
f.HeaderComment("Code generated, DO NOT EDIT.")
// f.HeaderComment("// +build google")
f.ImportName("github.com/stephenafamo/fonts", "fonts")
gTextFonts := f.Var().Id("TextFontsGoogle").Op("=").Map(jen.String()).Qual("github.com/abdullahiqbal1996/goldmark-pdf", "Font")
f.Line()
gCodeFonts := f.Var().Id("CodeFontsGoogle").Op("=").Map(jen.String()).Qual("github.com/abdullahiqbal1996/goldmark-pdf", "Font")
f.Line()
bodyFonts := make(jen.Dict)
codeFonts := make(jen.Dict)
// create variables for the fonts we use
for _, font := range fonts.Items {
var hasRegular, hasItalic, hasBold, hasBoldItalic bool
regular, italic, bold, boldItalic := "regular", "italic", "700", "700italic"
for _, variant := range font.Variants {
switch variant {
case regular:
hasRegular = true
case italic:
hasItalic = true
case bold:
hasBold = true
case boldItalic:
hasBoldItalic = true
}
}
canUseForText := hasRegular && hasItalic && hasBold && hasBoldItalic
canUseForCode := font.Category == "monospace"
if !canUseForText && !canUseForCode {
continue
}
// Populate any missing files...
// useful for monospace fonts that do not have all weights
if !hasItalic {
italic = regular
}
if !hasBold {
bold = regular
}
if !hasBoldItalic {
boldItalic = bold
}
family := strings.ReplaceAll(font.Family, " ", "")
variableName := "Font" + family
f.Var().Id(variableName).Op("=").
Qual("github.com/abdullahiqbal1996/goldmark-pdf", "Font").Values(jen.Dict{
jen.Id("CanUseForText"): jen.Lit(canUseForText),
jen.Id("CanUseForCode"): jen.Lit(canUseForCode),
jen.Id("Category"): jen.Lit(font.Category),
jen.Id("Family"): jen.Lit(font.Family),
jen.Id("FileRegular"): jen.Lit(regular),
jen.Id("FileItalic"): jen.Lit(italic),
jen.Id("FileBold"): jen.Lit(bold),
jen.Id("FileBoldItalic"): jen.Lit(boldItalic),
jen.Id("Type"): jen.Qual("github.com/abdullahiqbal1996/goldmark-pdf", "FontTypeGoogle"),
})
f.Line()
if canUseForText {
key := jen.Qual("github.com/abdullahiqbal1996/goldmark-pdf", variableName+".Family")
val := jen.Qual("github.com/abdullahiqbal1996/goldmark-pdf", variableName)
bodyFonts[key] = val
}
if canUseForCode {
key := jen.Qual("github.com/abdullahiqbal1996/goldmark-pdf", variableName+".Family")
val := jen.Qual("github.com/abdullahiqbal1996/goldmark-pdf", variableName)
codeFonts[key] = val
}
}
gTextFonts.Values(bodyFonts)
gCodeFonts.Values(codeFonts)
return f.Save("fonts_google.go")
}