forked from charmbracelet/freeze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
55 lines (50 loc) · 1.2 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
package main
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"github.com/alecthomas/chroma/v2/formatters/svg"
"github.com/charmbracelet/freeze/font"
)
func fontOptions(config *Config) ([]svg.Option, error) {
if config.Font.File != "" {
bts, err := os.ReadFile(config.Font.File)
if err != nil {
return nil, fmt.Errorf("invalid font file: %w", err)
}
var format svg.FontFormat
switch ext := filepath.Ext(config.Font.File); ext {
case ".ttf":
format = svg.TRUETYPE
case ".woff2":
format = svg.WOFF2
case ".woff":
format = svg.WOFF
default:
return nil, fmt.Errorf("%s is not a supported font extension", ext)
}
return []svg.Option{
svg.EmbedFont(
config.Font.Family,
base64.StdEncoding.EncodeToString(bts),
format,
),
svg.FontFamily(config.Font.Family),
}, nil
}
if config.Font.Family != "JetBrains Mono" {
return []svg.Option{
svg.FontFamily(config.Font.Family),
}, nil
}
config.Font.Family = "JetBrains Mono"
fontBase64 := font.JetBrainsMono
if !config.Font.Ligatures {
fontBase64 = font.JetBrainsMonoNL
}
return []svg.Option{
svg.EmbedFont(config.Font.Family, fontBase64, svg.WOFF2),
svg.FontFamily(config.Font.Family),
}, nil
}