diff --git a/src/pixel_font_knife/glyph_file_util.py b/src/pixel_font_knife/glyph_file_util.py index 8252a80..e447562 100644 --- a/src/pixel_font_knife/glyph_file_util.py +++ b/src/pixel_font_knife/glyph_file_util.py @@ -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 diff --git a/tests/test_glyph_file_util.py b/tests/test_glyph_file_util.py index 8e7a074..d11bdef 100644 --- a/tests/test_glyph_file_util.py +++ b/tests/test_glyph_file_util.py @@ -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 @@ -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')), + ]