Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read TeX DVI glyph/rule positions in sp, using the reference's actual TFM.
|
||||
|
||||
External test oracle only. No native engine code or geometry is imported.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import struct
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def read_dvi(path):
|
||||
data = Path(path).read_bytes()
|
||||
at = 0
|
||||
fonts = {}
|
||||
pages = []
|
||||
stack = []
|
||||
pos = [0] * 6 # h,v,w,x,y,z; current font is not pushed.
|
||||
current = None
|
||||
|
||||
def number(n, signed=False):
|
||||
nonlocal at
|
||||
value = int.from_bytes(data[at:at+n], 'big', signed=signed)
|
||||
at += n
|
||||
return value
|
||||
|
||||
def font_definition(n):
|
||||
key = number(n)
|
||||
number(4) # checksum
|
||||
scale, design = number(4), number(4)
|
||||
area, length = number(1), number(1)
|
||||
nonlocal at
|
||||
name = data[at:at+area+length].decode('ascii')
|
||||
at += area+length
|
||||
tfm_path = subprocess.check_output(['kpsewhich', name+'.tfm'], text=True).strip()
|
||||
tfm = Path(tfm_path).read_bytes()
|
||||
_, lh, bc, ec, nw = struct.unpack('>5H', tfm[:10])
|
||||
char_base = 24 + lh*4
|
||||
width_base = char_base + (ec-bc+1)*4
|
||||
widths = struct.unpack(f'>{nw}i', tfm[width_base:width_base+nw*4])
|
||||
# TeX's TFM scaling for the supported real 10/7/5pt masters; all
|
||||
# bundled advance fix_words are nonnegative and scale < 2^23.
|
||||
advances = {c: (widths[tfm[char_base+(c-bc)*4]]*scale)//(1 << 20)
|
||||
for c in range(bc, ec+1)}
|
||||
fonts[key] = (name, scale, design, advances)
|
||||
|
||||
while at < len(data):
|
||||
op = number(1)
|
||||
if op <= 131 or 133 <= op <= 136:
|
||||
slot = op if op < 128 else number(op-127 if op <= 131 else op-132)
|
||||
name, scale, _, widths = fonts[current]
|
||||
pages[-1]['paint_order'].append(['glyph', len(pages[-1]['glyphs'])])
|
||||
pages[-1]['glyphs'].append(dict(font=name, size=scale, slot=slot,
|
||||
x=pos[0], y=pos[1], advance=widths[slot]))
|
||||
if op <= 131:
|
||||
pos[0] += widths[slot]
|
||||
elif op in (132, 137):
|
||||
height, width = number(4, True), number(4, True)
|
||||
if height > 0 and width > 0:
|
||||
pages[-1]['paint_order'].append(['rule', len(pages[-1]['rules'])])
|
||||
pages[-1]['rules'].append(dict(x=pos[0], y=pos[1]-height,
|
||||
width=width, height=height))
|
||||
if op == 132:
|
||||
pos[0] += width
|
||||
elif op == 138:
|
||||
pass
|
||||
elif op == 139:
|
||||
at += 44
|
||||
pos = [0]*6
|
||||
stack = []
|
||||
pages.append(dict(glyphs=[], rules=[], paint_order=[]))
|
||||
elif op == 140:
|
||||
assert not stack
|
||||
elif op == 141:
|
||||
stack.append(pos.copy())
|
||||
elif op == 142:
|
||||
pos = stack.pop()
|
||||
elif 143 <= op <= 146:
|
||||
pos[0] += number(op-142, True)
|
||||
elif 147 <= op <= 156:
|
||||
base, register = (147, 2) if op <= 151 else (152, 3)
|
||||
if op != base:
|
||||
pos[register] = number(op-base, True)
|
||||
pos[0] += pos[register]
|
||||
elif 157 <= op <= 160:
|
||||
pos[1] += number(op-156, True)
|
||||
elif 161 <= op <= 170:
|
||||
base, register = (161, 4) if op <= 165 else (166, 5)
|
||||
if op != base:
|
||||
pos[register] = number(op-base, True)
|
||||
pos[1] += pos[register]
|
||||
elif 171 <= op <= 234:
|
||||
current = op-171
|
||||
elif 235 <= op <= 238:
|
||||
current = number(op-234)
|
||||
elif 239 <= op <= 242:
|
||||
length = number(op-238)
|
||||
at += length
|
||||
elif 243 <= op <= 246:
|
||||
font_definition(op-242)
|
||||
elif op == 247:
|
||||
assert number(1) == 2
|
||||
assert [number(4) for _ in range(3)] == [25400000, 473628672, 1000]
|
||||
length = number(1)
|
||||
at += length
|
||||
elif op == 248:
|
||||
break
|
||||
else:
|
||||
raise ValueError(f'unsupported DVI opcode {op}')
|
||||
return pages
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('dvi', type=Path)
|
||||
print(json.dumps(read_dvi(parser.parse_args().dvi)))
|
||||
Reference in new issue
Block a user