Skip to content

Commit

Permalink
修复参数类型约束
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 7, 2024
1 parent d874971 commit fc3a654
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
20 changes: 10 additions & 10 deletions tools/configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
]
width_modes = list[WidthMode](get_args(WidthMode.__value__))

type LanguageFileFlavors = Literal[
type LanguageFlavor = Literal[
'latin',
'zh_cn',
'zh_hk',
'zh_tw',
'zh_tr',
'zh_hans',
'zh_hant',
'ja',
'ko',
]
language_file_flavors = list[LanguageFileFlavors](get_args(LanguageFileFlavors.__value__))
language_flavors = list[LanguageFlavor](get_args(LanguageFlavor.__value__))

type LanguageFlavor = Literal[
type LanguageFileFlavor = Literal[
'latin',
'zh_hans',
'zh_hant',
'zh_cn',
'zh_hk',
'zh_tw',
'zh_tr',
'ja',
'ko',
]
language_flavors = list[LanguageFlavor](get_args(LanguageFlavor.__value__))
language_file_flavors = list[LanguageFileFlavor](get_args(LanguageFileFlavor.__value__))

type FontFormat = Literal['otf', 'woff2', 'ttf', 'bdf', 'pcf']
font_formats = list[FontFormat](get_args(FontFormat.__value__))
Expand Down
6 changes: 3 additions & 3 deletions tools/configs/fallback.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path

from tools import configs
from tools.configs import path_define, FontSize, LanguageFileFlavors
from tools.configs import path_define, FontSize, LanguageFileFlavor
from tools.utils import fs_util


Expand All @@ -26,14 +26,14 @@ def load() -> dict[FontSize, list['FallbackConfig']]:
font_size: FontSize
dir_from: Path
dir_to: Path
flavor: LanguageFileFlavors | None
flavor: LanguageFileFlavor | None

def __init__(
self,
font_size: FontSize,
dir_from: Path,
dir_to: Path,
flavor: LanguageFileFlavors | None,
flavor: LanguageFileFlavor | None,
):
self.font_size = font_size
self.dir_from = dir_from
Expand Down
30 changes: 15 additions & 15 deletions tools/services/font_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pixel_font_knife.mono_bitmap import MonoBitmap

from tools import configs
from tools.configs import path_define
from tools.configs import path_define, WidthMode, LanguageFlavor, LanguageFileFlavor, FontFormat, FontCollectionFormat
from tools.configs.font import FontConfig
from tools.utils import fs_util

Expand Down Expand Up @@ -201,7 +201,7 @@ def fallback(self, other: 'DesignContext'):
self._character_mapping_pool.clear()
self._glyph_files_pool.clear()

def _get_sequence(self, width_mode: str) -> list[int]:
def _get_sequence(self, width_mode: WidthMode) -> list[int]:
if width_mode in self._sequence_pool:
sequence = self._sequence_pool[width_mode]
else:
Expand All @@ -211,7 +211,7 @@ def _get_sequence(self, width_mode: str) -> list[int]:
self._sequence_pool[width_mode] = sequence
return sequence

def get_alphabet(self, width_mode: str) -> set[str]:
def get_alphabet(self, width_mode: WidthMode) -> set[str]:
if width_mode in self._alphabet_pool:
alphabet = self._alphabet_pool[width_mode]
else:
Expand All @@ -220,16 +220,16 @@ def get_alphabet(self, width_mode: str) -> set[str]:
return alphabet

@staticmethod
def _compat_language_flavor(language_flavor: str):
def _compat_language_file_flavor(language_flavor: LanguageFlavor | None) -> LanguageFileFlavor:
if language_flavor == 'zh_hans':
return 'zh_cn'
elif language_flavor == 'zh_hant':
return 'zh_tr'
else:
return language_flavor

def get_character_mapping(self, width_mode: str, language_flavor: str) -> dict[int, str]:
language_flavor = DesignContext._compat_language_flavor(language_flavor)
def get_character_mapping(self, width_mode: WidthMode, language_flavor: LanguageFlavor) -> dict[int, str]:
language_flavor = DesignContext._compat_language_file_flavor(language_flavor)
key = f'{width_mode}#{language_flavor}'
if key in self._character_mapping_pool:
character_mapping = self._character_mapping_pool[key]
Expand All @@ -246,15 +246,15 @@ def get_character_mapping(self, width_mode: str, language_flavor: str) -> dict[i
self._character_mapping_pool[key] = character_mapping
return character_mapping

def get_glyph_files(self, width_mode: str, language_flavor: str | None = None) -> list[GlyphFile]:
language_flavor = DesignContext._compat_language_flavor(language_flavor)
def get_glyph_files(self, width_mode: WidthMode, language_flavor: LanguageFlavor | None = None) -> list[GlyphFile]:
language_flavor = DesignContext._compat_language_file_flavor(language_flavor)
key = f'{width_mode}#{'' if language_flavor is None else language_flavor}'
if key in self._glyph_files_pool:
glyph_files = self._glyph_files_pool[key]
else:
glyph_files = []
if language_flavor is None:
language_flavors = [DesignContext._compat_language_flavor(language_flavor) for language_flavor in configs.language_flavors]
language_flavors = [DesignContext._compat_language_file_flavor(language_flavor) for language_flavor in configs.language_flavors]
else:
language_flavors = [language_flavor]
sequence = self._get_sequence(width_mode)
Expand All @@ -273,8 +273,8 @@ def get_glyph_files(self, width_mode: str, language_flavor: str | None = None) -
def _create_builder(
design_context: DesignContext,
glyph_pool: dict[Path, Glyph],
width_mode: str,
language_flavor: str,
width_mode: WidthMode,
language_flavor: LanguageFlavor,
is_collection: bool,
) -> FontBuilder:
layout_param = design_context.font_config.layout_params[width_mode]
Expand Down Expand Up @@ -331,19 +331,19 @@ def _create_builder(

class FontContext:
design_context: DesignContext
width_mode: str
width_mode: WidthMode
_glyph_pool: dict[Path, Glyph]
_builders: dict[str, FontBuilder]
_collection_builder: FontCollectionBuilder | None

def __init__(self, design_context: DesignContext, width_mode: str):
def __init__(self, design_context: DesignContext, width_mode: WidthMode):
self.design_context = design_context
self.width_mode = width_mode
self._glyph_pool = {}
self._builders = {}
self._collection_builder = None

def _get_builder(self, language_flavor: str) -> FontBuilder:
def _get_builder(self, language_flavor: LanguageFlavor) -> FontBuilder:
if language_flavor in self._builders:
builder = self._builders[language_flavor]
else:
Expand All @@ -360,7 +360,7 @@ def _get_collection_builder(self) -> FontCollectionBuilder:
self._collection_builder = collection_builder
return self._collection_builder

def make_fonts(self, font_format: str):
def make_fonts(self, font_format: FontFormat | FontCollectionFormat):
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
if font_format in configs.font_formats:
for language_flavor in configs.language_flavors:
Expand Down
4 changes: 2 additions & 2 deletions tools/services/image_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from PIL.ImageFont import FreeTypeFont
from loguru import logger

from tools.configs import path_define
from tools.configs import path_define, WidthMode, LanguageFlavor
from tools.configs.font import FontConfig


def _load_font(font_config: FontConfig, width_mode: str, language_flavor: str, scale: int = 1) -> FreeTypeFont:
def _load_font(font_config: FontConfig, width_mode: WidthMode, language_flavor: LanguageFlavor, scale: int = 1) -> FreeTypeFont:
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{font_config.font_size}px-{width_mode}-{language_flavor}.woff2')
return ImageFont.truetype(file_path, font_config.font_size * scale)

Expand Down
6 changes: 3 additions & 3 deletions tools/services/info_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from unidata_blocks import UnicodeBlock

from tools import configs
from tools.configs import path_define
from tools.configs import path_define, WidthMode
from tools.services.font_service import DesignContext


Expand Down Expand Up @@ -99,7 +99,7 @@ def _write_locale_chr_count_infos_table(output: TextIO, infos: list[tuple[str, i
output.write(f'| {name} | {count} / {total} | {missing} | {progress:.2%} {finished_emoji} |\n')


def make_font_info(design_context: DesignContext, width_mode: str):
def make_font_info(design_context: DesignContext, width_mode: WidthMode):
alphabet = design_context.get_alphabet(width_mode)

output = StringIO()
Expand Down Expand Up @@ -148,7 +148,7 @@ def make_font_info(design_context: DesignContext, width_mode: str):
logger.info("Make font info: '{}'", file_path)


def make_alphabet_txt(design_context: DesignContext, width_mode: str):
def make_alphabet_txt(design_context: DesignContext, width_mode: WidthMode):
alphabet = sorted(design_context.get_alphabet(width_mode))

path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions tools/services/publish_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from loguru import logger

from tools import configs
from tools.configs import path_define
from tools.configs import path_define, FontSize, WidthMode, FontFormat, FontCollectionFormat


def make_release_zip(font_size: int, width_mode: str, font_format: str):
def make_release_zip(font_size: FontSize, width_mode: WidthMode, font_format: FontFormat | FontCollectionFormat):
path_define.releases_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.releases_dir.joinpath(f'fusion-pixel-font-{font_size}px-{width_mode}-{font_format}-v{configs.font_version}.zip')
with zipfile.ZipFile(file_path, 'w') as file:
Expand Down
8 changes: 4 additions & 4 deletions tools/services/template_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from loguru import logger

from tools import configs
from tools.configs import path_define
from tools.configs import path_define, FontSize, WidthMode
from tools.configs.font import FontConfig
from tools.services.font_service import DesignContext

Expand Down Expand Up @@ -40,7 +40,7 @@ def _make_html(template_name: str, file_name: str, params: dict[str, object] | N
logger.info("Make html: '{}'", file_path)


def make_alphabet_html(design_context: DesignContext, width_mode: str):
def make_alphabet_html(design_context: DesignContext, width_mode: WidthMode):
_make_html('alphabet.html', f'alphabet-{design_context.font_config.font_size}px-{width_mode}.html', {
'font_config': design_context.font_config,
'width_mode': width_mode,
Expand Down Expand Up @@ -119,13 +119,13 @@ def make_demo_html(design_context: DesignContext):
})


def make_index_html(font_configs: dict[int, FontConfig]):
def make_index_html(font_configs: dict[FontSize, FontConfig]):
_make_html('index.html', 'index.html', {
'font_configs': font_configs,
})


def make_playground_html(font_configs: dict[int, FontConfig]):
def make_playground_html(font_configs: dict[FontSize, FontConfig]):
_make_html('playground.html', 'playground.html', {
'font_configs': font_configs,
})

0 comments on commit fc3a654

Please sign in to comment.