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
use std::convert::TryInto; use std::ffi::CString; use std::io::Read; use std::ptr; use std::slice; use bitflags::bitflags; use mupdf_sys::*; use num_enum::TryFromPrimitive; use crate::{context, Buffer, Error, Image, Matrix, Point, Quad, Rect, WriteMode}; bitflags! { pub struct TextPageOptions: u32 { const BLOCK_IMAGE = FZ_STEXT_BLOCK_IMAGE as _; const BLOCK_TEXT = FZ_STEXT_BLOCK_TEXT as _; const INHIBIT_SPACES = FZ_STEXT_INHIBIT_SPACES as _; const PRESERVE_IMAGES = FZ_STEXT_PRESERVE_IMAGES as _; const PRESERVE_LIGATURES = FZ_STEXT_PRESERVE_LIGATURES as _; const PRESERVE_WHITESPACE = FZ_STEXT_PRESERVE_WHITESPACE as _; } } #[derive(Debug)] pub struct TextPage { pub(crate) inner: *mut fz_stext_page, } impl TextPage { pub(crate) unsafe fn from_raw(ptr: *mut fz_stext_page) -> Self { Self { inner: ptr } } pub fn to_text(&self) -> Result<String, Error> { let mut buf = unsafe { let inner = ffi_try!(mupdf_stext_page_to_text(context(), self.inner)); Buffer::from_raw(inner) }; let mut text = String::new(); buf.read_to_string(&mut text)?; Ok(text) } pub fn blocks(&self) -> TextBlockIter { TextBlockIter { next: unsafe { (*self.inner).first_block }, } } pub fn search(&self, needle: &str, hit_max: u32) -> Result<Vec<Quad>, Error> { struct Quads(*mut fz_quad); impl Drop for Quads { fn drop(&mut self) { if !self.0.is_null() { unsafe { fz_free(context(), self.0 as _) }; } } } let c_needle = CString::new(needle)?; let hit_max = if hit_max < 1 { 16 } else { hit_max }; let mut hit_count = 0; unsafe { let quads = Quads(ffi_try!(mupdf_search_stext_page( context(), self.inner, c_needle.as_ptr(), hit_max as _, &mut hit_count ))); if hit_count == 0 { return Ok(Vec::new()); } let items = slice::from_raw_parts(quads.0, hit_count as usize); Ok(items.iter().map(|quad| (*quad).into()).collect()) } } } impl Drop for TextPage { fn drop(&mut self) { if !self.inner.is_null() { unsafe { fz_drop_stext_page(context(), self.inner); } } } } #[derive(Debug, Clone, Copy, PartialEq, TryFromPrimitive)] #[repr(u32)] pub enum TextBlockType { Text = FZ_STEXT_BLOCK_TEXT as u32, Image = FZ_STEXT_BLOCK_IMAGE as u32, } #[derive(Debug)] pub struct TextBlock { inner: *mut fz_stext_block, } impl TextBlock { pub fn r#type(&self) -> TextBlockType { unsafe { ((*self.inner).type_ as u32).try_into().unwrap() } } pub fn bounds(&self) -> Rect { unsafe { (*self.inner).bbox.into() } } pub fn lines(&self) -> TextLineIter { unsafe { if (*self.inner).type_ == FZ_STEXT_BLOCK_TEXT as _ { return TextLineIter { next: (*self.inner).u.t.first_line, }; } } TextLineIter { next: ptr::null_mut(), } } pub fn ctm(&self) -> Option<Matrix> { unsafe { if (*self.inner).type_ == FZ_STEXT_BLOCK_IMAGE as _ { return Some((*self.inner).u.i.transform.into()); } } None } pub fn image(&self) -> Option<Image> { unsafe { if (*self.inner).type_ == FZ_STEXT_BLOCK_IMAGE as _ { let inner = (*self.inner).u.i.image; fz_keep_image(context(), inner); return Some(Image::from_raw(inner)); } } None } } #[derive(Debug)] pub struct TextBlockIter { next: *mut fz_stext_block, } impl Iterator for TextBlockIter { type Item = TextBlock; fn next(&mut self) -> Option<Self::Item> { if self.next.is_null() { return None; } let node = self.next; self.next = unsafe { (*node).next }; Some(TextBlock { inner: node }) } } #[derive(Debug)] pub struct TextLine { inner: *mut fz_stext_line, } impl TextLine { pub fn bounds(&self) -> Rect { unsafe { (*self.inner).bbox.into() } } pub fn wmode(&self) -> WriteMode { unsafe { ((*self.inner).wmode as u32).try_into().unwrap() } } pub fn chars(&self) -> TextCharIter { TextCharIter { next: unsafe { (*self.inner).first_char }, } } } #[derive(Debug)] pub struct TextLineIter { next: *mut fz_stext_line, } impl Iterator for TextLineIter { type Item = TextLine; fn next(&mut self) -> Option<Self::Item> { if self.next.is_null() { return None; } let node = self.next; self.next = unsafe { (*node).next }; Some(TextLine { inner: node }) } } #[derive(Debug)] pub struct TextChar { inner: *mut fz_stext_char, } impl TextChar { pub fn char(&self) -> Option<char> { std::char::from_u32(unsafe { (*self.inner).c as u32 }) } pub fn origin(&self) -> Point { unsafe { (*self.inner).origin.into() } } pub fn size(&self) -> f32 { unsafe { (*self.inner).size } } } #[derive(Debug)] pub struct TextCharIter { next: *mut fz_stext_char, } impl Iterator for TextCharIter { type Item = TextChar; fn next(&mut self) -> Option<Self::Item> { if self.next.is_null() { return None; } let node = self.next; self.next = unsafe { (*node).next }; Some(TextChar { inner: node }) } } #[cfg(test)] mod test { use crate::{Document, TextPageOptions}; #[test] fn test_text_page_search() { use crate::{Point, Quad}; let doc = Document::open("tests/files/dummy.pdf").unwrap(); let page0 = doc.load_page(0).unwrap(); let text_page = page0.to_text_page(TextPageOptions::BLOCK_IMAGE).unwrap(); let hits = text_page.search("Dummy", 1).unwrap(); assert_eq!(hits.len(), 1); assert_eq!( hits, [Quad { ul: Point { x: 56.8, y: 69.32512 }, ur: Point { x: 115.85405, y: 69.32512 }, ll: Point { x: 56.8, y: 87.311844 }, lr: Point { x: 115.85405, y: 87.311844 } }] ); let hits = text_page.search("Not Found", 1).unwrap(); assert_eq!(hits.len(), 0); } }