Skip to content

Add initial OCR support in document_writer #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- run: sudo apt-get -y install libfontconfig1-dev
if: matrix.os == 'ubuntu-latest'

- name: Download tesseract training data
run: curl -LO https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata

- run: cargo test --features serde

- name: Test package mupdf-sys
Expand Down Expand Up @@ -90,9 +93,14 @@ jobs:
with:
submodules: "recursive"
fetch-depth: 500
- run: |
rustc --version --verbose
cargo test --features serde

- name: Download tesseract training data
run: curl -LO https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata

- run: rustc --version --verbose
- run: cargo test --features serde
env:
CARGO_TERM_COLOR: always

asan:
name: Address Sanitizer
Expand All @@ -105,10 +113,14 @@ jobs:
- uses: dtolnay/rust-toolchain@nightly
with:
components: rust-src

- run: sudo apt-get -y install libfontconfig1-dev llvm

- name: Download tesseract training data
run: curl -LO https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata

- name: cargo test --features serde
run: |
cargo test -Zbuild-std --target x86_64-unknown-linux-gnu --features serde
run: cargo test -Zbuild-std --target x86_64-unknown-linux-gnu --features serde
env:
RUSTFLAGS: -Zsanitizer=address
LSAN_OPTIONS: report_objects=1:suppressions=lsan_suppressions.txt
Expand All @@ -123,7 +135,12 @@ jobs:
fetch-depth: 500
- uses: dtolnay/rust-toolchain@stable
- uses: taiki-e/install-action@valgrind

- run: sudo apt-get -y install libfontconfig1-dev

- name: Download tesseract training data
run: curl -LO https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata

- run: cargo test --features serde
env:
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER: "valgrind --error-exitcode=1 --track-origins=yes"
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ target
Cargo.lock
**/*.rs.bk
/build
tests/output/*.png
tests/output/*
14 changes: 14 additions & 0 deletions mupdf-sys/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,20 @@ fz_document_writer *mupdf_new_document_writer(fz_context *ctx, const char *filen
return writer;
}

fz_document_writer *mupdf_new_pdfocr_writer(fz_context *ctx, const char *path, const char *options, mupdf_error_t **errptr)
{
fz_document_writer *writer = NULL;
fz_try(ctx)
{
writer = fz_new_pdfocr_writer(ctx, path, options);
}
fz_catch(ctx)
{
mupdf_save_error(ctx, errptr);
}
return writer;
}

fz_device *mupdf_document_writer_begin_page(fz_context *ctx, fz_document_writer *writer, fz_rect mediabox, mupdf_error_t **errptr)
{
fz_device *device = NULL;
Expand Down
69 changes: 66 additions & 3 deletions src/document_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ use std::ptr;

use mupdf_sys::*;

use crate::{context, Device, Error, Rect};
use crate::{context, Device, Error, FilePath, Rect};

#[derive(Debug)]
pub struct DocumentWriter {
inner: *mut fz_document_writer,
}

impl DocumentWriter {
pub fn new(filename: &str, format: &str, options: &str) -> Result<Self, Error> {
let c_filename = CString::new(filename)?;
pub fn new<P: AsRef<FilePath> + ?Sized>(
filename: &P,
format: &str,
options: &str,
) -> Result<Self, Error> {
let c_filename = CString::new(filename.as_ref().as_bytes())?;
let c_format = CString::new(format)?;
let c_options = CString::new(options)?;
unsafe {
Expand All @@ -26,6 +30,21 @@ impl DocumentWriter {
.map(|inner| Self { inner })
}

#[cfg(feature = "tesseract")]
pub fn with_ocr<P: AsRef<FilePath> + ?Sized>(path: &P, options: &str) -> Result<Self, Error> {
let c_path = CString::new(path.as_ref().as_bytes())?;
let c_options = CString::new(options)?;

unsafe {
ffi_try!(mupdf_new_pdfocr_writer(
context(),
c_path.as_ptr(),
c_options.as_ptr()
))
}
.map(|inner| Self { inner })
}

pub fn begin_page(&mut self, media_box: Rect) -> Result<Device, Error> {
unsafe {
ffi_try!(mupdf_document_writer_begin_page(
Expand Down Expand Up @@ -57,3 +76,47 @@ impl Drop for DocumentWriter {
}
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod test {
use crate::{pdf::PdfDocument, ColorParams, Image, Matrix, Rect};

use super::DocumentWriter;

#[test]
fn test_writer_ocr() {
let output = "tests/output/ocr.pdf";

{
let mut writer = DocumentWriter::with_ocr(output, "").unwrap();

let image = Image::from_file("tests/files/ocr.png").unwrap();
let width = image.width() as f32;
let height = image.height() as f32;

let device = writer
.begin_page(Rect {
x0: 0.0,
y0: 0.0,
x1: width,
y1: height,
})
.unwrap();
device
.fill_image(
&image,
&Matrix::new_scale(width, height),
1.0,
ColorParams::default(),
)
.unwrap();
writer.end_page(device).unwrap();
}

let doc = PdfDocument::open(output).unwrap();
let page = doc.load_page(0).unwrap();
let res = page.search("A short OCR test", 0).unwrap();
assert_eq!(res.len(), 1);
}
}
Empty file modified tests/files/i32-box.pdf
100755 → 100644
Empty file.
Empty file modified tests/files/no-json.pdf
100755 → 100644
Empty file.
Binary file added tests/files/ocr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 1 addition & 10 deletions tests/test_issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ fn test_issue_27_flatten() {
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn test_issue_43_malloc() {
const IDENTITY: mupdf::Matrix = mupdf::Matrix {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
};

let density = 300;
let height = 1500;
let options = format!("resolution={},height={}", density, height);
Expand All @@ -57,7 +48,7 @@ fn test_issue_43_malloc() {
let page0 = doc.load_page(0).unwrap();
let mediabox = page0.bounds().unwrap();
let device = writer.begin_page(mediabox).unwrap();
page0.run(&device, &IDENTITY).unwrap();
page0.run(&device, &mupdf::Matrix::IDENTITY).unwrap();
writer.end_page(device).unwrap();
}
}
Expand Down