Skip to content

Commit

Permalink
fonts all good -- robust fallbacks etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Randall C. O'Reilly committed Aug 18, 2018
1 parent 1f9ac36 commit d5499f5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 17 deletions.
72 changes: 56 additions & 16 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,42 +220,78 @@ func FontAlts(fams string) (fns []string, serif, mono bool) {

// FaceNm returns the full FaceName to use for the current FontStyle spec, robustly
func (fs *FontStyle) FaceNm() string {
// these are the parameters we need to generate to get the font:
basenm := fs.Family
str := fs.Stretch
wt := fs.Weight
sty := fs.Style

if basenm == "" {
basenm = Prefs.FontFamily
fnm := FontFaceName(fs.Family, fs.Stretch, fs.Weight, fs.Style)
return fnm
}

// FontFaceName returns the best full FaceName to use for the given font
// family(ies) (comma separated) and modifier parameters
func FontFaceName(fam string, str FontStretch, wt FontWeights, sty FontStyles) string {
if fam == "" {
fam = Prefs.FontFamily
}
if basenm != "" { // start off with any styles implicit in font name
basenm, str, wt, sty = FontNameToMods(basenm)
nms := strings.Split(fam, ",")
basenm := ""
if len(nms) > 0 { // start off with any styles implicit in font name
_, fstr, fwt, fsty := FontNameToMods(strings.TrimSpace(nms[0]))
if fstr != FontStrNormal {
str = fstr
}
if fwt != WeightNormal {
wt = fwt
}
if fsty != FontNormal {
sty = fsty
}
}

nms, _, _ := FontAlts(fs.Family) // nms are all base names now
nms, _, _ = FontAlts(fam) // nms are all base names now

// we try multiple iterations, going through list of alternatives (which
// should be from most specific to least, all of which have an existing
// base name) -- first iter we look for an exact match for given
// modifiers, then we start relaxing things in terms of most likely
// issues..
didItalic := false
didOblique := false
iterloop:
for iter := 0; iter < 6; iter++ {
for _, basenm := range nms {
for iter := 0; iter < 10; iter++ {
for _, basenm = range nms {
fn := FontNameFromMods(basenm, str, wt, sty)
if FontLibrary.FontAvail(fn) {
break iterloop
}
}
if str != FontStrNormal { // very rare, normalize..
str = FontStrNormal
if str != FontStrNormal {
hasStr := false
for _, basenm = range nms {
fn := FontNameFromMods(basenm, str, WeightNormal, FontNormal)
if FontLibrary.FontAvail(fn) {
hasStr = true
break
}
}
if !hasStr { // if even basic stretch not avail, move on
str = FontStrNormal
continue
}
continue
}
if sty == FontItalic { // italic is more common, but maybe oblique exists
sty = FontOblique
didItalic = true
if !didOblique {
sty = FontOblique
continue
}
sty = FontNormal
continue
}
if sty == FontOblique { // by now we've tried both, try nothing
didOblique = true
if !didItalic {
sty = FontItalic
continue
}
sty = FontNormal
continue
}
Expand All @@ -274,6 +310,10 @@ iterloop:
wt = WeightNormal
continue
}
if str != FontStrNormal { // time to give up
str = FontStrNormal
continue
}
break // tried everything
}
fnm := FontNameFromMods(basenm, str, wt, sty)
Expand Down
40 changes: 39 additions & 1 deletion font_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gi

import (
"fmt"
"testing"
)

Expand All @@ -31,7 +32,7 @@ var testFontNames = []testFontSpec{
{"NotoSansNormal", "NotoSans", FontStrNormal, WeightNormal, FontNormal},
}

func TestFixFontMods(t *testing.T) {
func TestFontMods(t *testing.T) {
for _, ft := range testFontNames {
fo := FixFontMods(ft.fn)
if fo != ft.cor {
Expand All @@ -58,3 +59,40 @@ func TestFixFontMods(t *testing.T) {
}
}
}

// note: the responses to the following two tests depend on what is installed on the system

func TestFontAlts(t *testing.T) {
fa, serif, mono := FontAlts("serif")
fmt.Printf("FontAlts: serif: %v serif: %v, mono: %v\n", fa, serif, mono)

fa, serif, mono = FontAlts("sans-serif")
fmt.Printf("FontAlts: sans-serif: %v serif: %v, mono: %v\n", fa, serif, mono)

fa, serif, mono = FontAlts("monospace")
fmt.Printf("FontAlts: monospace: %v serif: %v, mono: %v\n", fa, serif, mono)

fa, serif, mono = FontAlts("cursive")
fmt.Printf("FontAlts: cursive: %v serif: %v, mono: %v\n", fa, serif, mono)

fa, serif, mono = FontAlts("fantasy")
fmt.Printf("FontAlts: fantasy: %v serif: %v, mono: %v\n", fa, serif, mono)
}

var testStrs = []FontStretch{FontStrNormal, FontStrCondensed, FontStrExpanded}
var testWts = []FontWeights{WeightNormal, WeightLight, WeightBold, WeightBlack}
var testStys = []FontStyles{FontNormal, FontItalic, FontOblique}
var testNms = []string{"serif", "sans-serif", "monospace", "courier", "cursive", "fantasy"}

func TestFontFaceName(t *testing.T) {
for _, nm := range testNms {
for _, str := range testStrs {
for _, wt := range testWts {
for _, sty := range testStys {
fn := FontFaceName(nm, str, wt, sty)
fmt.Printf("FontName: nm:\t%v\t str:\t%v\t wt:\t%v\t sty:\t%v\t res:\t%v\n", nm, str, wt, sty, fn)
}
}
}
}
}

0 comments on commit d5499f5

Please sign in to comment.