Display all styles from STAT table

This commit is contained in:
Kovid Goyal
2024-05-04 08:02:13 +05:30
parent d1ba421e8c
commit 9dc8374d8b
2 changed files with 50 additions and 14 deletions

View File

@@ -7,15 +7,21 @@ import (
var _ = fmt.Print
type style_group struct {
name string
ordering int
styles []string
}
type family_style_data struct {
styles []string
style_groups []style_group
has_variable_faces bool
has_style_attribute_data bool
}
func styles_in_family(family string, fonts []ListedFont) (ans *family_style_data) {
_ = family
ans = &family_style_data{styles: make([]string, 0)}
ans = &family_style_data{style_groups: make([]style_group, 0)}
for _, f := range fonts {
vd := variable_data_for(f)
if len(vd.Design_axes) > 0 {
@@ -25,25 +31,53 @@ func styles_in_family(family string, fonts []ListedFont) (ans *family_style_data
ans.has_variable_faces = true
}
}
seen := utils.NewSet[string]()
add := func(x string) {
if !seen.Has(x) {
seen.Add(x)
ans.styles = append(ans.styles, x)
}
}
if ans.has_style_attribute_data {
groups := make(map[string]*style_group)
seen_map := make(map[string]*utils.Set[string])
get := func(key string, ordering int) (*style_group, *utils.Set[string]) {
sg := groups[key]
seen := seen_map[key]
if sg == nil {
ans.style_groups = append(ans.style_groups, style_group{name: key, ordering: ordering, styles: make([]string, 0)})
sg = &ans.style_groups[len(ans.style_groups)-1]
groups[key] = sg
seen = utils.NewSet[string]()
seen_map[key] = seen
}
return sg, seen
}
for _, f := range fonts {
vd := variable_data_for(f)
for _, ax := range vd.Design_axes {
if ax.Name == "" {
continue
}
sg, seen := get(ax.Name, ax.Ordering)
for _, v := range ax.Values {
add(v.Name)
if v.Name != "" && !seen.Has(v.Name) {
seen.Add(v.Name)
sg.styles = append(sg.styles, v.Name)
}
}
}
for _, ma := range vd.Multi_axis_styles {
sg, seen := get("Styles", 0)
if ma.Name != "" && !seen.Has(ma.Name) {
seen.Add(ma.Name)
sg.styles = append(sg.styles, ma.Name)
}
}
}
utils.StableSortWithKey(ans.style_groups, func(sg style_group) int { return sg.ordering })
} else {
ans.style_groups = append(ans.style_groups, style_group{name: "Styles", styles: make([]string, 0)})
sg := &ans.style_groups[0]
seen := utils.NewSet[string]()
for _, f := range fonts {
add(f.Style)
if f.Style != "" && !seen.Has(f.Style) {
seen.Add(f.Style)
sg.styles = append(sg.styles, f.Style)
}
}
}
return

View File

@@ -89,9 +89,11 @@ func (h *handler) draw_family_summary(start_x int, sz loop.ScreenSize) (err erro
}
if has_variable_data_for_font(fonts[0]) {
s := styles_in_family(family, fonts)
styles := "Styles: " + strings.Join(s.styles, ", ")
add_line(styles)
add_line("")
for _, sg := range s.style_groups {
styles := sg.name + ": " + strings.Join(sg.styles, ", ")
add_line(styles)
add_line("")
}
add_line(fmt.Sprintf("Press the %s key to choose this family", h.lp.SprintStyled("fg=yellow", "Enter")))
} else {
lines = append(lines, "Reading font data, please wait…")