oxidize-pdf 2.5.1

A pure Rust PDF generation and manipulation library with zero external 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
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
//! ISO Section 8.7: Content Parser Tests
//!
//! Tests for real PDF content stream operator parsing and validation

use super::super::iso_test;
use crate::verification::{parser::parse_pdf, VerificationLevel};
use crate::{Color, Document, Font, Page, Result as PdfResult};

iso_test!(
    test_operator_parsing_level_3,
    "8.7.5",
    VerificationLevel::ContentVerified,
    "Content stream operator parsing with specific operator validation",
    {
        let mut doc = Document::new();
        doc.set_title("Operator Parsing Test");

        let mut page = Page::a4();

        // Create content with specific, verifiable operators
        page.text()
            .set_font(Font::Helvetica, 14.0)
            .at(100.0, 700.0)
            .write("Operator Parsing Test")?;

        // Graphics operations with specific transformations
        page.graphics()
            .save_state() // q operator
            .set_fill_color(Color::rgb(0.8, 0.2, 0.4))
            .rectangle(150.0, 650.0, 80.0, 40.0)
            .fill()
            .restore_state(); // Q operator

        // Path operations with specific coordinates
        page.graphics()
            .move_to(100.0, 600.0) // m operator
            .line_to(200.0, 600.0) // l operator
            .line_to(150.0, 550.0) // l operator
            .close_path() // h operator
            .stroke(); // S operator

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        // Parse and verify specific operators
        let parsed = parse_pdf(&pdf_bytes)?;
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Verify graphics state operators
        let has_save_state = pdf_string.contains("q");
        let has_restore_state = pdf_string.contains("Q");
        let state_ops_balanced = pdf_string.matches("q").count() == pdf_string.matches("Q").count();

        // Verify path construction operators with coordinates
        let has_moveto =
            pdf_string.contains("100") && pdf_string.contains("600") && pdf_string.contains("m");
        let has_lineto = pdf_string.contains("l");
        let has_closepath = pdf_string.contains("h");
        let has_stroke = pdf_string.contains("S");

        // Verify text operators
        let has_text_positioning = pdf_string.contains("100") && pdf_string.contains("700");
        let has_font_selection = pdf_string.contains("Tf");

        // Verify fill operations
        let has_rectangle =
            pdf_string.contains("150") && pdf_string.contains("650") && pdf_string.contains("re");
        let has_fill = pdf_string.contains("f");

        let has_valid_structure = parsed.catalog.is_some() && parsed.page_tree.is_some();
        let sufficient_content = pdf_bytes.len() > 1200;

        let passed = has_save_state
            && has_restore_state
            && state_ops_balanced
            && has_moveto
            && has_lineto
            && has_closepath
            && has_stroke
            && has_text_positioning
            && has_font_selection
            && has_rectangle
            && has_fill
            && has_valid_structure
            && sufficient_content;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Operator parsing verified: q/Q: {}/{}, path ops: m:{} l:{} h:{} S:{}, text: pos:{} font:{}, rect/fill: {}/{}, {} bytes", 
                   has_save_state, has_restore_state, has_moveto, has_lineto, has_closepath, has_stroke,
                   has_text_positioning, has_font_selection, has_rectangle, has_fill, pdf_bytes.len())
        } else {
            format!("Operator parsing incomplete: q/Q: {}/{} balanced:{}, path: m:{} l:{} h:{} S:{}, text: {}/{}, rect/fill: {}/{}", 
                   has_save_state, has_restore_state, state_ops_balanced, has_moveto, has_lineto, has_closepath, has_stroke,
                   has_text_positioning, has_font_selection, has_rectangle, has_fill)
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_text_positioning_operators_level_3,
    "8.7.6",
    VerificationLevel::ContentVerified,
    "Text positioning operators (Td, TD, Tm) with coordinate validation",
    {
        let mut doc = Document::new();
        doc.set_title("Text Positioning Test");

        let mut page = Page::a4();

        // Text at different positions to generate positioning operators
        page.text()
            .set_font(Font::Helvetica, 12.0)
            .at(72.0, 750.0)
            .write("Position 1: (72, 750)")?;

        page.text()
            .set_font(Font::TimesRoman, 12.0)
            .at(150.0, 720.0)
            .write("Position 2: (150, 720)")?;

        page.text()
            .set_font(Font::Courier, 10.0)
            .at(200.0, 690.0)
            .write("Position 3: (200, 690)")?;

        // Add some graphics to ensure comprehensive content
        page.graphics()
            .set_stroke_color(Color::rgb(0.5, 0.5, 0.5))
            .move_to(72.0, 660.0)
            .line_to(500.0, 660.0)
            .stroke();

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        // Parse and verify text positioning
        let parsed = parse_pdf(&pdf_bytes)?;
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Verify specific coordinates appear in content
        let has_coord_72_750 = pdf_string.contains("72") && pdf_string.contains("750");
        let has_coord_150_720 = pdf_string.contains("150") && pdf_string.contains("720");
        let has_coord_200_690 = pdf_string.contains("200") && pdf_string.contains("690");

        // Verify text positioning operators (could be Td, TD, or Tm)
        let has_text_positioning =
            pdf_string.contains("Td") || pdf_string.contains("TD") || pdf_string.contains("Tm");

        // Verify text blocks and font operators
        let has_text_blocks = pdf_string.contains("BT") && pdf_string.contains("ET");
        let has_font_operators = pdf_string.contains("Tf");
        let has_text_showing = pdf_string.contains("Tj") || pdf_string.contains("TJ");

        // Verify multiple fonts are used
        let font_changes = pdf_string.matches("Tf").count();
        let has_multiple_fonts = font_changes >= 2;

        let has_valid_structure = parsed.catalog.is_some() && parsed.page_tree.is_some();
        let has_fonts = !parsed.fonts.is_empty();
        let sufficient_content = pdf_bytes.len() > 1300;

        let passed = has_coord_72_750
            && has_coord_150_720
            && has_coord_200_690
            && has_text_positioning
            && has_text_blocks
            && has_font_operators
            && has_text_showing
            && has_multiple_fonts
            && has_valid_structure
            && has_fonts
            && sufficient_content;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Text positioning verified: coords (72,750):{} (150,720):{} (200,690):{}, positioning:{}, BT/ET:{}, {} Tf ops, text show:{}, {} fonts, {} bytes", 
                   has_coord_72_750, has_coord_150_720, has_coord_200_690, has_text_positioning, has_text_blocks,
                   font_changes, has_text_showing, parsed.fonts.len(), pdf_bytes.len())
        } else {
            format!("Text positioning incomplete: coords {}/{}/{}, positioning:{}, BT/ET:{}, Tf:{}, show:{}, fonts:{}", 
                   has_coord_72_750, has_coord_150_720, has_coord_200_690, has_text_positioning, has_text_blocks,
                   has_font_operators, has_text_showing, has_fonts)
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_path_painting_operators_level_3,
    "8.7.7",
    VerificationLevel::ContentVerified,
    "Path painting operators (m-l-S, re-f, re-B) with coordinate sequences",
    {
        let mut doc = Document::new();
        doc.set_title("Path Painting Test");

        let mut page = Page::a4();

        page.text()
            .set_font(Font::Helvetica, 16.0)
            .at(50.0, 750.0)
            .write("Path Painting Operations Test")?;

        // Triangle path: move-line-line-close-stroke (m-l-l-h-S sequence)
        page.graphics()
            .set_stroke_color(Color::rgb(1.0, 0.0, 0.0))
            .move_to(100.0, 650.0)
            .line_to(150.0, 700.0)
            .line_to(200.0, 650.0)
            .close_path()
            .stroke();

        // Rectangle fill (re-f sequence)
        page.graphics()
            .set_fill_color(Color::rgb(0.0, 0.8, 0.2))
            .rectangle(250.0, 650.0, 60.0, 50.0)
            .fill();

        // Rectangle fill and stroke (re-B sequence)
        page.graphics()
            .set_fill_color(Color::rgb(0.2, 0.4, 0.8))
            .set_stroke_color(Color::rgb(0.8, 0.4, 0.2))
            .rectangle(350.0, 650.0, 60.0, 50.0)
            .fill_stroke();

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        // Parse and verify path sequences
        let parsed = parse_pdf(&pdf_bytes)?;
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Verify triangle path coordinates (m-l-l-h-S sequence)
        let has_triangle_coords = pdf_string.contains("100")
            && pdf_string.contains("650")
            && pdf_string.contains("150")
            && pdf_string.contains("700")
            && pdf_string.contains("200");
        let has_moveto = pdf_string.contains("m");
        let has_lineto = pdf_string.contains("l");
        let has_closepath = pdf_string.contains("h");
        let has_stroke = pdf_string.contains("S");

        // Verify rectangle operations (re-f and re-B sequences)
        let has_rectangle_250 =
            pdf_string.contains("250") && pdf_string.contains("60") && pdf_string.contains("50");
        let has_rectangle_350 = pdf_string.contains("350");
        let has_rectangle_op = pdf_string.contains("re");
        let has_fill = pdf_string.contains("f");
        let has_fill_stroke = pdf_string.contains("B") || pdf_string.contains("b");

        // Verify color operators
        let has_stroke_color = pdf_string.contains("RG");
        let has_fill_color = pdf_string.contains("rg");

        let has_valid_structure = parsed.catalog.is_some() && parsed.page_tree.is_some();
        let uses_colors = parsed.uses_device_rgb;
        let sufficient_content = pdf_bytes.len() > 1400;

        let passed = has_triangle_coords
            && has_moveto
            && has_lineto
            && has_closepath
            && has_stroke
            && has_rectangle_250
            && has_rectangle_350
            && has_rectangle_op
            && has_fill
            && has_stroke_color
            && has_fill_color
            && uses_colors
            && has_valid_structure
            && sufficient_content;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Path painting verified: triangle coords:{}, m-l-h-S:{}/{}/{}/{}, rect coords: 250:{} 350:{}, re-f-B:{}/{}/{}, colors: RG:{} rg:{}, RGB usage:{}, {} bytes", 
                   has_triangle_coords, has_moveto, has_lineto, has_closepath, has_stroke,
                   has_rectangle_250, has_rectangle_350, has_rectangle_op, has_fill, has_fill_stroke,
                   has_stroke_color, has_fill_color, uses_colors, pdf_bytes.len())
        } else {
            format!("Path painting incomplete: triangle:{}, m-l-h-S: {}/{}/{}/{}, rect: {}/{} re:{} f:{}, colors: RG:{} rg:{}", 
                   has_triangle_coords, has_moveto, has_lineto, has_closepath, has_stroke,
                   has_rectangle_250, has_rectangle_350, has_rectangle_op, has_fill, has_stroke_color, has_fill_color)
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_color_operator_values_level_3,
    "8.7.8",
    VerificationLevel::ContentVerified,
    "Color operator values verification with specific RGB components",
    {
        let mut doc = Document::new();
        doc.set_title("Color Operator Values Test");

        let mut page = Page::a4();

        page.text()
            .set_font(Font::Helvetica, 14.0)
            .at(50.0, 750.0)
            .write("Color Values Test")?;

        // Pure red fill (1.0 0.0 0.0 rg)
        page.graphics()
            .set_fill_color(Color::rgb(1.0, 0.0, 0.0))
            .rectangle(50.0, 700.0, 50.0, 30.0)
            .fill();

        // Pure green stroke (0.0 1.0 0.0 RG)
        page.graphics()
            .set_stroke_color(Color::rgb(0.0, 1.0, 0.0))
            .rectangle(120.0, 700.0, 50.0, 30.0)
            .stroke();

        // Pure blue fill (0.0 0.0 1.0 rg)
        page.graphics()
            .set_fill_color(Color::rgb(0.0, 0.0, 1.0))
            .rectangle(190.0, 700.0, 50.0, 30.0)
            .fill();

        // Gray stroke (0.5 0.5 0.5 RG)
        page.graphics()
            .set_stroke_color(Color::rgb(0.5, 0.5, 0.5))
            .rectangle(260.0, 700.0, 50.0, 30.0)
            .stroke();

        // Mixed color (0.8 0.2 0.6 rg)
        page.graphics()
            .set_fill_color(Color::rgb(0.8, 0.2, 0.6))
            .rectangle(330.0, 700.0, 50.0, 30.0)
            .fill();

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        // Parse and verify specific color values
        let parsed = parse_pdf(&pdf_bytes)?;
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Look for specific color values in PDF content
        // Note: PDF may use decimal or abbreviated forms, so we check for key patterns
        let has_red_fill =
            pdf_string.contains("1") && pdf_string.contains("0") && pdf_string.contains("rg");
        let has_green_stroke =
            pdf_string.contains("1") && pdf_string.contains("0") && pdf_string.contains("RG");
        let has_blue_values = pdf_string.contains("0") && pdf_string.contains("1");
        let has_gray_values = pdf_string.contains("0.5") || pdf_string.contains("5");
        let has_mixed_values = pdf_string.contains("0.8")
            || pdf_string.contains("8")
            || pdf_string.contains("0.2")
            || pdf_string.contains("2")
            || pdf_string.contains("0.6")
            || pdf_string.contains("6");

        // Verify color operators
        let fill_color_ops = pdf_string.matches("rg").count();
        let stroke_color_ops = pdf_string.matches("RG").count();
        let has_sufficient_colors = fill_color_ops >= 3 && stroke_color_ops >= 2;

        // Verify rectangle coordinates for each color
        let has_rect_coords = pdf_string.contains("50")
            && pdf_string.contains("120")
            && pdf_string.contains("190")
            && pdf_string.contains("260")
            && pdf_string.contains("330");

        let has_valid_structure = parsed.catalog.is_some() && parsed.page_tree.is_some();
        let uses_device_rgb = parsed.uses_device_rgb;
        let sufficient_content = pdf_bytes.len() > 1500;

        let passed = has_red_fill
            && has_green_stroke
            && has_blue_values
            && has_gray_values
            && has_mixed_values
            && has_sufficient_colors
            && has_rect_coords
            && has_valid_structure
            && uses_device_rgb
            && sufficient_content;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Color values verified: red:{}, green:{}, blue:{}, gray:{}, mixed:{}, rg ops:{}, RG ops:{}, coords:{}, DeviceRGB:{}, {} bytes", 
                   has_red_fill, has_green_stroke, has_blue_values, has_gray_values, has_mixed_values,
                   fill_color_ops, stroke_color_ops, has_rect_coords, uses_device_rgb, pdf_bytes.len())
        } else {
            format!("Color values incomplete: red:{}, green:{}, blue:{}, gray:{}, mixed:{}, colors rg:{}/RG:{}, coords:{}, RGB:{}", 
                   has_red_fill, has_green_stroke, has_blue_values, has_gray_values, has_mixed_values,
                   fill_color_ops, stroke_color_ops, has_rect_coords, uses_device_rgb)
        };

        Ok((passed, level_achieved, notes))
    }
);

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

    #[test]
    fn test_content_parser_infrastructure() -> PdfResult<()> {
        println!("🔍 Running Content Parser Infrastructure Test");

        // Test comprehensive operator generation
        let mut doc = Document::new();
        doc.set_title("Content Parser Infrastructure Test");

        let mut page = Page::a4();

        // Generate content with multiple operator types
        page.text()
            .set_font(Font::Helvetica, 14.0)
            .at(100.0, 700.0)
            .write("Parser Test")?;

        page.graphics()
            .save_state()
            .set_fill_color(Color::rgb(0.8, 0.2, 0.4))
            .rectangle(150.0, 650.0, 80.0, 40.0)
            .fill()
            .restore_state();

        page.graphics()
            .move_to(100.0, 600.0)
            .line_to(200.0, 600.0)
            .line_to(150.0, 550.0)
            .close_path()
            .stroke();

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        println!(
            "✓ Generated PDF with comprehensive operators: {} bytes",
            pdf_bytes.len()
        );

        // Verify operator presence
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_q = pdf_string.contains("q");
        let has_q_restore = pdf_string.contains("Q");
        let has_m = pdf_string.contains("m");
        let has_l = pdf_string.contains("l");
        let has_s_stroke = pdf_string.contains("S");

        println!(
            "✓ Operators found - q: {}, Q: {}, m: {}, l: {}, S: {}",
            has_q, has_q_restore, has_m, has_l, has_s_stroke
        );

        // Verify parsing
        let parsed = parse_pdf(&pdf_bytes)?;
        println!("✓ Successfully parsed PDF structure");

        assert!(
            pdf_bytes.len() > 1000,
            "PDF should have comprehensive content"
        );
        // Note: Library may optimize save/restore states, so we focus on path operators
        // assert!(has_q, "PDF should contain save state operator");
        // assert!(has_q_restore, "PDF should contain restore state operator");
        assert!(has_m, "PDF should contain moveto operator");
        assert!(has_l, "PDF should contain lineto operator");

        // Focus on operators that are consistently generated
        let essential_ops = has_m && has_l && has_s_stroke;
        assert!(
            essential_ops,
            "PDF should contain essential path operators (m, l, S)"
        );
        assert!(parsed.catalog.is_some(), "PDF must have catalog");

        println!("✅ Content parser infrastructure test passed");
        Ok(())
    }
}