pub fn hocr_y_to_pdf(pixel_y: f32, page_height_pt: f32, image_dpi: f32) -> f32 {
let pt_per_px = 72.0 / image_dpi;
page_height_pt - (pixel_y * pt_per_px)
}
pub fn hocr_x_to_pdf(pixel_x: f32, image_dpi: f32) -> f32 {
pixel_x * 72.0 / image_dpi
}
pub fn pixel_size_to_pt(pixel_size: f32, image_dpi: f32) -> f32 {
pixel_size * 72.0 / image_dpi
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn y_conversion_top_of_page() {
let pdf_y = hocr_y_to_pdf(0.0, 842.0, 72.0);
assert!((pdf_y - 842.0).abs() < 0.01);
}
#[test]
fn y_conversion_bottom_of_page() {
let dpi = 72.0_f32;
let page_h_pt = 842.0_f32;
let page_h_px = page_h_pt; let pdf_y = hocr_y_to_pdf(page_h_px, page_h_pt, dpi);
assert!(pdf_y.abs() < 0.01);
}
#[test]
fn x_conversion_300dpi() {
let pdf_x = hocr_x_to_pdf(300.0, 300.0);
assert!((pdf_x - 72.0).abs() < 0.01);
}
}