pdfpurr 0.4.0

A comprehensive pure-Rust PDF library for reading, writing, rendering, and validating PDF documents
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
//! Windows OCR engine via the built-in Windows.Media.Ocr API.
//!
//! Uses PowerShell to invoke the Windows Runtime OCR API, which provides
//! high-quality recognition with no additional software installation.
//! Available on Windows 10 and later.
//!
//! # Requirements
//!
//! - Windows 10 or later
//! - PowerShell 5.1+ (included with Windows)
//! - Language packs for non-English recognition (English is always available)

use std::io::Write;
use std::process::Command;

use super::engine::{OcrEngine, OcrImage, OcrResult, OcrWord};
use crate::error::{PdfError, PdfResult};

/// OCR engine backed by the Windows.Media.Ocr API.
///
/// Invokes PowerShell to run Windows Runtime OCR on a temporary BMP image.
/// Returns words with bounding boxes and confidence scores.
///
/// # Example
///
/// ```no_run
/// use pdfpurr::ocr::windows_engine::WindowsOcrEngine;
/// use pdfpurr::ocr::OcrEngine;
///
/// let engine = WindowsOcrEngine::new("en-US");
/// ```
pub struct WindowsOcrEngine {
    /// BCP 47 language tag (e.g., "en-US", "de-DE", "ja").
    language: String,
}

impl WindowsOcrEngine {
    /// Creates a new Windows OCR engine for the given language.
    ///
    /// The language must be installed on the system. English ("en-US")
    /// is always available on Windows 10+.
    pub fn new(language: &str) -> Self {
        Self {
            language: language.to_string(),
        }
    }

    /// Creates a Windows OCR engine for English.
    pub fn english() -> Self {
        Self::new("en-US")
    }
}

impl OcrEngine for WindowsOcrEngine {
    fn recognize(&self, image: &OcrImage) -> PdfResult<OcrResult> {
        // Write image as RGB PNG (WinRT BitmapDecoder may not handle grayscale PNGs).
        let temp_dir = std::env::temp_dir();
        let bmp_path = temp_dir.join(super::constants::TEMP_INPUT_PNG);
        write_rgb_png(&bmp_path, image)?;

        // PowerShell script that invokes Windows OCR and outputs JSON.
        // Uses WindowsRuntime interop with proper async/await pattern.
        let ps_script = format!(
            r#"
Add-Type -AssemblyName System.Runtime.WindowsRuntime

# Load WinRT types
$null = [Windows.Media.Ocr.OcrEngine,Windows.Foundation,ContentType=WindowsRuntime]
$null = [Windows.Graphics.Imaging.BitmapDecoder,Windows.Foundation,ContentType=WindowsRuntime]
$null = [Windows.Storage.StorageFile,Windows.Foundation,ContentType=WindowsRuntime]

# Load WinRT async extensions via reflection (reliable in subprocess mode)
$asm = [System.Reflection.Assembly]::LoadWithPartialName('System.Runtime.WindowsRuntime')
$extType = $asm.GetType('System.WindowsRuntimeSystemExtensions')
$asTask = ($extType.GetMethods() | Where-Object {{
    $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and
    $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation``1'
}})[0]

function AwaitT($AsyncOp, [Type]$ResultType) {{
    $task = $asTask.MakeGenericMethod($ResultType).Invoke($null, @($AsyncOp))
    $task.Wait()
    $task.Result
}}

# Create engine — TryCreateFromUserProfileLanguages is most reliable
$engine = [Windows.Media.Ocr.OcrEngine]::TryCreateFromUserProfileLanguages()
if ($engine -eq $null) {{
    # Fall back to specific language
    try {{
        $lang = [Windows.Globalization.Language]::new("{lang}")
        $engine = [Windows.Media.Ocr.OcrEngine]::TryCreateFromLanguage($lang)
    }} catch {{}}
}}
if ($engine -eq $null) {{
    Write-Error "No OCR engine available"
    exit 1
}}

$path = "{bmp_path}"
$file = AwaitT ([Windows.Storage.StorageFile]::GetFileFromPathAsync($path)) ([Windows.Storage.StorageFile])
$stream = AwaitT ($file.OpenAsync([Windows.Storage.FileAccessMode]::Read)) ([Windows.Storage.Streams.IRandomAccessStream])
$decoder = AwaitT ([Windows.Graphics.Imaging.BitmapDecoder]::CreateAsync($stream)) ([Windows.Graphics.Imaging.BitmapDecoder])
$bitmap = AwaitT ($decoder.GetSoftwareBitmapAsync()) ([Windows.Graphics.Imaging.SoftwareBitmap])
$result = AwaitT ($engine.RecognizeAsync($bitmap)) ([Windows.Media.Ocr.OcrResult])

$stream.Dispose()

$words = @()
foreach ($line in $result.Lines) {{
    foreach ($word in $line.Words) {{
        $rect = $word.BoundingRect
        $words += @{{
            text = $word.Text
            x = [int]$rect.X
            y = [int]$rect.Y
            width = [int]$rect.Width
            height = [int]$rect.Height
        }}
    }}
}}

$output = @{{ words = $words }} | ConvertTo-Json -Depth 3 -Compress
Write-Output $output
"#,
            lang = self.language,
            bmp_path = bmp_path.to_string_lossy().replace('\\', "/"),
        );

        // Must use Windows PowerShell 5.1 (not pwsh/PS 7) for WinRT type projection.
        // PS 7 removed WinRT support. Use "powershell" (not "pwsh") to get 5.1.
        let output = Command::new(super::constants::POWERSHELL_CMD)
            .args(["-NoProfile", "-NonInteractive", "-Command", &ps_script])
            .output()
            .map_err(|e| PdfError::OcrError(format!("Failed to run PowerShell: {e}")))?;

        // Clean up temp file (keep on failure for debugging)
        if output.status.success() {
            let _ = std::fs::remove_file(&bmp_path);
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        if !output.status.success() {
            return Err(PdfError::OcrError(format!(
                "Windows OCR failed: {stderr}\nstdout: {stdout}"
            )));
        }

        parse_windows_ocr_output(&stdout, image.width, image.height)
    }
}

/// Writes a grayscale image as RGB PNG (WinRT BitmapDecoder handles RGB reliably).
fn write_rgb_png(path: &std::path::Path, image: &OcrImage) -> PdfResult<()> {
    let file = std::fs::File::create(path)
        .map_err(|e| PdfError::OcrError(format!("Cannot create PNG: {e}")))?;
    let w = std::io::BufWriter::new(file);

    let mut encoder = png::Encoder::new(w, image.width, image.height);
    encoder.set_color(png::ColorType::Rgb);
    encoder.set_depth(png::BitDepth::Eight);

    // Expand grayscale → RGB (triple each byte)
    let rgb_data: Vec<u8> = image.data.iter().flat_map(|&g| [g, g, g]).collect();

    let mut writer = encoder
        .write_header()
        .map_err(|e| PdfError::OcrError(format!("PNG header error: {e}")))?;
    writer
        .write_image_data(&rgb_data)
        .map_err(|e| PdfError::OcrError(format!("PNG write error: {e}")))?;

    Ok(())
}

/// Writes a grayscale image as a 8-bit BMP file (kept for platforms where PNG crate unavailable).
#[allow(dead_code)]
fn write_grayscale_bmp(path: &std::path::Path, image: &OcrImage) -> PdfResult<()> {
    let w = image.width as usize;
    let h = image.height as usize;

    // BMP row stride must be multiple of 4
    let row_stride = (w + 3) & !3;
    let pixel_data_size = row_stride * h;

    // BMP file header (14 bytes) + DIB header (40 bytes) + color table (256 * 4 bytes)
    let color_table_size = 256 * 4;
    let header_size = 14 + 40 + color_table_size;
    let file_size = header_size + pixel_data_size;

    let mut file = std::fs::File::create(path)
        .map_err(|e| PdfError::OcrError(format!("Cannot create BMP: {e}")))?;

    // BMP file header
    file.write_all(b"BM")?; // signature
    file.write_all(&(file_size as u32).to_le_bytes())?;
    file.write_all(&0u32.to_le_bytes())?; // reserved
    file.write_all(&(header_size as u32).to_le_bytes())?; // pixel data offset

    // DIB header (BITMAPINFOHEADER)
    file.write_all(&40u32.to_le_bytes())?; // header size
    file.write_all(&(w as i32).to_le_bytes())?; // width
    file.write_all(&(-(h as i32)).to_le_bytes())?; // height (negative = top-down)
    file.write_all(&1u16.to_le_bytes())?; // planes
    file.write_all(&8u16.to_le_bytes())?; // bits per pixel
    file.write_all(&0u32.to_le_bytes())?; // compression (none)
    file.write_all(&(pixel_data_size as u32).to_le_bytes())?;
    file.write_all(&2835u32.to_le_bytes())?; // x pixels per meter (72 DPI)
    file.write_all(&2835u32.to_le_bytes())?; // y pixels per meter
    file.write_all(&256u32.to_le_bytes())?; // colors used
    file.write_all(&0u32.to_le_bytes())?; // important colors

    // Grayscale color table (256 entries: R,G,B,0)
    for i in 0..=255u8 {
        file.write_all(&[i, i, i, 0])?;
    }

    // Pixel data (top-down, padded rows)
    let padding = [0u8; 4];
    let pad_bytes = row_stride - w;
    for y in 0..h {
        let row_start = y * w;
        let row_end = row_start + w;
        file.write_all(&image.data[row_start..row_end])?;
        if pad_bytes > 0 {
            file.write_all(&padding[..pad_bytes])?;
        }
    }

    Ok(())
}

/// Parses the JSON output from the PowerShell OCR script.
fn parse_windows_ocr_output(
    json: &str,
    image_width: u32,
    image_height: u32,
) -> PdfResult<OcrResult> {
    let json = json.trim();
    if json.is_empty() {
        return Ok(OcrResult {
            words: Vec::new(),
            image_width,
            image_height,
        });
    }

    // Simple JSON parsing — avoid adding serde_json as required dep.
    // Format: {"words":[{"text":"Hello","x":10,"y":20,"width":80,"height":25},...]}
    let mut words = Vec::new();

    // Find the words array
    let words_start = match json.find('[') {
        Some(i) => i,
        None => {
            return Ok(OcrResult {
                words,
                image_width,
                image_height,
            })
        }
    };
    let words_end = match json.rfind(']') {
        Some(i) => i,
        None => {
            return Ok(OcrResult {
                words,
                image_width,
                image_height,
            })
        }
    };
    if words_start >= words_end {
        return Ok(OcrResult {
            words,
            image_width,
            image_height,
        });
    }

    let words_str = &json[words_start + 1..words_end];

    // Split by },{ to get individual word objects
    for obj_str in words_str.split("},{") {
        let obj = obj_str.trim().trim_start_matches('{').trim_end_matches('}');

        // Skip entries with missing required fields instead of using defaults
        let text = match extract_json_string(obj, "text") {
            Some(t) if !t.is_empty() => t,
            _ => continue,
        };
        let x = match extract_json_int(obj, "x") {
            Some(v) => v,
            None => continue,
        };
        let y = match extract_json_int(obj, "y") {
            Some(v) => v,
            None => continue,
        };
        let width = match extract_json_int(obj, "width") {
            Some(v) if v > 0 => v,
            _ => continue,
        };
        let height = match extract_json_int(obj, "height") {
            Some(v) if v > 0 => v,
            _ => continue,
        };

        {
            words.push(OcrWord {
                text,
                x: x as u32,
                y: y as u32,
                width: width as u32,
                height: height as u32,
                confidence: 0.9, // Windows OCR doesn't expose per-word confidence
            });
        }
    }

    Ok(OcrResult {
        words,
        image_width,
        image_height,
    })
}

fn extract_json_string(obj: &str, key: &str) -> Option<String> {
    let pattern = format!("\"{}\":\"", key);
    let start = obj.find(&pattern)? + pattern.len();
    let end = obj[start..].find('"')? + start;
    Some(obj[start..end].to_string())
}

fn extract_json_int(obj: &str, key: &str) -> Option<i64> {
    let pattern = format!("\"{}\":", key);
    let start = obj.find(&pattern)? + pattern.len();
    let num_str: String = obj[start..]
        .chars()
        .take_while(|c| c.is_ascii_digit() || *c == '-')
        .collect();
    num_str.parse().ok()
}

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

    #[test]
    fn parse_empty_json() {
        let result = parse_windows_ocr_output("", 100, 100).unwrap();
        assert!(result.words.is_empty());
    }

    #[test]
    fn parse_single_word() {
        let json = r#"{"words":[{"text":"Hello","x":10,"y":20,"width":80,"height":25}]}"#;
        let result = parse_windows_ocr_output(json, 800, 600).unwrap();
        assert_eq!(result.words.len(), 1);
        assert_eq!(result.words[0].text, "Hello");
        assert_eq!(result.words[0].x, 10);
        assert_eq!(result.words[0].y, 20);
        assert_eq!(result.words[0].width, 80);
        assert_eq!(result.words[0].height, 25);
    }

    #[test]
    fn parse_multiple_words() {
        let json = r#"{"words":[{"text":"Hello","x":10,"y":20,"width":80,"height":25},{"text":"World","x":100,"y":20,"width":90,"height":25}]}"#;
        let result = parse_windows_ocr_output(json, 800, 600).unwrap();
        assert_eq!(result.words.len(), 2);
        assert_eq!(result.words[0].text, "Hello");
        assert_eq!(result.words[1].text, "World");
    }

    #[test]
    fn write_and_verify_bmp() {
        let image = OcrImage {
            data: vec![128; 100],
            width: 10,
            height: 10,
        };
        let path = std::env::temp_dir().join("pdfpurr_test_bmp.bmp");
        write_grayscale_bmp(&path, &image).unwrap();
        let data = std::fs::read(&path).unwrap();
        assert_eq!(&data[0..2], b"BM"); // BMP signature
        std::fs::remove_file(&path).unwrap();
    }

    #[test]
    fn extract_json_fields() {
        let obj = r#""text":"Hello","x":42,"y":10,"width":80,"height":25"#;
        assert_eq!(extract_json_string(obj, "text"), Some("Hello".to_string()));
        assert_eq!(extract_json_int(obj, "x"), Some(42));
        assert_eq!(extract_json_int(obj, "width"), Some(80));
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_ocr_engine_creates() {
        let engine = WindowsOcrEngine::english();
        assert_eq!(engine.language, "en-US");
    }
}