File size: 10,635 Bytes
eaaeb1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import difflib
import hashlib
import os
import textwrap
from dataclasses import dataclass
from pathlib import Path


def sha256_short(data: bytes, n: int = 12) -> str:
    return hashlib.sha256(data).hexdigest()[:n]


def is_probably_text(path: Path) -> bool:
    ext = path.suffix.lower()
    return ext in {".tex", ".bib", ".txt", ".md", ".rst", ".csv", ".json", ".yaml", ".yml"}


def escape_pdf_string(text: str) -> str:
    return (
        text.replace("\\", "\\\\")
        .replace("(", "\\(")
        .replace(")", "\\)")
        .replace("\r", "\\r")
        .replace("\t", "\\t")
    )


@dataclass(frozen=True)
class PdfLine:
    text: str
    rgb: tuple[float, float, float]


class SimplePdf:
    def __init__(
        self,
        page_width: int = 612,  # US Letter
        page_height: int = 792,
        margin_left: int = 40,
        margin_top: int = 760,
        margin_bottom: int = 40,
        font_size: int = 8,
        leading: int = 10,
        max_chars: int = 100,
    ) -> None:
        self.page_width = page_width
        self.page_height = page_height
        self.margin_left = margin_left
        self.margin_top = margin_top
        self.margin_bottom = margin_bottom
        self.font_size = font_size
        self.leading = leading
        self.max_chars = max_chars

    def _lines_per_page(self) -> int:
        usable = self.margin_top - self.margin_bottom
        return max(1, usable // self.leading)

    def _chunk_lines(self, lines: list[PdfLine]) -> list[list[PdfLine]]:
        chunks: list[list[PdfLine]] = []
        current: list[PdfLine] = []
        for line in lines:
            parts = self._wrap_preserve(line.text, self.max_chars)
            for part in parts:
                current.append(PdfLine(part, line.rgb))
                if len(current) >= self._lines_per_page():
                    chunks.append(current)
                    current = []
        if current:
            chunks.append(current)
        return chunks

    @staticmethod
    def _wrap_preserve(text: str, width: int) -> list[str]:
        if len(text) <= width:
            return [text]
        # Preserve leading whitespace for diff readability.
        leading_ws_len = len(text) - len(text.lstrip(" "))
        prefix = " " * min(leading_ws_len, width - 1)
        rest = text.lstrip(" ")
        wrapped = textwrap.wrap(
            rest,
            width=width - len(prefix),
            break_long_words=True,
            break_on_hyphens=False,
            drop_whitespace=False,
        )
        if not wrapped:
            return [text[:width], prefix + text[width:]]
        out = [prefix + wrapped[0]]
        out.extend(prefix + w for w in wrapped[1:])
        return out

    def render(self, lines: list[PdfLine], out_path: Path) -> None:
        pages = self._chunk_lines(lines)

        objects: list[bytes] = []

        def add_object(payload: bytes) -> int:
            objects.append(payload)
            return len(objects)

        # Font (Courier)
        font_obj = add_object(b"<< /Type /Font /Subtype /Type1 /BaseFont /Courier >>")

        content_objs: list[int] = []
        page_objs: list[int] = []

        for page_lines in pages:
            stream = self._content_stream(page_lines).encode("utf-8")
            content_obj = add_object(
                b"<< /Length "
                + str(len(stream)).encode("ascii")
                + b" >>\nstream\n"
                + stream
                + b"\nendstream"
            )
            content_objs.append(content_obj)

        # Pages object created after page objects, but we need its object number for /Parent.
        # Reserve a slot.
        pages_obj_placeholder = add_object(b"")
        pages_obj_num = pages_obj_placeholder

        for content_obj in content_objs:
            page_obj = add_object(
                (
                    f"<< /Type /Page /Parent {pages_obj_num} 0 R "
                    f"/MediaBox [0 0 {self.page_width} {self.page_height}] "
                    f"/Resources << /Font << /F1 {font_obj} 0 R >> >> "
                    f"/Contents {content_obj} 0 R >>"
                ).encode("ascii")
            )
            page_objs.append(page_obj)

        kids = " ".join(f"{n} 0 R" for n in page_objs)
        pages_obj = (
            f"<< /Type /Pages /Kids [{kids}] /Count {len(page_objs)} >>".encode("ascii")
        )
        objects[pages_obj_num - 1] = pages_obj

        catalog_obj = add_object(
            f"<< /Type /Catalog /Pages {pages_obj_num} 0 R >>".encode("ascii")
        )

        self._write_pdf(objects, catalog_obj, out_path)

    def _content_stream(self, page_lines: list[PdfLine]) -> str:
        x = self.margin_left
        y = self.margin_top
        parts = [
            "BT",
            f"/F1 {self.font_size} Tf",
            f"{x} {y} Td",
        ]

        prev_rgb: tuple[float, float, float] | None = None
        first = True
        for line in page_lines:
            if first:
                first = False
            else:
                parts.append(f"0 -{self.leading} Td")

            if prev_rgb != line.rgb:
                r, g, b = line.rgb
                parts.append(f"{r:.3f} {g:.3f} {b:.3f} rg")
                prev_rgb = line.rgb

            parts.append(f"({escape_pdf_string(line.text)}) Tj")

        parts.append("ET")
        return "\n".join(parts)

    @staticmethod
    def _write_pdf(objects: list[bytes], root_obj_num: int, out_path: Path) -> None:
        # PDF header: include binary chars line per spec recommendation.
        out = bytearray()
        out += b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n"

        xref_positions = [0]  # object 0
        for i, payload in enumerate(objects, start=1):
            xref_positions.append(len(out))
            out += f"{i} 0 obj\n".encode("ascii")
            out += payload + b"\nendobj\n"

        xref_start = len(out)
        out += f"xref\n0 {len(objects) + 1}\n".encode("ascii")
        out += b"0000000000 65535 f \n"
        for pos in xref_positions[1:]:
            out += f"{pos:010d} 00000 n \n".encode("ascii")

        out += (
            b"trailer\n"
            + f"<< /Size {len(objects) + 1} /Root {root_obj_num} 0 R >>\n".encode(
                "ascii"
            )
            + b"startxref\n"
            + str(xref_start).encode("ascii")
            + b"\n%%EOF\n"
        )

        out_path.parent.mkdir(parents=True, exist_ok=True)
        out_path.write_bytes(out)


def diff_to_pdf_lines(old_root: Path, new_root: Path) -> list[PdfLine]:
    old_files = {p.relative_to(old_root) for p in old_root.rglob("*") if p.is_file()}
    new_files = {p.relative_to(new_root) for p in new_root.rglob("*") if p.is_file()}

    added = sorted(new_files - old_files)
    removed = sorted(old_files - new_files)
    common = sorted(old_files & new_files)

    changed_text: list[Path] = []
    changed_bin: list[Path] = []

    for rel in common:
        p1 = old_root / rel
        p2 = new_root / rel
        b1 = p1.read_bytes()
        b2 = p2.read_bytes()
        if b1 == b2:
            continue
        if is_probably_text(rel):
            changed_text.append(rel)
        else:
            changed_bin.append(rel)

    lines: list[PdfLine] = []

    def add_line(text: str, rgb: tuple[float, float, float] = (0, 0, 0)) -> None:
        lines.append(PdfLine(text, rgb))

    add_line(f"Diff: {old_root}  ->  {new_root}", (0.1, 0.1, 0.1))
    add_line("", (0, 0, 0))

    add_line("Summary", (0.1, 0.1, 0.6))
    add_line(f"- Changed text files: {len(changed_text)}", (0, 0, 0))
    for rel in changed_text:
        add_line(f"  - {rel}", (0, 0, 0))
    add_line(f"- Changed binary files: {len(changed_bin)}", (0, 0, 0))
    for rel in changed_bin:
        p1 = old_root / rel
        p2 = new_root / rel
        b1 = p1.read_bytes()
        b2 = p2.read_bytes()
        add_line(
            f"  - {rel} (old {len(b1)}B {sha256_short(b1)}, new {len(b2)}B {sha256_short(b2)})",
            (0, 0, 0),
        )
    add_line(f"- Added files: {len(added)}", (0, 0, 0))
    for rel in added:
        p = new_root / rel
        b = p.read_bytes()
        add_line(f"  - {rel} ({len(b)}B {sha256_short(b)})", (0, 0, 0))
    add_line(f"- Removed files: {len(removed)}", (0, 0, 0))
    for rel in removed:
        add_line(f"  - {rel}", (0, 0, 0))

    add_line("", (0, 0, 0))
    add_line("Unified diffs (text)", (0.1, 0.1, 0.6))
    add_line("", (0, 0, 0))

    def color_for_diff_line(s: str) -> tuple[float, float, float]:
        if s.startswith("diff "):
            return (0.2, 0.2, 0.2)
        if s.startswith("--- ") or s.startswith("+++ "):
            return (0.2, 0.2, 0.2)
        if s.startswith("@@"):
            return (0.1, 0.1, 0.6)
        if s.startswith("+") and not s.startswith("+++"):
            return (0.0, 0.45, 0.0)
        if s.startswith("-") and not s.startswith("---"):
            return (0.75, 0.0, 0.0)
        return (0, 0, 0)

    for rel in changed_text:
        p1 = old_root / rel
        p2 = new_root / rel
        old_lines = p1.read_text(encoding="utf-8", errors="replace").splitlines()
        new_lines = p2.read_text(encoding="utf-8", errors="replace").splitlines()
        add_line(f"File: {rel}", (0.2, 0.2, 0.2))
        diff_lines = difflib.unified_diff(
            old_lines,
            new_lines,
            fromfile=str(old_root / rel),
            tofile=str(new_root / rel),
            lineterm="",
            n=3,
        )
        for s in diff_lines:
            add_line(s, color_for_diff_line(s))
        add_line("", (0, 0, 0))

    return lines


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Generate a PDF containing unified diffs between two paper versions."
    )
    parser.add_argument("old_dir", type=Path)
    parser.add_argument("new_dir", type=Path)
    parser.add_argument(
        "-o",
        "--out",
        type=Path,
        default=Path("paper-sources/paper-301-vs-757-diff.pdf"),
        help="Output PDF path (default: paper-sources/paper-301-vs-757-diff.pdf)",
    )
    args = parser.parse_args()

    old_dir = args.old_dir.resolve()
    new_dir = args.new_dir.resolve()
    if not old_dir.is_dir():
        raise SystemExit(f"Not a directory: {old_dir}")
    if not new_dir.is_dir():
        raise SystemExit(f"Not a directory: {new_dir}")

    pdf = SimplePdf()
    lines = diff_to_pdf_lines(old_dir, new_dir)
    pdf.render(lines, args.out)
    print(args.out)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())