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
//! HTML output backends. There are two, and they answer different
//! questions.
//!
//! **This module is the LAYOUT-FAITHFUL one** (`--format html-fixed`,
//! [`render_html_fixed`]): it serializes the SAME post-page-break
//! `Page`/`PlacedLine` model the PDF writer (`rustyfi-pdf`'s `lib.rs`)
//! consumes — the design doc's "Option A", a non-reflowing "PDF-in-a-div"
//! twin of the PDF output. Its use is visual diffing: putting this port's
//! layout in a browser where a run's coordinates can be inspected, rather
//! than eyeballing two renderings side by side. It is not a web page and
//! is not meant to be read as one.
//!
//! **The [`reflow`] submodule is the readable one** (`--format html`,
//! [`render_html_reflow`]): one continuous, semantic document with no pages
//! in it, built from the flat block stream as it stood BEFORE page
//! breaking. See its own doc comment.
//!
//! Everything below concerns the faithful backend.
//!
//! **Slice 1** (§Slice 1, "text + block layout of a single-page document"):
//! `InnerString` runs as positioned `<span>`s, plus the `<div class="page">`
//! wrapper. **Slice 2** (§Slice 2, "graphics (inline SVG)"), this revision:
//! `Graphics` as inline SVG (`svg.rs`) and the `Tabular`/`EmbeddedBlock`/
//! `Frame` composite recursions, mirroring the PDF writer's own `emit_box`
//! (`lib.rs:646-671`). `Image`/`Math` and `DocExtras::page_graphics` remain
//! Slice 3+ territory — see `emit_box`'s doc comment below.
//!
//! **Slice 3** (§Slice 3, "real fonts + math"): `@font-face` data-URI
//! embedding (`fonts.rs`) so text/math runs use the SAME TrueType face
//! the [`rustyfi_pdf::TtfFontStore`] PDF path embeds (metric-faithful
//! positioning — see this module's `Ctx`/`render_html_fixed_ttf_with`), `Image`
//! boxes as `<img>` data URIs (`image.rs`, a hand-rolled uncompressed
//! BMP container — no PNG/image-codec dependency), and `Math` glyphs as
//! positioned `<span>`s (reusing the same run-emission path as
//! `InnerString`, per the design doc's math row) with `Math.rules` (the
//! fraction bar/radical) through the Slice-2 SVG path. The base-14 (no font
//! store) path is UNCHANGED from Slice 1/2: [`render_html_fixed`] still emits the
//! generic `.run` CSS default font-family, no `@font-face` block at all.
//!
//! **Slice 4** (§Slice 4, "multi-page + print pagination"), this revision:
//! print pagination CSS (a `@page { size: …; margin: 0 }` rule matching
//! `geometry.paper_width`/`paper_height`, plus `.page:not(:last-child) {
//! page-break-after: always; break-after: page }` so a browser print/
//! print-to-PDF paginates 1:1 with the document — a single-page document has
//! no non-last `.page`, so this selector matches nothing and its output is
//! byte-identical to Slice 1-3's, per the design doc's "keep single-page
//! docs looking identical" requirement) and `DocExtras::page_graphics` (the
//! per-page deco-graphics underlay this doc comment had flagged as deferred
//! since Slice 1/2 — see `render_html_impl`'s per-page loop below for the
//! coordinate-frame reconciliation).
//!
//! **Location.** This is its own `rustyfi-html` crate, a peer of
//! `rustyfi-pdf` (per the design doc's original spec, survey #6). It depends
//! on `rustyfi-backend` for every box/geometry type used below, plus
//! `rustyfi-pdf` for [`rustyfi_pdf::TtfFontStore`] (the one type this module
//! reuses rather than re-implements — only its `pub` `file_index`/
//! `file_bytes` accessors are used, so this is a plain one-way dependency,
//! not a cycle: `rustyfi-pdf` does not depend on `rustyfi-html`). Nothing
//! here touches `pdf_writer` or any other PDF-specific type, only
//! `rustyfi_backend`/`rustyfi_pdf::TtfFontStore` types and `String`
//! building.
// Reflowable/semantic HTML output mode (`reflow/mod.rs`'s doc comment) —
// re-exported at the crate root so CLI dispatch calls it exactly like the
// faithful `render_html_fixed`/`render_html_fixed_ttf_with` pair above (argument-for-
// argument symmetry, not a new API shape to learn).
pub use ;
use RefCell;
use BTreeSet;
use Write as _;
use ;
use TtfFontStore;
/// Slice 1 never actually constructs this — every text run is valid
/// UTF-8/HTML-escapable, and no font/image embedding (the error-prone parts,
/// per the design doc's later slices) happens yet. The `Result` return
/// shape is kept anyway so `render_html_fixed` is argument-for-argument (module
/// signature, not module fallibility) with `render_pdf_with`
/// (`lib.rs:459`), and so Slices 2/3 (SVG graphics, real fonts/`@font-face`,
/// image data-URIs) can surface a real error without a breaking signature
/// change.
/// Shared render-time state threaded through every `emit_*` function below
/// (Slice 3): the document's image table (so `Image` boxes can resolve an
/// `ImageId` to its `ImageResource`) and, when rendering under a real
/// [`TtfFontStore`] (`render_html_fixed_ttf_with`), the store itself plus a
/// running set of which physical font FILES (`TtfFontStore::file_index`,
/// not `FontKey` slots — bold/oblique with no configured face dedup to the
/// regular file exactly like the CID PDF writer's own `FontUsage`,
/// `cid.rs`) were actually referenced by an emitted run, so the caller only
/// writes `@font-face` for fonts the document actually used.
///
/// `used_fonts` is a `RefCell` rather than threaded as an extra `&mut`
/// parameter through every recursive call because [`svg::NestedEmitter`]'s
/// callback type has no `Ctx` slot — every callsite instead closes over
/// `ctx: &Ctx` by value (a `&Ctx` copy). This module is single-threaded, so
/// interior mutability here is exactly as safe as (and far less invasive to
/// plumb than) a threaded `&mut BTreeSet`.
/// Serialize typeset pages to a single, self-contained HTML document, using
/// generic system-font fallback (Slice 1/2 behavior, unchanged): no
/// `@font-face` block, every run styled by the plain `.run` CSS class. This
/// is the base-14 twin of [`rustyfi_pdf::render_pdf_with`] — pass
/// [`render_html_fixed_ttf_with`] a real [`TtfFontStore`] instead when the
/// document was typeset against real embedded fonts, for metric-faithful
/// output (Slice 3, see this module's doc comment).
///
/// Argument-for-argument with [`rustyfi_pdf::render_pdf_with`] (`lib.rs:459`):
/// `geometry` (reads only `paper_width`/`paper_height`, exactly like the PDF
/// writer), `pages` (the post-page-break `Vec<PlacedLine>` per `Page`),
/// `images` (the document-wide image table — `Image` boxes resolve their
/// `ImageId` against it, Slice 3), and `extras` (Slice 4: `page_graphics` is
/// now rendered as a per-page `<svg>` underlay, see `render_html_impl`;
/// `annotations`/`outline`/`doc_info` remain a documented gap — there is no
/// HTML analogue of a PDF `/Annots`/`/Outlines` tree in this Option-A
/// serializer).
///
/// One `<div class="page">` per `Page`, sized to `paper_width`/`paper_height`
/// in CSS `pt` (1:1 with SATySFi's own point unit). Inside, every
/// `PureHorzBox::InnerString` run on a `PlacedLine` becomes one
/// absolutely-positioned `<span>` at its resolved `(x, y)` — SATySFi's page
/// coordinates are already y-**down** from the paper top
/// (`PlacedLine`'s own doc comment, `pagebreak.rs:13`), which is exactly
/// CSS's `top` convention, so unlike the PDF writer this needs **no y-flip**.
/// Same as [`render_html_fixed`], but rendering under a real [`TtfFontStore`] —
/// the HTML twin of [`rustyfi_pdf::render_pdf_ttf_with`] (`cid.rs`). Every text
/// and math run's `<span>` gets an explicit `font-family` naming the
/// `@font-face` embedding this function adds to the `<style>` block for
/// every physical font file the document actually referenced, so the
/// browser lays text out in the SAME face whose metrics the layout was
/// computed with (the design doc's §Risks "font-metric fidelity" mitigation,
/// Slice 3's whole point).
/// Emit one already-placed `PureHorzBox` at absolute page coordinates
/// `(tx, ty)` — `tx` the box's left edge, `ty` its **baseline**, both in
/// SATySFi's own y-down page space (no flip needed for HTML/CSS, unlike the
/// PDF writer's `emit_box`, `lib.rs:604`, which this mirrors in shape).
///
/// Slice 1 handled only `InnerString` (a positioned `<span>`, via
/// [`emit_run`]) and `OuterEmpty`/`FixedEmpty` (inter-word glue/skips —
/// already fully accounted for by the caller's `dx` offsets, so they render
/// nothing extra). Slice 2 added `Graphics` (inline SVG, via
/// [`svg::emit_graphics`]) and the three composite recursions the PDF
/// writer's own `emit_box` has (`Tabular`/`EmbeddedBlock`/`Frame`,
/// `lib.rs:646-671`) so nested content inside them renders too. Slice 3
/// adds `Image` (an `<img>` data URI, via [`image::data_uri`]) and `Math`
/// (per-glyph `<span>`s through the same [`emit_run`] path, plus `rules`
/// through [`svg::emit_graphics`] — see the design doc's math row: the
/// semantic tree is already flattened by `read_math` by the time a box
/// exists, so this needs no math-specific rendering beyond reusing the
/// text/SVG paths). The remaining zero-width markers still hit the wildcard
/// arm — exactly `emit_box`'s own `_ => {}` (`lib.rs:672`).
/// Stack an `EmbeddedBlock`'s already-broken `block` lines from its placed
/// anchor `(tx, ty)`, the HTML twin of `rustyfi-pdf`'s `place_embedded_block`
/// (`lib.rs:226`). **Sign differs from the PDF version on purpose**: a
/// `VertBox`/`PlacedLine`'s own `baseline_y` grows DOWNWARD (the same
/// page-down convention this whole module uses, `pagebreak.rs:13`), so here
/// each later line's growing `baseline_y` delta is ADDED to the page-down
/// `ty` (moving further down the page) — the PDF version SUBTRACTS the same
/// delta because ITS `ty` lives in PDF's y-**up** space, where "further
/// down" means a smaller value.
/// One `InnerString`/`Math`-glyph-shaped run: an absolutely-positioned
/// `<span>` at its top-left corner `(tx, top)`, sized in CSS `pt` (1:1 with
/// SATySFi points) via `info.size`, with `text` HTML-escaped. Slice 3: when
/// `ctx` carries a real [`TtfFontStore`] (`render_html_fixed_ttf_with`), the span
/// gets an explicit inline `font-family` naming the `@font-face` this
/// document's `<style>` block embeds for `info.font`'s backing file
/// (`Ctx::font_family_for`, which also records the file as used); in
/// base-14 mode (`ctx.fonts` is `None`) this stays Slice 1's behavior
/// exactly — no inline `font-family`, so the `.run` CSS class's generic
/// system serif default applies.
/// Escape the five HTML/attribute-hostile characters. Slice 1's `<span>`
/// text is never re-parsed as markup, so this is the standard minimal set
/// (no need for a full entity table).