Skip to main content

normordis_pdf/
lib.rs

1//! # normordis-pdf
2//!
3//! Institutional PDF generation library for NORMAXIS mini-apps.
4//!
5//! Generates professional PDF documents for Portuguese public administration,
6//! with support for:
7//! - Flow and Fixed Box layout modes
8//! - NORMAXIS Canonical Rich Text Format (NCRTF v1.1)
9//! - NORMAXIS Document Template format (NDT v1.3.0)
10//! - Named paragraph/table styles with inheritance (equivalent to Word Styles)
11//! - Tab stops (left, right, center, decimal) with leader characters
12//! - TTF/OTF font loading with real glyph metrics (rustybuzz + ttf-parser)
13//! - Left, Justify, Center, Right text alignment
14//!
15//! ## Quick Start
16//!
17//! ```rust
18//! use normordis_pdf::{DocumentBuilder, Section, Paragraph, TextAlign};
19//!
20//! let pdf = DocumentBuilder::new("Annual Report")
21//!     .push(Section::new("1. Introduction", 1))
22//!     .push(Paragraph::new("Document body text.").align(TextAlign::Justify))
23//!     .render_to_bytes()?;
24//! # Ok::<(), normordis_pdf::NormaxisPdfError>(())
25//! ```
26//!
27//! ## Named Styles
28//!
29//! ```rust
30//! use normordis_pdf::{DocumentBuilder, Paragraph, Section};
31//!
32//! let pdf = DocumentBuilder::new("Styled Document")
33//!     .push(Section::new("Introduction", 1))
34//!     .push(Paragraph::new("Caption text.").style("caption"))
35//!     .render_to_bytes()?;
36//! # Ok::<(), normordis_pdf::NormaxisPdfError>(())
37//! ```
38//!
39//! ## NDT Templates
40//!
41//! ```rust
42//! use normordis_pdf::DocumentBuilder;
43//!
44//! let data = r#"{"ndt_data":"1.0.0","data":{"entity":"Câmara Municipal"}}"#;
45//! let template = r#"{"ndt":"1.0.0","body":[{"type":"paragraph","text":"{{entity}}"}]}"#;
46//!
47//! let pdf = DocumentBuilder::new("Ofício")
48//!     .push_ndt(template, data)?
49//!     .render_to_bytes()?;
50//! # Ok::<(), normordis_pdf::NormaxisPdfError>(())
51//! ```
52
53// ── Modules ───────────────────────────────────────────────────────────────────
54
55pub mod error;
56pub mod styles;
57pub mod fonts;
58pub mod page;
59pub mod backend;
60pub mod document;
61pub mod builder;
62pub mod layout;
63pub mod elements;
64pub mod richtext;
65pub mod template;
66pub mod signing;
67pub mod ndf;
68pub mod compliance;
69#[cfg(feature = "ffi")]
70pub mod ffi;
71
72// ── Error handling ────────────────────────────────────────────────────────────
73
74pub use error::{NormaxisPdfError, Result};
75
76// ── Digital signing ───────────────────────────────────────────────────────────
77
78pub use signing::{PreparedPdf, SignatureConfig, SignatureField, SignatureOptions, sign_pdf};
79
80// ── Styles ────────────────────────────────────────────────────────────────────
81
82pub use styles::{
83    default_named_styles, DocumentStyle, NamedStyle, Orientation, PageSize, ResolvedStyle,
84    RgbColor, SecurityClassification, StyleResolver, TraceabilityMetadata, Watermark,
85};
86
87// ── Fonts ─────────────────────────────────────────────────────────────────────
88
89pub use fonts::{
90    liberation_sans_family, liberation_serif_family, liberation_mono_family,
91    FontData, FontFallbackChain, FontVariants, ShapedGlyph,
92    // v1.3.x backward-compatibility aliases
93    FontFamily, FontVariant,
94    FontRegistry,
95};
96
97// ── Page ─────────────────────────────────────────────────────────────────────
98
99pub use page::PageLayout;
100
101// ── Layout ───────────────────────────────────────────────────────────────────
102
103pub use layout::{
104    AppliedStyle, BorderStyle, BoxBorder, DecorationLine, FixedBox, GlyphUsageTracker,
105    HighlightColor, KnuthPlassOptimizer, LayoutResult, LineBox, LineBreakingMode,
106    LineSegment, OpenTypeFeatures, OverflowPolicy, PageFlow, TabStop, TabStopAlign,
107    TextAlign, TextDecoration, TextLayoutEngine, TextRun, WordBox,
108};
109
110// ── Builder / Document ───────────────────────────────────────────────────────
111
112pub use builder::{DocumentBuilder, SigningBuilder};
113pub use backend::{FontRef, ImageRef, PdfBackend};
114pub use backend::pdf_writer_backend::{
115    encode_for_identity_h, generate_to_unicode_cmap, subset_font, to_cff_if_possible,
116};
117pub use document::{CompressionLevel, Document, PdfStandard};
118
119// ── Elements — Flow ──────────────────────────────────────────────────────────
120
121pub use elements::{
122    footnote::{FootnoteMarkStyle, FootnoteRef, FOOTNOTE_SEPARATOR_THICKNESS_MM},
123    footer::{PageFooter, SectionedFooter},
124    form::{
125        CheckBoxDef, ComboBoxDef, FieldRect, FormField, ListBoxDef,
126        RadioButtonDef, TextFieldDef,
127    },
128    header::{InstitutionalHeader, SectionedHeader},
129    image::ImageElement,
130    list::{BulletList, CheckList, CheckListItem, ListItem, ListItemElement, OrderedList},
131    page_break::PageBreakElement,
132    paragraph::{Paragraph, ParagraphBorder, ParagraphContent},
133    section::Section,
134    section_break::{Orientation as SectionOrientation, SectionBreak, SectionMargins},
135    spacer::{HorizontalRuleElement, Spacer},
136    table::{
137        BorderLineStyle, CellBorder, CellBorders, CellPadding, RowHeight,
138        Table, TableBuilder, TableCell, TableRow, TableStyle,
139    },
140    toc::{TableOfContents, TocEntry},
141    Element, LayoutMode, RenderContext, RenderResult,
142};
143
144// ── Elements — Fixed ─────────────────────────────────────────────────────────
145
146pub use elements::fixed::{FixedImageBox, FixedLineElement, FixedTextBox, ImageFit, VerticalAlign};
147
148// ── Rich text ────────────────────────────────────────────────────────────────
149
150pub use richtext::{ncrtf_to_elements, parse_ncrtf, NcrtfDocument};
151
152// ── Templates ────────────────────────────────────────────────────────────────
153
154pub use template::{
155    parse_ndt, parse_ndt_data, render as render_ndt,
156    serialize_ndt_json, serialize_ndt_toml,
157    NdtDocument, TemplateError,
158    ENGINE_NDT_DATA_VERSION, ENGINE_NDT_VERSION,
159    resolve_runtime_fields, RuntimeContext,
160    check_version_compatibility,
161};
162
163// ── NDT 2.0.0 types ───────────────────────────────────────────────────────────
164
165pub use template::model::{NdtOutput, NdtSignature, NdtSignatureField};
166
167// ── NDT template registry ────────────────────────────────────────────────────
168
169pub use template::{
170    NdtRegistry, NdtTemplateRecord, NdtTemplateSummary, TemplateFilter, TemplateStatus,
171};
172
173// ── NDF pipeline ─────────────────────────────────────────────────────────────
174
175pub use template::{
176    compile_ndt, parse_ndf,
177    render_ndf, render_ndf_with_fonts,
178    render_ndf_prepared_for_signing, render_ndf_prepared_for_signing_with_fonts,
179    verify_ndf, CompileOptions,
180};
181
182// ── NDF types ─────────────────────────────────────────────────────────────────
183
184pub use ndf::{
185    canonical_hash,
186    Actor, AuditEvent, EventType,
187    IntegrityFailure, IntegrityReport,
188    NdfAudit, NdfDocument, NdfEmbeddedFont, NdfIntegrity,
189    NdfMeta, NdfMetaNumbering, NdfOrigin, NdfOutput, NdfRevision, NdfRevisionRef, NdfSignature,
190};
191
192// ── NDF document registry ────────────────────────────────────────────────────
193
194pub use ndf::{
195    NdfFilter, NdfRecord, NdfRecordStatus, NdfRecordSummary, NdfRegistry,
196};
197
198// ── NCRTF 1.3.0 types ────────────────────────────────────────────────────────
199
200pub use richtext::marks::MarkValue as NcrtfMark;
201pub use richtext::model::ImageBlock as NcrtfImage;
202
203// ── Version constants ─────────────────────────────────────────────────────────
204
205/// Version of the normordis-pdf library.
206pub const VERSION: &str = env!("CARGO_PKG_VERSION");
207
208// ── Accessibility / PDF/UA-2 ─────────────────────────────────────────────────
209
210pub use compliance::ua::{
211    AccessibilityConfig, ArtifactType, StructEvent, StructTag, StructureTree,
212    UaError, UaValidator, UaWarning,
213};
214
215/// NDT format version supported by this release.
216pub const NDT_VERSION: &str = "2.1.0";
217
218/// PDF backend crate powering the output engine.
219pub const PDF_BACKEND: &str = "pdf-writer";
220
221/// NDF format version produced by this release.
222pub const NDF_VERSION: &str = ndf::NDF_VERSION;
223
224/// NCRTF format version supported by this release.
225pub const NCRTF_VERSION: &str = richtext::NCRTF_VERSION;
226