pdf_oxide 0.3.32

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
#![allow(clippy::useless_vec)]
//! Complex Script Processing Benchmarks
//!
//! This benchmark suite measures the performance of complex script processing,
//! including Devanagari, Thai, Khmer, and other South/Southeast Asian scripts.
//!
//! ## Performance Targets
//!
//! - Diacritic detection: <10µs per check
//! - Virama/COENG handling: <15µs
//! - Boundary detection: <20µs
//! - All 15 complex scripts: O(1) detection

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use pdf_oxide::text::complex_script_detector::{
    detect_complex_script, handle_devanagari_boundary, handle_indic_boundary,
    handle_khmer_boundary, handle_thai_boundary, is_complex_script, is_devanagari_anusvar_visarga,
    is_devanagari_consonant, is_devanagari_diacritic, is_devanagari_matra, is_devanagari_nukta,
    is_devanagari_virama, is_thai_digit, is_thai_major_punctuation, is_thai_tone_mark,
    is_thai_vowel_modifier,
};
use pdf_oxide::text::CharacterInfo;
use std::hint::black_box;

// ============================================================================
// DEVANAGARI BENCHMARKS
// ============================================================================

fn bench_devanagari(c: &mut Criterion) {
    let mut group = c.benchmark_group("devanagari");
    group.sample_size(50);

    // Diacritic detection
    group.bench_function("diacritics", |b| {
        let marks = vec![
            0x0902, 0x0903, // ANUSVARA, VISARGA
            0x093C, // NUKTA
            0x093E, 0x093F, 0x0940, // Matras
            0x094D, // VIRAMA
        ];
        b.iter(|| {
            for &code in &marks {
                black_box(is_devanagari_diacritic(black_box(code)));
            }
        });
    });

    // Virama detection
    group.bench_function("virama", |b| {
        let codes = vec![0x094D, 0x0915, 0x0924, 0x093E]; // virama and non-virama
        b.iter(|| {
            for &code in &codes {
                black_box(is_devanagari_virama(black_box(code)));
            }
        });
    });

    // Consonant detection
    group.bench_function("consonants", |b| {
        let consonants = vec![
            0x0915, // KA
            0x0916, // KHA
            0x0917, // GA
            0x091A, // CA
            0x0924, // TA
        ];
        b.iter(|| {
            for &code in &consonants {
                black_box(is_devanagari_consonant(black_box(code)));
            }
        });
    });

    // Matra detection
    group.bench_function("matras", |b| {
        let matras = vec![
            0x093E, // AA
            0x093F, // I
            0x0940, // II
            0x0941, // U
            0x0942, // UU
        ];
        b.iter(|| {
            for &code in &matras {
                black_box(is_devanagari_matra(black_box(code)));
            }
        });
    });

    // Anusvara/Visarga detection
    group.bench_function("anusvara_visarga", |b| {
        let codes = vec![0x0902, 0x0903, 0x0915, 0x0924];
        b.iter(|| {
            for &code in &codes {
                black_box(is_devanagari_anusvar_visarga(black_box(code)));
            }
        });
    });

    // Nukta detection
    group.bench_function("nukta", |b| {
        let codes = vec![0x093C, 0x0915, 0x0924];
        b.iter(|| {
            for &code in &codes {
                black_box(is_devanagari_nukta(black_box(code)));
            }
        });
    });

    // Boundary handling
    group.bench_function("boundary_handling", |b| {
        let chars = vec![
            create_char(0x0928, 0.0, 7.0),  // NA
            create_char(0x092E, 7.0, 7.0),  // MA
            create_char(0x0938, 14.0, 7.0), // SA
            create_char(0x094D, 21.0, 0.0), // VIRAMA
            create_char(0x0924, 21.0, 7.0), // TA
            create_char(0x0947, 28.0, 0.0), // VOWEL SIGN E
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_devanagari_boundary(
                    black_box(&chars[i]),
                    black_box(&chars[i + 1]),
                ));
            }
        });
    });

    group.finish();
}

// ============================================================================
// THAI BENCHMARKS
// ============================================================================

fn bench_thai(c: &mut Criterion) {
    let mut group = c.benchmark_group("thai");
    group.sample_size(50);

    // Tone mark detection
    group.bench_function("tone_marks", |b| {
        let marks = vec![
            0x0E48, // MAI EK
            0x0E49, // MAI THO
            0x0E4A, // MAI TRI
            0x0E4B, // MAI CHATTAWA
        ];
        b.iter(|| {
            for &code in &marks {
                black_box(is_thai_tone_mark(black_box(code)));
            }
        });
    });

    // Vowel modifier detection
    group.bench_function("vowel_modifiers", |b| {
        let modifiers = vec![
            0x0E31, // MAI HAN-AKAT
            0x0E34, 0x0E35, 0x0E36, 0x0E37, // Above vowels
            0x0E39, 0x0E3A, // Below vowels
        ];
        b.iter(|| {
            for &code in &modifiers {
                black_box(is_thai_vowel_modifier(black_box(code)));
            }
        });
    });

    // Digit detection
    group.bench_function("digits", |b| {
        let digits = vec![
            0x0E50, 0x0E51, 0x0E52, // Thai digits
            0x0030, 0x0031, 0x0032, // Western digits
        ];
        b.iter(|| {
            for &code in &digits {
                black_box(is_thai_digit(black_box(code)));
            }
        });
    });

    // Major punctuation detection
    group.bench_function("major_punctuation", |b| {
        let punct = vec![
            0x0E2F, // PAIYANNOI
            0x0E46, // MAIYAMOK
            0x0E4F, // FONGMAN
        ];
        b.iter(|| {
            for &code in &punct {
                black_box(is_thai_major_punctuation(black_box(code)));
            }
        });
    });

    // Boundary handling
    group.bench_function("boundary_handling", |b| {
        let chars = vec![
            create_char(0x0E01, 0.0, 7.0),  // KO KAI
            create_char(0x0E48, 0.0, 0.0),  // MAI EK (tone mark)
            create_char(0x0E32, 7.0, 7.0),  // SARA AA
            create_char(0x0E23, 14.0, 7.0), // RO RUA
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_thai_boundary(black_box(&chars[i]), black_box(&chars[i + 1])));
            }
        });
    });

    group.finish();
}

// ============================================================================
// KHMER BENCHMARKS
// ============================================================================

fn bench_khmer(c: &mut Criterion) {
    let mut group = c.benchmark_group("khmer");
    group.sample_size(50);

    // COENG detection (Khmer subscript marker)
    group.bench_function("coeng", |b| {
        let codes = vec![0x17D2, 0x1780, 0x1781, 0x1782];
        b.iter(|| {
            for &code in &codes {
                // COENG is U+17D2
                black_box(code == 0x17D2);
            }
        });
    });

    // Khmer vowel sign detection
    group.bench_function("vowel_signs", |b| {
        let vowels = vec![
            0x17B6, 0x17B7, 0x17B8, 0x17B9, 0x17BA, 0x17BB, 0x17BC, 0x17BD,
        ];
        b.iter(|| {
            for &code in &vowels {
                // Khmer vowel signs range
                black_box(matches!(code, 0x17B6..=0x17BD));
            }
        });
    });

    // Boundary handling
    group.bench_function("boundary_handling", |b| {
        let chars = vec![
            create_char(0x1780, 0.0, 7.0), // KA
            create_char(0x17D2, 0.0, 0.0), // COENG (subscript marker)
            create_char(0x1781, 0.0, 7.0), // KHA (subscript)
            create_char(0x17B6, 7.0, 0.0), // AA vowel sign
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_khmer_boundary(black_box(&chars[i]), black_box(&chars[i + 1])));
            }
        });
    });

    group.finish();
}

// ============================================================================
// OTHER INDIC SCRIPTS BENCHMARKS
// ============================================================================

fn bench_indic_scripts(c: &mut Criterion) {
    let mut group = c.benchmark_group("indic_scripts");
    group.sample_size(50);

    // Tamil boundary handling
    group.bench_function("tamil", |b| {
        let chars = vec![
            create_char(0x0B95, 0.0, 7.0), // KA
            create_char(0x0BBE, 7.0, 0.0), // AA vowel sign
            create_char(0x0B99, 7.0, 7.0), // NGA
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_indic_boundary(black_box(&chars[i]), black_box(&chars[i + 1])));
            }
        });
    });

    // Telugu boundary handling
    group.bench_function("telugu", |b| {
        let chars = vec![
            create_char(0x0C15, 0.0, 7.0), // KA
            create_char(0x0C3E, 7.0, 0.0), // AA vowel sign
            create_char(0x0C17, 7.0, 7.0), // GA
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_indic_boundary(black_box(&chars[i]), black_box(&chars[i + 1])));
            }
        });
    });

    // Bengali boundary handling
    group.bench_function("bengali", |b| {
        let chars = vec![
            create_char(0x0995, 0.0, 7.0), // KA
            create_char(0x09BE, 7.0, 0.0), // AA vowel sign
            create_char(0x0997, 7.0, 7.0), // GA
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_indic_boundary(black_box(&chars[i]), black_box(&chars[i + 1])));
            }
        });
    });

    group.finish();
}

// ============================================================================
// COMPLEX SCRIPT DETECTION BENCHMARKS
// ============================================================================

fn bench_all_complex_scripts(c: &mut Criterion) {
    let mut group = c.benchmark_group("all_complex_scripts");
    group.sample_size(50);

    // Test detection for all 15 complex scripts
    group.bench_function("detect_all_scripts", |b| {
        let test_codes = vec![
            0x0928, // Devanagari
            0x0995, // Bengali
            0x0A15, // Gurmukhi
            0x0A95, // Gujarati
            0x0B15, // Oriya
            0x0B95, // Tamil
            0x0C15, // Telugu
            0x0C95, // Kannada
            0x0D15, // Malayalam
            0x0D95, // Sinhala
            0x0E01, // Thai
            0x0E81, // Lao
            0x1780, // Khmer
            0x1000, // Burmese
            0x1820, // Mongolian
        ];

        b.iter(|| {
            for &code in &test_codes {
                black_box(detect_complex_script(black_box(code)));
            }
        });
    });

    // is_complex_script convenience function
    group.bench_function("is_complex_script", |b| {
        let codes = vec![
            0x0928, 0x0995, 0x0E01, 0x1780, // Complex scripts
            0x0041, 0x4E00, 0x0627, // Non-complex
        ];

        b.iter(|| {
            for &code in &codes {
                black_box(is_complex_script(black_box(code)));
            }
        });
    });

    group.finish();
}

// ============================================================================
// BATCH COMPLEX SCRIPT PROCESSING
// ============================================================================

fn bench_batch_complex_processing(c: &mut Criterion) {
    let mut group = c.benchmark_group("batch_complex");
    group.sample_size(20);

    for size in [10, 100, 1000] {
        // Devanagari text
        group.bench_with_input(BenchmarkId::new("devanagari", size), &size, |b, &size| {
            let chars = generate_devanagari_text(size);
            b.iter(|| {
                for &code in &chars {
                    black_box(detect_complex_script(black_box(code)));
                    black_box(is_devanagari_diacritic(black_box(code)));
                    black_box(is_devanagari_virama(black_box(code)));
                }
            });
        });

        // Thai text
        group.bench_with_input(BenchmarkId::new("thai", size), &size, |b, &size| {
            let chars = generate_thai_text(size);
            b.iter(|| {
                for &code in &chars {
                    black_box(detect_complex_script(black_box(code)));
                    black_box(is_thai_tone_mark(black_box(code)));
                    black_box(is_thai_vowel_modifier(black_box(code)));
                }
            });
        });

        // Mixed complex scripts
        group.bench_with_input(BenchmarkId::new("mixed", size), &size, |b, &size| {
            let chars = generate_mixed_complex_text(size);
            b.iter(|| {
                for &code in &chars {
                    black_box(detect_complex_script(black_box(code)));
                }
            });
        });
    }

    group.finish();
}

// ============================================================================
// VIRAMA AND COENG HANDLING BENCHMARKS
// ============================================================================

fn bench_virama_coeng_handling(c: &mut Criterion) {
    let mut group = c.benchmark_group("virama_coeng");
    group.sample_size(50);

    // Devanagari virama (conjunct consonants)
    group.bench_function("devanagari_virama_sequence", |b| {
        let chars = vec![
            create_char(0x0915, 0.0, 7.0), // KA
            create_char(0x094D, 0.0, 0.0), // VIRAMA
            create_char(0x0937, 0.0, 7.0), // SHA (conjunct)
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_devanagari_boundary(
                    black_box(&chars[i]),
                    black_box(&chars[i + 1]),
                ));
            }
        });
    });

    // Khmer COENG (subscript consonants)
    group.bench_function("khmer_coeng_sequence", |b| {
        let chars = vec![
            create_char(0x1780, 0.0, 7.0), // KA
            create_char(0x17D2, 0.0, 0.0), // COENG
            create_char(0x1781, 0.0, 7.0), // KHA (subscript)
        ];

        b.iter(|| {
            for i in 0..chars.len() - 1 {
                black_box(handle_khmer_boundary(black_box(&chars[i]), black_box(&chars[i + 1])));
            }
        });
    });

    group.finish();
}

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fn create_char(code: u32, x_pos: f32, width: f32) -> CharacterInfo {
    CharacterInfo {
        code,
        glyph_id: Some(1),
        width,
        x_position: x_pos,
        tj_offset: None,
        font_size: 12.0,
        is_ligature: false,
        original_ligature: None,
        protected_from_split: false,
    }
}

fn generate_devanagari_text(size: usize) -> Vec<u32> {
    let chars = vec![
        0x0928, 0x092E, 0x0938, // Consonants
        0x094D, // VIRAMA
        0x093E, 0x0947, // Matras
        0x0902, 0x0903, // Anusvara, Visarga
    ];
    (0..size).map(|i| chars[i % chars.len()]).collect()
}

fn generate_thai_text(size: usize) -> Vec<u32> {
    let chars = vec![
        0x0E01, 0x0E02, 0x0E03, // Consonants
        0x0E48, 0x0E49, // Tone marks
        0x0E31, 0x0E34, // Vowel modifiers
    ];
    (0..size).map(|i| chars[i % chars.len()]).collect()
}

fn generate_mixed_complex_text(size: usize) -> Vec<u32> {
    let chars = vec![
        0x0928, // Devanagari
        0x0995, // Bengali
        0x0B95, // Tamil
        0x0E01, // Thai
        0x1780, // Khmer
    ];
    (0..size).map(|i| chars[i % chars.len()]).collect()
}

criterion_group!(
    benches,
    bench_devanagari,
    bench_thai,
    bench_khmer,
    bench_indic_scripts,
    bench_all_complex_scripts,
    bench_batch_complex_processing,
    bench_virama_coeng_handling,
);
criterion_main!(benches);