diff --git a/cli/cli.go b/cli/cli.go index b6132c8..4486c6b 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -10,6 +10,7 @@ import ( "github.com/alecthomas/chroma/styles" flags "github.com/jessevdk/go-flags" "github.com/matsuyoshi30/germanium" + findfont "github.com/matsuyoshi30/go-findfont" "github.com/skanehira/clipboard-image/v2" ) @@ -57,7 +58,13 @@ func Run() (err error) { } if opts.ListFonts { - germanium.ListFonts() + for _, path := range findfont.List() { + base := filepath.Base(path) + ext := filepath.Ext(path) + if ext == ".ttf" { + fmt.Println(base[0 : len(base)-len(ext)]) + } + } return nil } @@ -105,7 +112,20 @@ func run(r io.Reader, filename string) error { } } - face, err := germanium.LoadFont(opts.Font) + var fontData []byte + if opts.Font != germanium.DefaultFont { + fontPath, err := findfont.Find(opts.Font + ".ttf") + if err != nil { + return err + } + + fontData, err = os.ReadFile(fontPath) + if err != nil { + return err + } + } + + face, err := germanium.LoadFont(fontData) if err != nil { return err } diff --git a/font.go b/font.go index a5e5ed0..d313531 100644 --- a/font.go +++ b/font.go @@ -2,15 +2,13 @@ package germanium import ( _ "embed" - "fmt" - "os" - "path/filepath" - findfont "github.com/flopp/go-findfont" "github.com/golang/freetype/truetype" "golang.org/x/image/font" ) +const DefaultFont = "Hack-Regular" + var ( fontSize = 24.0 @@ -18,18 +16,10 @@ var ( font_hack []byte ) -func LoadFont(font string) (font.Face, error) { +func LoadFont(data []byte) (font.Face, error) { fontData := font_hack - if font != "Hack-Regular" { - fontPath, err := findfont.Find(font + ".ttf") - if err != nil { - return nil, err - } - - fontData, err = os.ReadFile(fontPath) - if err != nil { - return nil, err - } + if len(data) > 0 { + fontData = data } ft, err := truetype.Parse(fontData) @@ -39,13 +29,3 @@ func LoadFont(font string) (font.Face, error) { return truetype.NewFace(ft, &truetype.Options{Size: fontSize}), nil } - -func ListFonts() { - for _, path := range findfont.List() { - base := filepath.Base(path) - ext := filepath.Ext(path) - if ext == ".ttf" { - fmt.Println(base[0 : len(base)-len(ext)]) - } - } -} diff --git a/go.mod b/go.mod index 600ef1a..25c4588 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,9 @@ go 1.16 require ( github.com/alecthomas/chroma v0.9.2 - github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/jessevdk/go-flags v1.5.0 + github.com/matsuyoshi30/go-findfont v0.0.0-20210813222338-a686fa15f1a5 // indirect github.com/sergi/go-diff v1.2.0 // indirect github.com/skanehira/clipboard-image/v2 v2.0.0 golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb diff --git a/go.sum b/go.sum index dd72e60..a70f379 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/matsuyoshi30/go-findfont v0.0.0-20210813222338-a686fa15f1a5 h1:4JX7657mlZR5yyBdvRVtEethrGhBbZHEEbphUmF627Q= +github.com/matsuyoshi30/go-findfont v0.0.0-20210813222338-a686fa15f1a5/go.mod h1:btdNGMVz5UfpSWElBQM8BKrXglwtRDNSIp/RAdDfJa8= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=