"""List the blanks of an official form: the runs of dots (or the ellipsis character) that the applicant writes on, with their page, position and the label before them. Positions are PDF points from the top left of the page, the system krilla draws in, so a value starts at x0 on the baseline. uv run --with pymupdf python scripts/form_blanks.py forms/tm7.pdf Used to measure a form's blanks for src/forms/layout.rs when a form is new or the Immigration Bureau publishes a new version of it. """ import sys import pymupdf DOTS = {".", "\u2026", "\u00b7"} def blanks(path): doc = pymupdf.open(path) for pno, page in enumerate(doc): raw = page.get_text("rawdict") for block in raw["blocks"]: for line in block.get("lines", []): chars = [c for span in line["spans"] for c in span["chars"]] origin_y = None i = 0 while i < len(chars): if chars[i]["c"] in DOTS: j = i while j < len(chars) and chars[j]["c"] in DOTS: j += 1 if j - i >= 4: label = "".join(c["c"] for c in chars[max(0, i - 28) : i]) x0 = chars[i]["bbox"][0] x1 = chars[j - 1]["bbox"][2] origin_y = chars[i]["origin"][1] yield pno + 1, round(x0, 1), round(x1, 1), round(origin_y, 1), label.strip() i = j else: i += 1 if __name__ == "__main__": for path in sys.argv[1:]: print(f"== {path}") rows = sorted(blanks(path), key=lambda r: (r[0], r[3], r[1])) for n, (page, x0, x1, y, label) in enumerate(rows, 1): print(f"{n:3} p{page} x {x0:6.1f}..{x1:6.1f} (w {x1 - x0:5.1f}) y {y:6.1f} after {label!r}")