File size: 28,815 Bytes
f66643d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
from __future__ import annotations

import re

from .background import RGB
from .config import RenderConfig, StyleSpec
from .labels import normalize_label, skip_oversize_element, style_key
from .markup import (
    _split_math_vars,
    escape_typst_string,
    has_bare_latex,
    has_malformed_typst_math,
    has_unbalanced_math_tags,
    is_pure_math_text,
    parse_toc_entries,
    to_typst_markup,
    to_typst_native,
)

CMARKER_VERSION = "0.1.8"


MITEX_VERSION = "0.2.6"

# Detects legacy LaTeX inside <math> tags (backslash commands like \frac, \sum)
_LATEX_IN_MATH = re.compile(r"<math[^>]*>[^<]*\\[a-zA-Z]", re.DOTALL)

# Detects bare Typst math function calls (frac(...), sqrt(...), etc.) outside <math> tags
_BARE_TYPST_MATH = re.compile(
    r"(?:^|[^a-zA-Z])(?:frac|sqrt|root|binom|sum|prod|integral|mat|vec|cases|abs|norm|floor|ceil)\s*\(",
    re.IGNORECASE,
)

_FIT_HELPERS = """\
#let pdftr_fit_size(lo, hi, eps, fits) = {
  if hi - lo <= eps {
    lo
  } else {
    let mid = lo + (hi - lo) / 2
    if fits(mid) {
      pdftr_fit_size(mid, hi, eps, fits)
    } else {
      pdftr_fit_size(lo, mid, eps, fits)
    }
  }
}
#let pdftr_floor_size(value, floor) = if value < floor { floor } else { value }
#let pdftr_floor_leading(value, floor) = if value < floor { floor } else { value }
#let pdftr_fit_markdown(markdown, max_size: 10pt, min_size: 9pt, max_leading: 0.66em, min_leading: 0.54em, fit_height: none, weight: "regular", style: "normal", eps: 0.08pt, math: none) = {
  layout(size => {
    let allowed-height = if fit_height == none { size.height } else { calc.min(size.height, fit_height) }
    let render(text_size, leading) = block(width: size.width)[#{
      set text(size: text_size, weight: weight, style: style)
      set par(leading: leading)
      cmarker.render(markdown, math: math)
    }]
    let fits(text_size, leading) = measure(width: size.width, render(text_size, leading)).height <= allowed-height
    if fits(max_size, max_leading) {
      render(max_size, max_leading)
    } else {
      let fallback_min_size = pdftr_floor_size(min_size - 1.6pt, 5.4pt)
      let fallback_min_leading = pdftr_floor_leading(min_leading - 0.12em, 0.14em)
      let emergency_min_size = pdftr_floor_size(fallback_min_size - 1.2pt, 4.8pt)
      let emergency_min_leading = pdftr_floor_leading(fallback_min_leading - 0.08em, 0.10em)
      let chosen_leading = if fits(min_size, max_leading) { max_leading } else { min_leading }
      let chosen_size = if not fits(min_size, chosen_leading) {
        let fallback_leading = pdftr_floor_leading(chosen_leading - 0.12em, fallback_min_leading)
        let emergency_leading = pdftr_floor_leading(fallback_leading - 0.08em, emergency_min_leading)
        if not fits(fallback_min_size, fallback_leading) {
          if not fits(emergency_min_size, emergency_leading) {
            emergency_min_size
          } else {
            pdftr_fit_size(emergency_min_size, fallback_min_size, eps, size_pt => fits(size_pt, emergency_leading))
          }
        } else {
          pdftr_fit_size(fallback_min_size, min_size, eps, size_pt => fits(size_pt, fallback_leading))
        }
      } else {
        pdftr_fit_size(min_size, max_size, eps, size_pt => fits(size_pt, chosen_leading))
      }
      let final_leading = if fits(min_size, chosen_leading) {
        chosen_leading
      } else if fits(fallback_min_size, pdftr_floor_leading(chosen_leading - 0.12em, fallback_min_leading)) {
        pdftr_floor_leading(chosen_leading - 0.12em, fallback_min_leading)
      } else {
        emergency_min_leading
      }
      render(chosen_size, final_leading)
    }
  })
}
#let pdftr_fit_typst(content, max_size: 10pt, min_size: 9pt, max_leading: 0.66em, min_leading: 0.54em, fit_height: none, weight: "regular", style: "normal", eps: 0.08pt, no_wrap: false) = {
  layout(size => {
    let allowed-height = if fit_height == none { size.height } else { calc.min(size.height, fit_height) }
    let render(text_size, leading) = block(width: size.width)[#{
      set text(size: text_size, weight: weight, style: style)
      set par(leading: leading)
      content
    }]
    if no_wrap {
      // Single-line mode: find largest font where content does not wrap.
      // Compare height at container width vs height at huge width β€” equal means no wrap.
      let no_wrap_fits(text_size) = {
        let h_narrow = measure(width: size.width, render(text_size, max_leading)).height
        let h_wide = measure(width: 10000pt, block(width: 10000pt)[#{
          set text(size: text_size, weight: weight, style: style)
          set par(leading: max_leading)
          content
        }]).height
        h_narrow <= h_wide
      }
      let chosen_size = if no_wrap_fits(max_size) {
        max_size
      } else if not no_wrap_fits(min_size) {
        min_size
      } else {
        pdftr_fit_size(min_size, max_size, eps, size_pt => no_wrap_fits(size_pt))
      }
      render(chosen_size, max_leading)
    } else {
      let fits(text_size, leading) = measure(width: size.width, render(text_size, leading)).height <= allowed-height
      if fits(max_size, max_leading) {
        render(max_size, max_leading)
      } else {
        let fallback_min_size = pdftr_floor_size(min_size - 1.6pt, 5.4pt)
        let fallback_min_leading = pdftr_floor_leading(min_leading - 0.12em, 0.14em)
        let emergency_min_size = pdftr_floor_size(fallback_min_size - 1.2pt, 4.8pt)
        let emergency_min_leading = pdftr_floor_leading(fallback_min_leading - 0.08em, 0.10em)
        let chosen_leading = if fits(min_size, max_leading) { max_leading } else { min_leading }
        let chosen_size = if not fits(min_size, chosen_leading) {
          let fallback_leading = pdftr_floor_leading(chosen_leading - 0.12em, fallback_min_leading)
          let emergency_leading = pdftr_floor_leading(fallback_leading - 0.08em, emergency_min_leading)
          if not fits(fallback_min_size, fallback_leading) {
            if not fits(emergency_min_size, emergency_leading) {
              emergency_min_size
            } else {
              pdftr_fit_size(emergency_min_size, fallback_min_size, eps, size_pt => fits(size_pt, emergency_leading))
            }
          } else {
            pdftr_fit_size(fallback_min_size, min_size, eps, size_pt => fits(size_pt, fallback_leading))
          }
        } else {
          pdftr_fit_size(min_size, max_size, eps, size_pt => fits(size_pt, chosen_leading))
        }
        let final_leading = if fits(min_size, chosen_leading) {
          chosen_leading
        } else if fits(fallback_min_size, pdftr_floor_leading(chosen_leading - 0.12em, fallback_min_leading)) {
          pdftr_floor_leading(chosen_leading - 0.12em, fallback_min_leading)
        } else {
          emergency_min_leading
        }
        render(chosen_size, final_leading)
      }
    }
  })
}"""


def _rgb_typst(rgb: RGB) -> str:
    return f"rgb({rgb[0]}, {rgb[1]}, {rgb[2]})"


def _font_typst(font: str | list[str]) -> str:
    """Render Typst font expression. Accepts a single name or fallback chain."""
    if isinstance(font, str):
        return f'"{font}"'
    return "(" + ", ".join(f'"{f}"' for f in font) + ")"


def _style_for(label: str, cfg: RenderConfig) -> StyleSpec:
    return cfg.styles.get(style_key(label), cfg.default_style)


def _cover_rect(
    var: str, x0: float, y0: float, x1: float, y1: float, bg: RGB, pad: float
) -> str:
    w = max(4.0, x1 - x0 + 2 * pad)
    h = max(4.0, y1 - y0 + 2 * pad)
    dx = x0 - pad
    dy = y0 - pad
    return (
        f"#let {var}_cover = rect(width: {w:.2f}pt, height: {h:.2f}pt,"
        f" fill: {_rgb_typst(bg)}, stroke: none)\n"
        f"#context {{ place(top + left, dx: {dx:.2f}pt, dy: {dy:.2f}pt, {var}_cover) }}\n"
    )


def _downward_avail_height(
    bbox: list[float],
    all_bboxes: list[list[float]],
    page_height: float,
    max_expand: float,
) -> float:
    """Block height that lets text overflow DOWN into empty space only.

    Extends the tight bbox height downward until it would reach the nearest
    element below that horizontally overlaps it β€” capped by ``max_expand`` and
    the page bottom. Mirrors the sizing heuristic: the Typst fit then keeps the
    chosen size when overflow lands in empty space and only shrinks when the
    text would actually collide with a neighbor below.
    """
    x0, y0, x1, y1 = bbox
    limit = min(page_height, y1 + max_expand)
    for ob in all_bboxes:
        if ob is bbox or len(ob) != 4:
            continue
        ox0, oy0, ox1, _ = ob
        if ox1 > x0 and ox0 < x1 and oy0 >= y1 - 0.5:  # below & horizontally overlaps
            limit = min(limit, oy0)
    return max(y1 - y0, limit - y0)


def _rightward_avail_width(
    bbox: list[float],
    all_bboxes: list[list[float]],
    page_width: float,
) -> float:
    """Block width that lets single-line text extend RIGHT into empty space only.

    Extends the tight bbox width rightward until it would reach the nearest
    element to the right that vertically overlaps it β€” capped by the page edge.
    Without this, a single-line block expands to the page margin and can overrun
    a right-hand neighbor (e.g. a TOC page number) without shrinking; bounding it
    lets the no-wrap fit shrink the text before it collides.
    """
    x0, y0, x1, y1 = bbox
    limit = page_width
    for ob in all_bboxes:
        if ob is bbox or len(ob) != 4:
            continue
        ox0, oy0, _, oy1 = ob
        if ox0 >= x1 - 0.5 and oy0 < y1 and oy1 > y0:  # right & vertically overlaps
            limit = min(limit, ox0)
    return max(x1 - x0, limit - x0)


def _text_block(
    var: str,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    markdown: str,
    font_size: float,
    min_font: float,
    weight: str,
    style_: str,
    text_color: RGB,
    font_family: str,
    expanded_w: float | None = None,
    expanded_h: float | None = None,
    valign: str = "top",
) -> str:
    w = max(4.0, expanded_w if expanded_w is not None else (x1 - x0))
    h = max(4.0, expanded_h if expanded_h is not None else (y1 - y0))
    # Ensure min_size <= max_size; otherwise the binary-search fit helper
    # gets lo > hi and behaves incorrectly.
    effective_min = min(min_font, font_size)
    escaped = escape_typst_string(markdown)
    fit_call = (
        f"pdftr_fit_markdown({var}_md,"
        f" max_size: {font_size:.2f}pt, min_size: {effective_min:.2f}pt,"
        f' weight: "{weight}", style: "{style_}")'
    )
    if valign == "bottom":
        content = f"align(bottom + left, {fit_call})"
    elif valign == "center":
        content = f"align(center + horizon, {fit_call})"
    else:
        content = fit_call
    return (
        f'#let {var}_md = "{escaped}"\n'
        f"#let {var}_body = block(width: {w:.2f}pt, height: {h:.2f}pt)[#{{\n"
        f"  set text(font: {_font_typst(font_family)}, fill: {_rgb_typst(text_color)})\n"
        f"  {content}\n"
        f"}}]\n"
        f"#context {{ place(top + left, dx: {x0:.2f}pt, dy: {y0:.2f}pt, {var}_body) }}\n"
    )


def _text_block_typst(
    var: str,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    typst_markup: str,
    font_size: float,
    min_font: float,
    weight: str,
    style_: str,
    text_color: RGB,
    font_family: str,
    no_wrap: bool = False,
    expanded_w: float | None = None,
    expanded_h: float | None = None,
    valign: str = "top",
) -> str:
    w = max(4.0, expanded_w if expanded_w is not None else (x1 - x0))
    h = max(4.0, expanded_h if expanded_h is not None else (y1 - y0))
    effective_min = min(min_font, font_size)
    no_wrap_arg = ", no_wrap: true" if no_wrap else ""
    fit_call = (
        f"pdftr_fit_typst({var}_tm,"
        f" max_size: {font_size:.2f}pt, min_size: {effective_min:.2f}pt,"
        f' weight: "{weight}", style: "{style_}"{no_wrap_arg})'
    )
    if valign == "center":
        content = f"align(center + horizon, {fit_call})"
    elif valign == "bottom":
        content = f"align(bottom + left, {fit_call})"
    else:
        content = fit_call
    return (
        f"#let {var}_tm = [{typst_markup}]\n"
        f"#let {var}_body = block(width: {w:.2f}pt, height: {h:.2f}pt)[#{{\n"
        f"  set text(font: {_font_typst(font_family)}, fill: {_rgb_typst(text_color)})\n"
        f"  {content}\n"
        f"}}]\n"
        f"#context {{ place(top + left, dx: {x0:.2f}pt, dy: {y0:.2f}pt, {var}_body) }}\n"
    )


_TOC_TOP_LEVEL_RE = re.compile(r"^\d+\s+\S")


def _toc_block(
    var: str,
    x0: float,
    y0: float,
    x1: float,
    y1: float,
    translated_text: str,
    font_size: float,
    min_font: float,
    text_color: RGB,
    font_family: str,
    rendered_pages: set[int] | None = None,
) -> str:
    """Render TOC entries with right-aligned page numbers + clickable links.

    Each entry whose target page is in `rendered_pages` (1-indexed) gets
    wrapped in `#link(<pdftr-page-N>)[...]` so clicking jumps to that page.

    Top-level entries (section number with no dot, e.g. '1 Introduction')
    render in bold; sub-entries stay regular.

    Auto-shrinks via `pdftr_fit_typst` when entries overflow the bbox.
    """
    entries = parse_toc_entries(translated_text)
    markup_lines: list[str] = []
    for title, page_num in entries:
        title_escaped = escape_typst_string(to_typst_markup(title))
        weight = "bold" if _TOC_TOP_LEVEL_RE.match(title) else "regular"
        if page_num:
            row = (
                f"grid(columns: (1fr, auto), gutter: 4pt, "
                f'text(weight: "{weight}", "{title_escaped} "), '
                f'align(right, text(weight: "{weight}", "{page_num}")))'
            )
            page_int = int(page_num)
            if rendered_pages is None or page_int in rendered_pages:
                markup_lines.append(f"#link(<pdftr-page-{page_int}>)[#{row}]")
            else:
                markup_lines.append(f"#{row}")
        else:
            markup_lines.append(f'#par(text(weight: "{weight}", "{title_escaped}"))')
    typst_markup = "\n".join(markup_lines)
    return _text_block_typst(
        var,
        x0,
        y0,
        x1,
        y1,
        typst_markup,
        font_size,
        min_font,
        "regular",
        "normal",
        text_color,
        font_family,
    )


def build_typst_source(
    parsed: dict,
    sizes: dict[str, float],
    bg_colors: dict[str, RGB],
    text_colors: dict[str, RGB],
    cfg: RenderConfig,
    fallback_vars: set[str] | frozenset[str] = frozenset(),
) -> str:
    """Build the overlay Typst source.

    Args:
        fallback_vars: element vars (e.g. ``e3_7``) whose markup previously
            broke the Typst compile β€” rendered via the plain markdown-string
            path instead of native Typst markup (always syntactically valid).
    """
    lines: list[str] = [
        f"#set text(font: {_font_typst(cfg.font_family)})",
        f'#import "@preview/cmarker:{CMARKER_VERSION}"',
        _FIT_HELPERS,
    ]

    pages = parsed.get("pages", [])
    # 1-indexed anchor numbers actually emitted (enumerate position of each
    # rendered page) β€” used to gate TOC links so we never #link a missing
    # anchor (Typst errors on dangling label references).
    rendered_pages = {
        page_idx + 1
        for page_idx, page in enumerate(pages)
        if cfg.pages is None or page.get("page_index", page_idx) in cfg.pages
    }
    for page_idx, page in enumerate(pages):
        if cfg.pages is not None and page.get("page_index", page_idx) not in cfg.pages:
            continue
        pw = page.get("page_width", 595.0)
        ph = page.get("page_height", 842.0)
        lines.append(
            f"#set page(width: {pw:.2f}pt, height: {ph:.2f}pt, margin: 0pt, fill: none)"
        )
        # Anchor for TOC links β€” page number is 1-indexed (user-facing).
        lines.append(f"#metadata(none)<pdftr-page-{page_idx + 1}>")

        elems = page.get("elements", [])
        # All element bboxes on this page β€” used to bound downward text overflow
        # so a block only shrinks when its overflow would hit a neighbor.
        page_bboxes = [e.get("bbox_pdf") for e in elems if e.get("bbox_pdf")]

        for elem_idx, elem in enumerate(elems):
            category = elem.get("category", "")
            label = normalize_label(elem.get("label", "Text"))
            uid = f"p{page_idx}:e{elem_idx}"
            var = f"e{page_idx}_{elem_idx}"

            if category == "BYPASS":
                continue

            bbox = elem.get("bbox_pdf", [0, 0, 100, 20])
            x0, y0, x1, y1 = bbox

            # A minor/structural element (section header, caption, footnote, …)
            # covering most of the page is a mis-detection β€” keep the original.
            # Real content (tables, body text, equations) is never skipped by
            # size. The redaction pass uses this same predicate, so the two
            # stages always agree on what to skip (else content gets erased but
            # not redrawn).
            if skip_oversize_element(label, bbox, pw, ph):
                continue
            bg = bg_colors.get(uid, cfg.background.fallback_bg)
            tc = text_colors.get(uid, (0, 0, 0))
            style = _style_for(label, cfg)
            font_size = sizes.get(uid, cfg.sizing.fallback_size)

            if category == "EQUATION":
                translated = elem.get("translated_text") or ""
                source = elem.get("source_text") or ""
                if not translated or translated == source:
                    continue
                # Malformed / bare-LaTeX output β†’ preserve original text layer.
                # For EQUATION, skip is_pure_math_text: translated_text is intentionally
                # Typst math markup and should be rendered even if it has no prose.
                if (
                    has_unbalanced_math_tags(translated)
                    or has_bare_latex(translated)
                    or has_malformed_typst_math(translated)
                ):
                    continue
                if category != "EQUATION" and is_pure_math_text(translated):
                    continue
                lines.append(
                    _cover_rect(
                        var, x0, y0, x1, y1, bg, cfg.background.eraser_padding_pt
                    )
                )
                # EQUATION elements that reach here always contain prose mixed
                # with math (pure-math equations have no translated_text and are
                # skipped above). Fractions make the bbox taller than the actual
                # text size, so use the cluster font_size, not y1 - y0.
                eq_max_size = font_size
                if var not in fallback_vars and (
                    "<math" in translated or "<typst" in translated
                ):
                    typst_markup = to_typst_native(translated)
                    lines.append(
                        _text_block_typst(
                            var,
                            x0,
                            y0,
                            x1,
                            y1,
                            typst_markup,
                            eq_max_size,
                            cfg.min_font_size_pt,
                            style.weight,
                            style.style_,
                            tc,
                            cfg.font_family,
                            valign="center",
                        )
                    )
                else:
                    markdown = to_typst_markup(translated)
                    lines.append(
                        _text_block(
                            var,
                            x0,
                            y0,
                            x1,
                            y1,
                            markdown,
                            eq_max_size,
                            cfg.min_font_size_pt,
                            style.weight,
                            style.style_,
                            tc,
                            cfg.font_family,
                            valign="center",
                        )
                    )

            elif category == "TABLE":
                cells = elem.get("cells", [])
                for cell_idx, cell in enumerate(cells):
                    cell_source = cell.get("source_text") or ""
                    if not cell_source.strip():
                        continue
                    cell_translated = cell.get("translated_text") or ""
                    if not cell_translated:
                        continue
                    cell_uid = f"{uid}:c{cell_idx}"
                    cell_var = f"{var}_c{cell_idx}"
                    cbbox = cell.get("bbox_pdf", bbox)
                    # Only touch the text region: both the eraser and the
                    # translated text use bbox_text (the tight box hugging the
                    # original text) instead of blanking the whole grid cell β€”
                    # this preserves the cell's borders and background.
                    tbbox = cell.get("bbox_text") or cbbox
                    tx0, ty0, tx1, ty1 = tbbox
                    cell_bg = bg_colors.get(cell_uid, bg)
                    cell_tc = text_colors.get(cell_uid, tc)
                    cell_size = sizes.get(cell_uid, font_size)
                    cell_md = to_typst_markup(cell_translated)
                    inset = cfg.sizing.cell_bbox_inset_pt
                    lines.append(
                        _cover_rect(
                            cell_var,
                            tx0,
                            ty0,
                            tx1,
                            ty1,
                            cell_bg,
                            cfg.background.eraser_padding_pt,
                        )
                    )
                    lines.append(
                        _text_block(
                            cell_var,
                            tx0 + inset,
                            ty0 + inset,
                            tx1 - inset,
                            ty1 - inset,
                            cell_md,
                            cell_size,
                            cfg.min_font_size_pt,
                            cfg.cell_style.weight,
                            cfg.cell_style.style_,
                            cell_tc,
                            cfg.font_family,
                        )
                    )

            else:  # FLOWING_TEXT, IN_PLACE
                translated = elem.get("translated_text") or ""
                source = elem.get("source_text") or ""
                if not translated:
                    continue
                # LLM returned text unchanged β†’ pure math notation, keep original.
                if translated.strip() == source.strip():
                    continue
                # Pure math / malformed LLM / bare LaTeX outside tags β†’
                # preserve original PDF text layer.
                if (
                    is_pure_math_text(translated)
                    or has_unbalanced_math_tags(translated)
                    or has_bare_latex(translated)
                    or has_malformed_typst_math(translated)
                ):
                    continue

                lines.append(
                    _cover_rect(
                        var, x0, y0, x1, y1, bg, cfg.background.eraser_padding_pt
                    )
                )

                use_fallback = var in fallback_vars
                if not use_fallback and label == "TableOfContents":
                    lines.append(
                        _toc_block(
                            var,
                            x0,
                            y0,
                            x1,
                            y1,
                            translated,
                            font_size,
                            cfg.min_font_size_pt,
                            tc,
                            cfg.font_family,
                            rendered_pages,
                        )
                    )
                elif not use_fallback and (
                    "<typst" in translated or "<math" in translated
                ):
                    typst_markup = to_typst_native(translated)
                    is_single_line = (y1 - y0) < font_size * 1.8
                    # Single-line: expand block width to available horizontal
                    # space so text flows right instead of wrapping down.
                    exp_w = (
                        _rightward_avail_width(bbox, page_bboxes, pw)
                        if is_single_line
                        else None
                    )
                    exp_h = (
                        _downward_avail_height(bbox, page_bboxes, ph, cfg.max_expand_pt)
                        if cfg.expand_downward and not is_single_line
                        else None
                    )
                    lines.append(
                        _text_block_typst(
                            var,
                            x0,
                            y0,
                            x1,
                            y1,
                            typst_markup,
                            font_size,
                            cfg.min_font_size_pt,
                            style.weight,
                            style.style_,
                            tc,
                            cfg.font_family,
                            no_wrap=is_single_line,
                            expanded_w=exp_w,
                            expanded_h=exp_h,
                        )
                    )
                elif not use_fallback and _BARE_TYPST_MATH.search(translated):
                    # Bare Typst math functions (no <math> tags) β€” wrap in $ and use native path
                    typst_markup = f"${_split_math_vars(translated)}$"
                    is_single_line = (y1 - y0) < font_size * 1.8
                    exp_w = (
                        _rightward_avail_width(bbox, page_bboxes, pw)
                        if is_single_line
                        else None
                    )
                    exp_h = (
                        _downward_avail_height(bbox, page_bboxes, ph, cfg.max_expand_pt)
                        if cfg.expand_downward and not is_single_line
                        else None
                    )
                    lines.append(
                        _text_block_typst(
                            var,
                            x0,
                            y0,
                            x1,
                            y1,
                            typst_markup,
                            font_size,
                            cfg.min_font_size_pt,
                            style.weight,
                            style.style_,
                            tc,
                            cfg.font_family,
                            no_wrap=is_single_line,
                            expanded_w=exp_w,
                            expanded_h=exp_h,
                        )
                    )
                else:
                    # No <math> tags, no Typst functions β€” plain text/LaTeX, use cmarker/mitex
                    markdown = to_typst_markup(translated)
                    is_single_line = (y1 - y0) < font_size * 1.8
                    exp_w = (
                        _rightward_avail_width(bbox, page_bboxes, pw)
                        if is_single_line
                        else None
                    )
                    exp_h = (
                        _downward_avail_height(bbox, page_bboxes, ph, cfg.max_expand_pt)
                        if cfg.expand_downward and not is_single_line
                        else None
                    )
                    lines.append(
                        _text_block(
                            var,
                            x0,
                            y0,
                            x1,
                            y1,
                            markdown,
                            font_size,
                            cfg.min_font_size_pt,
                            style.weight,
                            style.style_,
                            tc,
                            cfg.font_family,
                            expanded_w=exp_w,
                            expanded_h=exp_h,
                        )
                    )

        # Add pagebreak between pages (not after the last one)
        if page_idx < len(pages) - 1:
            lines.append("#pagebreak()")

    return "\n".join(lines) + "\n"