Skip to content

Commit

Permalink
调整配置结构
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed May 11, 2024
1 parent 01279a5 commit 548d34a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
98 changes: 73 additions & 25 deletions scripts/configs/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,89 @@ class FontConfig:
def load_all() -> dict[str, 'FontConfig']:
configs = []
for outputs_name in os.listdir(path_define.glyphs_dir):
config_file_path = os.path.join(path_define.glyphs_dir, outputs_name, 'config.toml')
if not os.path.isfile(config_file_path):
file_path = os.path.join(path_define.glyphs_dir, outputs_name, 'config.toml')
if not os.path.isfile(file_path):
continue
config_data: dict = fs_util.read_toml(config_file_path)['font']
config = FontConfig(config_data)
assert config.outputs_name == outputs_name, f"Config 'name' error: '{config_file_path}'"
assert (config.line_height - config.font_size) % 2 == 0, f"Config 'line_height' error: '{config_file_path}'"
config_data = fs_util.read_toml(file_path)['font']
name = config_data['name']
weight_name = WeightName(config_data['weight_name'])
serif_style = SerifStyle(config_data['serif_style'])
slant_style = SlantStyle(config_data['slant_style'])
width_mode = WidthMode(config_data['width_mode'])
description = config_data['description']
font_size = config_data['size']
ascent = config_data['ascent']
descent = config_data['descent']
x_height = config_data['x_height']
cap_height = config_data['cap_height']
fallback_lower_from_upper = config_data.get('fallback_lower_from_upper', False)
fallback_upper_from_lower = config_data.get('fallback_upper_from_lower', False)
readme_intro = config_data['readme_intro']
preview_text = config_data.get('preview_text', _DEFAULT_PREVIEW_TEXT).strip()
config = FontConfig(
name,
weight_name,
serif_style,
slant_style,
width_mode,
description,
font_size,
ascent,
descent,
x_height,
cap_height,
fallback_lower_from_upper,
fallback_upper_from_lower,
readme_intro,
preview_text,
)
assert config.outputs_name == outputs_name, f"Config 'name' error: '{file_path}'"
assert (config.line_height - font_size) % 2 == 0, f"Config 'line_height' error: '{file_path}'"
configs.append(config)
configs.sort(key=lambda x: x.name)
return {config.outputs_name: config for config in configs}

def __init__(self, config_data: dict):
self.name: str = config_data['name']
self.family_name = FontConfig.FAMILY_NAME_FORMAT.format(font_name=self.name)
self.outputs_name = self.name.lower().replace(' ', '-')
def __init__(
self,
name: str,
weight_name: WeightName,
serif_style: SerifStyle,
slant_style: SlantStyle,
width_mode: WidthMode,
description: str,
font_size: int,
ascent: int,
descent: int,
x_height: int,
cap_height: int,
fallback_lower_from_upper: bool,
fallback_upper_from_lower: bool,
readme_intro: str,
preview_text: str,
):
self.name = name
self.family_name = FontConfig.FAMILY_NAME_FORMAT.format(font_name=name)
self.outputs_name = name.lower().replace(' ', '-')
self.full_outputs_name = self.family_name.lower().replace(' ', '-')

self.weight_name: str = WeightName(config_data['weight_name'])
self.serif_style: str = SerifStyle(config_data['serif_style'])
self.slant_style: str = SlantStyle(config_data['slant_style'])
self.width_mode: str = WidthMode(config_data['width_mode'])
self.description: str = config_data['description']
self.copyright_info = FontConfig.COPYRIGHT_INFO_FORMAT.format(font_name=self.name)
self.weight_name = weight_name
self.serif_style = serif_style
self.slant_style = slant_style
self.width_mode = width_mode
self.description = description
self.copyright_info = FontConfig.COPYRIGHT_INFO_FORMAT.format(font_name=name)

self.font_size: int = config_data['size']
self.ascent: int = config_data['ascent']
self.descent: int = config_data['descent']
self.x_height: int = config_data['x_height']
self.cap_height: int = config_data['cap_height']
self.font_size = font_size
self.ascent = ascent
self.descent = descent
self.x_height = x_height
self.cap_height = cap_height

self.fallback_lower_from_upper: bool = config_data.get('fallback_lower_from_upper', False)
self.fallback_upper_from_lower: bool = config_data.get('fallback_upper_from_lower', False)
self.fallback_lower_from_upper = fallback_lower_from_upper
self.fallback_upper_from_lower = fallback_upper_from_lower

self.readme_intro: str = config_data['readme_intro']
self.preview_text: str = config_data.get('preview_text', _DEFAULT_PREVIEW_TEXT).strip()
self.readme_intro = readme_intro
self.preview_text = preview_text

self.outputs_dir = os.path.join(path_define.outputs_dir, self.outputs_name)
self.docs_dir = os.path.join(path_define.docs_dir, self.outputs_name)
Expand Down
3 changes: 2 additions & 1 deletion scripts/utils/fs_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import shutil
import tomllib
from typing import Any

logger = logging.getLogger('fs_util')

Expand Down Expand Up @@ -87,5 +88,5 @@ def write_str(text: str, path: str | bytes | os.PathLike[str] | os.PathLike[byte
file.write(text)


def read_toml(path: str | bytes | os.PathLike[str] | os.PathLike[bytes]) -> dict:
def read_toml(path: str | bytes | os.PathLike[str] | os.PathLike[bytes]) -> Any:
return tomllib.loads(read_str(path))

0 comments on commit 548d34a

Please sign in to comment.