Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Aug 11, 2023
1 parent 84a347b commit 1a0631b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
62 changes: 32 additions & 30 deletions services/font_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,44 @@

def format_glyph_files(font_config: FontConfig):
root_dir = os.path.join(path_define.glyphs_dir, font_config.outputs_name)
for glyph_file_dir, glyph_file_name in fs_util.walk_files(root_dir):
if not glyph_file_name.endswith('.png'):
continue
glyph_file_path = os.path.join(glyph_file_dir, glyph_file_name)
glyph_data, glyph_width, glyph_height = glyph_util.load_glyph_data_from_png(glyph_file_path)
assert (glyph_height - font_config.size) % 2 == 0, f"Incorrect glyph data: '{glyph_file_path}'"
if glyph_height > font_config.line_height:
for i in range((glyph_height - font_config.line_height) // 2):
glyph_data.pop(0)
glyph_data.pop()
elif glyph_height < font_config.line_height:
for i in range((font_config.line_height - glyph_height) // 2):
glyph_data.insert(0, [0 for _ in range(glyph_width)])
glyph_data.append([0 for _ in range(glyph_width)])
glyph_util.save_glyph_data_to_png(glyph_data, glyph_file_path)
logger.info("Format glyph file: '%s'", glyph_file_path)
for glyph_file_dir, _, glyph_file_names in os.walk(root_dir):
for glyph_file_name in glyph_file_names:
if not glyph_file_name.endswith('.png'):
continue
glyph_file_path = os.path.join(glyph_file_dir, glyph_file_name)
glyph_data, glyph_width, glyph_height = glyph_util.load_glyph_data_from_png(glyph_file_path)
assert (glyph_height - font_config.size) % 2 == 0, f"Incorrect glyph data: '{glyph_file_path}'"
if glyph_height > font_config.line_height:
for i in range((glyph_height - font_config.line_height) // 2):
glyph_data.pop(0)
glyph_data.pop()
elif glyph_height < font_config.line_height:
for i in range((font_config.line_height - glyph_height) // 2):
glyph_data.insert(0, [0 for _ in range(glyph_width)])
glyph_data.append([0 for _ in range(glyph_width)])
glyph_util.save_glyph_data_to_png(glyph_data, glyph_file_path)
logger.info("Format glyph file: '%s'", glyph_file_path)


def collect_glyph_files(font_config: FontConfig) -> tuple[dict[int, str], list[tuple[str, str]]]:
root_dir = os.path.join(path_define.glyphs_dir, font_config.outputs_name)

registry = {}
for glyph_file_dir, glyph_file_name in fs_util.walk_files(root_dir):
if not glyph_file_name.endswith('.png'):
continue
glyph_file_path = os.path.join(glyph_file_dir, glyph_file_name)
c_name = glyph_file_name.removesuffix('.png')
if c_name == 'notdef':
code_point = -1
elif c_name == 'space':
code_point = ord(' ')
elif c_name == 'full_stop':
code_point = ord('.')
else:
code_point = ord(c_name)
registry[code_point] = glyph_file_path
for glyph_file_dir, _, glyph_file_names in os.walk(root_dir):
for glyph_file_name in glyph_file_names:
if not glyph_file_name.endswith('.png'):
continue
glyph_file_path = os.path.join(glyph_file_dir, glyph_file_name)
c_name = glyph_file_name.removesuffix('.png')
if c_name == 'notdef':
code_point = -1
elif c_name == 'space':
code_point = ord(' ')
elif c_name == 'full_stop':
code_point = ord('.')
else:
code_point = ord(c_name)
registry[code_point] = glyph_file_path

sequence = list(registry.keys())
sequence.sort()
Expand Down
10 changes: 0 additions & 10 deletions utils/fs_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import shutil
from collections.abc import Iterator


def delete_dir(path: str):
Expand All @@ -16,12 +15,3 @@ def make_dirs(path: str):
raise Exception(f"Path exists but not a directory: '{path}'")
else:
os.makedirs(path)


def walk_files(path: str) -> Iterator[tuple[str, str]]:
if os.path.exists(path):
if not os.path.isdir(path):
raise Exception(f"Path not a directory: '{path}'")
for parent, _, names in os.walk(path):
for name in names:
yield parent, name

0 comments on commit 1a0631b

Please sign in to comment.