liteparse 2.9.0

Fast, lightweight PDF and document parsing with spatial text extraction
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use serde::Serialize;
use std::collections::{BTreeMap, HashMap};

#[doc(hidden)]
#[derive(Debug, Clone)]
pub enum PdfInput {
    /// Path to a PDF file on disk.
    Path(String),
    /// Raw PDF bytes (e.g. from a network response or in-memory buffer).
    Bytes(Vec<u8>),
}

/// Represents a single text item extracted from a PDF page,
/// including its content, position, size, rotation, and font metadata.
#[derive(Debug, Clone, Default, Serialize)]
pub struct TextItem {
    pub text: String,
    /// Viewport-space coordinates (top-left origin, 72 DPI).
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    /// Rotation in degrees (counter-clockwise, adjusted for page rotation).
    pub rotation: f32,
    pub font_name: Option<String>,
    pub font_size: Option<f32>,
    /// Font size * scale_y from the text matrix — accounts for CTM scaling.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_height: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_ascent: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_descent: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_weight: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_flags: Option<i32>,
    /// Sum of glyph widths (using charcode-based lookup when possible).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_width: Option<f32>,
    /// Whether the font has buggy encoding (private-use codepoints, TT subset, etc.)
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub font_is_buggy: bool,
    /// Whether most characters in this item could not be mapped to Unicode
    /// (e.g. a Type3 font with no ToUnicode map). The text content is
    /// PDFium's char-code fallback and does not reflect the rendered glyphs.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub has_unicode_map_error: bool,
    /// Marked content ID from the PDF structure tree.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mcid: Option<i32>,
    /// Fill color as ARGB hex string (e.g. "ff000000").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fill_color: Option<String>,
    /// Stroke color as ARGB hex string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_color: Option<String>,
    /// Raw character codes from the PDF content stream. These correspond to
    /// source glyphs rather than Unicode scalar values, so ligature expansion
    /// can produce more text characters than entries in this array.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub char_codes: Vec<u32>,
    /// Whether the trailing source space was synthesized by PDFium rather than
    /// represented by a real space glyph in the PDF content stream.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub trailing_space_generated: bool,
    /// OCR confidence score (0.0–1.0). None for native PDF text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confidence: Option<f32>,
    /// Target URI when this item falls inside a hyperlink annotation's
    /// rectangle. Populated in `extract.rs`; consumed by the markdown emitter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link: Option<String>,
    /// Whether a thin horizontal stroke/rect crosses this item's vertical middle
    /// band (a strikethrough line). Populated in `extract.rs`; consumed by the
    /// markdown emitter to wrap the text in `~~…~~`.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub strike: bool,
    /// Per-word sub-boxes within this item, split on the inter-word spaces seen
    /// during segment building. A segment groups several words together (it only
    /// breaks at line/column boundaries), so this exposes the finer word-level
    /// geometry needed for bbox attribution. Empty for items that produced no
    /// word split (e.g. OCR-sourced or single-token items). Internal/attribution
    /// use only — `#[serde(skip)]` keeps it out of the JSON output but it is
    /// marshalled across the napi boundary.
    #[serde(skip)]
    pub words: Vec<WordBox>,
}

/// The `TextItem` fields governed by `extract_text_metadata`, pre-gated by
/// [`TextItem::text_metadata`]. This struct defines which fields count as
/// rich text metadata: every output surface (CLI JSON, napi, python, wasm)
/// builds its public items from it, so a new metadata field added here is a
/// compile error in each surface until it is wired through.
///
/// All fields are `Option`/slice so "extraction disabled" is representable
/// even for the plain-`bool` fields on `TextItem`: `None`/empty means the
/// caller did not opt in and the field must be omitted from output.
pub struct TextMetadata<'a> {
    pub font_height: Option<f32>,
    pub font_ascent: Option<f32>,
    pub font_descent: Option<f32>,
    pub font_weight: Option<i32>,
    pub text_width: Option<f32>,
    pub font_is_buggy: Option<bool>,
    pub mcid: Option<i32>,
    pub fill_color: Option<&'a str>,
    pub stroke_color: Option<&'a str>,
    pub char_codes: Option<&'a [u32]>,
    pub trailing_space_generated: Option<bool>,
}

impl TextItem {
    /// The rich-metadata view of this item: real values when `enabled`, all
    /// absent otherwise. See [`TextMetadata`].
    pub fn text_metadata(&self, enabled: bool) -> TextMetadata<'_> {
        if enabled {
            TextMetadata {
                font_height: self.font_height,
                font_ascent: self.font_ascent,
                font_descent: self.font_descent,
                font_weight: self.font_weight,
                text_width: self.text_width,
                font_is_buggy: Some(self.font_is_buggy),
                mcid: self.mcid,
                fill_color: self.fill_color.as_deref(),
                stroke_color: self.stroke_color.as_deref(),
                char_codes: Some(&self.char_codes),
                trailing_space_generated: Some(self.trailing_space_generated),
            }
        } else {
            TextMetadata {
                font_height: None,
                font_ascent: None,
                font_descent: None,
                font_weight: None,
                text_width: None,
                font_is_buggy: None,
                mcid: None,
                fill_color: None,
                stroke_color: None,
                char_codes: None,
                trailing_space_generated: None,
            }
        }
    }
}

/// One word's bounding box within a `TextItem`, in the same viewport space
/// (top-left origin, 72 DPI) as the parent item. `text` is the word's content
/// with inter-word spaces excluded.
#[derive(Debug, Clone, Default, Serialize)]
pub struct WordBox {
    pub text: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

#[doc(hidden)]
#[derive(Debug, Serialize)]
pub struct Page {
    pub page_number: usize,
    pub page_width: f32,
    pub page_height: f32,
    /// Union bbox of the page's top-level content objects in viewport
    /// coords (visible content extent). `None` for empty pages.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_bounds: Option<Rect>,
    pub text_items: Vec<TextItem>,
    /// Vector graphics on the page, distilled from PDFium path objects.
    /// Not emitted in JSON/text outputs — consumed by the markdown layout pass.
    #[serde(skip)]
    pub graphics: Vec<GraphicPrimitive>,
    /// Lossless-enough, PDFium-compatible path output requested by the caller.
    /// Kept separate from the lossy internal `graphics` layout primitives.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vector_graphics: Option<VectorGraphics>,
    /// Structure-tree nodes for this page when the PDF is tagged. Each node
    /// carries its role, marked-content ids, and the union bbox of its tagged
    /// content. Empty for untagged PDFs.
    #[serde(skip)]
    pub struct_nodes: Vec<StructNode>,
    /// Raster image objects detected on the page. Empty when the page has no
    /// images. Threaded through to `ParsedPage.image_refs`.
    #[serde(skip)]
    pub image_refs: Vec<ImageRef>,
    /// Public annotation data when explicitly requested. `None` distinguishes
    /// disabled extraction from an enabled page with no annotations.
    #[serde(skip)]
    pub annotations: Option<Vec<DocumentAnnotation>>,
    /// AcroForm widgets when explicitly requested.
    #[serde(skip)]
    pub form_fields: Option<Vec<FormField>>,
    /// Tagged-PDF logical structure when explicitly requested.
    #[serde(skip)]
    pub structure_tree: Option<StructureTree>,
}

/// One PDF page annotation. Coordinates use the same top-left, 72-DPI
/// viewport space as [`TextItem`].
#[derive(Debug, Clone, Serialize)]
pub struct DocumentAnnotation {
    pub subtype: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contents: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modified: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rect: Option<Rect>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub quadpoint_rects: Vec<Rect>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

/// Scalar value from a tagged-PDF structure element's `/A` dictionary.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(untagged)]
pub enum StructureAttributeValue {
    Boolean(bool),
    Number(f32),
    String(String),
}

/// A complete page-scoped tagged-PDF logical structure tree.
#[derive(Debug, Clone, Serialize)]
pub struct StructureTree {
    pub roots: Vec<StructureTreeElement>,
}

/// One tagged-PDF structure element. Field names follow the
/// repository's snake_case JSON convention rather than PDFium's C spellings.
#[derive(Debug, Clone, Serialize)]
pub struct StructureTreeElement {
    #[serde(rename = "type")]
    pub element_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actual_text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alt_text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub attributes: BTreeMap<String, StructureAttributeValue>,
    pub marked_content_ids: Vec<i32>,
    pub children: Vec<StructureTreeElement>,
    pub annotations: Vec<DocumentAnnotation>,
}

/// One AcroForm widget and its resolved field metadata.
#[derive(Debug, Clone, Serialize)]
pub struct FormField {
    pub id: String,
    #[serde(rename = "type")]
    pub field_type: String,
    pub page: u32,
    pub annotation_index: i32,
    pub widget_index: i32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub object_number: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alternate_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub export_value: Option<String>,
    pub field_flags: i32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub control_count: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub control_index: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub checked: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rect: Option<Rect>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub options: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub selected_options: Vec<String>,
}

/// One raw packet from an XFA form document's `/XFA` array. Surfaced on
/// `ParseResult.xfa_packets` when `extract_xfa_packets` is enabled.
#[derive(Debug, Clone, Serialize)]
pub struct XfaPacket {
    /// Zero-based index in the XFA array.
    pub index: u32,
    /// Packet name (e.g. `template`, `datasets`), when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Decoded content length in bytes.
    pub content_length: u32,
    /// Packet content (usually XML), lossily decoded as UTF-8. `None` when
    /// the packet stream could not be read.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,
}

/// One solid rectangle (or thick line) detected in a rendered page bitmap.
/// Coordinates are in the same top-left, 72-DPI viewport space as text
/// items. Detection runs on the raster, so it also covers scanned/flattened
/// pages that carry no vector paths.
#[derive(Debug, Clone, Serialize)]
pub struct ScreenshotRect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    /// Fill color as ARGB hex string (e.g. "ff1a2b3c").
    pub color: String,
    /// True when only one dimension reaches the minimum rectangle size
    /// (a solid line rather than a filled area).
    pub is_line: bool,
}

/// One entry in the document outline (bookmarks). Coordinates are in PDF
/// user space (origin bottom-left) — convert to viewport with
/// `page_height - y` once you know the page.
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct OutlineTarget {
    /// Hierarchy depth, 1-based.
    pub level: u8,
    pub title: String,
    /// Zero-based page index of the destination.
    pub page_index: i32,
    /// Y in PDF user space, top of the target location. `None` when the
    /// destination doesn't specify a Y.
    pub y_pdf: Option<f32>,
}

/// One node from the structure tree of a page. Pre-flattened in pre-order
/// (parent before children).
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct StructNode {
    pub role: String,
    pub mcids: Vec<i32>,
    /// Union bbox of page objects tagged with `mcids`, in viewport coords.
    /// `None` when none of the mcids resolved to a bbox.
    pub bbox: Option<Rect>,
    pub alt_text: Option<String>,
}

/// Represents a fully parsed page with projected text layout.
#[derive(Debug, Serialize)]
pub struct ParsedPage {
    pub page_number: usize,
    pub page_width: f32,
    pub page_height: f32,
    /// Union bbox of the page's top-level content objects in viewport
    /// coords (visible content extent). `None` for empty pages.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_bounds: Option<Rect>,
    pub text: String,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub markdown: String,
    pub text_items: Vec<TextItem>,
    /// Per-line structural metadata used by the markdown emitter. Not part of
    /// the JSON/text outputs (consumed internally) so it is `#[serde(skip)]`.
    #[serde(skip)]
    pub projected_lines: Vec<ProjectedLine>,
    /// Root of the XY-cut region tree for this page. Leaves correspond to the
    /// `region_path` on each `ProjectedLine`. Internal-only.
    #[serde(skip)]
    pub regions: Region,
    /// Vector graphics on the page (decomposed paths) used by the markdown
    /// emitter for ruled-table / HR / figure-cluster detection. Not part of
    /// the JSON/text output.
    #[serde(skip)]
    pub graphics: Vec<GraphicPrimitive>,
    /// Public vector path extraction. Absent unless explicitly enabled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vector_graphics: Option<VectorGraphics>,
    /// Figure-region bounding rectangles derived from `graphics`. Pre-computed
    /// in `to_parsed_pages` so the XY-cut layout pass can treat them as
    /// obstacles, and reused downstream for figure classification.
    #[serde(skip)]
    pub figures: Vec<Rect>,
    /// Structure-tree nodes for this page (tagged PDFs only). Pre-flattened in
    /// pre-order. Consumed by the markdown classifier for highest-priority
    /// heading / figure / table detection.
    #[serde(skip)]
    pub struct_nodes: Vec<StructNode>,
    /// Raster image objects on the page. Bbox in viewport coords. Populated
    /// during extraction; consumed by the markdown emitter to interleave
    /// `Block::Figure` references at the right y position. Empty when the
    /// page has no embedded images. Not part of JSON/text output.
    #[serde(skip)]
    pub image_refs: Vec<ImageRef>,
    /// Per-page complexity signals (the same the `is_complex` API returns).
    /// Populated only when `LiteParseConfig::include_complexity` is set;
    /// `None` otherwise. Surfaced as a per-page `complexity` object in JSON.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub complexity: Option<crate::ocr_merge::PageComplexityStats>,
    /// Page annotations when `LiteParseConfig::extract_annotations` is true.
    /// `None` means extraction was disabled; `Some([])` means enabled with no
    /// annotations on this page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<Vec<DocumentAnnotation>>,
    /// AcroForm widgets when `extract_form_fields` is true.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub form_fields: Option<Vec<FormField>>,
    /// Tagged-PDF logical structure when `extract_structure_tree` is true.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub structure_tree: Option<StructureTree>,
}

/// One embedded raster image on a page. `id` is a stable, page-scoped slug
/// used as the markdown link target (e.g. `img_p1_1.png`). `obj_index` is
/// the image's position among image page-objects, so a later embed pass can
/// re-open the document and pull pixel bytes with `render_image_object`.
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct ImageRef {
    pub id: String,
    pub bbox: Rect,
    pub obj_index: usize,
    pub format: String,
    pub pixel_width: u32,
    pub pixel_height: u32,
    pub rotation: f32,
    pub jpeg_bytes: Option<Vec<u8>>,
    pub raw_bytes: Option<Vec<u8>>,
    pub bits_per_pixel: u32,
    pub colorspace: i32,
}

/// A raster image extracted from a page along with its pixel bytes. Surfaced
/// on `ParseResult.images` only when `extract_images` is enabled; otherwise
/// the extraction step skips the render and only `ImageRef`s are
/// produced. JPEG streams are preserved without re-encoding when PDFium
/// exposes a valid directly decoded DCT stream; other images are encoded as
/// PNG from PDFium's rendered bitmap.
#[derive(Debug, Clone, Serialize)]
pub struct ExtractedImage {
    pub id: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    pub page: u32,
    pub bbox: Rect,
    pub width: u32,
    pub height: u32,
    pub rotation: f32,
    pub format: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duplicate_of: Option<String>,
    /// Encoded image bytes. Shared (`Arc`) so duplicate entries reference the
    /// canonical image's buffer instead of copying it per occurrence.
    #[serde(skip)]
    pub bytes: std::sync::Arc<Vec<u8>>,
}

#[doc(hidden)]
#[derive(Debug, Clone, Default, Serialize)]
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

/// Page-scoped vector path output. Coordinates use the same top-left,
/// 72-DPI viewport space as text items.
#[derive(Debug, Clone, Default, Serialize)]
pub struct VectorGraphics {
    pub shapes: Vec<VectorShape>,
    pub lines: Vec<VectorLine>,
}

/// One PDF path object's paint state and viewport bounding box.
#[derive(Debug, Clone, Serialize)]
pub struct VectorShape {
    pub bbox: Rect,
    pub stroke: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_color: Option<String>,
    pub fill: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fill_color: Option<String>,
    pub has_curve: bool,
}

/// A strict horizontal or vertical path segment after adjacent compatible
/// segments have been merged, matching LlamaParse PDFium path semantics.
#[derive(Debug, Clone, Serialize)]
pub struct VectorLine {
    pub x1: f32,
    pub y1: f32,
    pub x2: f32,
    pub y2: f32,
    pub stroke: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_width: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_color: Option<String>,
    pub fill: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fill_color: Option<String>,
}

/// Lightweight vector-graphic primitive derived from PDFium path objects.
/// Only the shapes useful to the markdown emitter (ruled tables, HRs, figure
/// clusters) are kept — bezier curves and complex paths are decomposed into
/// straight strokes, or dropped.
#[doc(hidden)]
#[derive(Debug, Clone)]
pub enum GraphicPrimitive {
    /// A single straight line segment in viewport coords. Used for HR/table
    /// border detection.
    Stroke {
        x1: f32,
        y1: f32,
        x2: f32,
        y2: f32,
        color: Option<String>,
        width: f32,
    },
    /// An axis-aligned rectangle — typically a filled cell background, banner,
    /// or fully-stroked table border drawn as a single path.
    Rect {
        bbox: Rect,
        fill: Option<String>,
        stroke: Option<String>,
    },
}

impl GraphicPrimitive {
    /// Bbox of the primitive in viewport coords.
    pub fn bbox(&self) -> Rect {
        match self {
            GraphicPrimitive::Stroke { x1, y1, x2, y2, .. } => {
                let x = x1.min(*x2);
                let y = y1.min(*y2);
                Rect {
                    x,
                    y,
                    width: (x2 - x1).abs(),
                    height: (y2 - y1).abs(),
                }
            }
            GraphicPrimitive::Rect { bbox, .. } => bbox.clone(),
        }
    }
}

/// Per-line structural metadata derived during grid projection. Used by the
/// markdown emitter; not surfaced in JSON/text output.
#[doc(hidden)]
#[derive(Debug, Clone, Serialize)]
pub struct ProjectedLine {
    pub text: String,
    pub bbox: Rect,
    pub anchor: Anchor,
    pub indent_x: f32,
    pub dominant_font_size: f32,
    /// True when `dominant_font_size` was derived from bbox height (PDFium
    /// reported the font size baked into the text matrix, ~1.0) rather than a
    /// real font-size value. Height-derived sizes jitter ±1pt line-to-line
    /// based on glyph content (descenders, parens, capitals), so heading
    /// detection must use a wider margin over body for these lines.
    pub font_size_is_estimated: bool,
    /// Precise matrix-derived size (`Tf_size × text_matrix_scale`) for
    /// matrix-baked-size lines, when a glyph exposed a text matrix. Used
    /// *only* by heading detection (body-size + heading map), where the
    /// jitter-free value beats the bbox-height estimate in `dominant_font_size`.
    /// Deliberately NOT consumed by table/paragraph grouping, which stay on
    /// `dominant_font_size` to avoid perturbing well-tuned line grouping.
    pub heading_font_size: Option<f32>,
    pub dominant_font_name: Option<String>,
    pub all_bold: bool,
    pub all_italic: bool,
    pub all_mono: bool,
    pub all_strike: bool,
    pub spans: Vec<TextItem>,
    /// Path from the page's region-tree root to the leaf containing this line.
    /// Equality means "same leaf"; prefix relationship means "one contains the
    /// other". Replaces the prior flat `column_id` scheme so nested layouts
    /// (banded splits with sub-columns) survive paragraph/table grouping.
    pub region_path: Vec<u16>,
    pub mcid: Option<i32>,
    /// True when the line's original-coordinate bbox falls inside a detected
    /// figure/chart region. Chart text (axis labels, legends, category titles)
    /// is often set in a font larger than real headings; if it reached the
    /// heading-size histogram it would hijack the top heading levels and get
    /// promoted itself. Heading detection skips these lines.
    pub in_figure: bool,
}

/// XY-cut region tree node. A page's root region recursively splits along H or
/// V axes until each leaf holds a coherent block of items.
#[doc(hidden)]
#[derive(Debug, Clone, Default)]
pub struct Region {
    pub bbox: Rect,
    pub kind: RegionKind,
}

#[doc(hidden)]
#[derive(Debug, Clone)]
pub enum RegionKind {
    Leaf {
        item_indices: Vec<usize>,
    },
    Split {
        axis: CutAxis,
        children: Vec<Region>,
    },
}

impl Default for RegionKind {
    fn default() -> Self {
        RegionKind::Leaf {
            item_indices: Vec::new(),
        }
    }
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CutAxis {
    Horizontal,
    Vertical,
}

#[doc(hidden)]
#[derive(Debug, Serialize)]
pub enum Snap {
    Left,
    Right,
    Center,
}

#[doc(hidden)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum Anchor {
    Left,
    Right,
    Center,
    /// Inline span that does not snap to a column edge — used by lines whose
    /// dominant items couldn't be classified as Left/Right/Center.
    Floating,
}

#[doc(hidden)]
#[derive(Debug, Serialize)]
pub struct ProjectedTextItem {
    pub item: TextItem,
    pub snap: Snap,
    pub anchor: Anchor,
    pub is_dup: bool,
    pub rendered: bool,
    pub num_spaces: usize,
    pub force_unsnapped: bool,
    pub is_margin_line_number: bool,
    pub rotated: bool,
    pub d: f32,
    pub orig_x: f32,
    pub orig_y: f32,
    pub orig_width: f32,
    pub orig_height: f32,
    pub orig_rotation: f32,
}

#[doc(hidden)]
pub type AnchorMap = HashMap<i32, Vec<(usize, usize)>>;

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_item() -> TextItem {
        TextItem {
            text: "hi".into(),
            x: 1.0,
            y: 2.0,
            width: 10.0,
            height: 4.0,
            font_name: Some("Arial".into()),
            font_size: Some(12.0),
            ..Default::default()
        }
    }

    #[test]
    fn text_item_skips_none_fields() {
        let item = sample_item();
        let s = serde_json::to_string(&item).unwrap();
        assert!(!s.contains("font_height"));
        assert!(!s.contains("confidence"));
        assert!(!s.contains("font_is_buggy"));
        assert!(s.contains("\"text\":\"hi\""));
    }

    #[test]
    fn text_item_includes_buggy_flag_when_true() {
        let mut item = sample_item();
        item.font_is_buggy = true;
        let s = serde_json::to_string(&item).unwrap();
        assert!(s.contains("font_is_buggy"));
    }

    #[test]
    fn page_serializes() {
        let p = Page {
            page_number: 1,
            page_width: 100.0,
            page_height: 200.0,
            content_bounds: None,
            text_items: vec![sample_item()],
            graphics: vec![],
            vector_graphics: None,
            struct_nodes: vec![],
            image_refs: vec![],
            annotations: None,
            form_fields: None,
            structure_tree: None,
        };
        let s = serde_json::to_string(&p).unwrap();
        assert!(s.contains("\"page_number\":1"));
    }

    #[test]
    fn anchor_map_basic() {
        let mut m: AnchorMap = HashMap::new();
        m.entry(5).or_default().push((1, 2));
        assert_eq!(m.get(&5).unwrap()[0], (1, 2));
    }
}