experimently support narrowed CN glyphs

This commit is contained in:
subframe7536 2024-09-13 15:02:29 +08:00
parent 4f901a49dd
commit 5144c443ae
4 changed files with 44 additions and 3 deletions

View file

@ -28,6 +28,7 @@ if not package_installed:
release_mode = None release_mode = None
use_normal = None use_normal = None
use_cn_both = None use_cn_both = None
use_cn_narrow = None
use_hinted_font = None use_hinted_font = None
dir_prefix = None dir_prefix = None
@ -44,6 +45,9 @@ for arg in sys.argv:
elif arg == "--cn-both": elif arg == "--cn-both":
use_cn_both = True use_cn_both = True
elif arg == "--cn-narrow":
use_cn_narrow = True
# whether to use unhint font # whether to use unhint font
elif arg.startswith("--hinted="): elif arg.startswith("--hinted="):
use_hinted_font = arg.split("=")[1] == "1" use_hinted_font = arg.split("=")[1] == "1"
@ -121,6 +125,8 @@ build_config = {
"fix_meta_table": True, "fix_meta_table": True,
# whether to clean instantiated base CN fonts # whether to clean instantiated base CN fonts
"clean_cache": False, "clean_cache": False,
# whether to use narrow CN glyphs
"narrow": False,
}, },
} }
@ -135,6 +141,10 @@ try:
if use_hinted_font is not None: if use_hinted_font is not None:
build_config["use_hinted"] = use_hinted_font build_config["use_hinted"] = use_hinted_font
if use_cn_narrow:
build_config["cn"]["narrow"] = use_cn_narrow
except (): except ():
print("Fail to load config.json. Use default config.") 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": elif v == "disable":
suffix += f"-{k};" 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}" 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): def build_mono(f: str):
_path = path.join(output_ttf, f) _path = path.join(output_ttf, f)
font = TTFont(_path) font = TTFont(_path)
@ -521,6 +551,9 @@ def build_cn(f: str):
font, build_config[f"feature_freeze{'_italic' if is_italic else ''}"] 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 # https://github.com/subframe7536/maple-font/issues/239
# remove_locl(font) # remove_locl(font)

View file

@ -43,6 +43,7 @@
"enable": true, "enable": true,
"with_nerd_font": true, "with_nerd_font": true,
"fix_meta_table": true, "fix_meta_table": true,
"clean_cache": false "clean_cache": false,
"narrow": true
} }
} }

View file

@ -43,6 +43,7 @@
"enable": true, "enable": true,
"with_nerd_font": true, "with_nerd_font": true,
"fix_meta_table": true, "fix_meta_table": true,
"clean_cache": false "clean_cache": false,
"narrow": false
} }
} }

View file

@ -307,13 +307,19 @@
"type": "boolean", "type": "boolean",
"description": "Whether to clean instantiated base CN fonts", "description": "Whether to clean instantiated base CN fonts",
"default": false "default": false
},
"narrow": {
"type": "boolean",
"description": "[Experimental] Whether to use narrow CN characters. THIS WILL BREAK THE 2:1 METRIC",
"default": false
} }
}, },
"required": [ "required": [
"enable", "enable",
"with_nerd_font", "with_nerd_font",
"fix_meta_table", "fix_meta_table",
"clean_cache" "clean_cache",
"narrow"
] ]
} }
}, },