Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.16']
go: ['1.15', '1.16']
steps:
- name: Setup
uses: actions/setup-go@v3
Expand Down
19 changes: 2 additions & 17 deletions finder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sysfont

import (
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -43,14 +42,7 @@ func NewFinder(opts *FinderOpts) *Finder {
}

var fonts []*Font
walker := func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

findFonts(opts, func(filename string) error {
// Check file extension.
if extensions := opts.Extensions; len(extensions) > 0 {
extension := filepath.Ext(strings.ToLower(filename))
Expand All @@ -67,14 +59,7 @@ func NewFinder(opts *FinderOpts) *Finder {

fonts = append(fonts, matches...)
return nil
}

// Traverse OS font directories.
for _, dir := range opts.SearchPaths {
if err := filepath.Walk(dir, walker); err != nil {
continue
}
}
})

return &Finder{
fonts: fonts,
Expand Down
29 changes: 29 additions & 0 deletions finder_dirwalk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build go1.16
// +build go1.16

package sysfont

import (
"io/fs"
"path/filepath"
)

func findFonts(opts *FinderOpts, fn func(filename string) error) {
walker := func(filename string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

return fn(filename)
}

// Traverse OS font directories.
for _, dir := range opts.SearchPaths {
if err := filepath.WalkDir(dir, walker); err != nil {
continue
}
}
}
32 changes: 32 additions & 0 deletions finder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sysfont

import "testing"

func TestFinder(t *testing.T) {
f := NewFinder(&FinderOpts{SearchPaths: []string{"testdata/fonts"}})
fonts := f.List()

// We expect Arial twice as we get double matches due to the lowercase and capitalised versions.
exp := []*Font{
{Name: "Arial", Family: "Arial", Filename: "testdata/fonts/arial.ttf"},
{Name: "Arial", Family: "Arial", Filename: "testdata/fonts/arial.ttf"},
{Name: "Dingbats", Family: "Dingbats", Filename: "testdata/fonts/dingbats.ttc"},
{Name: "Helvetica", Family: "Helvetica", Filename: "testdata/fonts/helvetica.otf"},
}

if len(fonts) != len(exp) {
t.Errorf("Expected %d fonts, got %d", len(exp), len(fonts))
}

for i, font := range fonts {
if font.Name != exp[i].Name {
t.Errorf("Expected font name %s, got %s", exp[i].Name, font.Name)
}
if font.Family != exp[i].Family {
t.Errorf("Expected font family %s, got %s", exp[i].Family, font.Family)
}
if font.Filename != exp[i].Filename {
t.Errorf("Expected font filename %s, got %s", exp[i].Filename, font.Filename)
}
}
}
29 changes: 29 additions & 0 deletions finder_walk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !go1.16
// +build !go1.16

package sysfont

import (
"os"
"path/filepath"
)

func findFonts(opts *FinderOpts, fn func(filename string) error) {
walker := func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

return fn(filename)
}

// Traverse OS font directories.
for _, dir := range opts.SearchPaths {
if err := filepath.Walk(dir, walker); err != nil {
continue
}
}
}
Empty file added testdata/fonts/arial.ttf
Empty file.
Empty file added testdata/fonts/dingbats.ttc
Empty file.
Empty file added testdata/fonts/helvetica.otf
Empty file.