56 lines
2.6 KiB
Python
56 lines
2.6 KiB
Python
"""Use system TeX Live, or an explicitly configured local unpacked installation."""
|
|
from pathlib import Path
|
|
import os
|
|
import shutil
|
|
import shlex
|
|
import subprocess
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
|
|
|
|
def prepare_tex(env):
|
|
env = dict(env)
|
|
if shutil.which('latex', path=env['PATH']) and shutil.which('dvisvgm', path=env['PATH']):
|
|
return env
|
|
root = Path(env.get('SLIME_TEX_ROOT', HERE / '.native/texlive')).resolve()
|
|
if not (root/'usr/bin/pdftex').is_file():
|
|
raise RuntimeError('需要 LaTeX / dvisvgm。Ubuntu: sudo apt install texlive-latex-extra dvisvgm')
|
|
dist = root/'usr/share/texlive/texmf-dist'
|
|
formats = root/'formats'
|
|
formats.mkdir(exist_ok=True)
|
|
env.update({
|
|
'PATH': str(root/'usr/bin')+os.pathsep+env['PATH'],
|
|
'LD_LIBRARY_PATH': str(root/'usr/lib/x86_64-linux-gnu')+os.pathsep+env.get('LD_LIBRARY_PATH', ''),
|
|
'TEXMFCNF': str(dist/'web2c'),
|
|
'TEXMFROOT': str(root/'usr/share/texlive'),
|
|
'TEXMFDIST': str(dist),
|
|
# Database-only lookup avoids recursive misses on the WSL filesystem.
|
|
'TEXMF': '{!!'+str(dist)+',!!'+str(root/'usr/share/texmf')+'}',
|
|
'TEXMFDBS': '{'+str(dist)+','+str(root/'usr/share/texmf')+'}',
|
|
'TEXMFVAR': str(root/'var'),
|
|
'TEXMFCONFIG': str(root/'config'),
|
|
'TEXMFHOME': str(root/'texmf-home'),
|
|
'TEXFORMATS': str(formats)+'//:',
|
|
})
|
|
gs_roots = sorted((root/'usr/share/ghostscript').glob('*'))
|
|
if gs_roots:
|
|
gs = gs_roots[-1]
|
|
env['GS_LIB'] = os.pathsep.join(str(p) for p in (
|
|
gs/'Resource/Init', gs/'lib', gs/'Resource/Font',
|
|
root/'usr/share/fonts/type1/urw-base35'))
|
|
if not (formats/'latex.fmt').is_file():
|
|
subprocess.run(['mktexlsr', str(dist), str(root/'usr/share/texmf')], env=env, check=True)
|
|
subprocess.run(['pdftex', '-ini', '-etex', '-interaction=nonstopmode', '-halt-on-error',
|
|
'-jobname=latex', '-progname=latex', 'latex.ini'],
|
|
cwd=formats, env=env, check=True)
|
|
# These scenes use TeX glyphs and dvisvgm raw SVG groups, without PS artwork.
|
|
# The unpacked Ghostscript cannot initialize its device in this environment;
|
|
# skip only PS specials while preserving raw SVG and vector glyph outlines.
|
|
wrappers = root / 'tools'
|
|
wrappers.mkdir(exist_ok=True)
|
|
converter = wrappers / 'dvisvgm'
|
|
converter.write_text('#!/bin/sh\nexec '+shlex.quote(str(root/'usr/bin/dvisvgm'))+
|
|
' --no-specials=ps "$@"\n')
|
|
converter.chmod(0o755)
|
|
env['PATH'] = str(wrappers)+os.pathsep+env['PATH']
|
|
return env
|