add glyph width checking functionality to ensure monospace compliance #314
This commit is contained in:
parent
5dd2c2ea3d
commit
070b3a52f9
2 changed files with 30 additions and 0 deletions
13
build.py
13
build.py
|
@ -16,6 +16,7 @@ from fontTools.feaLib.builder import addOpenTypeFeatures
|
||||||
from fontTools.merge import Merger
|
from fontTools.merge import Merger
|
||||||
from source.py.utils import (
|
from source.py.utils import (
|
||||||
check_font_patcher,
|
check_font_patcher,
|
||||||
|
check_glyph_width,
|
||||||
download_cn_base_font,
|
download_cn_base_font,
|
||||||
get_font_forge_bin,
|
get_font_forge_bin,
|
||||||
get_font_name,
|
get_font_name,
|
||||||
|
@ -930,6 +931,7 @@ def build_cn(f: str, font_config: FontConfig, build_option: BuildOption):
|
||||||
}
|
}
|
||||||
cn_font["meta"] = meta
|
cn_font["meta"] = meta
|
||||||
|
|
||||||
|
# check_char_width(cn_font, [0, 600, 1200])
|
||||||
target_path = joinPaths(
|
target_path = joinPaths(
|
||||||
build_option.output_cn,
|
build_option.output_cn,
|
||||||
f"{font_config.family_name_compact}-{build_option.cn_suffix_compact}-{style_compact_cn}.ttf",
|
f"{font_config.family_name_compact}-{build_option.cn_suffix_compact}-{style_compact_cn}.ttf",
|
||||||
|
@ -955,6 +957,8 @@ def main():
|
||||||
build_option = BuildOption(font_config)
|
build_option = BuildOption(font_config)
|
||||||
build_option.load_cn_dir_and_suffix(font_config.should_build_nf_cn())
|
build_option.load_cn_dir_and_suffix(font_config.should_build_nf_cn())
|
||||||
|
|
||||||
|
glyph_width = 600
|
||||||
|
|
||||||
if parsed_args.dry:
|
if parsed_args.dry:
|
||||||
print("font_config:", json.dumps(font_config.__dict__, indent=4))
|
print("font_config:", json.dumps(font_config.__dict__, indent=4))
|
||||||
if not is_ci():
|
if not is_ci():
|
||||||
|
@ -1020,6 +1024,8 @@ def main():
|
||||||
3,
|
3,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
check_glyph_width(font, [0, glyph_width])
|
||||||
|
|
||||||
font.save(
|
font.save(
|
||||||
input_file.replace(build_option.src_dir, build_option.output_variable)
|
input_file.replace(build_option.src_dir, build_option.output_variable)
|
||||||
)
|
)
|
||||||
|
@ -1120,6 +1126,13 @@ def main():
|
||||||
|
|
||||||
build_option.is_cn_built = True
|
build_option.is_cn_built = True
|
||||||
|
|
||||||
|
check_glyph_width(
|
||||||
|
TTFont(
|
||||||
|
joinPaths(build_option.output_cn, listdir(build_option.output_cn)[0])
|
||||||
|
),
|
||||||
|
[0, glyph_width, glyph_width * 2]
|
||||||
|
)
|
||||||
|
|
||||||
# write config to output path
|
# write config to output path
|
||||||
with open(
|
with open(
|
||||||
joinPaths(build_option.output_dir, "build-config.json"), "w", encoding="utf-8"
|
joinPaths(build_option.output_dir, "build-config.json"), "w", encoding="utf-8"
|
||||||
|
|
|
@ -187,3 +187,20 @@ def match_unicode_names(file_path: str) -> dict[str, str]:
|
||||||
result[unicode_str] = glyph_name
|
result[unicode_str] = glyph_name
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def check_glyph_width(font: TTFont, matched_width: tuple[int]):
|
||||||
|
print("Checking glyph width...")
|
||||||
|
result: tuple[str, int] = []
|
||||||
|
for name in font.getGlyphOrder():
|
||||||
|
width, _ = font["hmtx"][name]
|
||||||
|
if width not in matched_width:
|
||||||
|
result.append([name, width])
|
||||||
|
|
||||||
|
if result.__len__() > 0:
|
||||||
|
print("Exist unmatched width:")
|
||||||
|
for item in result:
|
||||||
|
print(f"{item[0]}\t{item[1]}")
|
||||||
|
raise Exception(
|
||||||
|
f"The font may contain glyphs that are not {matched_width} wide, which may broke monospace rule."
|
||||||
|
)
|
Loading…
Reference in a new issue