From 9dc8374d8b8877061a74276c05f91addc616162d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 4 May 2024 08:02:13 +0530 Subject: [PATCH] Display all styles from STAT table --- tools/cmd/list_fonts/styles.go | 56 +++++++++++++++++++++++++++------- tools/cmd/list_fonts/ui.go | 8 +++-- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/tools/cmd/list_fonts/styles.go b/tools/cmd/list_fonts/styles.go index 6b3709fc5..301c3536a 100644 --- a/tools/cmd/list_fonts/styles.go +++ b/tools/cmd/list_fonts/styles.go @@ -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 diff --git a/tools/cmd/list_fonts/ui.go b/tools/cmd/list_fonts/ui.go index 0cf9d019a..b0e422548 100644 --- a/tools/cmd/list_fonts/ui.go +++ b/tools/cmd/list_fonts/ui.go @@ -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…")