add use_static_base_font in cn, improve downloader

This commit is contained in:
subframe7536 2024-12-03 21:32:46 +08:00
parent dc3fcdae9e
commit 664db94f28
5 changed files with 40 additions and 20 deletions

View file

@ -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

View file

@ -43,6 +43,7 @@
"fix_meta_table": true,
"clean_cache": false,
"narrow": false,
"use_hinted": false
"use_hinted": false,
"use_static_base_font": false
}
}

View file

@ -43,6 +43,7 @@
"fix_meta_table": true,
"clean_cache": false,
"narrow": false,
"use_hinted": false
"use_hinted": false,
"use_static_base_font": true
}
}

View file

@ -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
)

View file

@ -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"
]
}
},