Skip to content

Commit

Permalink
Merge pull request #24 from matsuyoshi30/fix-modules
Browse files Browse the repository at this point in the history
Fix using modules
  • Loading branch information
matsuyoshi30 authored Aug 13, 2021
2 parents b9dcd82 + 53303e0 commit af25942
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
24 changes: 22 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 5 additions & 25 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@ 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

//go:embed assets/fonts/Hack-Regular.ttf
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)
Expand All @@ -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)])
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit af25942

Please sign in to comment.