Skip to content

Commit

Permalink
使用 pathlib 代替 os.path
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 20, 2024
1 parent f3c949c commit 2c5a87a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 63 deletions.
9 changes: 4 additions & 5 deletions tools/build.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import logging
import os.path
import shutil

from tools import configs
from tools.configs import path_define
from tools.services import dump_service, font_service, image_service
from tools.utils import fs_util

logging.basicConfig(level=logging.DEBUG)


def main():
fs_util.delete_dir(path_define.build_dir)
if path_define.build_dir.exists():
shutil.rmtree(path_define.build_dir)

for dump_config in configs.dump_configs:
dump_service.dump_font(dump_config)
Expand All @@ -22,8 +21,8 @@ def main():
image_service.make_preview_image_file(font_config)

shutil.copy(
os.path.join(path_define.www_static_dir, 'index.html'),
os.path.join(path_define.outputs_dir, 'index.html'),
path_define.www_static_dir.joinpath('index.html'),
path_define.outputs_dir.joinpath('index.html'),
)


Expand Down
5 changes: 2 additions & 3 deletions tools/configs/dump_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import math
import os

from tools.configs import path_define


class DumpConfig:
def __init__(self, font_name: str, font_type: str, glyph_width: int, glyph_height: int):
self.font_name = font_name
self.font_file_path = os.path.join(path_define.fonts_dir, font_name)
self.dump_dir = os.path.join(path_define.dump_dir, font_name)
self.font_file_path = path_define.fonts_dir.joinpath(font_name)
self.dump_dir = path_define.dump_dir.joinpath(font_name)
self.font_type = font_type

self.glyph_width = glyph_width
Expand Down
22 changes: 10 additions & 12 deletions tools/configs/path_define.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
from pathlib import Path

project_root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
project_root_dir = Path(__file__).parent.joinpath('..', '..').resolve()

assets_dir = os.path.join(project_root_dir, 'assets')
glyphs_dir = os.path.join(assets_dir, 'glyphs')
fonts_dir = os.path.join(assets_dir, 'fonts')
www_static_dir = os.path.join(assets_dir, 'www-static')
assets_dir = project_root_dir.joinpath('assets')
glyphs_dir = assets_dir.joinpath('glyphs')
fonts_dir = assets_dir.joinpath('fonts')
www_static_dir = assets_dir.joinpath('www-static')

build_dir = os.path.join(project_root_dir, 'build')
dump_dir = os.path.join(build_dir, 'dump')
outputs_dir = os.path.join(build_dir, 'outputs')
releases_dir = os.path.join(build_dir, 'releases')
www_dir = os.path.join(build_dir, 'www')
build_dir = project_root_dir.joinpath('build')
dump_dir = build_dir.joinpath('dump')
outputs_dir = build_dir.joinpath('outputs')

docs_dir = os.path.join(project_root_dir, 'docs')
docs_dir = project_root_dir.joinpath('docs')
6 changes: 2 additions & 4 deletions tools/services/dump_service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging
import os
from typing import IO

from character_encoding_utils import gb2312
from character_encoding_utils.gb2312 import GB2312Exception

from tools.configs import DumpConfig
from tools.utils import fs_util
from tools.utils import glyph_util

logger = logging.getLogger('dump-service')
Expand All @@ -25,7 +23,7 @@ def _dump_glyph(dump_config: DumpConfig, c: str, glyph_bytes: bytes):
glyph_data_row.append(0)
glyph_data.append(glyph_data_row)
hex_name = f'{ord(c): 04X}'
file_path = os.path.join(dump_config.dump_dir, f'{hex_name}.png')
file_path = dump_config.dump_dir.joinpath(f'{hex_name}.png')
glyph_util.save_glyph_data_to_png(glyph_data, file_path)
logger.info('Dump %s %d*%d %s - %s', dump_config.font_name, dump_config.glyph_width, dump_config.glyph_height, c if c.isprintable() else ' ', hex_name)

Expand Down Expand Up @@ -58,7 +56,7 @@ def _dump_font_gb2312(dump_config: DumpConfig, file: IO, row_start: int, row_end


def dump_font(dump_config: DumpConfig):
fs_util.make_dirs(dump_config.dump_dir)
dump_config.dump_dir.mkdir(parents=True, exist_ok=True)

with open(dump_config.font_file_path, 'rb') as file:
if dump_config.font_type == 'asc':
Expand Down
20 changes: 9 additions & 11 deletions tools/services/font_service.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import logging
import math
import os

from pixel_font_builder import FontBuilder, WeightName, SerifStyle, SlantStyle, WidthStyle, Glyph
from pixel_font_builder.opentype import Flavor

from tools.configs import FontConfig
from tools.configs import path_define
from tools.utils import fs_util
from tools.utils import glyph_util

logger = logging.getLogger('font-service')


def collect_glyph_files(font_config: FontConfig) -> tuple[dict[int, str], list[tuple[str, str]]]:
root_dirs = [os.path.join(path_define.glyphs_dir, str(font_config.size))]
root_dirs = [path_define.glyphs_dir.joinpath(str(font_config.size))]
for source_name in font_config.source_names:
root_dirs.append(os.path.join(path_define.dump_dir, source_name))
root_dirs.append(path_define.dump_dir.joinpath(source_name))

registry = {}
for root_dir in reversed(root_dirs):
for glyph_file_dir, _, glyph_file_names in os.walk(root_dir):
for glyph_file_dir, _, glyph_file_names in root_dir.walk():
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_file_path = glyph_file_dir.joinpath(glyph_file_name)
if glyph_file_name == 'notdef.png':
code_point = -1
else:
Expand Down Expand Up @@ -80,22 +78,22 @@ def _create_builder(font_config: FontConfig, character_mapping: dict[int, str],


def make_font_files(font_config: FontConfig, character_mapping: dict[int, str], glyph_file_infos: list[tuple[str, str]]):
fs_util.make_dirs(path_define.outputs_dir)
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)

builder = _create_builder(font_config, character_mapping, glyph_file_infos)

otf_file_path = os.path.join(path_define.outputs_dir, f'{font_config.outputs_name}.otf')
otf_file_path = path_define.outputs_dir.joinpath(f'{font_config.outputs_name}.otf')
builder.save_otf(otf_file_path)
logger.info("Make font file: '%s'", otf_file_path)

woff2_file_path = os.path.join(path_define.outputs_dir, f'{font_config.outputs_name}.woff2')
woff2_file_path = path_define.outputs_dir.joinpath(f'{font_config.outputs_name}.woff2')
builder.save_otf(woff2_file_path, flavor=Flavor.WOFF2)
logger.info("Make font file: '%s'", woff2_file_path)

ttf_file_path = os.path.join(path_define.outputs_dir, f'{font_config.outputs_name}.ttf')
ttf_file_path = path_define.outputs_dir.joinpath(f'{font_config.outputs_name}.ttf')
builder.save_ttf(ttf_file_path)
logger.info("Make font file: '%s'", ttf_file_path)

bdf_file_path = os.path.join(path_define.outputs_dir, f'{font_config.outputs_name}.bdf')
bdf_file_path = path_define.outputs_dir.joinpath(f'{font_config.outputs_name}.bdf')
builder.save_bdf(bdf_file_path)
logger.info("Make font file: '%s'", bdf_file_path)
8 changes: 3 additions & 5 deletions tools/services/image_service.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import logging
import os

from PIL import Image, ImageFont, ImageDraw

from tools.configs import FontConfig
from tools.configs import path_define
from tools.utils import fs_util

logger = logging.getLogger('image-service')


def make_preview_image_file(font_config: FontConfig):
font = ImageFont.truetype(os.path.join(path_define.outputs_dir, f'{font_config.outputs_name}.woff2'), font_config.size)
font = ImageFont.truetype(path_define.outputs_dir.joinpath(f'{font_config.outputs_name}.woff2'), font_config.size)
text_color = (0, 0, 0, 255)

image = Image.new('RGBA', (font_config.size * 27, font_config.size * 11), (255, 255, 255, 255))
Expand All @@ -23,7 +21,7 @@ def make_preview_image_file(font_config: FontConfig):
draw.text((font_config.size, font_config.size * 9), '0123456789', fill=text_color, font=font)
image = image.resize((image.width * 2, image.height * 2), Image.Resampling.NEAREST)

fs_util.make_dirs(path_define.outputs_dir)
file_path = os.path.join(path_define.outputs_dir, font_config.preview_image_file_name)
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(font_config.preview_image_file_name)
image.save(file_path)
logger.info("Make preview image file: '%s'", file_path)
11 changes: 5 additions & 6 deletions tools/services/publish_service.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import logging
import os
import shutil
from pathlib import Path

from tools import configs
from tools.configs import path_define
from tools.utils import fs_util

logger = logging.getLogger('publish-service')


def _copy_file(file_name: str, from_dir: str, to_dir: str):
from_path = os.path.join(from_dir, file_name)
to_path = os.path.join(to_dir, file_name)
def _copy_file(file_name: str, from_dir: Path, to_dir: Path):
from_path = from_dir.joinpath(file_name)
to_path = to_dir.joinpath(file_name)
shutil.copyfile(from_path, to_path)
logger.info("Copy from '%s' to '%s'", from_path, to_path)


def update_docs():
fs_util.make_dirs(path_define.docs_dir)
path_define.docs_dir.mkdir(exist_ok=True)
for font_config in configs.font_configs:
_copy_file(font_config.preview_image_file_name, path_define.outputs_dir, path_define.docs_dir)
17 changes: 0 additions & 17 deletions tools/utils/fs_util.py

This file was deleted.

0 comments on commit 2c5a87a

Please sign in to comment.