#!/usr/bin/env python3 """Read the vector PGPLOT PostScript of the paper's figures as data (rule D). Runs nothing from the EPS: it tokenizes numbers and the few drawing operators PGPLOT writes. Usage: python -I read_figures.py [preview.png] """ import json import re import sys def parse(path): text = open(path, encoding="latin-1").read() # keep the body only i = text.find("/PGPLOT save def") body = text[i:] segs = [] # polylines: (color, width, [(x, y), ...]) marks = [] # (color, name, x, y, mfac) color = (0.0, 0.0, 0.0) width = 1.0 mfac = 1.0 stack = [] cur = None poly = None defs = {} tok_re = re.compile(r"/[A-Za-z0-9_]+|[A-Za-z_][A-Za-z0-9_]*|-?\d+\.\d*|-?\.\d+|-?\d+|[{}()\[\]]") tokens = tok_re.findall(body) n = len(tokens) k = 0 pending_mfac = False while k < n: t = tokens[k] k += 1 if re.fullmatch(r"-?\d+\.\d*|-?\.\d+|-?\d+", t): stack.append(float(t)) continue if t == "{": # skip a procedure body, marker definitions depth = 1 while k < n and depth: if tokens[k] == "{": depth += 1 elif tokens[k] == "}": depth -= 1 k += 1 stack.clear() continue if t == "/MFAC": if k < n and re.fullmatch(r"-?\d+\.\d*|-?\d+", tokens[k]): mfac = float(tokens[k]) k += 1 continue if t == "setrgbcolor": if len(stack) >= 3: color = tuple(stack[-3:]) stack.clear() continue if t == "SLW": if stack: width = stack[-1] stack.clear() continue if t == "L": if len(stack) >= 4: dx, dy, x, y = stack[-4:] poly = [(x, y), (x + dx, y + dy)] segs.append({"color": color, "width": width, "pts": poly}) cur = (x + dx, y + dy) stack.clear() continue if t == "C": if len(stack) >= 2 and poly is not None: dx, dy = stack[-2:] cur = (cur[0] + dx, cur[1] + dy) poly.append(cur) stack.clear() continue if t == "D": if len(stack) >= 2: x, y = stack[-2:] marks.append({"color": color, "name": "D", "x": x, "y": y, "mfac": mfac}) stack.clear() continue if re.fullmatch(r"M\d+", t) and len(stack) >= 2: x, y = stack[-2:] marks.append({"color": color, "name": t, "x": x, "y": y, "mfac": mfac}) stack.clear() continue if t in ("def", "bind", "save", "translate", "scale", "setlinejoin", "setlinecap", "SCF", "restore", "showpage", "PGPLOT", "gsave", "grestore"): stack.clear() continue stack.clear() return segs, marks def main(): path, out = sys.argv[1:3] segs, marks = parse(path) with open(out, "w", encoding="utf-8", newline="\n") as fh: json.dump({"segments": segs, "marks": marks}, fh) print(path, "segments", len(segs), "marks", len(marks)) if len(sys.argv) > 3: import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(12, 9)) for s in segs: xs = [p[0] for p in s["pts"]] ys = [p[1] for p in s["pts"]] ax.plot(xs, ys, color=s["color"], lw=0.6 + 0.2 * s["width"]) names = sorted(set(m["name"] for m in marks)) for nm, mk in zip(names, "ov^sD*x+pP"): xs = [m["x"] for m in marks if m["name"] == nm] ys = [m["y"] for m in marks if m["name"] == nm] cs = [m["color"] for m in marks if m["name"] == nm] ax.scatter(xs, ys, s=8, c=cs, marker=mk, label=nm) ax.legend() ax.set_aspect("equal") fig.savefig(sys.argv[3], dpi=110) if __name__ == "__main__": main()