mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-05 21:00:15 +08:00
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
"""Copy unmodified MiSans webfonts from Xiaomi's official archive.
|
|
|
|
Usage: python3 scripts/sync-fonts.py --archive /path/to/MiSans.zip
|
|
Normal homepage builds use the committed assets and do not need Python.
|
|
"""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DESTINATION = ROOT / "src/assets/fonts/misans"
|
|
SOURCE = "https://hyperos.mi.com/font-download/MiSans.zip"
|
|
ARCHIVE_SHA256 = "b6aa1fc827035922612df8edf36e5609bca1c5441e25cd57572204569b7b81d9"
|
|
# Map named styles to the site's standard CSS weights. MiSans 4.009 uses
|
|
# different numeric weights internally; the official files remain unchanged.
|
|
WEIGHTS = {350: "Normal", 400: "Regular", 600: "Semibold"}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--archive", required=True, type=Path)
|
|
args = parser.parse_args()
|
|
with args.archive.open("rb") as source:
|
|
digest = hashlib.file_digest(source, "sha256").hexdigest()
|
|
if digest != ARCHIVE_SHA256:
|
|
raise ValueError("Expected the pinned official MiSans.zip archive")
|
|
DESTINATION.mkdir(parents=True, exist_ok=True)
|
|
|
|
rules = []
|
|
files = {}
|
|
with zipfile.ZipFile(args.archive) as archive:
|
|
for weight, style in WEIGHTS.items():
|
|
filename = f"MiSans-{style}.woff2"
|
|
data = archive.read(f"MiSans/woff2/{filename}")
|
|
(DESTINATION / filename).write_bytes(data)
|
|
files[filename] = {
|
|
"weight": weight,
|
|
"bytes": len(data),
|
|
"sha256": hashlib.sha256(data).hexdigest(),
|
|
}
|
|
rules.append(
|
|
"@font-face {\n"
|
|
" font-family: 'MiSans';\n"
|
|
" font-style: normal;\n"
|
|
f" font-weight: {weight};\n"
|
|
" font-display: swap;\n"
|
|
f" src: url('./{filename}') format('woff2');\n"
|
|
"}\n"
|
|
)
|
|
|
|
header = (
|
|
"/* Generated by scripts/sync-fonts.py. Do not edit manually.\n"
|
|
" * MiSans 4.009. Original, unmodified WOFF2 files from Xiaomi.\n"
|
|
" * License and attribution: /licenses/misans.txt\n"
|
|
" */\n\n"
|
|
)
|
|
(DESTINATION / "font.css").write_text(header + "\n".join(rules))
|
|
metadata = {
|
|
"family": "MiSans",
|
|
"version": "4.009",
|
|
"source": SOURCE,
|
|
"sha256": ARCHIVE_SHA256,
|
|
"license": "https://hyperos.mi.com/font/zh/download/",
|
|
"weights": list(WEIGHTS),
|
|
"files": files,
|
|
}
|
|
(DESTINATION / "metadata.json").write_text(json.dumps(metadata, indent=2) + "\n")
|
|
for asset in DESTINATION.glob("MiSans-*.woff2"):
|
|
if asset.name not in files:
|
|
asset.unlink()
|
|
print(json.dumps(metadata, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|