Sort style names by variant axis value when available

This commit is contained in:
Kovid Goyal
2024-06-02 14:57:53 +05:30
parent e3a155266e
commit 785726d21d

View File

@@ -4,14 +4,16 @@ import (
"fmt"
"kitty/tools/utils"
"slices"
"strings"
)
var _ = fmt.Print
type style_group struct {
name string
ordering int
styles []string
name string
ordering int
styles []string
style_sort_map map[string]string
}
type family_style_data struct {
@@ -22,50 +24,57 @@ type family_style_data struct {
func styles_with_attribute_data(ans *family_style_data, items ...VariableData) {
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]) {
seen_map := make(map[string]map[string]string)
get := func(key string, ordering int) *style_group {
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]()
sg.style_sort_map = make(map[string]string)
seen = make(map[string]string)
seen_map[key] = seen
}
return sg, seen
return sg
}
has := func(n string, m map[string]string) bool {
_, found := m[n]
return found
}
for _, vd := range items {
for _, ax := range vd.Design_axes {
if ax.Name == "" {
continue
}
sg, seen := get(ax.Name, ax.Ordering)
sg := get(ax.Name, ax.Ordering)
for _, v := range ax.Values {
if v.Name != "" && !seen.Has(v.Name) {
seen.Add(v.Name)
if v.Name != "" && !has(v.Name, sg.style_sort_map) {
sort_key := fmt.Sprintf("%09d:%s", int(v.Value*10000), strings.ToLower(v.Name))
sg.style_sort_map[v.Name] = sort_key
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 := get("Styles", 0)
if ma.Name != "" && !has(ma.Name, sg.style_sort_map) {
sg.style_sort_map[ma.Name] = strings.ToLower(ma.Name)
sg.styles = append(sg.styles, ma.Name)
}
}
}
utils.StableSortWithKey(ans.style_groups, func(sg style_group) int { return sg.ordering })
ans.style_groups = utils.StableSortWithKey(ans.style_groups, func(sg style_group) int { return sg.ordering })
for _, sg := range ans.style_groups {
sg.styles = utils.StableSortWithKey(sg.styles, func(s string) string { return sg.style_sort_map[s] })
}
}
func styles_for_variable_data(vd VariableData) (ans *family_style_data) {
ans = &family_style_data{style_groups: make([]style_group, 0)}
styles_with_attribute_data(ans, vd)
for _, sg := range ans.style_groups {
slices.Sort(sg.styles)
}
return
}