Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manually load font file or fonts dirs when use resvg-go #83

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion font.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/alecthomas/chroma/v2/formatters/svg"
"github.com/charmbracelet/freeze/font"
Expand All @@ -18,7 +19,7 @@ func fontOptions(config *Config) ([]svg.Option, error) {
}

var format svg.FontFormat
switch ext := filepath.Ext(config.Font.File); ext {
switch ext := filepath.Ext(config.Font.File); strings.ToLower(ext) {
case ".ttf":
format = svg.TRUETYPE
case ".woff2":
Expand Down
36 changes: 36 additions & 0 deletions font/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ package font
import (
_ "embed"
"encoding/base64"
"os"
"path/filepath"
"runtime"
)

//go:embed JetBrainsMono-Regular.ttf
Expand All @@ -107,3 +110,36 @@ var JetBrainsMonoNLTTF []byte

var JetBrainsMono string = base64.StdEncoding.EncodeToString(JetBrainsMonoTTF)
var JetBrainsMonoNL string = base64.StdEncoding.EncodeToString(JetBrainsMonoNLTTF)

var DefaultFontsDirectories []string

func init() {
switch runtime.GOOS {
case "windows":
DefaultFontsDirectories = []string{filepath.Join(os.Getenv("windir"), "Fonts")}
case "linux":
Copy link
Contributor

@isabelroses isabelroses Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not make this (the linux case) the default case then it should also be able to handle BSD systems too (at least in theory)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if librsvg was installed, it won't meet this case.it easy for linux / BSD systems user to install librsvg

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to do this at build time, rather than runtime? I.e. have font_linux.go, font_darwin.go, font_windows.go files, with the //go:build directives.

for _, d := range []string{
"/usr/share/fonts",
"/usr/local/share/fonts",
} {
entries, err := os.ReadDir(d)
if os.IsNotExist(err) || os.IsPermission(err) {
continue
}

for _, e := range entries {
if e.IsDir() {
DefaultFontsDirectories = append(DefaultFontsDirectories, filepath.Join(d, e.Name()))
}
}
}

DefaultFontsDirectories = []string{
"/usr/share/X11/fonts/Type1",
"/usr/share/X11/fonts/TTF",
filepath.Join(os.Getenv("HOME"), ".fonts"),
}
case "darwin":
DefaultFontsDirectories = []string{"/System/Library/Fonts/", "/Library/Fonts/"}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func main() {
}

// could not convert with libsvg, try resvg
svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output)
svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output, config.Font.File)
if svgConversionErr != nil {
printErrorFatal("Unable to convert SVG to PNG", svgConversionErr)
}
Expand Down
34 changes: 26 additions & 8 deletions png.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string) error {
return err
}

func resvgConvert(doc *etree.Document, w, h float64, output string) error {
func resvgConvert(doc *etree.Document, w, h float64, output, fontFile string) error {
svg, err := doc.WriteToBytes()
if err != nil {
return err
Expand All @@ -47,13 +47,31 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error {
printErrorFatal("Unable to write output", err)
}
defer fontdb.Close()
err = fontdb.LoadFontData(font.JetBrainsMonoTTF)
if err != nil {
printErrorFatal("Unable to load font", err)
}
err = fontdb.LoadFontData(font.JetBrainsMonoNLTTF)
if err != nil {
printErrorFatal("Unable to load font", err)

if fontFile == "" {
err = fontdb.LoadFontData(font.JetBrainsMonoTTF)
if err != nil {
printErrorFatal("Unable to load font", err)
}
err = fontdb.LoadFontData(font.JetBrainsMonoNLTTF)
if err != nil {
printErrorFatal("Unable to load font", err)
}

for _, d := range font.DefaultFontsDirectories {
err = fontdb.LoadFontsDir(d)
if err != nil {
if os.IsNotExist(err) || os.IsPermission(err) {
continue
}
printErrorFatal("Unable to load font dir", err)
}
}
} else {
err = fontdb.LoadFontFile(fontFile)
if err != nil {
printErrorFatal("Unable to load font", err)
}
}

pixmap, err := worker.NewPixmap(uint32(w), uint32(h))
Expand Down
Loading