Skip to main content

rapiddoc_core/
document.rs

1//! PDF 事实层的自有数据契约;不把原生阅读顺序误称为最终版面顺序。
2
3use crate::Rect;
4use serde::{Deserialize, Serialize};
5
6/// 错误以外的能力诊断,页号为原始文档的零基索引。
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Diagnostic {
9    pub code: String,
10    pub page_index: usize,
11    pub message: String,
12}
13
14/// 单字符原始 PDFium 事实,字框和原点为 PDF 原生左下原点 point。
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct Character {
17    pub index: usize,
18    pub unicode: u32,
19    pub is_generated: bool,
20    pub text: Option<String>,
21    pub font_name: String,
22    pub font_size_pt: f64,
23    pub angle_degrees: f64,
24    pub origin: [f64; 2],
25    pub tight_bounds: Rect,
26    pub loose_bounds: Rect,
27    pub visual_tight_bounds: Rect,
28    pub visual_loose_bounds: Rect,
29    pub visual_origin: [f64; 2],
30}
31
32/// 已释放 PDFium 页句柄后仍可安全传递的页面事实。
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct Page {
35    pub page_index: usize,
36    pub width_pt: f64,
37    pub height_pt: f64,
38    pub text: String,
39    pub object_count: usize,
40    pub characters: Vec<Character>,
41    pub page_bounds: Rect,
42    pub rotation: u16,
43    pub visual_width_pt: f64,
44    pub visual_height_pt: f64,
45    pub paths: Vec<VectorPath>,
46    pub images: Vec<ImageFact>,
47    pub links: Vec<LinkFact>,
48    pub form_count: usize,
49    pub drawing_lines: Vec<DrawingLine>,
50}
51
52/// 表格规则可用的轴线候选;bounds 为经过页面与 Form clip 的视觉 point 框。
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct DrawingLine {
55    pub start: [f64; 2],
56    pub end: [f64; 2],
57    pub bounds: Rect,
58    pub width: f64,
59    pub orientation: String,
60}
61
62/// 路径片段保留控制点和闭合标记,point 为累积 Form 变换后的原生 PDF 坐标。
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct PathSegment {
65    pub kind: String,
66    pub point: [f64; 2],
67    pub local_point: [f64; 2],
68    pub close: bool,
69}
70
71/// 原始矢量路径事实;clip_bounds 是保守矩形,不冒充任意形状的精确裁剪。
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct VectorPath {
74    pub object_path: Vec<usize>,
75    pub bounds: Rect,
76    pub visual_bounds: Rect,
77    pub evidence_bounds: Option<Rect>,
78    pub clip_bounds: Option<Rect>,
79    pub matrix: [f64; 6],
80    pub segments: Vec<PathSegment>,
81    pub fill_rule: String,
82    pub fill_rgba: [u8; 4],
83    pub stroke: bool,
84    pub stroke_rgba: [u8; 4],
85    pub stroke_width_local: f64,
86    pub fill_visible: bool,
87    pub stroke_visible: bool,
88}
89
90/// 图片边界事实,不触发位图解码或把未导出的图片伪装成资产。
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ImageFact {
93    pub object_path: Vec<usize>,
94    pub bounds: Rect,
95    pub visual_bounds: Rect,
96    pub clip_bounds: Option<Rect>,
97}
98
99/// 链接注解的原始框与 URI;内部跳转的 uri 为 None,保留注解记录。
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct LinkFact {
102    pub index: usize,
103    pub bounds: Rect,
104    pub visual_bounds: Rect,
105    pub uri: Option<String>,
106}
107
108/// 当前版本的 PDF 事实集合;字符坐标明确保留原生坐标系。
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct Document {
111    pub schema_version: u32,
112    pub coordinate_system: String,
113    pub source_page_count: usize,
114    pub pages: Vec<Page>,
115    pub diagnostics: Vec<Diagnostic>,
116}