externalize config

This commit is contained in:
subframe7536 2024-07-09 16:48:01 +08:00
parent b86e37fd2c
commit bfe83b7b4c
4 changed files with 160 additions and 11 deletions

View file

@ -15,6 +15,7 @@ Base on `Jetbrains Mono` and **much "Opinioned"**
![](https://github.com/subframe7536/maple-font/assets/78338239/19383849-6be1-4cfc-9b34-7b33fc047ecf) ![](https://github.com/subframe7536/maple-font/assets/78338239/19383849-6be1-4cfc-9b34-7b33fc047ecf)
- generated by [CodeImg](https://github.com/subframe7536/vscode-codeimg) - generated by [CodeImg](https://github.com/subframe7536/vscode-codeimg)
- Theme: [Maple](https://github.com/subframe7536/vscode-theme-maple)
## Features ## Features
@ -50,7 +51,7 @@ WIP
run it in your local machine. run it in your local machine.
You can change config in `build.py` You can change config in `config.json`
Make sure you have `python3` and `pip` installed Make sure you have `python3` and `pip` installed

View file

@ -19,13 +19,18 @@ release_mode = True
# whether to clean built fonts # whether to clean built fonts
clean_cache = True clean_cache = True
# build process pool size # build process pool size
pool_size = 4 if not getenv("CODESPACE_NAME") else 2
build_config = { build_config = {
# the number of parallel tasks
# when run in codespace, this will be 1
"pool_size": 4 if not getenv("CODESPACE_NAME") else 1,
# font family name
"family_name": "Maple Mono", "family_name": "Maple Mono",
# whether to use hinted ttf as base font # whether to use hinted ttf as base font
"use_hinted": True, "use_hinted": True,
# nerd font settings
"nerd_font": { "nerd_font": {
# whether to enable Nerd Font
"enable": True, "enable": True,
# target version of Nerd Font if font-patcher not exists # target version of Nerd Font if font-patcher not exists
"version": "3.2.1", "version": "3.2.1",
@ -34,7 +39,7 @@ build_config = {
# prefer to use Font Patcher instead of using prebuild NerdFont base font # prefer to use Font Patcher instead of using prebuild NerdFont base font
# if you want to custom build nerd font using font-patcher, you need to set this to True # if you want to custom build nerd font using font-patcher, you need to set this to True
"use_font_patcher": False, "use_font_patcher": False,
# Symbol Fonts settings. # symbol Fonts settings.
# default args: ["--complete"] # default args: ["--complete"]
# if not, will use font-patcher to generate fonts # if not, will use font-patcher to generate fonts
# full args: https://github.com/ryanoasis/nerd-fonts?tab=readme-ov-file#font-patcher # full args: https://github.com/ryanoasis/nerd-fonts?tab=readme-ov-file#font-patcher
@ -45,7 +50,10 @@ build_config = {
# full args: https://github.com/ryanoasis/nerd-fonts?tab=readme-ov-file#font-patcher # full args: https://github.com/ryanoasis/nerd-fonts?tab=readme-ov-file#font-patcher
"extra_args": [], "extra_args": [],
}, },
# chinese font settings
"cn": { "cn": {
# whether to build Chinese fonts
# skip if Chinese base fonts are not founded
"enable": True, "enable": True,
# whether to patch Nerd Font # whether to patch Nerd Font
"with_nerd_font": True, "with_nerd_font": True,
@ -56,6 +64,15 @@ build_config = {
}, },
} }
try:
with open("config.json", "r") as f:
data = json.load(f)
if "$schema" in data:
del data["$schema"]
build_config = {**build_config, **data}
except:
print("config.json is not found. Use default config.")
package_name = "foundryToolsCLI" package_name = "foundryToolsCLI"
package_installed = importlib.util.find_spec(package_name) is not None package_installed = importlib.util.find_spec(package_name) is not None
@ -465,7 +482,7 @@ def main():
run(f"ftcli ttf fix-contours {output_ttf}") run(f"ftcli ttf fix-contours {output_ttf}")
run(f"ftcli ttf remove-overlaps {output_ttf}") run(f"ftcli ttf remove-overlaps {output_ttf}")
with Pool(pool_size) as p: with Pool(build_config["pool_size"]) as p:
p.map(build_mono, listdir(output_ttf)) p.map(build_mono, listdir(output_ttf))
# ========================================================================================= # =========================================================================================
@ -488,13 +505,11 @@ def main():
) )
use_font_patcher = False use_font_patcher = False
with Pool(pool_size) as p: with Pool(build_config["pool_size"]) as p:
_build_fn = partial(build_nf, use_font_patcher=use_font_patcher) _build_fn = partial(build_nf, use_font_patcher=use_font_patcher)
_version = build_config["nerd_font"]["version"] _version = build_config["nerd_font"]["version"]
_name = ( _name = (
f"FontPatcher v{_version}" f"FontPatcher v{_version}" if use_font_patcher else "prebuild Nerd Font"
if use_font_patcher
else "prebuild Nerd Font"
) )
print("========================================") print("========================================")
print(f"patch Nerd Font using {_name}") print(f"patch Nerd Font using {_name}")
@ -521,7 +536,7 @@ def main():
makedirs(output_nf_cn, exist_ok=True) makedirs(output_nf_cn, exist_ok=True)
with Pool(pool_size) as p: with Pool(build_config["pool_size"]) as p:
p.map(build_cn, listdir(cn_base_font_dir)) p.map(build_cn, listdir(cn_base_font_dir))
run(f"ftcli name del-mac-names -r {output_dir}") run(f"ftcli name del-mac-names -r {output_dir}")
@ -531,7 +546,9 @@ def main():
# ========================================================================================= # =========================================================================================
# write config to output path # write config to output path
with open(path.join(output_dir, "build-config.json"), "w", encoding="utf-8") as config_file: with open(
path.join(output_dir, "build-config.json"), "w", encoding="utf-8"
) as config_file:
config_file.write(conf) config_file.write(conf)
if release_mode: if release_mode:
@ -551,7 +568,9 @@ def main():
print(f"archieve: {f}") print(f"archieve: {f}")
# write sha1 # write sha1
with open(path.join(release_dir, "sha1.json"), "w", encoding="utf-8") as hash_file: with open(
path.join(release_dir, "sha1.json"), "w", encoding="utf-8"
) as hash_file:
hash_file.write(json.dumps(hash_map, indent=4)) hash_file.write(json.dumps(hash_map, indent=4))
# copy woff2 to root # copy woff2 to root

21
config.json Normal file
View file

@ -0,0 +1,21 @@
{
"$schema": "./source/schema.json",
"family_name": "Maple Mono",
"use_hinted": true,
"nerd_font": {
"enable": true,
"version": "3.2.1",
"mono": false,
"use_font_patcher": false,
"glyphs": [
"--complete"
],
"extra_args": []
},
"cn": {
"enable": true,
"with_nerd_font": true,
"fix_meta_table": true,
"clean_cache": false
}
}

108
source/schema.json Normal file
View file

@ -0,0 +1,108 @@
{
"title": "Build Configuration Schema",
"description": "Schema for the build configuration of a font project",
"type": "object",
"properties": {
"pool_size": {
"type": "number",
"description": "The number of parallel tasks. When run in codespace, this will be 1",
"default": 4
},
"family_name": {
"type": "string",
"description": "The font family name",
"default": "Maple Mono"
},
"use_hinted": {
"type": "boolean",
"description": "Whether to use hinted TTF as base font",
"default": true
},
"nerd_font": {
"type": "object",
"properties": {
"enable": {
"type": "boolean",
"description": "Whether to enable Nerd Font",
"default": true
},
"version": {
"type": "string",
"description": "Target version of Nerd Font. If font-patcher not exists when need to use it or there is no prebuild font for current version, will download from Github",
"default": "3.2.1"
},
"mono": {
"type": "boolean",
"description": "Whether to make icon width fixed",
"default": false
},
"use_font_patcher": {
"type": "boolean",
"description": "Prefer to use Font Patcher instead of using prebuilt NerdFont base font. If you want to custom build nerd font using font-patcher, you need to set this to True",
"default": false
},
"glyphs": {
"type": "array",
"items": {
"type": "string"
},
"default": ["--complete"],
"description": "Symbol Fonts settings. if is not [\"--complete\"], will use font-patcher to generate fonts. Full args: https://github.com/ryanoasis/nerd-fonts?tab=readme-ov-file#font-patcher"
},
"extra_args": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "Extra arguments for font-patcher, default args: [\"-l\", \"--careful\", \"--outputdir\", output_nf], if \"mono\" is set to true, \"--mono\" will be added, full args: https://github.com/ryanoasis/nerd-fonts?tab=readme-ov-file#font-patcher"
}
},
"required": [
"enable",
"version",
"mono",
"use_font_patcher",
"glyphs",
"extra_args"
]
},
"cn": {
"type": "object",
"properties": {
"enable": {
"type": "boolean",
"description": "Whether to build Chinese fonts. Skip if Chinese base fonts are not founded",
"default": true
},
"with_nerd_font": {
"type": "boolean",
"description": "Whether to patch Nerd Font",
"default": true
},
"fix_meta_table": {
"type": "boolean",
"description": "Fix design language and supported languages in META table",
"default": true
},
"clean_cache": {
"type": "boolean",
"description": "Whether to clean instantiated base CN fonts",
"default": false
}
},
"required": [
"enable",
"with_nerd_font",
"fix_meta_table",
"clean_cache"
]
}
},
"required": [
"family_name",
"use_hinted",
"nerd_font",
"cn"
]
}