diff --git a/kittens/choose_fonts/styles.go b/kittens/choose_fonts/styles.go index 035d07e83..e47214980 100644 --- a/kittens/choose_fonts/styles.go +++ b/kittens/choose_fonts/styles.go @@ -23,20 +23,16 @@ 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]map[string]string) + // Store indices, not pointers, as appending to ans.style_groups can reallocate it + groups := make(map[string]int) 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 - sg.style_sort_map = make(map[string]string) - seen = make(map[string]string) - seen_map[key] = seen + idx, found := groups[key] + if !found { + idx = len(ans.style_groups) + ans.style_groups = append(ans.style_groups, style_group{name: key, ordering: ordering, styles: make([]string, 0), style_sort_map: make(map[string]string)}) + groups[key] = idx } - return sg + return &ans.style_groups[idx] } has := func(n string, m map[string]string) bool { _, found := m[n] diff --git a/kittens/choose_fonts/styles_test.go b/kittens/choose_fonts/styles_test.go new file mode 100644 index 000000000..5523381c7 --- /dev/null +++ b/kittens/choose_fonts/styles_test.go @@ -0,0 +1,27 @@ +// License: GPLv3 Copyright: 2026, kitty contributors + +package choose_fonts + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestStylesWithAttributeData(t *testing.T) { + face := func(weight string, weight_value float64, italic string, italic_value float64) VariableData { + return VariableData{Design_axes: []DesignAxis{ + {Name: "Weight", Ordering: 0, Values: []DesignAxisValue{{Name: weight, Value: weight_value}}}, + {Name: "Italic", Ordering: 1, Values: []DesignAxisValue{{Name: italic, Value: italic_value}}}, + }} + } + ans := &family_style_data{} + styles_with_attribute_data(ans, face("Regular", 400, "Upright", 0), face("Bold", 700, "Italic", 1)) + actual := map[string][]string{} + for _, sg := range ans.style_groups { + actual[sg.name] = sg.styles + } + if diff := cmp.Diff(map[string][]string{"Weight": {"Regular", "Bold"}, "Italic": {"Upright", "Italic"}}, actual); diff != "" { + t.Fatalf("Unexpected styles:\n%s", diff) + } +}