Use a Unicode default with project-level LaTeX compatibility

This commit is contained in:
gloamxun committed 2026-12-02 21:43:38 +08:00
1 parent 38a2a4a958
commit 81ac3ae321
11 files changed
+741 -126

No files matched your search

+101 -22
View File
@@ -5,7 +5,7 @@ import argparse
import hashlib
import json
import lzma
from pathlib import Path
from pathlib import Path, PurePosixPath
import shutil
import struct
import tarfile
@@ -18,6 +18,16 @@ TOOLS = {
"bibtex.windows": ("bibtex.exe",),
"kpathsea.windows": ("kpsewhich.exe", "kpathsealibw64.dll", "msvcr100.dll"),
}
UNICODE_PACKAGES = ("ctex", "xecjk", "zhnumber", "fandol", "xetex")
UNICODE_NOTICES = {
"ctex": {"doc/latex/ctex/README.md": "ctex-README.md"},
"xecjk": {"doc/xelatex/xecjk/README.md": "xeCJK-README.md"},
"zhnumber": {"doc/latex/zhnumber/README.md": "zhnumber-README.md"},
"fandol": {
"doc/latex/fandol/COPYING": "Fandol-COPYING",
"doc/latex/fandol/README": "Fandol-README",
},
}
def digest(path, algorithm="sha256"):
@@ -35,27 +45,51 @@ def require_amd64_pe(path):
raise ValueError(f"Expected an x64 Windows executable: {path}")
def stage_bibtex(output, downloads):
output.mkdir(parents=True)
def read_texlive_database(downloads):
with lzma.open(downloads / "texlive.tlpdb.xz", "rt") as stream:
db = stream.read()
records = stream.read().split("\n\n")
packages = {}
for record in records:
if not record.startswith("name "):
continue
fields = dict(
line.split(" ", 1)
for line in record.splitlines()
if line and not line[0].isspace() and " " in line
)
packages[fields["name"]] = fields
return packages
def checked_texlive_archive(downloads, database, name):
package = name.removesuffix(".doc")
record = database[package]
field = "doccontainerchecksum" if name.endswith(".doc") else "containerchecksum"
checksum = record[field]
archive = downloads / f"{name}.tar.xz"
if digest(archive, "sha512") != checksum:
raise ValueError(f"TeX Live archive checksum differs for {name}")
return archive, {"revision": record["revision"], "sha512": checksum}
def copy_archive_file(archive, member, target):
if not member.isfile():
raise ValueError(f"Expected a regular TeX Live file: {member.name}")
target.parent.mkdir(parents=True, exist_ok=True)
with archive.extractfile(member) as source, target.open("xb") as output:
shutil.copyfileobj(source, output)
def stage_bibtex(output, downloads, database):
output.mkdir(parents=True)
receipts = {}
for package, names in TOOLS.items():
record = next(block for block in db.split("\n\n") if block.startswith(f"name {package}\n"))
fields = dict(line.split(" ", 1) for line in record.splitlines() if " " in line)
archive = downloads / f"{package}.tar.xz"
checksum = fields["containerchecksum"]
if digest(archive, "sha512") != checksum:
raise ValueError(f"TeX Live archive checksum differs for {package}")
archive, receipts[package] = checked_texlive_archive(downloads, database, package)
with tarfile.open(archive, "r:xz") as bundle:
for name in names:
member = bundle.getmember(f"bin/windows/{name}")
if not member.isfile():
raise ValueError(f"Expected a regular TeX Live file: {name}")
with bundle.extractfile(member) as source, (output / name).open("wb") as target:
shutil.copyfileobj(source, target)
require_amd64_pe(output / name)
receipts[package] = {"revision": fields["revision"], "sha512": checksum}
target = output / name
copy_archive_file(bundle, bundle.getmember(f"bin/windows/{name}"), target)
require_amd64_pe(target)
# Match TeX Live's width so a wrapped bibliography comment stays a comment.
(output / "texmf.cnf").write_text(
"BIBINPUTS = .\nBSTINPUTS = .\nopenin_any = p\nopenout_any = p\nshell_escape = f\n"
@@ -65,6 +99,40 @@ def stage_bibtex(output, downloads):
return receipts
def stage_unicode_resources(texmf, licenses, downloads, database):
receipts = {}
for package in UNICODE_PACKAGES:
archive, receipts[package] = checked_texlive_archive(downloads, database, package)
with tarfile.open(archive, "r:xz") as bundle:
for member in bundle:
if not member.isfile():
continue
# TeX Live archives use either TDS paths or a texmf-dist prefix.
relative = PurePosixPath(member.name.removeprefix("texmf-dist/"))
if not relative.parts or relative.parts[0] not in ("tex", "fonts"):
continue
if ".." in relative.parts or any("\\" in part or ":" in part for part in relative.parts):
raise ValueError(f"Invalid TeX Live resource path: {member.name}")
copy_archive_file(bundle, member, texmf.joinpath(*relative.parts))
# CTeX loads this documented configuration before processing user options.
ctex_options = texmf / "tex/latex/ctex/config/ctexopts.cfg"
with ctex_options.open("a", encoding="utf-8", newline="\n") as options:
options.write(
"\n% Texrra bundles Fandol independently of the system's installed fonts.\n"
r"\sys_if_engine_xetex:T { \ctex_set:nn { option } { fontset = fandol } }"
"\n"
)
for package, notices in UNICODE_NOTICES.items():
name = f"{package}.doc"
archive, receipts[name] = checked_texlive_archive(downloads, database, name)
with tarfile.open(archive, "r:xz") as bundle:
for member, filename in notices.items():
copy_archive_file(bundle, bundle.getmember(member), licenses / filename)
return receipts
def copy_texmf(source, target, skip_generated):
target.mkdir(parents=True)
for path in sorted(source.rglob("*")):
@@ -90,8 +158,9 @@ def stage(args, output):
font_maps = Path(reference["environment"]["TEXFONTMAPS"].rstrip(":"))
(texmf / "fonts").mkdir()
shutil.copy2(font_maps / "pdftex.map", texmf / "fonts")
if not (texmf / "dist/tex/latex/tex-ini-files/pdflatex.ini").is_file():
raise ValueError("Missing pdfLaTeX initialization source")
for entry in ("pdflatex.ini", "xelatex.ini"):
if not (texmf / "dist/tex/latex/tex-ini-files" / entry).is_file():
raise ValueError(f"Missing LaTeX initialization source: {entry}")
web = output / "web"
web.mkdir()
@@ -100,7 +169,8 @@ def stage(args, output):
if digest(source) != desktop["sha256"][f"web/{name}"]:
raise ValueError(f"Reference Web asset has changed: {name}")
shutil.copyfile(source, web / name)
tools = stage_bibtex(output / "tools/bibtex", args.downloads)
database = read_texlive_database(args.downloads)
tools = stage_bibtex(output / "tools/bibtex", args.downloads, database)
(output / "bin").mkdir()
require_amd64_pe(args.editor)
shutil.copyfile(args.editor, output / "bin/native_editor.exe")
@@ -108,11 +178,16 @@ def stage(args, output):
"TeX Live resources and bundled BibTeX and Kpathsea binaries\n"
"TeX Live licensing and sources: https://tug.org/texlive/copying.html\n"
"TeX Live source archives: https://tug.org/texlive/svn/\n"
"CTeX, xeCJK, zhnumber and Fandol notices: licenses/texlive/\n"
f"TeX Live binaries and package metadata: {MIRROR}\n",
encoding="ascii",
)
license_source = Path(__file__).resolve().with_name("licenses")
shutil.copytree(license_source, output / "licenses")
licenses = output / "licenses"
shutil.copytree(license_source, licenses)
unicode_resources = stage_unicode_resources(
texmf / "overlay", licenses / "texlive", args.downloads, database
)
files = {
path.relative_to(output).as_posix(): digest(path)
@@ -125,6 +200,7 @@ def stage(args, output):
"web_commit": desktop["web_commit"],
"resources_sha256": hashlib.sha256(fingerprints).hexdigest(),
"texlive_tools": tools,
"texlive_unicode": unicode_resources,
"sha256": files,
}, indent=2, sort_keys=True) + "\n")
@@ -134,7 +210,10 @@ def main():
parser.add_argument("output", type=Path)
parser.add_argument("--editor", type=Path, required=True)
parser.add_argument("--reference", type=Path, required=True)
parser.add_argument("--downloads", type=Path, required=True)
parser.add_argument(
"--downloads", type=Path, required=True,
help="TeX Live package archives and texlive.tlpdb.xz from the same mirror snapshot",
)
parser.add_argument("--commit", required=True)
args = parser.parse_args()
args.output.parent.mkdir(parents=True, exist_ok=True)