pdfox 0.1.0

A pure-Rust PDF library — create, parse, and render PDF documents with zero C dependencies
Documentation
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
/// Multi-page text flow engine.
///
/// Allows a body of text (with styled spans) to be reflowed across multiple
/// pages automatically, with configurable margins, line height, and column layout.
///
/// # Example
/// ```rust,ignore
/// use pdfox::flow::{TextFlow, FlowStyle};
///
/// let flow = TextFlow::new(FlowStyle::default())
///     .heading("Chapter 1: Introduction", 1)
///     .paragraph("Lorem ipsum dolor sit amet...")
///     .paragraph("Another paragraph...");
///
/// // Renders into one or more PageBuilders
/// let pages = flow.render(595.276, 841.890);
/// ```

use crate::color::Color;
use crate::content::TextAlign;
use crate::font::BuiltinFont;
use crate::page::PageBuilder;

/// Style configuration for the flow layout
#[derive(Debug, Clone)]
pub struct FlowStyle {
    pub margin_top: f64,
    pub margin_bottom: f64,
    pub margin_left: f64,
    pub margin_right: f64,

    pub body_font: BuiltinFont,
    pub body_font_size: f64,
    pub body_line_height: f64,
    pub body_color: Color,

    pub h1_font: BuiltinFont,
    pub h1_size: f64,
    pub h1_color: Color,
    pub h1_space_before: f64,
    pub h1_space_after: f64,

    pub h2_font: BuiltinFont,
    pub h2_size: f64,
    pub h2_color: Color,
    pub h2_space_before: f64,
    pub h2_space_after: f64,

    pub h3_font: BuiltinFont,
    pub h3_size: f64,
    pub h3_color: Color,
    pub h3_space_before: f64,
    pub h3_space_after: f64,

    pub paragraph_spacing: f64,
    pub code_font: BuiltinFont,
    pub code_size: f64,
    pub code_bg: Color,
}

impl Default for FlowStyle {
    fn default() -> Self {
        Self {
            margin_top:    60.0,
            margin_bottom: 50.0,
            margin_left:   60.0,
            margin_right:  60.0,

            body_font:        BuiltinFont::TimesRoman,
            body_font_size:   11.0,
            body_line_height: 16.0,
            body_color:       Color::BLACK,

            h1_font:         BuiltinFont::HelveticaBold,
            h1_size:         22.0,
            h1_color:        Color::rgb_u8(20, 50, 120),
            h1_space_before: 20.0,
            h1_space_after:  10.0,

            h2_font:         BuiltinFont::HelveticaBold,
            h2_size:         16.0,
            h2_color:        Color::rgb_u8(40, 70, 140),
            h2_space_before: 14.0,
            h2_space_after:  6.0,

            h3_font:         BuiltinFont::HelveticaBold,
            h3_size:         13.0,
            h3_color:        Color::rgb_u8(60, 90, 160),
            h3_space_before: 10.0,
            h3_space_after:  4.0,

            paragraph_spacing: 8.0,
            code_font:  BuiltinFont::Courier,
            code_size:  9.5,
            code_bg:    Color::rgb_u8(245, 246, 250),
        }
    }
}

/// A styled text span — the atomic unit of content
#[derive(Debug, Clone)]
pub struct Span {
    pub text: String,
    pub font: BuiltinFont,
    pub size: f64,
    pub color: Color,
}

impl Span {
    pub fn text(t: impl Into<String>) -> Self {
        Self {
            text: t.into(),
            font: BuiltinFont::TimesRoman,
            size: 11.0,
            color: Color::BLACK,
        }
    }

    pub fn bold(mut self) -> Self {
        self.font = BuiltinFont::TimesBold;
        self
    }

    pub fn italic(mut self) -> Self {
        self.font = BuiltinFont::TimesItalic;
        self
    }

    pub fn size(mut self, s: f64) -> Self {
        self.size = s;
        self
    }

    pub fn color(mut self, c: Color) -> Self {
        self.color = c;
        self
    }
}

/// A block-level element in the flow
#[derive(Debug, Clone)]
enum Block {
    Heading { text: String, level: u8 },
    Paragraph(Vec<Span>),
    CodeBlock(String),
    /// A hard horizontal rule
    Rule { color: Color, thickness: f64 },
    /// Vertical spacer
    Space(f64),
    /// Bullet list items
    BulletList(Vec<String>),
}

/// The text flow builder
pub struct TextFlow {
    style: FlowStyle,
    blocks: Vec<Block>,
}

impl TextFlow {
    pub fn new(style: FlowStyle) -> Self {
        Self { style, blocks: Vec::new() }
    }

    // ── Content builder methods (chainable) ───────────────────────────────────

    pub fn heading(mut self, text: impl Into<String>, level: u8) -> Self {
        self.blocks.push(Block::Heading { text: text.into(), level: level.clamp(1, 3) });
        self
    }

    pub fn paragraph(mut self, text: impl Into<String>) -> Self {
        self.blocks.push(Block::Paragraph(vec![Span::text(text)]));
        self
    }

    pub fn rich_paragraph(mut self, spans: Vec<Span>) -> Self {
        self.blocks.push(Block::Paragraph(spans));
        self
    }

    pub fn code(mut self, text: impl Into<String>) -> Self {
        self.blocks.push(Block::CodeBlock(text.into()));
        self
    }

    pub fn rule(mut self) -> Self {
        self.blocks.push(Block::Rule {
            color: Color::rgb_u8(200, 210, 220),
            thickness: 0.5,
        });
        self
    }

    pub fn space(mut self, pts: f64) -> Self {
        self.blocks.push(Block::Space(pts));
        self
    }

    pub fn bullets(mut self, items: Vec<impl Into<String>>) -> Self {
        self.blocks.push(Block::BulletList(items.into_iter().map(Into::into).collect()));
        self
    }

    // ── Renderer ─────────────────────────────────────────────────────────────

    /// Render all blocks onto one or more pages.
    /// Returns a Vec of PageBuilders ready to be added to a Document.
    pub fn render(&self, page_width: f64, page_height: f64) -> Vec<PageBuilder> {
        let mut pages: Vec<PageBuilder> = Vec::new();
        let mut current = PageBuilder::new(page_width, page_height);

        // Register font families
        let _body_f = current.use_times();
        let _head_f = current.use_helvetica();
        let _code_f = current.use_courier();

        let text_width = page_width - self.style.margin_left - self.style.margin_right;
        let mut y = page_height - self.style.margin_top;

        let min_y = self.style.margin_bottom;

        macro_rules! new_page {
            () => {{
                pages.push(current);
                current = PageBuilder::new(page_width, page_height);
                let _bf = current.use_times();
                let _hf = current.use_helvetica();
                let _cf = current.use_courier();
                y = page_height - self.style.margin_top;
            }};
        }

        macro_rules! check_space {
            ($needed:expr) => {
                if y - $needed < min_y {
                    new_page!();
                }
            };
        }

        for block in &self.blocks {
            match block {
                Block::Heading { text, level } => {
                    let (font, size, color, space_before, space_after) = match level {
                        1 => (BuiltinFont::HelveticaBold, self.style.h1_size, self.style.h1_color,
                              self.style.h1_space_before, self.style.h1_space_after),
                        2 => (BuiltinFont::HelveticaBold, self.style.h2_size, self.style.h2_color,
                              self.style.h2_space_before, self.style.h2_space_after),
                        _ => (BuiltinFont::HelveticaBold, self.style.h3_size, self.style.h3_color,
                              self.style.h3_space_before, self.style.h3_space_after),
                    };

                    let key = self.heading_key(*level);
                    check_space!(space_before + size + space_after);

                    y -= space_before;

                    // Heading text
                    current.content_stream()
                        .draw_text(text, self.style.margin_left, y, &key, font, size, color, TextAlign::Left);
                    y -= size;

                    // Underline for h1
                    if *level == 1 {
                        let line_w = font.string_width(text, size);
                        current.content_stream()
                            .line(self.style.margin_left, y,
                                  self.style.margin_left + line_w, y,
                                  color, 0.8);
                        y -= 3.0;
                    }

                    y -= space_after;
                }

                Block::Paragraph(spans) => {
                    // Merge all span text for word-wrapping
                    // For full rich text we'd need a per-span layout engine;
                    // for now we flow by word with per-span font switching
                    let merged: String = spans.iter().map(|s| s.text.as_str()).collect::<Vec<_>>().join("");
                    let font = self.style.body_font;
                    let key = self.body_key();
                    let size = self.style.body_font_size;
                    let lh = self.style.body_line_height;

                    // Split into words, measure per word
                    let words: Vec<&str> = merged.split_whitespace().collect();
                    if words.is_empty() {
                        y -= self.style.paragraph_spacing;
                        continue;
                    }

                    let space_w = font.char_width(' ') * size / 1000.0;
                    let mut line_words: Vec<&str> = Vec::new();
                    let mut line_w = 0.0;

                    let mut lines: Vec<String> = Vec::new();
                    for word in &words {
                        let ww = font.string_width(word, size);
                        if line_words.is_empty() {
                            line_words.push(word);
                            line_w = ww;
                        } else if line_w + space_w + ww <= text_width {
                            line_words.push(word);
                            line_w += space_w + ww;
                        } else {
                            lines.push(line_words.join(" "));
                            line_words = vec![word];
                            line_w = ww;
                        }
                    }
                    if !line_words.is_empty() {
                        lines.push(line_words.join(" "));
                    }

                    // Draw lines, breaking to new page as needed
                    for line in &lines {
                        check_space!(lh);
                        current.content_stream()
                            .draw_text(line, self.style.margin_left, y, &key, font, size,
                                       self.style.body_color, TextAlign::Left);
                        y -= lh;
                    }

                    y -= self.style.paragraph_spacing;
                }

                Block::CodeBlock(code) => {
                    let font = self.style.code_font;
                    let key = self.code_key();
                    let size = self.style.code_size;
                    let lh = size + 3.0;
                    let pad = 8.0;

                    let code_lines: Vec<&str> = code.lines().collect();
                    let block_h = (code_lines.len() as f64) * lh + pad * 2.0;

                    check_space!(block_h);

                    // Background
                    current.content_stream()
                        .filled_rect(self.style.margin_left - pad, y - block_h + pad,
                                     text_width + pad * 2.0, block_h,
                                     self.style.code_bg);
                    current.content_stream()
                        .stroked_rect(self.style.margin_left - pad, y - block_h + pad,
                                      text_width + pad * 2.0, block_h,
                                      Color::rgb_u8(200, 210, 220), 0.5);

                    y -= pad;
                    for line in &code_lines {
                        check_space!(lh);
                        current.content_stream()
                            .draw_text(line, self.style.margin_left, y, &key, font, size,
                                       Color::rgb_u8(40, 40, 120), TextAlign::Left);
                        y -= lh;
                    }
                    y -= pad + self.style.paragraph_spacing;
                }

                Block::Rule { color, thickness } => {
                    check_space!(10.0);
                    y -= 4.0;
                    current.content_stream()
                        .line(self.style.margin_left, y,
                              page_width - self.style.margin_right, y,
                              *color, *thickness);
                    y -= 6.0;
                }

                Block::Space(pts) => {
                    y -= pts;
                    if y < min_y { new_page!(); }
                }

                Block::BulletList(items) => {
                    let font = self.style.body_font;
                    let key = self.body_key();
                    let size = self.style.body_font_size;
                    let lh = self.style.body_line_height;
                    let indent = self.style.margin_left + 18.0;
                    let bullet_x = self.style.margin_left + 6.0;
                    let item_width = text_width - 18.0;

                    for item in items {
                        let words: Vec<&str> = item.split_whitespace().collect();
                        if words.is_empty() { continue; }

                        let space_w = font.char_width(' ') * size / 1000.0;
                        let mut lines: Vec<String> = Vec::new();
                        let mut line_words: Vec<&str> = Vec::new();
                        let mut line_w = 0.0;

                        for word in &words {
                            let ww = font.string_width(word, size);
                            if line_words.is_empty() {
                                line_words.push(word);
                                line_w = ww;
                            } else if line_w + space_w + ww <= item_width {
                                line_words.push(word);
                                line_w += space_w + ww;
                            } else {
                                lines.push(line_words.join(" "));
                                line_words = vec![word];
                                line_w = ww;
                            }
                        }
                        if !line_words.is_empty() {
                            lines.push(line_words.join(" "));
                        }

                        check_space!(lh * lines.len() as f64);

                        // Bullet dot
                        current.content_stream()
                            .draw_text("", bullet_x, y, &key, font, size,
                                       self.style.body_color, TextAlign::Left);

                        for (li, line) in lines.iter().enumerate() {
                            if li > 0 { check_space!(lh); }
                            current.content_stream()
                                .draw_text(line, indent, y, &key, font, size,
                                           self.style.body_color, TextAlign::Left);
                            y -= lh;
                        }
                    }
                    y -= self.style.paragraph_spacing;
                }
            }
        }

        pages.push(current);
        pages
    }

    // ── Font key helpers (must match registration order in render()) ─────────

    fn body_key(&self) -> String { "F1Reg".into() }   // use_times -> F1
    fn heading_key(&self, _level: u8) -> String { "F2Reg".into() }  // use_helvetica -> F2
    fn code_key(&self) -> String { "F3Reg".into() }    // use_courier -> F3
}