From 8a0b562f4f9c6e0d560d4fba159ae56421687cd3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 3 May 2024 19:57:02 +0530 Subject: [PATCH] Work on listing available styles for a family --- kitty/fonts/__init__.py | 1 + kitty/fonts/core_text.py | 2 +- kitty/fonts/fontconfig.py | 1 + tools/cmd/list_fonts/styles.go | 35 ++++++++++++++++++++++++++++++++++ tools/cmd/list_fonts/types.go | 1 + tools/cmd/list_fonts/ui.go | 5 ++--- 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tools/cmd/list_fonts/styles.go diff --git a/kitty/fonts/__init__.py b/kitty/fonts/__init__.py index 5974c6e1c..b79d9a629 100644 --- a/kitty/fonts/__init__.py +++ b/kitty/fonts/__init__.py @@ -6,6 +6,7 @@ from kitty.typing import CoreTextFont, FontConfigPattern class ListedFont(TypedDict): family: str + style: str full_name: str postscript_name: str is_monospace: bool diff --git a/kitty/fonts/core_text.py b/kitty/fonts/core_text.py index daaf16c8f..7777566a4 100644 --- a/kitty/fonts/core_text.py +++ b/kitty/fonts/core_text.py @@ -47,7 +47,7 @@ def list_fonts() -> Generator[ListedFont, None, None]: if not fn: fn = f'{f} {fd["style"]}'.strip() yield {'family': f, 'full_name': fn, 'postscript_name': fd['postscript_name'] or '', 'is_monospace': fd['monospace'], - 'is_variable': fd['variable'], 'descriptor': fd} + 'is_variable': fd['variable'], 'descriptor': fd, 'style': fd['style']} def create_scorer(bold: bool = False, italic: bool = False, monospaced: bool = True, prefer_variable: bool = False) -> Scorer: diff --git a/kitty/fonts/fontconfig.py b/kitty/fonts/fontconfig.py index 9c27210cb..04d1563fe 100644 --- a/kitty/fonts/fontconfig.py +++ b/kitty/fonts/fontconfig.py @@ -61,6 +61,7 @@ def list_fonts(only_variable: bool = False) -> Generator[ListedFont, None, None] yield { 'family': f, 'full_name': fn, 'postscript_name': str(fd.get('postscript_name', '')), 'is_monospace': is_mono, 'descriptor': fd, 'is_variable': fd.get('variable', False), + 'style': fd['style'], } diff --git a/tools/cmd/list_fonts/styles.go b/tools/cmd/list_fonts/styles.go new file mode 100644 index 000000000..62d974099 --- /dev/null +++ b/tools/cmd/list_fonts/styles.go @@ -0,0 +1,35 @@ +package list_fonts + +import ( + "fmt" + "kitty/tools/utils" +) + +var _ = fmt.Print + +func styles_in_family(family string, fonts []ListedFont) (ans []string, is_variable bool) { + has_style_attribute_data := false + for _, f := range fonts { + vd := variable_data_for(f) + if len(vd.Design_axes) > 0 { + has_style_attribute_data = true + break + } + } + ans = make([]string, 0) + seen := utils.NewSet[string]() + add := func(x string) { + if !seen.Has(x) { + seen.Add(x) + ans = append(ans, x) + } + } + if has_style_attribute_data { + } else { + for _, f := range fonts { + add(f.Style) + } + } + debugprintln(111111111, family, has_style_attribute_data, ans) + return +} diff --git a/tools/cmd/list_fonts/types.go b/tools/cmd/list_fonts/types.go index 63f2d6d46..8c15d340b 100644 --- a/tools/cmd/list_fonts/types.go +++ b/tools/cmd/list_fonts/types.go @@ -45,6 +45,7 @@ type MultiAxisStyle struct { type ListedFont struct { Family string `json:"family"` + Style string `json:"style"` Fullname string `json:"full_name"` Postscript_name string `json:"postscript_name"` Is_monospace bool `json:"is_monospace"` diff --git a/tools/cmd/list_fonts/ui.go b/tools/cmd/list_fonts/ui.go index 115fa95c0..7e547a3fa 100644 --- a/tools/cmd/list_fonts/ui.go +++ b/tools/cmd/list_fonts/ui.go @@ -83,6 +83,7 @@ func (h *handler) draw_family_summary(start_x int, sz loop.ScreenSize) (err erro return fmt.Errorf("The family: %s has no fonts", family) } if has_variable_data_for_font(fonts[0]) { + styles_in_family(family, fonts) } else { lines = append(lines, "Reading font data, please wait…") key := fonts[0].cache_key() @@ -261,11 +262,9 @@ func (h *handler) on_wakeup() (err error) { case SCANNING_FAMILIES: h.state = LISTING_FAMILIES h.family_list.UpdateFamilies(utils.StableSortWithKey(maps.Keys(h.fonts), strings.ToLower)) - return h.draw_screen() case LISTING_FAMILIES: - return h.draw_screen() } - return + return h.draw_screen() } func (h *handler) on_key_event(event *loop.KeyEvent) (err error) {