support apply custom feature file #304

This commit is contained in:
subframe7536 2024-12-27 12:53:53 +08:00
parent 51a949375f
commit 3a99fc0542
2 changed files with 64 additions and 29 deletions

View file

@ -138,8 +138,11 @@ There are three kind of options for feature freeze:
2. `disable`: Remove the features in `cvXX` / `ssXX` / `zero`, which will no longer effect, even if you enable it manually
3. `ignore`: Do nothing
### Chinese version
#### Load Custom Feature File
Run `build.py` with `--apply-fea-file` flag, the feature file from [`source/features/{regular,italic}.fea`](./source/features) will be applied into variable font. You can modify it to change all features, e.g. remove some ligatures in `calt`.
### Chinese version
Run `python build.py --cn`, the CN base fonts (about 135 MB) will download from GitHub.
@ -152,9 +155,10 @@ The build script will auto download neccessory assets from GitHub. If you have t
### Build Script Usage
```
usage: build.py [-h] [-v] [-d] [--debug] [-n] [--feat FEAT] [--hinted] [--no-hinted]
[--liga] [--no-liga] [--cn-narrow] [--nerd-font | --no-nerd-font]
[--cn | --no-cn] [--cn-both] [--ttf-only] [--cache] [--archive]
usage: build.py [-h] [-v] [-d] [--debug] [-n] [--feat FEAT] [--apply-fea-file] [--hinted]
[--no-hinted] [--liga] [--no-liga] [--cn-narrow]
[--nerd-font | --no-nerd-font] [--cn | --no-cn] [--cn-both] [--ttf-only]
[--cache] [--archive]
✨ Builder and optimizer for Maple Mono
@ -168,6 +172,8 @@ Feature Options:
-n, --normal Use normal preset, just like `JetBrains Mono` with slashed zero
--feat FEAT Freeze font features, splited by `,` (e.g. `--feat
zero,cv01,ss07,ss08`). No effect on variable format
--apply-fea-file Load feature file from `source/features/{regular,italic}.fea` to
variable font
--hinted Use hinted font as base font
--no-hinted Use unhinted font as base font
--liga Preserve all the ligatures

View file

@ -12,6 +12,7 @@ from os import listdir, makedirs, path, remove, walk, getenv
from typing import Callable
from zipfile import ZIP_DEFLATED, ZipFile
from fontTools.ttLib import TTFont, newTable
from fontTools.feaLib.builder import addOpenTypeFeatures
from fontTools.merge import Merger
from source.py.utils import (
check_font_patcher,
@ -79,6 +80,12 @@ def parse_args():
type=lambda x: x.strip().split(","),
help="Freeze font features, splited by `,` (e.g. `--feat zero,cv01,ss07,ss08`). No effect on variable format",
)
feature_group.add_argument(
"--apply-fea-file",
default=None,
action="store_true",
help="Load feature file from `source/features/{regular,italic}.fea` to variable font",
)
feature_group.add_argument(
"--hinted",
dest="hinted",
@ -177,6 +184,7 @@ class FontConfig:
self.use_cn_both = None
self.ttf_only = None
self.debug = None
self.apply_fea_file = None
# the number of parallel tasks
# when run in codespace, this will be 1
self.pool_size = 1 if not getenv("CODESPACE_NAME") else 4
@ -318,6 +326,9 @@ class FontConfig:
if args.ttf_only:
self.ttf_only = True
if args.apply_fea_file:
self.apply_fea_file = True
name_arr = [word.capitalize() for word in self.family_name.split(" ")]
if not self.enable_liga:
name_arr.append("NL")
@ -549,7 +560,11 @@ def rename_glyph_name(
glyph_names = font.getGlyphOrder()
extra_names = font["post"].extraNames
modified = False
extra_map = {"uni2047.liga": "question_question.liga", "dotlessi": "idotless"}
extra_map = {
"uni2047.liga": "question_question.liga",
"dotlessi": "idotless",
"f_f": "f_f.liga",
}
for i, _ in enumerate(glyph_names):
old_name = str(glyph_names[i])
@ -920,13 +935,14 @@ def main():
if not should_use_cache or not build_option.has_cache():
input_files = [
f"{build_option.src_dir}/MapleMono-Italic[wght]-VF.ttf",
f"{build_option.src_dir}/MapleMono[wght]-VF.ttf",
joinPaths(build_option.src_dir, "MapleMono-Italic[wght]-VF.ttf"),
joinPaths(build_option.src_dir, "MapleMono[wght]-VF.ttf"),
]
for input_file in input_files:
font = TTFont(input_file)
# fix auto rename by FontLab
print("Fix names")
rename_glyph_name(
font=font,
map=match_unicode_names(
@ -934,6 +950,19 @@ def main():
),
)
if font_config.apply_fea_file:
fea_path = joinPaths(
build_option.src_dir,
"features/italic.fea"
if "Italic" in input_file
else "features/regular.fea",
)
print(f"Apply feature file {fea_path} into {path.basename(input_file)}")
addOpenTypeFeatures(
font,
fea_path,
)
font.save(
input_file.replace(build_option.src_dir, build_option.output_variable)
)