From 5144c443ae2fed44d95b2bd04528132da7a409fe Mon Sep 17 00:00:00 2001 From: subframe7536 <1667077010@qq.com> Date: Fri, 13 Sep 2024 15:02:29 +0800 Subject: [PATCH] experimently support narrowed CN glyphs --- build.py | 33 +++++++++++++++++++++++++++++++++ config.json | 3 ++- source/preset-normal.json | 3 ++- source/schema.json | 8 +++++++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/build.py b/build.py index d63c1ad..2eb001b 100644 --- a/build.py +++ b/build.py @@ -28,6 +28,7 @@ if not package_installed: release_mode = None use_normal = None use_cn_both = None +use_cn_narrow = None use_hinted_font = None dir_prefix = None @@ -44,6 +45,9 @@ for arg in sys.argv: elif arg == "--cn-both": use_cn_both = True + elif arg == "--cn-narrow": + use_cn_narrow = True + # whether to use unhint font elif arg.startswith("--hinted="): use_hinted_font = arg.split("=")[1] == "1" @@ -121,6 +125,8 @@ build_config = { "fix_meta_table": True, # whether to clean instantiated base CN fonts "clean_cache": False, + # whether to use narrow CN glyphs + "narrow": False, }, } @@ -135,6 +141,10 @@ try: if use_hinted_font is not None: build_config["use_hinted"] = use_hinted_font + + if use_cn_narrow: + build_config["cn"]["narrow"] = use_cn_narrow + except (): print("Fail to load config.json. Use default config.") @@ -339,9 +349,29 @@ def get_unique_identifier(is_italic: bool, postscript_name: str, suffix="") -> s elif v == "disable": suffix += f"-{k};" + if "CN" in postscript_name and build_config["cn"]["narrow"]: + suffix = "Narrow;" + suffix + return f"Version 7.000;SUBF;{postscript_name};2024;FL830;{suffix}" +def change_char_width(font: TTFont, match_width: int, target_width: int): + font["hhea"].advanceWidthMax = target_width + for name in font.getGlyphOrder(): + glyph = font["glyf"][name] + width, lsb = font["hmtx"][name] + + if width != match_width or glyph.numberOfContours < 1: + continue + + delta = round((target_width - width) / 2) + glyph.coordinates.translate((delta, 0)) + glyph.xMin, glyph.yMin, glyph.xMax, glyph.yMax = ( + glyph.coordinates.calcIntBounds() + ) + font["hmtx"][name] = (target_width, lsb + delta) + + def build_mono(f: str): _path = path.join(output_ttf, f) font = TTFont(_path) @@ -521,6 +551,9 @@ def build_cn(f: str): font, build_config[f"feature_freeze{'_italic' if is_italic else ''}"] ) + if build_config["cn"]["narrow"]: + change_char_width(font, match_width=1200, target_width=1000) + # https://github.com/subframe7536/maple-font/issues/239 # remove_locl(font) diff --git a/config.json b/config.json index 56cf8b6..b64532d 100644 --- a/config.json +++ b/config.json @@ -43,6 +43,7 @@ "enable": true, "with_nerd_font": true, "fix_meta_table": true, - "clean_cache": false + "clean_cache": false, + "narrow": true } } \ No newline at end of file diff --git a/source/preset-normal.json b/source/preset-normal.json index 61189de..bc299a7 100644 --- a/source/preset-normal.json +++ b/source/preset-normal.json @@ -43,6 +43,7 @@ "enable": true, "with_nerd_font": true, "fix_meta_table": true, - "clean_cache": false + "clean_cache": false, + "narrow": false } } \ No newline at end of file diff --git a/source/schema.json b/source/schema.json index 2423514..824ac45 100644 --- a/source/schema.json +++ b/source/schema.json @@ -307,13 +307,19 @@ "type": "boolean", "description": "Whether to clean instantiated base CN fonts", "default": false + }, + "narrow": { + "type": "boolean", + "description": "[Experimental] Whether to use narrow CN characters. THIS WILL BREAK THE 2:1 METRIC", + "default": false } }, "required": [ "enable", "with_nerd_font", "fix_meta_table", - "clean_cache" + "clean_cache", + "narrow" ] } },