|
| 1 | +#!/usr/bin/python3 |
| 2 | +"""Ui Fonts Menu, to select a font from a list.""" |
| 3 | +import os |
| 4 | +import textwrap |
| 5 | +from collections.abc import Awaitable |
| 6 | +from collections.abc import Callable |
| 7 | + |
| 8 | +from nicegui import app |
| 9 | +from nicegui import ui |
| 10 | + |
| 11 | +from pretty_gpx.common.drawing.utils.fonts import CustomFont |
| 12 | +from pretty_gpx.common.utils.paths import FONTS_DIR |
| 13 | + |
| 14 | +app.add_static_files('/fonts', os.path.abspath(FONTS_DIR)) |
| 15 | + |
| 16 | + |
| 17 | +class UiFontSelect: |
| 18 | + """NiceGui menu to select a font in a list.""" |
| 19 | + |
| 20 | + def __init__(self, |
| 21 | + *, |
| 22 | + label: str, |
| 23 | + fonts: tuple[CustomFont, ...], |
| 24 | + on_change: Callable[[], Awaitable[None]], |
| 25 | + start_font: CustomFont | None = None) -> None: |
| 26 | + """Create a UiFontsMenu.""" |
| 27 | + if start_font is None: |
| 28 | + current_idx = 0 |
| 29 | + else: |
| 30 | + current_idx = fonts.index(start_font) # Can raise ValueError if not found |
| 31 | + |
| 32 | + def on_click_idx(idx: int) -> Callable[[], Awaitable[None]]: |
| 33 | + """On click handler.""" |
| 34 | + async def handler() -> None: |
| 35 | + self.change_current_idx(idx) |
| 36 | + await on_change() |
| 37 | + return handler |
| 38 | + |
| 39 | + with ui.dropdown_button(label, icon="font_download", auto_close=True) as main_button: |
| 40 | + main_button.classes("bg-white text-black normal-case") |
| 41 | + |
| 42 | + for font in fonts: |
| 43 | + font_css_header = get_css_header(font) |
| 44 | + if font_css_header is not None: |
| 45 | + ui.add_css(font_css_header) |
| 46 | + |
| 47 | + items = [ui.item(font.font_name, on_click=on_click_idx(idx)) |
| 48 | + .style(f'font-family:"{font.font_name}";') |
| 49 | + for idx, font in enumerate(fonts)] |
| 50 | + |
| 51 | + ### |
| 52 | + |
| 53 | + self.main_button = main_button |
| 54 | + self.fonts = fonts |
| 55 | + self.items = items |
| 56 | + self.current_idx = current_idx |
| 57 | + |
| 58 | + self.change_current_idx(current_idx) |
| 59 | + |
| 60 | + def change_current_idx(self, new_idx: int) -> None: |
| 61 | + """Change the current index.""" |
| 62 | + if new_idx < 0 or new_idx >= len(self.fonts): |
| 63 | + raise IndexError(f"Index {new_idx} out of bounds for fonts list of length {len(self.fonts)}.") |
| 64 | + self.items[self.current_idx].classes(replace="bg-white text-black") |
| 65 | + self.items[new_idx].classes(replace="bg-primary text-white") |
| 66 | + self.current_idx = new_idx |
| 67 | + self.main_button.style(f'font-family: "{self.font.font_name}";') |
| 68 | + |
| 69 | + @property |
| 70 | + def font(self) -> CustomFont: |
| 71 | + """Return the selected font.""" |
| 72 | + return self.fonts[self.current_idx] |
| 73 | + |
| 74 | + |
| 75 | +def get_css_header(font: CustomFont) -> str | None: |
| 76 | + """Get the CSS header for the font.""" |
| 77 | + font_path = font.value.get_file() |
| 78 | + if font_path is None or not isinstance(font_path, str): |
| 79 | + return None |
| 80 | + |
| 81 | + font_path = os.path.basename(font_path) |
| 82 | + if font_path.lower().endswith('.otf'): |
| 83 | + font_format = 'opentype' |
| 84 | + elif font_path.lower().endswith('.ttf'): |
| 85 | + font_format = 'truetype' |
| 86 | + else: |
| 87 | + raise ValueError("Unsupported font format. Please provide a .otf or .ttf file.") |
| 88 | + |
| 89 | + header = f''' |
| 90 | + @font-face {{ |
| 91 | + font-family: '{font.font_name}'; |
| 92 | + src: url('/fonts/{font_path}') format('{font_format}'); |
| 93 | + }} |
| 94 | + ''' |
| 95 | + return textwrap.dedent(header) |
0 commit comments