From 664db94f28f99cd1ecaf88ab4ba35b4b9b763e6a Mon Sep 17 00:00:00 2001 From: subframe7536 <1667077010@qq.com> Date: Tue, 3 Dec 2024 21:32:46 +0800 Subject: [PATCH] add `use_static_base_font` in `cn`, improve downloader --- build.py | 28 +++++++++++++++++++--------- config.json | 3 ++- source/preset-normal.json | 3 ++- source/py/utils.py | 17 +++++++++-------- source/schema.json | 9 ++++++++- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/build.py b/build.py index a782045..cc30874 100644 --- a/build.py +++ b/build.py @@ -13,7 +13,7 @@ from fontTools.ttLib import TTFont, newTable from fontTools.merge import Merger from source.py.utils import ( check_font_patcher, - download_cn_static_fonts, + download_cn_base_font, get_font_forge_bin, is_ci, run, @@ -237,6 +237,8 @@ class FontConfig: "narrow": False, # whether to hint CN font (will increase about 33% size) "use_hinted": False, + # whether to use pre-instantiated static CN font as base font + "use_static_base_font": True } self.__load_external(args) @@ -378,17 +380,25 @@ class BuildOption: def should_build_cn(self, config: FontConfig) -> bool: if not config.cn["enable"] and not config.use_cn_both: + print("No `\"cn.enable\": true` config or no `--cn` / `--cn-both` setup. Skip CN build.") return False - if path.exists(self.cn_static_dir) and listdir(self.cn_static_dir).__len__() == 16: - return True - if not path.exists(self.cn_variable_dir) and listdir(self.cn_variable_dir).__len__() < 1: - if is_ci(): - return download_cn_static_fonts( - tag="cn_static", + if not path.exists(self.cn_static_dir) or listdir(self.cn_static_dir).__len__() != 16: + tag = "cn-base" + if is_ci() or config.cn["use_static_base_font"]: + return download_cn_base_font( + tag=tag, + zip_path="cn-base-static.zip", target_dir=self.cn_static_dir, - github_mirror=self.nerd_font["github_mirror"], + github_mirror=config.nerd_font["github_mirror"], ) - print("CN varaible fonts does not exist. Skip CN build.") + if not config.cn["use_static_base_font"]: + return download_cn_base_font( + tag=tag, + zip_path="cn-base-variable.zip", + target_dir=self.cn_variable_dir, + github_mirror=config.nerd_font["github_mirror"], + ) + print("CN base fonts don't exist. Skip CN build.") return False return True diff --git a/config.json b/config.json index acae05f..49bea0c 100644 --- a/config.json +++ b/config.json @@ -43,6 +43,7 @@ "fix_meta_table": true, "clean_cache": false, "narrow": false, - "use_hinted": false + "use_hinted": false, + "use_static_base_font": false } } \ No newline at end of file diff --git a/source/preset-normal.json b/source/preset-normal.json index 390344b..e843bf6 100644 --- a/source/preset-normal.json +++ b/source/preset-normal.json @@ -43,6 +43,7 @@ "fix_meta_table": true, "clean_cache": false, "narrow": false, - "use_hinted": false + "use_hinted": false, + "use_static_base_font": true } } \ No newline at end of file diff --git a/source/py/utils.py b/source/py/utils.py index 3f727c8..77d7904 100644 --- a/source/py/utils.py +++ b/source/py/utils.py @@ -2,7 +2,7 @@ from os import environ, path, remove import platform import shutil import subprocess -from urllib.request import urlopen +from urllib.request import Request, urlopen from zipfile import ZipFile from fontTools.ttLib import TTFont @@ -80,8 +80,10 @@ def download_zip_and_extract( try: if not path.exists(zip_path): try: - print(f"NerdFont Patcher does not exist, download from {url}") - with urlopen(url) as response, open(zip_path, "wb") as out_file: + print(f"{name} does not exist, download from {url}") + user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" + req = Request(url, headers={'User-Agent': user_agent}) + with urlopen(req) as response, open(zip_path, "wb") as out_file: shutil.copyfileobj(response, out_file) except Exception as e: print( @@ -115,11 +117,10 @@ def check_font_patcher(version: str, github_mirror: str = "github.com") -> bool: ) -def download_cn_static_fonts( - tag: str, target_dir: str, github_mirror: str = "github.com" +def download_cn_base_font( + tag: str, zip_path: str, target_dir: str, github_mirror: str = "github.com" ) -> bool: - url = f"{parse_github_mirror(github_mirror)}/subframe7536/maple-font/releases/download/{tag}/cn-base.zip" - zip_path = "cn-base-static.zip" + url = f"{parse_github_mirror(github_mirror)}/subframe7536/maple-font/releases/download/{tag}/{zip_path}" return download_zip_and_extract( - name="Nerd Font Patcher", url=url, zip_path=zip_path, output_dir=target_dir + name="CN base font", url=url, zip_path=zip_path, output_dir=target_dir ) diff --git a/source/schema.json b/source/schema.json index fd7bbfc..d51c29f 100644 --- a/source/schema.json +++ b/source/schema.json @@ -223,6 +223,11 @@ "type": "boolean", "description": "whether to hint CN font (will increase about 33% size)", "default": false + }, + "use_static_base_font": { + "type": "boolean", + "description": "whether to use large pre-instantiated static CN font as base font (instantiate will cost 40-50 minutes)", + "default": true } }, "required": [ @@ -230,7 +235,9 @@ "with_nerd_font", "fix_meta_table", "clean_cache", - "narrow" + "narrow", + "use_hinted", + "use_static_base_font" ] } },