styles.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package choose_fonts
  2. import (
  3. "fmt"
  4. "kitty/tools/utils"
  5. "slices"
  6. "strings"
  7. )
  8. var _ = fmt.Print
  9. type style_group struct {
  10. name string
  11. ordering int
  12. styles []string
  13. style_sort_map map[string]string
  14. }
  15. type family_style_data struct {
  16. style_groups []style_group
  17. has_variable_faces bool
  18. has_style_attribute_data bool
  19. }
  20. func styles_with_attribute_data(ans *family_style_data, items ...VariableData) {
  21. groups := make(map[string]*style_group)
  22. seen_map := make(map[string]map[string]string)
  23. get := func(key string, ordering int) *style_group {
  24. sg := groups[key]
  25. seen := seen_map[key]
  26. if sg == nil {
  27. ans.style_groups = append(ans.style_groups, style_group{name: key, ordering: ordering, styles: make([]string, 0)})
  28. sg = &ans.style_groups[len(ans.style_groups)-1]
  29. groups[key] = sg
  30. sg.style_sort_map = make(map[string]string)
  31. seen = make(map[string]string)
  32. seen_map[key] = seen
  33. }
  34. return sg
  35. }
  36. has := func(n string, m map[string]string) bool {
  37. _, found := m[n]
  38. return found
  39. }
  40. for _, vd := range items {
  41. for _, ax := range vd.Design_axes {
  42. if ax.Name == "" {
  43. continue
  44. }
  45. sg := get(ax.Name, ax.Ordering)
  46. for _, v := range ax.Values {
  47. if v.Name != "" && !has(v.Name, sg.style_sort_map) {
  48. sort_key := fmt.Sprintf("%09d:%s", int(v.Value*10000), strings.ToLower(v.Name))
  49. sg.style_sort_map[v.Name] = sort_key
  50. sg.styles = append(sg.styles, v.Name)
  51. }
  52. }
  53. }
  54. for _, ma := range vd.Multi_axis_styles {
  55. sg := get("Styles", 0)
  56. if ma.Name != "" && !has(ma.Name, sg.style_sort_map) {
  57. sg.style_sort_map[ma.Name] = strings.ToLower(ma.Name)
  58. sg.styles = append(sg.styles, ma.Name)
  59. }
  60. }
  61. }
  62. ans.style_groups = utils.StableSortWithKey(ans.style_groups, func(sg style_group) int { return sg.ordering })
  63. for _, sg := range ans.style_groups {
  64. sg.styles = utils.StableSortWithKey(sg.styles, func(s string) string { return sg.style_sort_map[s] })
  65. }
  66. }
  67. func styles_for_variable_data(vd VariableData) (ans *family_style_data) {
  68. ans = &family_style_data{style_groups: make([]style_group, 0)}
  69. styles_with_attribute_data(ans, vd)
  70. return
  71. }
  72. func styles_in_family(family string, fonts []ListedFont) (ans *family_style_data) {
  73. _ = family
  74. ans = &family_style_data{style_groups: make([]style_group, 0)}
  75. vds := make([]VariableData, len(fonts))
  76. for i, f := range fonts {
  77. vds[i] = variable_data_for(f)
  78. }
  79. for _, vd := range vds {
  80. if len(vd.Design_axes) > 0 {
  81. ans.has_style_attribute_data = true
  82. }
  83. if len(vd.Axes) > 0 {
  84. ans.has_variable_faces = true
  85. }
  86. }
  87. if ans.has_style_attribute_data {
  88. styles_with_attribute_data(ans, vds...)
  89. } else {
  90. ans.style_groups = append(ans.style_groups, style_group{name: "Styles", styles: make([]string, 0)})
  91. sg := &ans.style_groups[0]
  92. seen := utils.NewSet[string]()
  93. for _, f := range fonts {
  94. if f.Style != "" && !seen.Has(f.Style) {
  95. seen.Add(f.Style)
  96. sg.styles = append(sg.styles, f.Style)
  97. }
  98. }
  99. }
  100. for _, sg := range ans.style_groups {
  101. slices.Sort(sg.styles)
  102. }
  103. return
  104. }