Skip to content

Commit

Permalink
代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 7, 2024
1 parent 868d7f1 commit beee89f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
18 changes: 11 additions & 7 deletions tools/services/font_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pixel_font_knife.glyph_file_util import GlyphFile, GlyphFlavorGroup

from tools import configs
from tools.configs import path_define, WidthMode, LanguageFlavor, FontFormat, FontCollectionFormat
from tools.configs import path_define, FontSize, WidthMode, LanguageFlavor, FontFormat, FontCollectionFormat
from tools.configs.font import FontConfig


Expand Down Expand Up @@ -74,6 +74,10 @@ def __init__(
self._builder_cache = {}
self._collection_builder_cache = {}

@property
def font_size(self) -> FontSize:
return self.font_size

def get_alphabet(self, width_mode: WidthMode) -> set[str]:
if width_mode in self._alphabet_cache:
alphabet = self._alphabet_cache[width_mode]
Expand Down Expand Up @@ -124,7 +128,7 @@ def _create_builder(self, width_mode: WidthMode, language_flavor: LanguageFlavor
layout_param = self.font_config.layout_params[width_mode]

builder = FontBuilder()
builder.font_metric.font_size = self.font_config.font_size
builder.font_metric.font_size = self.font_size
builder.font_metric.horizontal_layout.ascent = layout_param.ascent
builder.font_metric.horizontal_layout.descent = layout_param.descent
builder.font_metric.vertical_layout.ascent = math.ceil(layout_param.line_height / 2)
Expand All @@ -135,7 +139,7 @@ def _create_builder(self, width_mode: WidthMode, language_flavor: LanguageFlavor
builder.meta_info.version = configs.font_version
builder.meta_info.created_time = datetime.datetime.fromisoformat(f'{configs.font_version.replace('.', '-')}T00:00:00Z')
builder.meta_info.modified_time = builder.meta_info.created_time
builder.meta_info.family_name = f'Fusion Pixel {self.font_config.font_size}px {width_mode.capitalize()} {language_flavor}'
builder.meta_info.family_name = f'Fusion Pixel {self.font_size}px {width_mode.capitalize()} {language_flavor}'
builder.meta_info.weight_name = WeightName.REGULAR
builder.meta_info.serif_style = SerifStyle.SANS_SERIF
builder.meta_info.slant_style = SlantStyle.NORMAL
Expand All @@ -158,11 +162,11 @@ def _create_builder(self, width_mode: WidthMode, language_flavor: LanguageFlavor
glyph = glyph_pool[glyph_file.file_path]
else:
horizontal_origin_y = math.floor((layout_param.ascent + layout_param.descent - glyph_file.height) / 2)
vertical_origin_y = (self.font_config.font_size - glyph_file.height) // 2 - 1
vertical_origin_y = (self.font_size - glyph_file.height) // 2 - 1
glyph = Glyph(
name=_get_glyph_name(glyph_file),
advance_width=glyph_file.width,
advance_height=self.font_config.font_size,
advance_height=self.font_size,
horizontal_origin=(0, horizontal_origin_y),
vertical_origin_y=vertical_origin_y,
bitmap=glyph_file.bitmap.data,
Expand Down Expand Up @@ -197,14 +201,14 @@ def make_fonts(self, width_mode: WidthMode, font_format: FontFormat | FontCollec
if font_format in configs.font_formats:
for language_flavor in configs.language_flavors:
builder = self._get_builder(width_mode, language_flavor)
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{self.font_config.font_size}px-{width_mode}-{language_flavor}.{font_format}')
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{self.font_size}px-{width_mode}-{language_flavor}.{font_format}')
if font_format == 'woff2':
builder.save_otf(file_path, flavor=Flavor.WOFF2)
else:
getattr(builder, f'save_{font_format}')(file_path)
logger.info("Make font: '{}'", file_path)
else:
collection_builder = self._get_collection_builder(width_mode)
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{self.font_config.font_size}px-{width_mode}.{font_format}')
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{self.font_size}px-{width_mode}.{font_format}')
getattr(collection_builder, f'save_{font_format}')(file_path)
logger.info("Make font collection: '{}'", file_path)
6 changes: 3 additions & 3 deletions tools/services/info_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def make_font_info(design_context: DesignContext, width_mode: WidthMode):
alphabet = design_context.get_alphabet(width_mode)

output = StringIO()
output.write(f'# Fusion Pixel {design_context.font_config.font_size}px {'等宽模式' if width_mode == 'monospaced' else '比例模式'}\n')
output.write(f'# Fusion Pixel {design_context.font_size}px {'等宽模式' if width_mode == 'monospaced' else '比例模式'}\n')
output.write('\n')
output.write('## 基本信息\n')
output.write('\n')
Expand Down Expand Up @@ -143,7 +143,7 @@ def make_font_info(design_context: DesignContext, width_mode: WidthMode):
_write_locale_chr_count_infos_table(output, _get_ksx1001_chr_count_infos(alphabet))

path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(f'font-info-{design_context.font_config.font_size}px-{width_mode}.md')
file_path = path_define.outputs_dir.joinpath(f'font-info-{design_context.font_size}px-{width_mode}.md')
file_path.write_text(output.getvalue(), 'utf-8')
logger.info("Make font info: '{}'", file_path)

Expand All @@ -152,6 +152,6 @@ def make_alphabet_txt(design_context: DesignContext, width_mode: WidthMode):
alphabet = sorted(design_context.get_alphabet(width_mode))

path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(f'alphabet-{design_context.font_config.font_size}px-{width_mode}.txt')
file_path = path_define.outputs_dir.joinpath(f'alphabet-{design_context.font_size}px-{width_mode}.txt')
file_path.write_text(''.join(alphabet), 'utf-8')
logger.info("Make alphabet txt: '{}'", file_path)
4 changes: 2 additions & 2 deletions tools/services/template_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _make_html(template_name: str, file_name: str, params: dict[str, object] | N


def make_alphabet_html(design_context: DesignContext, width_mode: WidthMode):
_make_html('alphabet.html', f'alphabet-{design_context.font_config.font_size}px-{width_mode}.html', {
_make_html('alphabet.html', f'alphabet-{design_context.font_size}px-{width_mode}.html', {
'font_config': design_context.font_config,
'width_mode': width_mode,
'alphabet': ''.join(sorted(c for c in design_context.get_alphabet(width_mode) if ord(c) >= 128)),
Expand Down Expand Up @@ -113,7 +113,7 @@ def make_demo_html(design_context: DesignContext):
_handle_demo_html_element(design_context, soup, soup)
content_html = str(soup)

_make_html('demo.html', f'demo-{design_context.font_config.font_size}px.html', {
_make_html('demo.html', f'demo-{design_context.font_size}px.html', {
'font_config': design_context.font_config,
'content_html': content_html,
})
Expand Down

0 comments on commit beee89f

Please sign in to comment.