Skip to content

Commit

Permalink
重构构建逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 20, 2024
1 parent daa29aa commit 388cd0d
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 255 deletions.
13 changes: 5 additions & 8 deletions tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ def main():
dump_service.dump_font(dump_config)

for font_config in configs.font_configs:
character_mapping, glyph_file_infos = font_service.collect_glyph_files(font_config)
font_service.make_font_files(font_config, character_mapping, glyph_file_infos)
image_service.make_preview_image_file(font_config)

shutil.copy(
path_define.www_static_dir.joinpath('index.html'),
path_define.outputs_dir.joinpath('index.html'),
)
character_mapping, glyph_sequence = font_service.collect_glyph_files(font_config)
font_service.make_fonts(font_config, character_mapping, glyph_sequence)
image_service.make_preview_image(font_config)

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


if __name__ == '__main__':
Expand Down
92 changes: 42 additions & 50 deletions tools/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,114 +1,106 @@
from tools.configs.dump_config import DumpConfig
from tools.configs.font_config import FontConfig
from typing import Literal, get_args

font_configs = [
FontConfig(
size=12,
ascent=9,
descent=-3,
x_height=6,
cap_height=8,
source_names=['ASC12', 'HZK12'],
),
FontConfig(
size=16,
ascent=12,
descent=-4,
x_height=7,
cap_height=10,
source_names=['ASC16', 'HZK16'],
),
]
from tools.configs.dump import DumpConfig
from tools.configs.font import FontConfig

version = '1.4.0'

dump_configs = [
DumpConfig(
font_name='ASC12',
font_type='asc',
glyph_width=6,
glyph_height=12,
font_size=12,
),
DumpConfig(
font_name='ASC16',
font_type='asc',
glyph_width=8,
glyph_height=16,
font_size=16,
),
DumpConfig(
font_name='ASC48',
font_type='asc',
glyph_width=24,
glyph_height=48,
font_size=48,
),
DumpConfig(
font_name='HZK12',
font_type='hzk',
glyph_width=12,
glyph_height=12,
font_size=12,
),
DumpConfig(
font_name='HZK14',
font_type='hzk',
glyph_width=14,
glyph_height=14,
font_size=14,
),
DumpConfig(
font_name='HZK16',
font_type='hzk',
glyph_width=16,
glyph_height=16,
font_size=16,
),
DumpConfig(
font_name='HZK16F',
font_type='hzk',
glyph_width=16,
glyph_height=16,
font_size=16,
),
DumpConfig(
font_name='HZK16S',
font_type='hzk',
glyph_width=16,
glyph_height=16,
font_size=16,
),
DumpConfig(
font_name='HZK24F',
font_type='hzk',
glyph_width=24,
glyph_height=24,
font_size=24,
),
DumpConfig(
font_name='HZK24H',
font_type='hzk',
glyph_width=24,
glyph_height=24,
font_size=24,
),
DumpConfig(
font_name='HZK24K',
font_type='hzk',
glyph_width=24,
glyph_height=24,
font_size=24,
),
DumpConfig(
font_name='HZK24S',
font_type='hzk',
glyph_width=24,
glyph_height=24,
font_size=24,
),
DumpConfig(
font_name='HZK32',
font_type='hzk',
glyph_width=32,
glyph_height=32,
font_size=32,
),
DumpConfig(
font_name='HZK40',
font_type='hzk',
glyph_width=40,
glyph_height=40,
font_size=40,
),
DumpConfig(
font_name='HZK48',
font_type='hzk',
glyph_width=48,
glyph_height=48,
font_size=48,
),
]

font_configs = [
FontConfig(
font_size=12,
ascent=9,
descent=-3,
x_height=6,
cap_height=8,
source_names=['ASC12', 'HZK12'],
),
FontConfig(
font_size=16,
ascent=12,
descent=-4,
x_height=7,
cap_height=10,
source_names=['ASC16', 'HZK16'],
),
]

type FontFormat = Literal['otf', 'ttf', 'woff2', 'bdf', 'pcf']
font_formats = list[FontFormat](get_args(FontFormat.__value__))
19 changes: 19 additions & 0 deletions tools/configs/dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Literal

type FontType = Literal['asc', 'hzk']


class DumpConfig:
font_name: str
font_type: FontType
font_size: int

def __init__(
self,
font_name: str,
font_type: FontType,
font_size: int,
):
self.font_name = font_name
self.font_type = font_type
self.font_size = font_size
19 changes: 0 additions & 19 deletions tools/configs/dump_config.py

This file was deleted.

24 changes: 24 additions & 0 deletions tools/configs/font.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

class FontConfig:
font_size: int
ascent: int
descent: int
x_height: int
cap_height: int
source_names: list[str]

def __init__(
self,
font_size: int,
ascent: int,
descent: int,
x_height: int,
cap_height: int,
source_names: list[str],
):
self.font_size = font_size
self.ascent = ascent
self.descent = descent
self.x_height = x_height
self.cap_height = cap_height
self.source_names = source_names
19 changes: 0 additions & 19 deletions tools/configs/font_config.py

This file was deleted.

96 changes: 42 additions & 54 deletions tools/services/dump_service.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,55 @@
from typing import IO
import math

from character_encoding_utils import gb2312
from character_encoding_utils.gb2312 import GB2312Exception
from loguru import logger
from pixel_font_knife.mono_bitmap import MonoBitmap

from tools.configs import DumpConfig
from tools.utils import glyph_util
from tools.configs import DumpConfig, path_define


def _dump_glyph(dump_config: DumpConfig, c: str, glyph_bytes: bytes):
glyph_data = []
for row_index in range(dump_config.glyph_height):
glyph_data_row = []
for col_index in range(dump_config.glyph_col_bytes_length):
b = glyph_bytes[row_index * dump_config.glyph_col_bytes_length + col_index]
for bit_index in range(dump_config.glyph_last_col_byte_bit_stop if col_index == dump_config.glyph_col_bytes_length - 1 else 8):
if 0b1 << (7 - bit_index) & b:
glyph_data_row.append(1)
else:
glyph_data_row.append(0)
glyph_data.append(glyph_data_row)
hex_name = f'{ord(c): 04X}'
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 {} {}*{} {} - {}', dump_config.font_name, dump_config.glyph_width, dump_config.glyph_height, c if c.isprintable() else ' ', hex_name)


def _dump_font_ascii(dump_config: DumpConfig, file: IO, num_start: int, num_end: int):
for num in range(num_start, num_end + 1):
c = chr(num)
glyph_offset = num * dump_config.glyph_bytes_length
file.seek(glyph_offset)
glyph_bytes = file.read(dump_config.glyph_bytes_length)
if len(glyph_bytes) == 0:
break
_dump_glyph(dump_config, c, glyph_bytes)


def _dump_font_gb2312(dump_config: DumpConfig, file: IO, row_start: int, row_end: int):
count = 0
for row in range(row_start, row_end + 1):
for col in range(1, 94 + 1):
try:
c = gb2312.query_chr(row, col)
glyph_offset = ((row - 1) * 94 + (col - 1)) * dump_config.glyph_bytes_length
file.seek(glyph_offset)
glyph_bytes = file.read(dump_config.glyph_bytes_length)
_dump_glyph(dump_config, c, glyph_bytes)
count += 1
except GB2312Exception:
pass
return count
def _parse_bitmap(bitmap_bytes: bytes, row_bytes_size: int, width: int, height: int) -> MonoBitmap:
bitmap = MonoBitmap()
bitmap.width = width
bitmap.height = height
for row_index in range(height):
bitmap_row = []
for col_index in range(row_bytes_size):
b = bitmap_bytes[row_index * row_bytes_size + col_index]
row_string = f'{b:08b}'
bitmap_row.extend(int(c) for c in row_string)
bitmap.append(bitmap_row[:width])
return bitmap


def dump_font(dump_config: DumpConfig):
dump_config.dump_dir.mkdir(parents=True, exist_ok=True)
dump_dir = path_define.dump_dir.joinpath(dump_config.font_name)
dump_dir.mkdir(parents=True, exist_ok=True)

with dump_config.font_file_path.open('rb') as file:
with path_define.fonts_dir.joinpath(dump_config.font_name).open('rb') as file:
if dump_config.font_type == 'asc':
_dump_font_ascii(dump_config, file, 0, 255)
elif dump_config.font_type == 'hzk':
assert _dump_font_gb2312(dump_config, file, 1, 9) == gb2312.get_other_count()
assert _dump_font_gb2312(dump_config, file, 16, 55) == gb2312.get_level_1_count()
assert _dump_font_gb2312(dump_config, file, 56, 87) == gb2312.get_level_2_count()
row_bytes_size = math.ceil(dump_config.font_size / 2 / 8)
bitmap_bytes_size = row_bytes_size * dump_config.font_size
for code_point in range(0, 255 + 1):
file.seek(code_point * bitmap_bytes_size)
bitmap_bytes = file.read(bitmap_bytes_size)
if len(bitmap_bytes) == 0:
break
bitmap = _parse_bitmap(bitmap_bytes, row_bytes_size, dump_config.font_size // 2, dump_config.font_size)
bitmap.save_png(dump_dir.joinpath(f'{code_point:04X}.png'))
else:
raise Exception(f"Unknown font type: '{dump_config.font_type}'")
assert dump_config.font_type == 'hzk'
row_bytes_size = math.ceil(dump_config.font_size / 8)
bitmap_bytes_size = row_bytes_size * dump_config.font_size
for row in range(1, 94 + 1):
for col in range(1, 94 + 1):
try:
c = gb2312.query_chr(row, col)
except GB2312Exception:
continue
file.seek(((row - 1) * 94 + (col - 1)) * bitmap_bytes_size)
bitmap_bytes = file.read(bitmap_bytes_size)
bitmap = _parse_bitmap(bitmap_bytes, row_bytes_size, dump_config.font_size, dump_config.font_size)
bitmap.save_png(dump_dir.joinpath(f'{ord(c):04X}.png'))

logger.info('Dump font: {}', dump_config.font_name)
Loading

0 comments on commit 388cd0d

Please sign in to comment.