Skip to content

Commit

Permalink
Merge branch 'fontmap-system-family' into input-segmenter
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitkugler committed Nov 24, 2023
2 parents 4ba01d9 + 09f0300 commit 107ca8a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions fontscan/fontmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,26 @@ func (fm *FontMap) FontMetadata(ft font.Font) (family string, aspect meta.Aspect
return item.Family, item.Aspect
}

// FindSystemFont looks for a system font with the given [family],
// returning the first match, or false is no one is found.
//
// User added fonts are ignored, and the [FontMap] must have been
// initialized with [UseSystemFonts] or this method will always return false.
//
// Family names are compared through [meta.Normalize].
func (fm *FontMap) FindSystemFont(family string) (Location, bool) {
family = meta.NormalizeFamily(family)
for _, footprint := range fm.database {
if footprint.isUserProvided {
continue
}
if footprint.Family == family {
return footprint.Location, true
}
}
return Location{}, false
}

// SetQuery set the families and aspect required, influencing subsequent
// `ResolveFace` calls.
func (fm *FontMap) SetQuery(query Query) {
Expand Down
30 changes: 30 additions & 0 deletions fontscan/fontmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,33 @@ func TestQueryHelveticaLinux(t *testing.T) {
family, _ := fm.FontMetadata(fm.ResolveFace('x').Font)
tu.Assert(t, family == meta.NormalizeFamily("Nimbus Sans")) // prefered Helvetica replacement
}

func TestFindSytemFont(t *testing.T) {
fm := NewFontMap(log.New(io.Discard, "", 0))
_, ok := fm.FindSystemFont("Nimbus")
tu.Assert(t, !ok) // no match on an empty fontmap

// simulate system fonts
fm.appendFootprints(footprint{
Family: meta.NormalizeFamily("Nimbus"),
Location: Location{File: "nimbus.ttf"},
},
footprint{
Family: meta.NormalizeFamily("Noto Sans"),
Location: Location{File: "noto.ttf"},
isUserProvided: true,
},
)

nimbus, ok := fm.FindSystemFont("Nimbus")
tu.Assert(t, ok && nimbus.File == "nimbus.ttf")

_, ok = fm.FindSystemFont("nimbus ")
tu.Assert(t, ok)

_, ok = fm.FindSystemFont("Arial")
tu.Assert(t, !ok)

_, ok = fm.FindSystemFont("Noto Sans")
tu.Assert(t, !ok) // user provided font are ignored
}

0 comments on commit 107ca8a

Please sign in to comment.