printpdf 0.9.1

Rust library for reading and writing PDF files
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
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
use printpdf::*;

fn main() {
    // Create a new PDF document
    let mut doc = PdfDocument::new("Advanced Text Positioning Example");

    // Create operations for advanced text positioning and styling
    let mut ops = Vec::new();

    // Title for the page
    ops.extend_from_slice(&[
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(280.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::HelveticaBold),
            size: Pt(24.0),
        },
        Op::SetLineHeight { lh: Pt(28.0) },
        Op::SetFillColor {
            col: Color::Rgb(Rgb {
                r: 0.0,
                g: 0.0,
                b: 0.0,
                icc_profile: None,
            }),
        },
        Op::ShowText {
            items: vec![TextItem::Text(
                "Advanced Text Positioning and Styling".to_string(),
            )],
        },
        Op::EndTextSection,
    ]);

    // 1. Text with character spacing
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(260.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::ShowText {
            items: vec![TextItem::Text(
                "1. Normal text without spacing:".to_string(),
            )],
        },
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text("CHARACTERSPACING".to_string())],
        },
        Op::AddLineBreak,
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text(
                "Text with added character spacing:".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Set character spacing to 2.0 points
        Op::SetCharacterSpacing { multiplier: 2.0 },
        Op::ShowText {
            items: vec![TextItem::Text("CHARACTERSPACING".to_string())],
        },
        // Reset character spacing
        Op::SetCharacterSpacing { multiplier: 0.0 },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // 2. Rotated text using TextMatrix
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(230.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::ShowText {
            items: vec![TextItem::Text("2. Rotated text:".to_string())],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // Draw rotated text at different angles
    ops.extend(
        (0..=7)
            .flat_map(|i| {
                let angle = i as f32 * 45.0; // Rotate by 0°, 45°, 90°, 135°, 180°, 225°, 270°, 315°
                vec![
                    Op::SaveGraphicsState,
                    Op::StartTextSection,
                    Op::SetTextCursor {
                        pos: Point::new(Mm(50.0), Mm(210.0)),
                    },
                    Op::SetTextMatrix {
                        matrix: TextMatrix::TranslateRotate(
                            Pt(50.0 + i as f32 * 50.0),
                            Pt(600.0),
                            angle,
                        ),
                    },
                    Op::SetFont {
                        font: PdfFontHandle::Builtin(BuiltinFont::TimesRoman),
                        size: Pt(10.0),
                    },
                    Op::SetLineHeight { lh: Pt(12.0) },
                    Op::SetFillColor {
                        col: Color::Rgb(Rgb {
                            r: 0.0,
                            g: 0.0,
                            b: i as f32 / 7.0,
                            icc_profile: None,
                        }),
                    },
                    Op::ShowText {
                        items: vec![TextItem::Text(format!("{}deg", angle))],
                    },
                    Op::EndTextSection,
                    Op::RestoreGraphicsState,
                ]
            })
            .collect::<Vec<_>>()
            .iter()
            .cloned(),
    );

    // 3. Text on a curved path (simulated with multiple rotations)
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(190.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::ShowText {
            items: vec![TextItem::Text("3. Text on a curved path:".to_string())],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // Create a curve and place text along it (arc)
    let center_x = 300.0;
    let center_y = 460.0;
    let radius = 100.0;
    let text = "Curved Text Around A Circle Path";

    for (i, c) in text.chars().rev().enumerate() {
        let angle = 180.0 - (i as f32 * 8.0);
        let radians = angle.to_radians();
        let x = center_x + radius * radians.cos();
        let y = center_y + radius * radians.sin();

        ops.extend_from_slice(&[
            Op::SaveGraphicsState,
            Op::StartTextSection,
            Op::SetTextCursor {
                pos: Point {
                    x: Pt(0.0),
                    y: Pt(0.0),
                },
            },
            Op::SetTextMatrix {
                matrix: TextMatrix::TranslateRotate(Pt(x), Pt(y), angle + 90.0),
            },
            Op::SetFont {
                font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
                size: Pt(12.0),
            },
            Op::SetLineHeight { lh: Pt(14.0) },
            Op::SetFillColor {
                col: Color::Rgb(Rgb {
                    r: i as f32 / text.len() as f32,
                    g: 0.3,
                    b: 1.0 - i as f32 / text.len() as f32,
                    icc_profile: None,
                }),
            },
            Op::ShowText {
                items: vec![TextItem::Text(c.to_string())],
            },
            Op::EndTextSection,
            Op::RestoreGraphicsState,
        ]);
    }

    // 4. Kerned text with manual spacing adjustments
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(150.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::ShowText {
            items: vec![TextItem::Text(
                "4. Kerned text with manual adjustments:".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Normal text without kerning
        Op::ShowText {
            items: vec![TextItem::Text("AV AWAY WAVE To Va".to_string())],
        },
        Op::AddLineBreak,
        // The same text with manual kerning adjustments using TJ operator
        Op::ShowText {
            items: vec![
                TextItem::Text("A".to_string()),
                TextItem::Offset(-30.0), // Move closer to V
                TextItem::Text("V".to_string()),
                TextItem::Offset(10.0), // Normal spacing
                TextItem::Text("A".to_string()),
                TextItem::Offset(-30.0), // Move closer to W
                TextItem::Text("W".to_string()),
                TextItem::Offset(-30.0), // Move closer to A
                TextItem::Text("A".to_string()),
                TextItem::Offset(-20.0), // Move closer to Y
                TextItem::Text("Y".to_string()),
                TextItem::Offset(20.0), // Extra spacing
                TextItem::Text("W".to_string()),
                TextItem::Offset(-30.0), // Move closer to A
                TextItem::Text("A".to_string()),
                TextItem::Offset(-30.0), // Move closer to V
                TextItem::Text("V".to_string()),
                TextItem::Offset(-30.0), // Move closer to E
                TextItem::Text("E".to_string()),
                TextItem::Offset(40.0), // Extra spacing
                TextItem::Text("T".to_string()),
                TextItem::Offset(-20.0), // Move closer to o
                TextItem::Text("o".to_string()),
                TextItem::Offset(20.0), // Extra spacing
                TextItem::Text("V".to_string()),
                TextItem::Offset(-40.0), // Move closer to a
                TextItem::Text("a".to_string()),
            ],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // 5. Text with different rendering modes
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(130.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::SetFillColor {
            col: Color::Rgb(Rgb {
                r: 0.0,
                g: 0.0,
                b: 0.0,
                icc_profile: None,
            }),
        },
        Op::ShowText {
            items: vec![TextItem::Text(
                "5. Text with different rendering modes:".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Fill mode (default)
        Op::SetTextRenderingMode {
            mode: TextRenderingMode::Fill,
        },
        Op::SetFillColor {
            col: Color::Rgb(Rgb {
                r: 0.0,
                g: 0.0,
                b: 0.8,
                icc_profile: None,
            }),
        },
        Op::ShowText {
            items: vec![TextItem::Text("Fill mode".to_string())],
        },
        Op::AddLineBreak,
        // Stroke mode
        Op::SetTextRenderingMode {
            mode: TextRenderingMode::Stroke,
        },
        Op::SetOutlineColor {
            col: Color::Rgb(Rgb {
                r: 0.8,
                g: 0.0,
                b: 0.0,
                icc_profile: None,
            }),
        },
        Op::SetOutlineThickness { pt: Pt(0.5) },
        Op::ShowText {
            items: vec![TextItem::Text("Stroke mode".to_string())],
        },
        Op::AddLineBreak,
        // Fill and stroke mode
        Op::SetTextRenderingMode {
            mode: TextRenderingMode::FillStroke,
        },
        Op::SetFillColor {
            col: Color::Rgb(Rgb {
                r: 0.0,
                g: 0.8,
                b: 0.0,
                icc_profile: None,
            }),
        },
        Op::SetOutlineColor {
            col: Color::Rgb(Rgb {
                r: 0.0,
                g: 0.3,
                b: 0.0,
                icc_profile: None,
            }),
        },
        Op::ShowText {
            items: vec![TextItem::Text("Fill and stroke mode".to_string())],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // 6. Text with horizontal scaling
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(100.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::SetFillColor {
            col: Color::Rgb(Rgb {
                r: 0.0,
                g: 0.0,
                b: 0.0,
                icc_profile: None,
            }),
        },
        Op::SetTextRenderingMode {
            mode: TextRenderingMode::Fill,
        },
        Op::ShowText {
            items: vec![TextItem::Text(
                "6. Text with horizontal scaling:".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Normal text (100% scaling)
        Op::SetHorizontalScaling { percent: 100.0 },
        Op::ShowText {
            items: vec![TextItem::Text("Normal text (100% scaling)".to_string())],
        },
        Op::AddLineBreak,
        // Condensed text (75% scaling)
        Op::SetHorizontalScaling { percent: 75.0 },
        Op::ShowText {
            items: vec![TextItem::Text("Condensed text (75% scaling)".to_string())],
        },
        Op::AddLineBreak,
        // Expanded text (150% scaling)
        Op::SetHorizontalScaling { percent: 150.0 },
        Op::ShowText {
            items: vec![TextItem::Text("Expanded text (150% scaling)".to_string())],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // 7. Text with word spacing
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(80.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::SetHorizontalScaling { percent: 100.0 }, // Reset scaling
        Op::ShowText {
            items: vec![TextItem::Text("7. Text with word spacing:".to_string())],
        },
        Op::AddLineBreak,
        // Normal word spacing
        Op::SetWordSpacing { pt: Pt(0.0) },
        Op::ShowText {
            items: vec![TextItem::Text(
                "This is text with normal word spacing.".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Extra word spacing
        Op::SetWordSpacing { pt: Pt(10.0) },
        Op::ShowText {
            items: vec![TextItem::Text(
                "This is text with extra word spacing.".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Even more word spacing
        Op::SetWordSpacing { pt: Pt(20.0) },
        Op::ShowText {
            items: vec![TextItem::Text(
                "This is text with even more word spacing.".to_string(),
            )],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // 8. Text with different fonts in the same line
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(60.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::SetWordSpacing { pt: Pt(0.0) }, // Reset word spacing
        Op::ShowText {
            items: vec![TextItem::Text(
                "8. Mixed fonts in a single line:".to_string(),
            )],
        },
        Op::AddLineBreak,
        // Create a mixed-font text line by changing fonts between text segments
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::ShowText {
            items: vec![TextItem::Text("This is in ".to_string())],
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::TimesRoman),
            size: Pt(12.0),
        },
        Op::ShowText {
            items: vec![TextItem::Text("Times Roman".to_string())],
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Courier),
            size: Pt(12.0),
        },
        Op::ShowText {
            items: vec![TextItem::Text(" and this is in Courier".to_string())],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // 9. Example of positioned glyphs with azul text3 integration  
    // This demonstrates the complete workflow: azul UnifiedLayout → Vec<Op>
    ops.extend_from_slice(&[
        Op::SaveGraphicsState,
        Op::StartTextSection,
        Op::SetTextCursor {
            pos: Point::new(Mm(20.0), Mm(40.0)),
        },
        Op::SetFont {
            font: PdfFontHandle::Builtin(BuiltinFont::Helvetica),
            size: Pt(12.0),
        },
        Op::SetLineHeight { lh: Pt(14.0) },
        Op::ShowText {
            items: vec![TextItem::Text(
                "9. Azul Text3 Integration for Complex Scripts:".to_string(),
            )],
        },
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text(
                "[OK] TextMatrix provides absolute positioning".to_string(),
            )],
        },
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text(
                "[OK] TextItem::GlyphIds preserves exact glyph positioning".to_string(),
            )],
        },
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text(
                "[OK] Perfect for Arabic/Indic script shaping via azul-layout".to_string(),
            )],
        },
        Op::AddLineBreak,
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text(
                "Architecture: ParsedFont → UnifiedLayout → Vec<Op>".to_string(),
            )],
        },
        Op::AddLineBreak,
        Op::ShowText {
            items: vec![TextItem::Text(
                "See printpdf::html::from_html() for full implementation".to_string(),
            )],
        },
        Op::EndTextSection,
        Op::RestoreGraphicsState,
    ]);

    // Create a page with our operations
    let page = PdfPage::new(Mm(210.0), Mm(297.0), ops);

    // Save the PDF to a file
    let bytes = doc
        .with_pages(vec![page])
        .save(&PdfSaveOptions::default(), &mut Vec::new());

    std::fs::write("./text_positioning_example.pdf", bytes).unwrap();
    println!("Created text_positioning_example.pdf");
}