micropdf 0.16.0

A pure Rust PDF library - A pure Rust PDF library with fz_/pdf_ API compatibility
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! OCR (Optical Character Recognition) integration
//!
//! This module provides interfaces for OCR engines like Tesseract.
//! The actual OCR implementation requires external libraries.

use std::ffi::{CStr, CString, c_char, c_int};
use std::sync::LazyLock;

use crate::ffi::{Handle, HandleStore};
use crate::fitz::geometry::Rect;
use crate::fitz::pixmap::Pixmap;

// ============================================================================
// Handle Management
// ============================================================================

/// Handle store for OCR engines
static OCR_ENGINES: LazyLock<HandleStore<OcrEngine>> = LazyLock::new(HandleStore::new);

/// Handle store for OCR results
static OCR_RESULTS: LazyLock<HandleStore<OcrResult>> = LazyLock::new(HandleStore::new);

// ============================================================================
// OCR Engine Types
// ============================================================================

/// Supported OCR engines
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OcrEngineType {
    /// No OCR engine (no-op baseline)
    None = 0,
    /// Tesseract OCR
    Tesseract = 1,
    /// Windows OCR (Windows.Media.Ocr)
    WindowsOcr = 2,
    /// Apple Vision Framework (macOS/iOS)
    AppleVision = 3,
    /// Google Cloud Vision
    GoogleVision = 4,
    /// Amazon Textract
    AmazonTextract = 5,
    /// Azure Computer Vision
    AzureVision = 6,
}

/// OCR page segmentation modes (Tesseract PSM)
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OcrPageSegMode {
    /// Orientation and script detection only
    OsdOnly = 0,
    /// Automatic page segmentation with OSD
    AutoOsd = 1,
    /// Automatic page segmentation, no OSD or OCR
    AutoOnly = 2,
    /// Fully automatic page segmentation, no OSD (default)
    Auto = 3,
    /// Single column of text
    SingleColumn = 4,
    /// Single uniform block of vertically aligned text
    SingleBlockVertText = 5,
    /// Single uniform block of text
    SingleBlock = 6,
    /// Single text line
    SingleLine = 7,
    /// Single word
    SingleWord = 8,
    /// Single word in a circle
    CircleWord = 9,
    /// Single character
    SingleChar = 10,
    /// Sparse text - find as much text as possible
    SparseText = 11,
    /// Sparse text with OSD
    SparseTextOsd = 12,
    /// Raw line - treat image as a single text line
    RawLine = 13,
}

/// OCR engine mode (Tesseract OEM)
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OcrEngineMode {
    /// Legacy engine only
    TesseractOnly = 0,
    /// LSTM neural network only
    LstmOnly = 1,
    /// Legacy + LSTM combined
    TesseractLstmCombined = 2,
    /// Default (currently LSTM)
    Default = 3,
}

// ============================================================================
// OCR Configuration
// ============================================================================

/// OCR configuration
#[derive(Debug, Clone)]
pub struct OcrConfig {
    /// Engine type
    pub engine_type: OcrEngineType,
    /// Language code (e.g., "eng", "fra", "deu")
    pub language: String,
    /// Page segmentation mode
    pub psm: OcrPageSegMode,
    /// Engine mode
    pub oem: OcrEngineMode,
    /// DPI for image preprocessing
    pub dpi: u32,
    /// Enable image preprocessing
    pub preprocess: bool,
    /// Confidence threshold (0-100)
    pub min_confidence: i32,
}

impl Default for OcrConfig {
    fn default() -> Self {
        Self {
            engine_type: OcrEngineType::None,
            language: "eng".to_string(),
            psm: OcrPageSegMode::Auto,
            oem: OcrEngineMode::Default,
            dpi: 300,
            preprocess: true,
            min_confidence: 60,
        }
    }
}

// ============================================================================
// OCR Word
// ============================================================================

/// A recognized word
#[derive(Debug, Clone)]
pub struct OcrWord {
    /// Word text
    pub text: String,
    /// Bounding box
    pub bounds: Rect,
    /// Confidence (0-100)
    pub confidence: i32,
    /// Font name (if detected)
    pub font: Option<String>,
    /// Font size (if detected)
    pub font_size: Option<f32>,
    /// Is bold
    pub bold: bool,
    /// Is italic
    pub italic: bool,
}

impl OcrWord {
    /// Create a new OCR word
    pub fn new(text: impl Into<String>, bounds: Rect, confidence: i32) -> Self {
        Self {
            text: text.into(),
            bounds,
            confidence,
            font: None,
            font_size: None,
            bold: false,
            italic: false,
        }
    }
}

// ============================================================================
// OCR Line
// ============================================================================

/// A recognized line of text
#[derive(Debug, Clone)]
pub struct OcrLine {
    /// Line text
    pub text: String,
    /// Bounding box
    pub bounds: Rect,
    /// Words in this line
    pub words: Vec<OcrWord>,
    /// Average confidence
    pub confidence: i32,
}

impl OcrLine {
    /// Create a new OCR line
    pub fn new(bounds: Rect) -> Self {
        Self {
            text: String::new(),
            bounds,
            words: Vec::new(),
            confidence: 0,
        }
    }

    /// Add a word to this line
    pub fn add_word(&mut self, word: OcrWord) {
        if !self.text.is_empty() {
            self.text.push(' ');
        }
        self.text.push_str(&word.text);
        self.words.push(word);
        self.update_confidence();
    }

    fn update_confidence(&mut self) {
        if self.words.is_empty() {
            self.confidence = 0;
        } else {
            let sum: i32 = self.words.iter().map(|w| w.confidence).sum();
            self.confidence = sum / self.words.len() as i32;
        }
    }
}

// ============================================================================
// OCR Block
// ============================================================================

/// A block of recognized text
#[derive(Debug, Clone)]
pub struct OcrBlock {
    /// Block bounds
    pub bounds: Rect,
    /// Lines in this block
    pub lines: Vec<OcrLine>,
    /// Block type (paragraph, table, image, etc.)
    pub block_type: i32,
}

impl OcrBlock {
    /// Create a new OCR block
    pub fn new(bounds: Rect) -> Self {
        Self {
            bounds,
            lines: Vec::new(),
            block_type: 0, // Paragraph
        }
    }

    /// Add a line to this block
    pub fn add_line(&mut self, line: OcrLine) {
        self.lines.push(line);
    }

    /// Get full text
    pub fn text(&self) -> String {
        self.lines
            .iter()
            .map(|l| l.text.as_str())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

// ============================================================================
// OCR Result
// ============================================================================

/// OCR recognition result
#[derive(Debug, Clone)]
pub struct OcrResult {
    /// Recognized blocks
    pub blocks: Vec<OcrBlock>,
    /// Overall confidence
    pub confidence: i32,
    /// Processing time in milliseconds
    pub processing_time_ms: u64,
    /// Image width
    pub width: u32,
    /// Image height
    pub height: u32,
    /// Error message (if any)
    pub error: Option<String>,
}

impl OcrResult {
    /// Create a new empty result
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            blocks: Vec::new(),
            confidence: 0,
            processing_time_ms: 0,
            width,
            height,
            error: None,
        }
    }

    /// Create an error result
    pub fn error(msg: impl Into<String>) -> Self {
        let mut result = Self::new(0, 0);
        result.error = Some(msg.into());
        result
    }

    /// Get full text
    pub fn text(&self) -> String {
        self.blocks
            .iter()
            .map(|b| b.text())
            .collect::<Vec<_>>()
            .join("\n\n")
    }

    /// Get word count
    pub fn word_count(&self) -> usize {
        self.blocks
            .iter()
            .flat_map(|b| &b.lines)
            .flat_map(|l| &l.words)
            .count()
    }

    /// Get line count
    pub fn line_count(&self) -> usize {
        self.blocks.iter().map(|b| b.lines.len()).sum()
    }

    /// Add a block
    pub fn add_block(&mut self, block: OcrBlock) {
        self.blocks.push(block);
        self.update_confidence();
    }

    fn update_confidence(&mut self) {
        let total_words: usize = self.word_count();
        if total_words == 0 {
            self.confidence = 0;
            return;
        }

        let sum: i64 = self
            .blocks
            .iter()
            .flat_map(|b| &b.lines)
            .flat_map(|l| &l.words)
            .map(|w| w.confidence as i64)
            .sum();

        self.confidence = (sum / total_words as i64) as i32;
    }
}

// ============================================================================
// OCR Engine
// ============================================================================

/// OCR engine wrapper
pub struct OcrEngine {
    /// Configuration
    config: OcrConfig,
    /// Is initialized
    initialized: bool,
}

impl OcrEngine {
    /// Create a new OCR engine
    pub fn new(config: OcrConfig) -> Self {
        Self {
            config,
            initialized: false,
        }
    }

    /// Initialize the engine
    pub fn init(&mut self) -> Result<(), String> {
        match self.config.engine_type {
            OcrEngineType::None => {
                // No-op engine — always succeeds
                self.initialized = true;
                Ok(())
            }
            OcrEngineType::Tesseract => {
                // Tesseract requires the 'tesseract' feature and libtesseract
                Err("Tesseract OCR not compiled in. Enable the 'tesseract' feature.".to_string())
            }
            _ => Err(format!(
                "OCR engine {:?} not supported on this platform.",
                self.config.engine_type
            )),
        }
    }

    /// Check if initialized
    pub fn is_initialized(&self) -> bool {
        self.initialized
    }

    /// Recognize text in a pixmap
    pub fn recognize(&self, _pixmap: &Pixmap) -> OcrResult {
        if !self.initialized {
            return OcrResult::error("OCR engine not initialized");
        }

        match self.config.engine_type {
            OcrEngineType::None => {
                // No-op engine returns an empty result
                OcrResult::new(_pixmap.width() as u32, _pixmap.height() as u32)
            }
            _ => OcrResult::error("OCR recognition not implemented for this engine"),
        }
    }

    /// Set language
    pub fn set_language(&mut self, lang: &str) {
        self.config.language = lang.to_string();
        self.initialized = false; // Requires re-initialization
    }

    /// Get current language
    pub fn language(&self) -> &str {
        &self.config.language
    }

    /// Set page segmentation mode
    pub fn set_psm(&mut self, psm: OcrPageSegMode) {
        self.config.psm = psm;
    }

    /// Set engine mode
    pub fn set_oem(&mut self, oem: OcrEngineMode) {
        self.config.oem = oem;
        self.initialized = false;
    }

    /// Get available languages
    pub fn available_languages(&self) -> Vec<String> {
        // This would query the actual OCR engine
        vec!["eng".to_string()]
    }
}

// ============================================================================
// FFI Functions
// ============================================================================

/// Create a new OCR engine
#[unsafe(no_mangle)]
pub extern "C" fn fz_new_ocr_engine(_ctx: Handle, engine_type: c_int) -> Handle {
    let config = OcrConfig {
        engine_type: match engine_type {
            1 => OcrEngineType::Tesseract,
            2 => OcrEngineType::WindowsOcr,
            3 => OcrEngineType::AppleVision,
            _ => OcrEngineType::None,
        },
        ..Default::default()
    };

    let engine = OcrEngine::new(config);
    OCR_ENGINES.insert(engine)
}

/// Drop an OCR engine
#[unsafe(no_mangle)]
pub extern "C" fn fz_drop_ocr_engine(_ctx: Handle, engine: Handle) {
    OCR_ENGINES.remove(engine);
}

/// Initialize an OCR engine
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_engine_init(_ctx: Handle, engine: Handle) -> c_int {
    if let Some(arc) = OCR_ENGINES.get(engine) {
        if let Ok(mut e) = arc.lock() {
            return if e.init().is_ok() { 1 } else { 0 };
        }
    }
    0
}

/// Check if OCR engine is initialized
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_engine_is_initialized(_ctx: Handle, engine: Handle) -> c_int {
    if let Some(arc) = OCR_ENGINES.get(engine) {
        if let Ok(e) = arc.lock() {
            return if e.is_initialized() { 1 } else { 0 };
        }
    }
    0
}

/// Set OCR language
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_engine_set_language(
    _ctx: Handle,
    engine: Handle,
    lang: *const c_char,
) -> c_int {
    if lang.is_null() {
        return 0;
    }

    let lang_str = unsafe { CStr::from_ptr(lang) };
    let lang_str = match lang_str.to_str() {
        Ok(s) => s,
        Err(_) => return 0,
    };

    if let Some(arc) = OCR_ENGINES.get(engine) {
        if let Ok(mut e) = arc.lock() {
            e.set_language(lang_str);
            return 1;
        }
    }
    0
}

/// Get OCR language (returns allocated string)
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_engine_get_language(_ctx: Handle, engine: Handle) -> *mut c_char {
    if let Some(arc) = OCR_ENGINES.get(engine) {
        if let Ok(e) = arc.lock() {
            if let Ok(s) = CString::new(e.language()) {
                return s.into_raw();
            }
        }
    }
    std::ptr::null_mut()
}

/// Set page segmentation mode
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_engine_set_psm(_ctx: Handle, engine: Handle, psm: c_int) {
    let mode = match psm {
        0 => OcrPageSegMode::OsdOnly,
        1 => OcrPageSegMode::AutoOsd,
        2 => OcrPageSegMode::AutoOnly,
        3 => OcrPageSegMode::Auto,
        4 => OcrPageSegMode::SingleColumn,
        5 => OcrPageSegMode::SingleBlockVertText,
        6 => OcrPageSegMode::SingleBlock,
        7 => OcrPageSegMode::SingleLine,
        8 => OcrPageSegMode::SingleWord,
        9 => OcrPageSegMode::CircleWord,
        10 => OcrPageSegMode::SingleChar,
        11 => OcrPageSegMode::SparseText,
        12 => OcrPageSegMode::SparseTextOsd,
        13 => OcrPageSegMode::RawLine,
        _ => OcrPageSegMode::Auto,
    };

    if let Some(arc) = OCR_ENGINES.get(engine) {
        if let Ok(mut e) = arc.lock() {
            e.set_psm(mode);
        }
    }
}

/// Create a new OCR result
#[unsafe(no_mangle)]
pub extern "C" fn fz_new_ocr_result(_ctx: Handle, width: u32, height: u32) -> Handle {
    let result = OcrResult::new(width, height);
    OCR_RESULTS.insert(result)
}

/// Drop an OCR result
#[unsafe(no_mangle)]
pub extern "C" fn fz_drop_ocr_result(_ctx: Handle, result: Handle) {
    OCR_RESULTS.remove(result);
}

/// Get OCR result text (returns allocated string)
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_result_text(_ctx: Handle, result: Handle) -> *mut c_char {
    if let Some(arc) = OCR_RESULTS.get(result) {
        if let Ok(r) = arc.lock() {
            if let Ok(s) = CString::new(r.text()) {
                return s.into_raw();
            }
        }
    }
    std::ptr::null_mut()
}

/// Get OCR result confidence
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_result_confidence(_ctx: Handle, result: Handle) -> c_int {
    if let Some(arc) = OCR_RESULTS.get(result) {
        if let Ok(r) = arc.lock() {
            return r.confidence;
        }
    }
    0
}

/// Get OCR result word count
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_result_word_count(_ctx: Handle, result: Handle) -> c_int {
    if let Some(arc) = OCR_RESULTS.get(result) {
        if let Ok(r) = arc.lock() {
            return r.word_count() as c_int;
        }
    }
    0
}

/// Get OCR result line count
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_result_line_count(_ctx: Handle, result: Handle) -> c_int {
    if let Some(arc) = OCR_RESULTS.get(result) {
        if let Ok(r) = arc.lock() {
            return r.line_count() as c_int;
        }
    }
    0
}

/// Free an OCR string
#[unsafe(no_mangle)]
pub extern "C" fn fz_free_ocr_string(_ctx: Handle, s: *mut c_char) {
    if !s.is_null() {
        unsafe {
            drop(CString::from_raw(s));
        }
    }
}

/// Check if OCR is available
#[unsafe(no_mangle)]
pub extern "C" fn fz_ocr_is_available(_ctx: Handle, engine_type: c_int) -> c_int {
    // Only the no-op engine is available without external OCR libraries
    if engine_type == 0 { 1 } else { 0 }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_ocr_config_default() {
        let config = OcrConfig::default();
        assert_eq!(config.engine_type, OcrEngineType::None);
        assert_eq!(config.language, "eng");
        assert_eq!(config.dpi, 300);
    }

    #[test]
    fn test_ocr_word() {
        let word = OcrWord::new("hello", Rect::new(0.0, 0.0, 50.0, 20.0), 95);
        assert_eq!(word.text, "hello");
        assert_eq!(word.confidence, 95);
    }

    #[test]
    fn test_ocr_line() {
        let mut line = OcrLine::new(Rect::new(0.0, 0.0, 200.0, 20.0));
        line.add_word(OcrWord::new("hello", Rect::new(0.0, 0.0, 50.0, 20.0), 90));
        line.add_word(OcrWord::new("world", Rect::new(60.0, 0.0, 120.0, 20.0), 80));

        assert_eq!(line.text, "hello world");
        assert_eq!(line.words.len(), 2);
        assert_eq!(line.confidence, 85); // Average
    }

    #[test]
    fn test_ocr_block() {
        let mut block = OcrBlock::new(Rect::new(0.0, 0.0, 200.0, 100.0));
        let mut line = OcrLine::new(Rect::new(0.0, 0.0, 200.0, 20.0));
        line.add_word(OcrWord::new("test", Rect::new(0.0, 0.0, 40.0, 20.0), 100));
        block.add_line(line);

        assert_eq!(block.text(), "test");
    }

    #[test]
    fn test_ocr_result() {
        let mut result = OcrResult::new(100, 100);
        assert_eq!(result.word_count(), 0);
        assert_eq!(result.confidence, 0);

        let mut block = OcrBlock::new(Rect::new(0.0, 0.0, 100.0, 50.0));
        let mut line = OcrLine::new(Rect::new(0.0, 0.0, 100.0, 20.0));
        line.add_word(OcrWord::new("OCR", Rect::new(0.0, 0.0, 30.0, 20.0), 100));
        block.add_line(line);
        result.add_block(block);

        assert_eq!(result.word_count(), 1);
        assert_eq!(result.confidence, 100);
        assert_eq!(result.text(), "OCR");
    }

    #[test]
    fn test_ocr_engine_noop() {
        let config = OcrConfig::default();
        let mut engine = OcrEngine::new(config);

        assert!(!engine.is_initialized());
        assert!(engine.init().is_ok());
        assert!(engine.is_initialized());
    }

    #[test]
    fn test_ocr_engine_ffi() {
        let handle = fz_new_ocr_engine(0, 0); // No-op engine
        assert!(handle != 0);

        let init_result = fz_ocr_engine_init(0, handle);
        assert_eq!(init_result, 1);

        let is_init = fz_ocr_engine_is_initialized(0, handle);
        assert_eq!(is_init, 1);

        fz_drop_ocr_engine(0, handle);
    }

    #[test]
    fn test_ocr_result_ffi() {
        let handle = fz_new_ocr_result(0, 100, 100);
        assert!(handle != 0);

        let word_count = fz_ocr_result_word_count(0, handle);
        assert_eq!(word_count, 0);

        let confidence = fz_ocr_result_confidence(0, handle);
        assert_eq!(confidence, 0);

        fz_drop_ocr_result(0, handle);
    }

    #[test]
    fn test_ocr_availability() {
        assert_eq!(fz_ocr_is_available(0, 0), 1);
        assert_eq!(fz_ocr_is_available(0, 1), 0);
    }

    #[test]
    fn test_ocr_engine_init_invalid() {
        assert_eq!(fz_ocr_engine_init(0, 0), 0);
        assert_eq!(fz_ocr_engine_init(0, 99999), 0);
    }

    #[test]
    fn test_ocr_engine_is_initialized_invalid() {
        assert_eq!(fz_ocr_engine_is_initialized(0, 0), 0);
    }

    #[test]
    fn test_ocr_engine_set_language_null() {
        let h = fz_new_ocr_engine(0, 0);
        assert_eq!(fz_ocr_engine_set_language(0, h, std::ptr::null()), 0);
        fz_drop_ocr_engine(0, h);
    }

    #[test]
    fn test_ocr_engine_set_language_valid() {
        let h = fz_new_ocr_engine(0, 0);
        let lang = std::ffi::CString::new("fra").unwrap();
        assert_eq!(fz_ocr_engine_set_language(0, h, lang.as_ptr()), 1);
        let out = fz_ocr_engine_get_language(0, h);
        assert!(!out.is_null());
        fz_free_ocr_string(0, out);
        fz_drop_ocr_engine(0, h);
    }

    #[test]
    fn test_ocr_engine_get_language_invalid() {
        assert!(fz_ocr_engine_get_language(0, 0).is_null());
    }

    #[test]
    fn test_ocr_engine_set_psm() {
        let h = fz_new_ocr_engine(0, 0);
        fz_ocr_engine_set_psm(0, h, 7);
        fz_ocr_engine_set_psm(0, h, 99);
        fz_drop_ocr_engine(0, h);
    }

    #[test]
    fn test_ocr_result_invalid() {
        assert_eq!(fz_ocr_result_confidence(0, 0), 0);
        assert_eq!(fz_ocr_result_word_count(0, 0), 0);
        assert_eq!(fz_ocr_result_line_count(0, 0), 0);
        assert!(fz_ocr_result_text(0, 0).is_null());
    }

    #[test]
    fn test_ocr_result_text_empty() {
        let h = fz_new_ocr_result(0, 100, 100);
        let text = fz_ocr_result_text(0, h);
        assert!(!text.is_null());
        let s = unsafe { std::ffi::CStr::from_ptr(text).to_str().unwrap() };
        assert!(s.is_empty());
        fz_free_ocr_string(0, text);
        fz_drop_ocr_result(0, h);
    }

    #[test]
    fn test_fz_free_ocr_string_null() {
        fz_free_ocr_string(0, std::ptr::null_mut());
    }

    #[test]
    fn test_ocr_engine_tesseract_init_fails() {
        let h = fz_new_ocr_engine(0, 1);
        assert_eq!(fz_ocr_engine_init(0, h), 0);
        fz_drop_ocr_engine(0, h);
    }
}