Skip to content

Commit

Permalink
glyph_file_util 添加 get_character_mapping() 和 get_glyph_sequence()
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 9, 2024
1 parent 41c938d commit 7e9866a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/pixel_font_knife/glyph_file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,32 @@ def load_context(root_dir: str | PathLike[str]) -> dict[int, GlyphFlavorGroup]:
raise RuntimeError(f"default flavor already exists: '{glyph_file.file_path}' -> '{flavor_group[''].file_path}'")
flavor_group[''] = glyph_file
return context


def get_character_mapping(
context: dict[int, GlyphFlavorGroup],
flavor: str = '',
fallback_default: bool = True,
) -> dict[int, str]:
character_mapping = {}
for code_point, flavor_group in context.items():
if code_point < 0:
continue
glyph_file = flavor_group.get_file(flavor, fallback_default)
character_mapping[code_point] = glyph_file.glyph_name
return character_mapping


def get_glyph_sequence(
context: dict[int, GlyphFlavorGroup],
flavors: list[str] | None = None,
fallback_default: bool = True,
) -> list[GlyphFile]:
glyph_sequence = []
flavor_group_sequence = sorted(context.items())
for flavor in [''] if flavors is None else flavors:
for code_point, flavor_group in flavor_group_sequence:
glyph_file = flavor_group.get_file(flavor, fallback_default)
if glyph_file not in glyph_sequence:
glyph_sequence.append(glyph_file)
return glyph_sequence
20 changes: 20 additions & 0 deletions tests/test_glyph_file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def test_context(glyphs_dir: Path):
assert 0x6AA4 in context
assert 0x4E10 not in context

group_notdef = context[-1]
assert len(group_notdef) == 1

group_4e11 = context[0x4E11]
assert len(group_4e11) == 2
assert '' in group_4e11
Expand All @@ -86,3 +89,20 @@ def test_context(glyphs_dir: Path):
assert '' in group_6aa4
assert group_6aa4.get_file('zh_hk') is group_6aa4.get_file('zh_tw')
assert group_6aa4.get_file('zh_tr') is group_6aa4.get_file('ko')

assert glyph_file_util.get_character_mapping(context) == {
0x4E11: '4E11',
0x6AA4: '6AA4',
}
assert glyph_file_util.get_character_mapping(context, 'zh_cn') == {
0x4E11: '4E11-ZH_CN',
0x6AA4: '6AA4',
}

assert glyph_file_util.get_glyph_sequence(context, ['', 'zh_cn', 'zh_hk']) == [
GlyphFile.load(glyphs_dir.joinpath('context', 'notdef.png')),
GlyphFile.load(glyphs_dir.joinpath('context', '4E11.png')),
GlyphFile.load(glyphs_dir.joinpath('context', '6AA4.png')),
GlyphFile.load(glyphs_dir.joinpath('context', '4E11 zh_cn.png')),
GlyphFile.load(glyphs_dir.joinpath('context', '6AA4 zh_hk,zh_tw.png')),
]

0 comments on commit 7e9866a

Please sign in to comment.