Skip to content

Commit

Permalink
优化 format_glyphs()
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Nov 12, 2024
1 parent ab7cbaf commit 86c0df1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
2 changes: 1 addition & 1 deletion tools/format.py → tools/format_glyphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def main():
for font_size in configs.font_sizes:
font_config = FontConfig.load(font_size)
format_service.format_glyph_files(font_config)
format_service.format_glyphs(font_config)


if __name__ == '__main__':
Expand Down
30 changes: 2 additions & 28 deletions tools/services/format_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import shutil
from pathlib import Path

import unidata_blocks
from loguru import logger
from pixel_font_knife import glyph_file_util

from tools import configs
Expand All @@ -19,35 +17,11 @@ def _is_empty_dir(path: Path) -> bool:
return True


def format_glyph_files(font_config: FontConfig):
def format_glyphs(font_config: FontConfig):
for width_mode_dir_name in itertools.chain(['common'], configs.width_modes):
width_mode_dir = path_define.patch_glyphs_dir.joinpath(str(font_config.font_size), width_mode_dir_name)
context = glyph_file_util.load_context(width_mode_dir)
for code_point, flavor_group in context.items():
if code_point == -1:
code_name = 'notdef'
file_dir = width_mode_dir
else:
code_name = f'{code_point:04X}'
block = unidata_blocks.get_block_by_code_point(code_point)
file_dir = width_mode_dir.joinpath(f'{block.code_start:04X}-{block.code_end:04X} {block.name}')
if block.code_start == 0x4E00:
file_dir = file_dir.joinpath(f'{code_name[0:-2]}-')

for glyph_file in set(flavor_group.values()):
if len(glyph_file.flavors) > 0:
file_name = f'{code_name} {','.join(sorted(glyph_file.flavors, key=lambda x: configs.language_file_flavors.index(x)))}.png'
else:
file_name = f'{code_name}.png'
file_path = file_dir.joinpath(file_name)
if glyph_file.file_path != file_path:
assert not file_path.exists(), f"[{font_config.font_size}px] duplicate glyph files: '{glyph_file.file_path}' -> '{file_path}'"
file_dir.mkdir(parents=True, exist_ok=True)
glyph_file.file_path.rename(file_path)
logger.info("Format glyph file path: '{}' -> '{}'", glyph_file.file_path, file_path)
glyph_file.file_path = file_path

glyph_file.bitmap.save_png(glyph_file.file_path)
glyph_file_util.normalize_context(context, width_mode_dir, configs.language_file_flavors)

for file_dir, _, _ in width_mode_dir.walk(top_down=False):
if _is_empty_dir(file_dir):
Expand Down

0 comments on commit 86c0df1

Please sign in to comment.