Skip to content

Commit

Permalink
Use adrg/sysfont instead of fc-match, fixes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed May 11, 2021
1 parent 3b03659 commit e4adb5f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 27 deletions.
61 changes: 34 additions & 27 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"image/color"
"io/ioutil"
"math"
"os/exec"
"reflect"

"github.com/adrg/sysfont"
"github.com/tdewolff/canvas/font"
"github.com/tdewolff/canvas/text"
)
Expand Down Expand Up @@ -147,32 +147,39 @@ func (family *FontFamily) SetFeatures(features string) {

// LoadLocalFont loads a font from the system's fonts.
func (family *FontFamily) LoadLocalFont(name string, style FontStyle) error {
match := name
if style&FontExtraLight == FontExtraLight {
match += ":weight=40"
} else if style&FontLight == FontLight {
match += ":weight=50"
} else if style&FontBook == FontBook {
match += ":weight=75"
} else if style&FontMedium == FontMedium {
match += ":weight=100"
} else if style&FontSemibold == FontSemibold {
match += ":weight=180"
} else if style&FontBold == FontBold {
match += ":weight=200"
} else if style&FontBlack == FontBlack {
match += ":weight=205"
} else if style&FontExtraBlack == FontExtraBlack {
match += ":weight=210"
}
if style&FontItalic == FontItalic {
match += ":italic"
}
b, err := exec.Command("fc-match", "--format=%{file}", match).Output()
if err != nil {
return err
}
return family.LoadFontFile(string(b), style)
// TODO: use style to match font
finder := sysfont.NewFinder(&sysfont.FinderOpts{
Extensions: []string{".ttf", ".otf", ".ttc", ".woff", ".woff2", ".eot"},
})
font := finder.Match(name)
return family.LoadFontFile(font.Filename, style)

//match := name
//if style&FontExtraLight == FontExtraLight {
// match += ":weight=40"
//} else if style&FontLight == FontLight {
// match += ":weight=50"
//} else if style&FontBook == FontBook {
// match += ":weight=75"
//} else if style&FontMedium == FontMedium {
// match += ":weight=100"
//} else if style&FontSemibold == FontSemibold {
// match += ":weight=180"
//} else if style&FontBold == FontBold {
// match += ":weight=200"
//} else if style&FontBlack == FontBlack {
// match += ":weight=205"
//} else if style&FontExtraBlack == FontExtraBlack {
// match += ":weight=210"
//}
//if style&FontItalic == FontItalic {
// match += ":italic"
//}
//b, err := exec.Command("fc-match", "--format=%{file}", match).Output()
//if err != nil {
// return err
//}
//return family.LoadFontFile(string(b), style)
}

// LoadFontFile loads a font from a file.
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module github.com/tdewolff/canvas

require (
fyne.io/fyne v1.4.3
gioui.org v0.0.0-20210427144906-23a839a29d27
github.com/ByteArena/poly2tri-go v0.0.0-20170716161910-d102ad91854f
github.com/adrg/sysfont v0.1.2
github.com/adrg/xdg v0.3.3 // indirect
github.com/benoitkugler/textlayout v0.0.0-20210503180247-5030dcb3bcc0
github.com/dsnet/compress v0.0.1
github.com/go-gl/gl v0.0.0-20210426225639-a3bfa832c8aa
Expand Down
Loading

0 comments on commit e4adb5f

Please sign in to comment.