1use std::collections::{HashMap, HashSet};
10use std::sync::Arc;
11
12use crate::content::ContentParser;
13use crate::document::{page_content, Document, PageNode};
14use crate::error::{Error, Result};
15use crate::font::glyph::PathCmd;
16use crate::font::{CodeItem, Font, FontFlags};
17use crate::geom::{Matrix, Point, Rect};
18use crate::object::{text_string, Dict, ObjRef, Object};
19
20const MAX_DEPTH: usize = 12;
22const SPACE_GAP_EM: f32 = 0.12;
24const BASELINE_EPSILON_EM: f32 = 0.10;
26
27#[derive(Debug, Clone)]
29pub struct Glyph {
30 pub text: String,
37 pub origin: Point,
39 pub bbox: Rect,
42 pub advance: f32,
44 pub font_size: f32,
46 pub font_name: String,
48 pub base_font: String,
50 pub flags: FontFlags,
52 pub rotation: f32,
54 pub is_generated_space: bool,
57 pub render_mode: i32,
59 pub color: [f32; 3],
61 pub alpha: f32,
63 pub order: u32,
67
68 font: Option<Arc<Font>>,
72 code: CodeItem,
73 glyph_matrix: Matrix,
75}
76
77impl Glyph {
78 pub fn is_visible(&self) -> bool {
81 self.render_mode != 3 && self.render_mode != 7
82 }
83
84 pub fn outline(&self) -> Option<Arc<crate::font::glyph::Outline>> {
89 if self.is_generated_space {
90 return None;
91 }
92 self.font.as_ref()?.outline(&self.code)
93 }
94
95 pub fn outline_matrix(&self) -> Matrix {
97 self.glyph_matrix
98 }
99
100 pub fn code(&self) -> u32 {
105 self.code.code
106 }
107
108 pub fn cid(&self) -> u32 {
110 self.code.cid
111 }
112}
113
114#[derive(Debug, Clone)]
116pub struct PathItem {
117 pub cmds: Vec<PathCmd>,
119 pub bbox: Rect,
120 pub fill: Option<[f32; 3]>,
121 pub stroke: Option<[f32; 3]>,
122 pub line_width: f32,
124 pub even_odd: bool,
126 pub fill_alpha: f32,
128 pub stroke_alpha: f32,
129 pub order: u32,
131}
132
133#[derive(Debug, Clone)]
135pub enum ImageData {
136 Raw {
138 bits_per_component: u8,
139 components: u8,
140 data: Vec<u8>,
141 },
142 Encoded { filter: String, data: Vec<u8> },
145}
146
147#[derive(Debug, Clone)]
149pub struct ImageItem {
150 pub name: String,
152 pub ctm: Matrix,
155 pub bbox: Rect,
156 pub width: u32,
157 pub height: u32,
158 pub is_mask: bool,
160 pub bits_per_component: u8,
162 pub components: u8,
163 pub fill: [f32; 3],
165 pub alpha: f32,
166 pub order: u32,
168 source: ImageSource,
169}
170
171#[derive(Debug, Clone)]
177enum ImageSource {
178 Object(ObjRef),
179 Inline(Arc<ImageData>),
180}
181
182impl ImageItem {
183 pub fn decode(&self, doc: &Document) -> Result<ImageData> {
185 match &self.source {
186 ImageSource::Inline(data) => Ok((**data).clone()),
187 ImageSource::Object(r) => {
188 let obj = doc.resolve(&Object::Ref(*r));
189 let stream = obj.as_stream().ok_or_else(|| {
190 Error::Parse(format!("image object {} is not a stream", r.num))
191 })?;
192 let decoded = doc.file.decode_stream(stream)?;
193 Ok(match decoded.image_filter {
194 Some(filter) => ImageData::Encoded {
195 filter,
196 data: decoded.data,
197 },
198 None => ImageData::Raw {
199 bits_per_component: self.bits_per_component,
200 components: self.components,
201 data: decoded.data,
202 },
203 })
204 }
205 }
206 }
207}
208
209pub struct Page {
211 pub index: usize,
212 pub media_box: Rect,
213 pub crop_box: Rect,
214 pub rotation: i32,
216 pub glyphs: Vec<Glyph>,
217 pub paths: Vec<PathItem>,
218 pub images: Vec<ImageItem>,
219}
220
221impl Page {
222 pub(crate) fn build(doc: &Document, node: &PageNode, index: usize) -> Result<Page> {
223 let content = page_content(doc, &node.dict);
224 let mut interp = Interpreter::new(doc);
225 interp.run(&content, &node.resources, State::new(node.crop_box), 0);
226
227 Ok(Page {
228 index,
229 media_box: node.media_box,
230 crop_box: node.crop_box,
231 rotation: node.rotation,
232 glyphs: interp.glyphs,
233 paths: interp.paths,
234 images: interp.images,
235 })
236 }
237
238 pub fn width(&self) -> f32 {
240 if self.rotation % 180 == 0 {
241 self.crop_box.width()
242 } else {
243 self.crop_box.height()
244 }
245 }
246
247 pub fn height(&self) -> f32 {
249 if self.rotation % 180 == 0 {
250 self.crop_box.height()
251 } else {
252 self.crop_box.width()
253 }
254 }
255
256 pub fn page_matrix(&self, scale: f32) -> Matrix {
259 let to_origin = Matrix::translate(-self.crop_box.x0, -self.crop_box.y0);
261 let w = self.crop_box.width();
262 let h = self.crop_box.height();
263 let flip = Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, h);
264 let rotate = match self.rotation.rem_euclid(360) {
265 90 => Matrix::new(0.0, 1.0, -1.0, 0.0, h, 0.0),
266 180 => Matrix::new(-1.0, 0.0, 0.0, -1.0, w, h),
267 270 => Matrix::new(0.0, -1.0, 1.0, 0.0, 0.0, w),
268 _ => Matrix::IDENTITY,
269 };
270 to_origin
271 .concat(&flip)
272 .concat(&rotate)
273 .concat(&Matrix::scale(scale, scale))
274 }
275
276 pub fn is_likely_scanned(&self) -> bool {
286 const MIN_COVERAGE: f32 = 0.5;
287 const MAX_GLYPHS_PER_PAGE: usize = 32;
288
289 if self
290 .glyphs
291 .iter()
292 .filter(|g| !g.text.trim().is_empty())
293 .count()
294 > MAX_GLYPHS_PER_PAGE
295 {
296 return false;
297 }
298 let page = self.crop_box;
299 let area = page.width() * page.height();
300 if area <= 0.0 {
301 return false;
302 }
303 self.images.iter().any(|img| {
306 let visible = img.bbox.intersect(&page);
307 !visible.is_empty() && (visible.width() * visible.height()) / area >= MIN_COVERAGE
308 })
309 }
310
311 pub fn text(&self) -> String {
316 let mut out = String::new();
317 let mut prev: Option<&Glyph> = None;
318 for g in &self.glyphs {
319 if let Some(p) = prev {
324 if starts_new_line(p, g) {
325 out.push('\n');
326 }
327 }
328 out.push_str(&g.text);
329 if !g.text.is_empty() {
330 prev = Some(g);
331 }
332 }
333 out
334 }
335}
336
337fn starts_new_line(prev: &Glyph, g: &Glyph) -> bool {
342 let size = prev.font_size.max(g.font_size);
343 if size <= 0.0 {
344 return false;
345 }
346 if (prev.rotation - g.rotation).abs() > 0.01 {
349 return true;
350 }
351 let (dx, dy) = (g.origin.x - prev.origin.x, g.origin.y - prev.origin.y);
352 let (cos, sin) = (prev.rotation.cos(), prev.rotation.sin());
353 let along = dx * cos + dy * sin;
354 let across = -dx * sin + dy * cos;
355
356 across.abs() > size * 0.55 || along < -size
359}
360
361#[derive(Clone)]
365struct State {
366 ctm: Matrix,
367 clip: Rect,
368 fill: [f32; 3],
369 stroke: [f32; 3],
370 line_width: f32,
371 fill_components: usize,
373 stroke_components: usize,
374 fill_alpha: f32,
376 stroke_alpha: f32,
377 font: Option<Arc<Font>>,
379 font_name: String,
380 font_size: f32,
381 char_spacing: f32,
382 word_spacing: f32,
383 horizontal_scale: f32,
385 leading: f32,
386 rise: f32,
387 render_mode: i32,
388}
389
390impl State {
391 fn new(clip: Rect) -> State {
392 State {
393 ctm: Matrix::IDENTITY,
394 clip,
395 fill: [0.0; 3],
396 stroke: [0.0; 3],
397 line_width: 1.0,
398 fill_components: 1,
399 stroke_components: 1,
400 fill_alpha: 1.0,
401 stroke_alpha: 1.0,
402 font: None,
403 font_name: String::new(),
404 font_size: 0.0,
405 char_spacing: 0.0,
406 word_spacing: 0.0,
407 horizontal_scale: 1.0,
408 leading: 0.0,
409 rise: 0.0,
410 render_mode: 0,
411 }
412 }
413}
414
415#[derive(Clone, Copy)]
417struct PenTrail {
418 end: Point,
419 baseline_y: f32,
420 size: f32,
421}
422
423struct Interpreter<'a> {
424 doc: &'a Document,
425 glyphs: Vec<Glyph>,
426 paths: Vec<PathItem>,
427 images: Vec<ImageItem>,
428 font_cache: HashMap<u32, Arc<Font>>,
430 active_forms: HashSet<u32>,
432 trail: Option<PenTrail>,
433 last_was_space: bool,
436 marked: Vec<bool>,
440 span: Option<ActualText>,
442 order: u32,
444}
445
446struct ActualText {
452 pending: String,
455}
456
457struct TextState {
459 matrix: Matrix,
460 line_matrix: Matrix,
461}
462
463impl<'a> Interpreter<'a> {
464 fn new(doc: &'a Document) -> Self {
465 Interpreter {
466 doc,
467 glyphs: Vec::new(),
468 paths: Vec::new(),
469 images: Vec::new(),
470 font_cache: HashMap::new(),
471 active_forms: HashSet::new(),
472 trail: None,
473 last_was_space: false,
474 marked: Vec::new(),
475 span: None,
476 order: 0,
477 }
478 }
479
480 fn begin_marked(&mut self, properties: Option<&Object>, resources: &Dict) {
483 let replacement = match properties {
486 Some(props) if self.span.is_none() => {
487 let dict = match props {
488 Object::Name(name) => self
489 .doc
490 .dict_get(resources, "Properties")
491 .and_then(|p| p.as_dict().and_then(|d| self.doc.dict_get(d, name))),
492 other => Some(self.doc.resolve(other)),
493 };
494 dict.and_then(|d| self.doc.dict_get(d.as_dict()?, "ActualText"))
495 .and_then(|o| o.as_string().map(text_string))
496 }
497 _ => None,
498 };
499 self.marked.push(replacement.is_some());
500 if let Some(text) = replacement {
501 let pending = text
510 .chars()
511 .filter(|&c| c != '\u{00AD}')
512 .map(|c| if matches!(c, '\n' | '\r') { ' ' } else { c })
513 .collect();
514 self.span = Some(ActualText { pending });
515 }
516 }
517
518 fn end_marked(&mut self, floor: usize) {
521 if self.marked.len() > floor && self.marked.pop() == Some(true) {
522 self.span = None;
524 }
525 }
526
527 fn run(&mut self, content: &[u8], resources: &Dict, initial: State, depth: usize) {
528 if depth > MAX_DEPTH {
529 return;
530 }
531 let marked_floor = self.marked.len();
532 let mut stack: Vec<State> = Vec::new();
533 let mut gs = initial;
534 let mut text = TextState {
535 matrix: Matrix::IDENTITY,
536 line_matrix: Matrix::IDENTITY,
537 };
538 let mut path: Vec<PathCmd> = Vec::new();
540 let mut subpath_start = Point::default();
541 let mut current = Point::default();
542 let mut pending_clip: Option<bool> = None;
543
544 for op in ContentParser::new(content) {
545 let n = |i: usize| op.operands.get(i).and_then(|o| o.as_f32());
546 match op.operator.as_str() {
547 "q" => stack.push(gs.clone()),
549 "Q" => {
550 if let Some(prev) = stack.pop() {
551 gs = prev;
552 }
553 }
554 "cm" => {
555 if let (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) =
556 (n(0), n(1), n(2), n(3), n(4), n(5))
557 {
558 gs.ctm = Matrix::new(a, b, c, d, e, f).concat(&gs.ctm);
559 }
560 }
561 "w" => {
562 if let Some(v) = n(0) {
563 gs.line_width = v;
564 }
565 }
566 "gs" => {
567 if let Some(name) = op.operands.first().and_then(|o| o.as_name()) {
568 self.apply_ext_gstate(resources, name, &mut gs);
569 }
570 }
571
572 "g" | "G" => self.set_color(&mut gs, op.operator == "g", &op.operands, 1),
574 "rg" | "RG" => self.set_color(&mut gs, op.operator == "rg", &op.operands, 3),
575 "k" | "K" => self.set_color(&mut gs, op.operator == "k", &op.operands, 4),
576 "cs" | "CS" => {
577 let comps = self.colorspace_components(resources, &op.operands);
578 if op.operator == "cs" {
579 gs.fill_components = comps;
580 gs.fill = [0.0; 3];
581 } else {
582 gs.stroke_components = comps;
583 gs.stroke = [0.0; 3];
584 }
585 }
586 "sc" | "scn" | "SC" | "SCN" => {
587 let is_fill = op.operator.starts_with('s');
588 let numeric = op.operands.iter().filter_map(|o| o.as_f32()).count();
591 if numeric > 0 {
592 let declared = if is_fill {
596 gs.fill_components
597 } else {
598 gs.stroke_components
599 };
600 let components = if (1..=numeric).contains(&declared) {
601 declared
602 } else {
603 numeric
604 };
605 self.set_color(&mut gs, is_fill, &op.operands, components);
606 }
607 }
608
609 "m" => {
611 if let (Some(x), Some(y)) = (n(0), n(1)) {
612 current = gs.ctm.apply(Point::new(x, y));
613 subpath_start = current;
614 path.push(PathCmd::MoveTo(current));
615 }
616 }
617 "l" => {
618 if let (Some(x), Some(y)) = (n(0), n(1)) {
619 current = gs.ctm.apply(Point::new(x, y));
620 path.push(PathCmd::LineTo(current));
621 }
622 }
623 "c" => {
624 if let (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) =
625 (n(0), n(1), n(2), n(3), n(4), n(5))
626 {
627 let c1 = gs.ctm.apply(Point::new(a, b));
628 let c2 = gs.ctm.apply(Point::new(c, d));
629 current = gs.ctm.apply(Point::new(e, f));
630 path.push(PathCmd::CurveTo(c1, c2, current));
631 }
632 }
633 "v" => {
634 if let (Some(c), Some(d), Some(e), Some(f)) = (n(0), n(1), n(2), n(3)) {
636 let c2 = gs.ctm.apply(Point::new(c, d));
637 let end = gs.ctm.apply(Point::new(e, f));
638 path.push(PathCmd::CurveTo(current, c2, end));
639 current = end;
640 }
641 }
642 "y" => {
643 if let (Some(a), Some(b), Some(e), Some(f)) = (n(0), n(1), n(2), n(3)) {
645 let c1 = gs.ctm.apply(Point::new(a, b));
646 let end = gs.ctm.apply(Point::new(e, f));
647 path.push(PathCmd::CurveTo(c1, end, end));
648 current = end;
649 }
650 }
651 "h" => {
652 path.push(PathCmd::Close);
653 current = subpath_start;
654 }
655 "re" => {
656 if let (Some(x), Some(y), Some(w), Some(h)) = (n(0), n(1), n(2), n(3)) {
657 let p0 = gs.ctm.apply(Point::new(x, y));
658 let p1 = gs.ctm.apply(Point::new(x + w, y));
659 let p2 = gs.ctm.apply(Point::new(x + w, y + h));
660 let p3 = gs.ctm.apply(Point::new(x, y + h));
661 path.extend([
662 PathCmd::MoveTo(p0),
663 PathCmd::LineTo(p1),
664 PathCmd::LineTo(p2),
665 PathCmd::LineTo(p3),
666 PathCmd::Close,
667 ]);
668 subpath_start = p0;
669 current = p0;
670 }
671 }
672
673 "n" | "f" | "F" | "f*" | "S" | "s" | "B" | "B*" | "b" | "b*" => {
675 let o = op.operator.as_str();
676 if matches!(o, "s" | "b" | "b*") {
677 path.push(PathCmd::Close);
678 }
679 let even_odd = o.ends_with('*');
680 let fills = matches!(o, "f" | "F" | "f*" | "B" | "B*" | "b" | "b*");
681 let strokes = matches!(o, "S" | "s" | "B" | "B*" | "b" | "b*");
682 let region = pending_clip.take().map(|_| bounds(&path));
685 if !path.is_empty() && (fills || strokes) {
686 let bbox = bounds(&path);
687 let order = self.next_order();
688 self.paths.push(PathItem {
689 cmds: std::mem::take(&mut path),
690 bbox,
691 fill: fills.then_some(gs.fill),
692 stroke: strokes.then_some(gs.stroke),
693 line_width: gs.line_width * gs.ctm.x_scale().max(gs.ctm.y_scale()),
695 even_odd,
696 fill_alpha: gs.fill_alpha,
697 stroke_alpha: gs.stroke_alpha,
698 order,
699 });
700 }
701 if let Some(region) = region {
705 if !region.is_empty() {
706 gs.clip = gs.clip.intersect(®ion);
707 }
708 }
709 path.clear();
710 }
711 "W" => pending_clip = Some(false),
712 "W*" => pending_clip = Some(true),
713
714 "BT" => {
716 text.matrix = Matrix::IDENTITY;
717 text.line_matrix = Matrix::IDENTITY;
718 }
724 "ET" => {}
725 "Tc" => gs.char_spacing = n(0).unwrap_or(0.0),
726 "Tw" => gs.word_spacing = n(0).unwrap_or(0.0),
727 "Tz" => gs.horizontal_scale = n(0).unwrap_or(100.0) / 100.0,
728 "TL" => gs.leading = n(0).unwrap_or(0.0),
729 "Ts" => gs.rise = n(0).unwrap_or(0.0),
730 "Tr" => {
731 gs.render_mode =
732 op.operands.first().and_then(|o| o.as_int()).unwrap_or(0) as i32
733 }
734 "Tf" => {
735 gs.font_size = n(1).unwrap_or(0.0);
736 if let Some(name) = op.operands.first().and_then(|o| o.as_name()) {
737 gs.font_name = name.to_string();
738 gs.font = self.font_for(resources, name);
739 }
740 }
741 "Td" => {
742 if let (Some(tx), Some(ty)) = (n(0), n(1)) {
743 text.line_matrix = Matrix::translate(tx, ty).concat(&text.line_matrix);
744 text.matrix = text.line_matrix;
745 }
746 }
747 "TD" => {
748 if let (Some(tx), Some(ty)) = (n(0), n(1)) {
749 gs.leading = -ty;
750 text.line_matrix = Matrix::translate(tx, ty).concat(&text.line_matrix);
751 text.matrix = text.line_matrix;
752 }
753 }
754 "Tm" => {
755 if let (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) =
756 (n(0), n(1), n(2), n(3), n(4), n(5))
757 {
758 text.line_matrix = Matrix::new(a, b, c, d, e, f);
759 text.matrix = text.line_matrix;
760 }
761 }
762 "T*" => next_line(&mut text, gs.leading),
763 "Tj" => {
764 if let Some(Object::String(s)) = op.operands.first() {
765 let s = s.clone();
766 self.show(&s, &gs, &mut text, resources, depth);
767 }
768 }
769 "'" => {
770 next_line(&mut text, gs.leading);
771 if let Some(Object::String(s)) = op.operands.first() {
772 let s = s.clone();
773 self.show(&s, &gs, &mut text, resources, depth);
774 }
775 }
776 "\"" => {
777 gs.word_spacing = n(0).unwrap_or(gs.word_spacing);
779 gs.char_spacing = n(1).unwrap_or(gs.char_spacing);
780 next_line(&mut text, gs.leading);
781 if let Some(Object::String(s)) = op.operands.get(2) {
782 let s = s.clone();
783 self.show(&s, &gs, &mut text, resources, depth);
784 }
785 }
786 "TJ" => {
787 let Some(Object::Array(items)) = op.operands.first().cloned() else {
788 continue;
789 };
790 for item in items {
791 match item {
792 Object::String(s) => self.show(&s, &gs, &mut text, resources, depth),
793 other => {
794 if let Some(adj) = other.as_f32() {
795 let tx = -adj / 1000.0 * gs.font_size * gs.horizontal_scale;
797 text.matrix = Matrix::translate(tx, 0.0).concat(&text.matrix);
798 }
799 }
800 }
801 }
802 }
803
804 "Do" => {
806 if let Some(name) = op.operands.first().and_then(|o| o.as_name()) {
807 let name = name.to_string();
808 self.do_xobject(&name, resources, &gs, depth);
809 }
810 }
811 "BI" => {
812 if let (Some(dict), Some(data)) = (
813 op.operands.first().and_then(|o| o.as_dict()),
814 op.inline_data.as_ref(),
815 ) {
816 let dict = dict.clone();
817 let data = data.clone();
818 self.record_inline_image(&dict, &data, &gs, resources);
819 }
820 }
821
822 "BMC" => self.begin_marked(None, resources),
824 "BDC" => {
825 self.begin_marked(Some(op.operands.get(1).unwrap_or(&Object::Null)), resources)
826 }
827 "EMC" => self.end_marked(marked_floor),
828 _ => {}
829 }
830 }
831 while self.marked.len() > marked_floor {
834 self.end_marked(marked_floor);
835 }
836 }
837
838 fn show(
842 &mut self,
843 bytes: &[u8],
844 gs: &State,
845 text: &mut TextState,
846 resources: &Dict,
847 depth: usize,
848 ) {
849 let Some(font) = gs.font.clone() else {
850 return;
851 };
852 for item in font.decode(bytes) {
853 let param = Matrix::new(
856 gs.font_size * gs.horizontal_scale,
857 0.0,
858 0.0,
859 gs.font_size,
860 0.0,
861 gs.rise,
862 );
863 let trm = param.concat(&text.matrix).concat(&gs.ctm);
864
865 let advance_text = (item.width / 1000.0 * gs.font_size
866 + gs.char_spacing
867 + if item.is_word_space {
868 gs.word_spacing
869 } else {
870 0.0
871 })
872 * gs.horizontal_scale;
873
874 let text_to_user = text.matrix.concat(&gs.ctm);
877 self.emit_glyph(&font, &item, &trm, &text_to_user, gs, advance_text);
878
879 if font.is_type3 {
880 self.draw_type3_glyph(&font, &item, &trm, gs, resources, depth);
881 }
882 text.matrix = Matrix::translate(advance_text, 0.0).concat(&text.matrix);
883 }
884 }
885
886 #[allow(clippy::too_many_arguments)]
887 fn emit_glyph(
888 &mut self,
889 font: &Font,
890 item: &CodeItem,
891 trm: &Matrix,
892 text_to_user: &Matrix,
893 gs: &State,
894 advance_text: f32,
895 ) {
896 let own: String = item
899 .unicode
900 .as_deref()
901 .unwrap_or("")
902 .chars()
903 .filter(|&c| c != '\u{00AD}')
904 .collect();
905 let in_span = self.span.is_some();
911 let text = match self.span.as_mut() {
912 Some(span) => std::mem::take(&mut span.pending),
913 None => own,
914 };
915
916 let origin = trm.apply(Point::default());
917 let effective_size = trm.y_scale();
920 let (advance_dx, advance_dy) = text_to_user.apply_vector(advance_text, 0.0);
923 let advance_user = advance_dx.hypot(advance_dy);
924
925 if !text.is_empty() {
927 if let Some(prev) = self.trail {
928 let same_line =
929 (origin.y - prev.baseline_y).abs() <= BASELINE_EPSILON_EM * prev.size;
930 let gap = origin.x - prev.end.x;
931 if same_line
932 && prev.size > 0.0
933 && gap > SPACE_GAP_EM * prev.size
934 && !self.last_was_space
935 && !text.starts_with(char::is_whitespace)
936 {
937 let order = self.next_order();
938 self.glyphs.push(Glyph {
939 text: " ".into(),
940 origin: prev.end,
941 bbox: Rect::from_corners(
942 prev.end.x,
943 prev.baseline_y,
944 origin.x,
945 prev.baseline_y + prev.size,
946 ),
947 advance: gap,
948 font_size: prev.size,
949 font_name: gs.font_name.clone(),
950 base_font: font.base_font.clone(),
951 flags: font.flags,
952 rotation: trm.rotation(),
953 is_generated_space: true,
954 render_mode: gs.render_mode,
955 color: gs.fill,
956 alpha: gs.fill_alpha,
957 order,
958 font: None,
959 code: item.clone(),
960 glyph_matrix: *trm,
961 });
962 }
963 }
964 }
965
966 if in_span {
967 if !text.is_empty() {
969 self.last_was_space = text.ends_with(char::is_whitespace);
970 }
971 } else {
972 self.last_was_space = text.chars().all(char::is_whitespace);
975 }
976
977 let (bbox, glyph_matrix) = self.glyph_bbox(font, item, trm);
978 let order = self.next_order();
979 self.glyphs.push(Glyph {
980 text,
981 origin,
982 bbox,
983 advance: advance_user,
984 font_size: effective_size,
985 font_name: gs.font_name.clone(),
986 base_font: font.base_font.clone(),
987 flags: font.flags,
988 rotation: trm.rotation(),
989 is_generated_space: false,
990 render_mode: gs.render_mode,
991 color: gs.fill,
992 alpha: gs.fill_alpha,
993 order,
994 font: gs.font.clone(),
995 code: item.clone(),
996 glyph_matrix,
997 });
998
999 self.trail = Some(PenTrail {
1000 end: Point::new(origin.x + advance_dx, origin.y + advance_dy),
1001 baseline_y: origin.y,
1002 size: effective_size,
1003 });
1004 }
1005
1006 fn glyph_bbox(&self, font: &Font, item: &CodeItem, trm: &Matrix) -> (Rect, Matrix) {
1017 let to_text = if font.is_type3 {
1019 font.font_matrix
1020 } else {
1021 Matrix::scale(0.001, 0.001)
1022 };
1023 let m = to_text.concat(trm);
1024 let advance = item.width.max(1.0);
1025 if let Some(outline) = font.outline(item) {
1026 if let Some(b) = outline.bbox() {
1027 let b = if outline.is_substitute {
1028 Rect {
1029 x0: 0.0,
1030 x1: advance,
1031 ..b
1032 }
1033 } else {
1034 b
1035 };
1036 return (m.apply_rect(&b), m);
1037 }
1038 }
1039 let nominal = Rect::from_corners(0.0, -200.0, advance, 800.0);
1041 (m.apply_rect(&nominal), m)
1042 }
1043
1044 fn draw_type3_glyph(
1046 &mut self,
1047 font: &Font,
1048 item: &CodeItem,
1049 trm: &Matrix,
1050 gs: &State,
1051 resources: &Dict,
1052 depth: usize,
1053 ) {
1054 if depth >= MAX_DEPTH {
1055 return;
1056 }
1057 let Some(proc_obj) = font.type3_proc(self.doc, item.code) else {
1058 return;
1059 };
1060 let Some(stream) = proc_obj.as_stream() else {
1061 return;
1062 };
1063 let Ok(decoded) = self.doc.file.decode_stream(stream) else {
1064 return;
1065 };
1066 let mut inner = gs.clone();
1067 inner.ctm = font.font_matrix.concat(trm);
1069 inner.font = None;
1070 self.run(&decoded.data, resources, inner, depth + 1);
1071 }
1072
1073 fn font_for(&mut self, resources: &Dict, name: &str) -> Option<Arc<Font>> {
1076 let fonts = self.doc.dict_get(resources, "Font")?;
1077 let entry = fonts.as_dict()?.get(name)?.clone();
1078 if let Object::Ref(r) = &entry {
1079 if let Some(hit) = self.font_cache.get(&r.num) {
1080 return Some(hit.clone());
1081 }
1082 }
1083 let dict = self.doc.resolve(&entry).as_dict()?.clone();
1084 let font = Arc::new(Font::load(self.doc, &dict));
1085 if let Object::Ref(r) = &entry {
1086 self.font_cache.insert(r.num, font.clone());
1087 }
1088 Some(font)
1089 }
1090
1091 fn apply_ext_gstate(&self, resources: &Dict, name: &str, gs: &mut State) {
1093 let Some(ext) = self
1094 .doc
1095 .dict_get(resources, "ExtGState")
1096 .and_then(|o| o.as_dict().and_then(|d| d.get(name).cloned()))
1097 else {
1098 return;
1099 };
1100 let Some(dict) = self.doc.resolve(&ext).as_dict().cloned() else {
1101 return;
1102 };
1103 if let Some(lw) = self.doc.dict_get(&dict, "LW").and_then(|o| o.as_f32()) {
1104 gs.line_width = lw;
1105 }
1106 if let Some(ca) = self.doc.dict_get(&dict, "ca").and_then(|o| o.as_f32()) {
1109 gs.fill_alpha = ca.clamp(0.0, 1.0);
1110 }
1111 if let Some(ca) = self.doc.dict_get(&dict, "CA").and_then(|o| o.as_f32()) {
1112 gs.stroke_alpha = ca.clamp(0.0, 1.0);
1113 }
1114 if let Some(Object::Array(items)) = self.doc.dict_get(&dict, "Font") {
1116 if let Some(size) = items.get(1).and_then(|o| o.as_f32()) {
1117 gs.font_size = size;
1118 }
1119 }
1120 }
1121
1122 fn colorspace_components(&self, resources: &Dict, operands: &[Object]) -> usize {
1124 let Some(name) = operands.first().and_then(|o| o.as_name()) else {
1125 return 1;
1126 };
1127 match name {
1128 "DeviceGray" | "CalGray" | "G" => return 1,
1129 "DeviceRGB" | "CalRGB" | "Lab" | "RGB" => return 3,
1130 "DeviceCMYK" | "CMYK" => return 4,
1131 "Pattern" => return 0,
1132 _ => {}
1133 }
1134 let Some(space) = self
1136 .doc
1137 .dict_get(resources, "ColorSpace")
1138 .and_then(|o| o.as_dict().and_then(|d| d.get(name).cloned()))
1139 .map(|o| self.doc.resolve(&o))
1140 else {
1141 return 1;
1142 };
1143 self.components_of(&space, 0)
1144 }
1145
1146 fn components_of(&self, space: &Object, depth: usize) -> usize {
1147 if depth > 4 {
1148 return 1;
1149 }
1150 match space {
1151 Object::Name(n) => match n.as_str() {
1152 "DeviceRGB" | "CalRGB" | "Lab" => 3,
1153 "DeviceCMYK" => 4,
1154 _ => 1,
1155 },
1156 Object::Array(items) => {
1157 let head = items.first().and_then(|o| o.as_name()).unwrap_or("");
1158 match head {
1159 "Indexed" | "I" | "Separation" => 1,
1161 "DeviceN" => items
1162 .get(1)
1163 .map(|o| self.doc.resolve(o))
1164 .and_then(|o| o.as_array().map(<[Object]>::len))
1165 .unwrap_or(1),
1166 "ICCBased" => items
1167 .get(1)
1168 .map(|o| self.doc.resolve(o))
1169 .and_then(|o| o.as_dict().and_then(|d| d.get("N").cloned()))
1170 .and_then(|o| o.as_int())
1171 .unwrap_or(3)
1172 .clamp(1, 4) as usize,
1173 "CalRGB" | "Lab" => 3,
1174 "CalGray" => 1,
1175 "Pattern" => 0,
1176 _ => self
1177 .components_of(&items.first().cloned().unwrap_or(Object::Null), depth + 1),
1178 }
1179 }
1180 _ => 1,
1181 }
1182 }
1183
1184 fn set_color(&self, gs: &mut State, is_fill: bool, operands: &[Object], components: usize) {
1185 let v: Vec<f32> = operands.iter().filter_map(|o| o.as_f32()).collect();
1186 let rgb = match components.min(v.len()) {
1187 1 => {
1188 let g = v[0].clamp(0.0, 1.0);
1189 [g, g, g]
1190 }
1191 3 => [
1192 v[0].clamp(0.0, 1.0),
1193 v[1].clamp(0.0, 1.0),
1194 v[2].clamp(0.0, 1.0),
1195 ],
1196 4 => {
1197 let k = v[3].clamp(0.0, 1.0);
1199 [
1200 (1.0 - v[0].clamp(0.0, 1.0)) * (1.0 - k),
1201 (1.0 - v[1].clamp(0.0, 1.0)) * (1.0 - k),
1202 (1.0 - v[2].clamp(0.0, 1.0)) * (1.0 - k),
1203 ]
1204 }
1205 _ => return,
1206 };
1207 if is_fill {
1208 gs.fill = rgb;
1209 gs.fill_components = components;
1210 } else {
1211 gs.stroke = rgb;
1212 gs.stroke_components = components;
1213 }
1214 }
1215
1216 fn do_xobject(&mut self, name: &str, resources: &Dict, gs: &State, depth: usize) {
1219 let Some(xobjects) = self.doc.dict_get(resources, "XObject") else {
1220 return;
1221 };
1222 let Some(entry) = xobjects.as_dict().and_then(|d| d.get(name).cloned()) else {
1223 return;
1224 };
1225 let obj_num = entry.as_ref_id().map(|r| r.num);
1226 let resolved = self.doc.resolve(&entry);
1227 let Some(stream) = resolved.as_stream() else {
1228 return;
1229 };
1230 let subtype = stream.dict.get("Subtype").and_then(|o| o.as_name());
1231
1232 match subtype {
1233 Some("Image") => self.record_image(name, stream, entry.as_ref_id(), gs),
1234 Some("Form") => {
1235 if let Some(num) = obj_num {
1237 if !self.active_forms.insert(num) {
1238 return;
1239 }
1240 }
1241 let Ok(decoded) = self.doc.file.decode_stream(stream) else {
1242 if let Some(num) = obj_num {
1243 self.active_forms.remove(&num);
1244 }
1245 return;
1246 };
1247 let mut inner = gs.clone();
1248 if let Some(m) = stream
1249 .dict
1250 .get("Matrix")
1251 .map(|o| self.doc.resolve(o))
1252 .as_ref()
1253 .and_then(|o| o.as_array())
1254 {
1255 let v: Vec<f32> = m.iter().filter_map(|o| o.as_f32()).collect();
1256 if v.len() >= 6 {
1257 inner.ctm = Matrix::new(v[0], v[1], v[2], v[3], v[4], v[5]).concat(&gs.ctm);
1258 }
1259 }
1260 if let Some(bbox) = stream
1261 .dict
1262 .get("BBox")
1263 .map(|o| self.doc.resolve(o))
1264 .as_ref()
1265 .and_then(|o| o.as_array())
1266 {
1267 let v: Vec<f32> = bbox.iter().filter_map(|o| o.as_f32()).collect();
1268 if v.len() >= 4 {
1269 let r = inner
1270 .ctm
1271 .apply_rect(&Rect::from_corners(v[0], v[1], v[2], v[3]));
1272 if !r.is_empty() {
1273 inner.clip = inner.clip.intersect(&r);
1274 }
1275 }
1276 }
1277 let inner_resources = self
1279 .doc
1280 .dict_get(&stream.dict, "Resources")
1281 .and_then(|o| o.as_dict().cloned())
1282 .unwrap_or_else(|| resources.clone());
1283 let data = decoded.data;
1284 self.run(&data, &inner_resources, inner, depth + 1);
1285 if let Some(num) = obj_num {
1286 self.active_forms.remove(&num);
1287 }
1288 }
1289 _ => {}
1290 }
1291 }
1292
1293 fn record_image(
1294 &mut self,
1295 name: &str,
1296 stream: &crate::object::Stream,
1297 obj_ref: Option<ObjRef>,
1298 gs: &State,
1299 ) {
1300 let dict = &stream.dict;
1301 let width = self
1302 .doc
1303 .dict_get(dict, "Width")
1304 .and_then(|o| o.as_int())
1305 .unwrap_or(0)
1306 .max(0) as u32;
1307 let height = self
1308 .doc
1309 .dict_get(dict, "Height")
1310 .and_then(|o| o.as_int())
1311 .unwrap_or(0)
1312 .max(0) as u32;
1313 if width == 0 || height == 0 {
1314 return;
1315 }
1316 let is_mask = self
1317 .doc
1318 .dict_get(dict, "ImageMask")
1319 .and_then(|o| o.as_bool())
1320 .unwrap_or(false);
1321 let bpc = self
1322 .doc
1323 .dict_get(dict, "BitsPerComponent")
1324 .and_then(|o| o.as_int())
1325 .unwrap_or(if is_mask { 1 } else { 8 })
1326 .clamp(1, 16) as u8;
1327 let components = if is_mask {
1328 1
1329 } else {
1330 let space = self
1331 .doc
1332 .dict_get(dict, "ColorSpace")
1333 .unwrap_or(Object::Null);
1334 self.components_of(&space, 0).max(1) as u8
1335 };
1336
1337 let source = match obj_ref {
1341 Some(r) => ImageSource::Object(r),
1342 None => {
1343 let Ok(decoded) = self.doc.file.decode_stream(stream) else {
1344 return;
1345 };
1346 ImageSource::Inline(Arc::new(match decoded.image_filter {
1347 Some(filter) => ImageData::Encoded {
1348 filter,
1349 data: decoded.data,
1350 },
1351 None => ImageData::Raw {
1352 bits_per_component: bpc,
1353 components,
1354 data: decoded.data,
1355 },
1356 }))
1357 }
1358 };
1359 self.push_image(name, gs, width, height, is_mask, bpc, components, source);
1360 }
1361
1362 fn record_inline_image(&mut self, dict: &Dict, raw: &[u8], gs: &State, resources: &Dict) {
1363 let width = self
1364 .doc
1365 .dict_get(dict, "Width")
1366 .and_then(|o| o.as_int())
1367 .unwrap_or(0)
1368 .max(0) as u32;
1369 let height = self
1370 .doc
1371 .dict_get(dict, "Height")
1372 .and_then(|o| o.as_int())
1373 .unwrap_or(0)
1374 .max(0) as u32;
1375 if width == 0 || height == 0 {
1376 return;
1377 }
1378 let is_mask = self
1379 .doc
1380 .dict_get(dict, "ImageMask")
1381 .and_then(|o| o.as_bool())
1382 .unwrap_or(false);
1383 let bpc = self
1384 .doc
1385 .dict_get(dict, "BitsPerComponent")
1386 .and_then(|o| o.as_int())
1387 .unwrap_or(if is_mask { 1 } else { 8 })
1388 .clamp(1, 16) as u8;
1389 let components = if is_mask {
1391 1
1392 } else {
1393 match self.doc.dict_get(dict, "ColorSpace") {
1394 Some(Object::Name(n)) => {
1395 self.colorspace_components(resources, &[Object::Name(n)]) as u8
1396 }
1397 Some(other) => self.components_of(&other, 0) as u8,
1398 None => 1,
1399 }
1400 .max(1)
1401 };
1402
1403 let resolve = |o: &Object| self.doc.resolve(o);
1404 let data = match crate::filters::decode(dict, raw, &resolve) {
1405 Ok(d) => match d.image_filter {
1406 Some(filter) => ImageData::Encoded {
1407 filter,
1408 data: d.data,
1409 },
1410 None => ImageData::Raw {
1411 bits_per_component: bpc,
1412 components,
1413 data: d.data,
1414 },
1415 },
1416 Err(_) => return,
1417 };
1418 let source = ImageSource::Inline(Arc::new(data));
1419 self.push_image(
1420 "inline", gs, width, height, is_mask, bpc, components, source,
1421 );
1422 }
1423
1424 #[allow(clippy::too_many_arguments)]
1425 fn push_image(
1426 &mut self,
1427 name: &str,
1428 gs: &State,
1429 width: u32,
1430 height: u32,
1431 is_mask: bool,
1432 bits_per_component: u8,
1433 components: u8,
1434 source: ImageSource,
1435 ) {
1436 let ctm = Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, 1.0).concat(&gs.ctm);
1438 let bbox = gs.ctm.apply_rect(&Rect::from_corners(0.0, 0.0, 1.0, 1.0));
1439 let order = self.next_order();
1440 self.images.push(ImageItem {
1441 name: name.to_string(),
1442 ctm,
1443 bbox,
1444 width,
1445 height,
1446 is_mask,
1447 bits_per_component,
1448 components,
1449 fill: gs.fill,
1450 alpha: gs.fill_alpha,
1451 order,
1452 source,
1453 });
1454 }
1455
1456 fn next_order(&mut self) -> u32 {
1458 let n = self.order;
1459 self.order = self.order.saturating_add(1);
1460 n
1461 }
1462}
1463
1464fn next_line(text: &mut TextState, leading: f32) {
1465 text.line_matrix = Matrix::translate(0.0, -leading).concat(&text.line_matrix);
1466 text.matrix = text.line_matrix;
1467}
1468
1469fn bounds(cmds: &[PathCmd]) -> Rect {
1471 let mut out: Option<Rect> = None;
1472 let mut add = |p: Point| {
1473 out = Some(match out {
1474 None => Rect {
1475 x0: p.x,
1476 y0: p.y,
1477 x1: p.x,
1478 y1: p.y,
1479 },
1480 Some(r) => Rect {
1481 x0: r.x0.min(p.x),
1482 y0: r.y0.min(p.y),
1483 x1: r.x1.max(p.x),
1484 y1: r.y1.max(p.y),
1485 },
1486 });
1487 };
1488 for cmd in cmds {
1489 match *cmd {
1490 PathCmd::MoveTo(p) | PathCmd::LineTo(p) => add(p),
1491 PathCmd::CurveTo(a, b, c) => {
1492 add(a);
1493 add(b);
1494 add(c);
1495 }
1496 PathCmd::Close => {}
1497 }
1498 }
1499 out.unwrap_or(Rect::EMPTY)
1500}
1501
1502#[cfg(test)]
1503pub(crate) mod tests {
1504 use super::*;
1505 use crate::parser::tests::tiny_pdf;
1506
1507 pub(crate) fn doc_with(content: &str) -> Document {
1509 let base = String::from_utf8(tiny_pdf()).unwrap();
1510 let old = "BT /F1 12 Tf 72 720 Td (Hi) Tj ET";
1511 Document::from_bytes(base.replace(old, content).into_bytes(), None).unwrap()
1514 }
1515
1516 #[test]
1517 fn extracts_text_with_positions() {
1518 let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1519 let page = doc.page(0).unwrap();
1520 assert_eq!(page.text(), "Hi");
1521 assert_eq!(page.glyphs.len(), 2);
1522
1523 let h = &page.glyphs[0];
1524 assert_eq!(h.text, "H");
1525 assert_eq!(h.base_font, "Helvetica");
1526 assert!((h.origin.x - 72.0).abs() < 0.01);
1527 assert!((h.origin.y - 720.0).abs() < 0.01);
1528 assert!((h.font_size - 12.0).abs() < 0.01);
1529 assert!((h.advance - 722.0 / 1000.0 * 12.0).abs() < 0.01);
1531 assert!((page.glyphs[1].origin.x - (72.0 + 8.664)).abs() < 0.01);
1533 }
1534
1535 fn text_of(content: &str) -> String {
1536 doc_with(content).page(0).unwrap().text()
1537 }
1538
1539 #[test]
1540 fn actual_text_replaces_what_its_glyphs_say_and_keeps_where_they_are() {
1541 let plain = doc_with("BT /F1 12 Tf 72 720 Td (a) Tj (xy) Tj (b) Tj ET");
1542 let tagged = doc_with(
1543 "BT /F1 12 Tf 72 720 Td (a) Tj /Span <</ActualText (fi)>> BDC (xy) Tj EMC (b) Tj ET",
1544 );
1545 let (plain, tagged) = (plain.page(0).unwrap(), tagged.page(0).unwrap());
1546 assert_eq!(plain.text(), "axyb");
1547 assert_eq!(tagged.text(), "afib");
1548
1549 let texts: Vec<&str> = tagged.glyphs.iter().map(|g| g.text.as_str()).collect();
1551 assert_eq!(texts, ["a", "fi", "", "b"]);
1552
1553 assert_eq!(plain.glyphs.len(), tagged.glyphs.len());
1555 for (p, t) in plain.glyphs.iter().zip(&tagged.glyphs) {
1556 assert_eq!((p.origin, p.bbox, p.advance), (t.origin, t.bbox, t.advance));
1557 assert_eq!(p.code(), t.code());
1558 }
1559 }
1560
1561 #[test]
1562 fn actual_text_spans_several_show_operators_as_one_unit() {
1563 let doc = doc_with(
1566 "BT /F1 12 Tf 72 720 Td (A) Tj /Span <</ActualText (68)>> BDC \
1567 (x) Tj 20 0 Td (y) Tj [(z) -500 (w)] TJ EMC (B) Tj ET",
1568 );
1569 let page = doc.page(0).unwrap();
1570 assert_eq!(page.text(), "A68B");
1571 assert_eq!(page.glyphs.len(), 6);
1572 assert!(page.glyphs.iter().all(|g| !g.is_generated_space));
1573 }
1574
1575 #[test]
1576 fn the_outermost_actual_text_wins() {
1577 assert_eq!(
1579 text_of(
1580 "BT /F1 12 Tf 72 720 Td /Span <</ActualText (out)>> BDC (a) Tj \
1581 /Span <</ActualText (in)>> BDC (b) Tj EMC (c) Tj EMC (d) Tj ET"
1582 ),
1583 "outd"
1584 );
1585 assert_eq!(
1588 text_of(
1589 "/P <</MCID 0>> BDC /Artifact BMC BT /F1 12 Tf 72 720 Td (a) Tj \
1590 /Span <</ActualText (X)>> BDC (b) Tj EMC (c) Tj ET EMC EMC \
1591 BT 1 0 0 1 100 720 Tm (d) Tj ET"
1592 ),
1593 "aXc d"
1594 );
1595 }
1596
1597 #[test]
1598 fn an_empty_actual_text_silences_its_glyphs() {
1599 for empty in ["()", "<FEFF>", "<>"] {
1600 let doc = doc_with(&format!(
1601 "BT /F1 12 Tf 72 720 Td (a) Tj /Span <</ActualText {empty}>> BDC (-) Tj EMC \
1602 (b) Tj ET"
1603 ));
1604 let page = doc.page(0).unwrap();
1605 assert_eq!(page.text(), "ab", "ActualText {empty}");
1606 assert_eq!(page.glyphs.len(), 3);
1608 assert_eq!(page.glyphs[1].text, "");
1609 assert_eq!(page.glyphs[1].code(), u32::from(b'-'));
1610 }
1611 }
1612
1613 #[test]
1614 fn actual_text_is_decoded_as_a_text_string() {
1615 let span = |value: &str| {
1616 text_of(&format!(
1617 "BT /F1 12 Tf 72 720 Td /Span <</ActualText {value}>> BDC (x) Tj EMC ET"
1618 ))
1619 };
1620 assert_eq!(span("<FEFF00660069>"), "fi");
1622 assert_eq!(span("<FEFF03B1D835DC00>"), "\u{3b1}\u{1D400}");
1623 assert_eq!(span("(fi)"), "fi");
1625 assert_eq!(span("(\\223 \\351)"), "\u{FB01} \u{e9}");
1626 assert_eq!(span("<93>"), "\u{FB01}");
1627 }
1628
1629 #[test]
1630 fn a_gap_beside_a_span_is_one_space() {
1631 assert_eq!(
1633 text_of(
1634 "BT /F1 12 Tf 72 720 Td [(A) -500] TJ /Span <</ActualText (6)>> BDC (x) Tj EMC \
1635 [-500 (B)] TJ ET"
1636 ),
1637 "A 6 B"
1638 );
1639 assert_eq!(
1641 text_of(
1642 "BT /F1 12 Tf 72 720 Td [(A) -500] TJ /Span <</ActualText ( 6 )>> BDC (x) Tj \
1643 EMC [-500 (B)] TJ ET"
1644 ),
1645 "A 6 B"
1646 );
1647 assert_eq!(
1649 text_of(
1650 "BT /F1 12 Tf 72 720 Td (A) Tj /Span <</ActualText (6)>> BDC (x) Tj EMC (B) Tj ET"
1651 ),
1652 "A6B"
1653 );
1654 assert_eq!(
1657 text_of(
1658 "BT /F1 12 Tf 72 720 Td (A) Tj /Span <</ActualText (fi)>> BDC (xy) Tj EMC \
1659 [-500 (B)] TJ ET"
1660 ),
1661 "Afi B"
1662 );
1663 assert_eq!(
1665 text_of(
1666 "BT /F1 12 Tf 72 720 Td [(A) -500] TJ /Span <</ActualText ()>> BDC (x) Tj EMC \
1667 [-500 (B)] TJ ET"
1668 ),
1669 "A B"
1670 );
1671 }
1672
1673 #[test]
1677 fn an_actual_text_line_feed_is_a_space_not_a_line_break() {
1678 assert_eq!(
1680 text_of(
1681 "BT /F1 12 Tf 72 720 Td (one) Tj /Span <</ActualText <FEFF000A>>> BDC ( ) Tj EMC \
1682 (two) Tj ET"
1683 ),
1684 "one two"
1685 );
1686 assert_eq!(
1688 text_of(
1689 "BT /F1 12 Tf 72 720 Td (one) Tj /Span <</ActualText <FEFF000A>>> BDC ( ) Tj EMC \
1690 1 0 0 1 72 700 Tm (two) Tj ET"
1691 ),
1692 "one \ntwo"
1693 );
1694 }
1695
1696 #[test]
1697 fn actual_text_is_found_through_the_properties_resource() {
1698 let base = String::from_utf8(tiny_pdf()).unwrap();
1699 let pdf = base
1700 .replace(
1701 "/Resources << /Font",
1702 "/Resources << /Properties << /MC0 << /ActualText (fl) >> >> /Font",
1703 )
1704 .replace(
1705 "BT /F1 12 Tf 72 720 Td (Hi) Tj ET",
1706 "BT /F1 12 Tf 72 720 Td /Span /MC0 BDC (x) Tj EMC /Span /Nope BDC (y) Tj EMC ET",
1707 );
1708 let doc = Document::from_bytes(pdf.into_bytes(), None).unwrap();
1709 assert_eq!(doc.page(0).unwrap().text(), "fly");
1711 }
1712
1713 #[test]
1714 fn unbalanced_marked_content_does_not_leak_or_panic() {
1715 assert_eq!(
1717 text_of("BT /F1 12 Tf 72 720 Td (a) Tj /Span <</ActualText (X)>> BDC (bc) Tj ET"),
1718 "aX"
1719 );
1720 assert_eq!(
1722 text_of(
1723 "EMC EMC BT /F1 12 Tf 72 720 Td BDC (a) Tj EMC /Span 7 BDC (b) Tj EMC \
1724 /Span <</ActualText 7>> BDC (c) Tj EMC EMC ET"
1725 ),
1726 "abc"
1727 );
1728 }
1729
1730 #[test]
1731 fn page_geometry_matches_media_box() {
1732 let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1733 let page = doc.page(0).unwrap();
1734 assert_eq!((page.width(), page.height()), (612.0, 792.0));
1735 let m = page.page_matrix(1.0);
1737 let p = m.apply(Point::new(72.0, 720.0));
1738 assert!((p.x - 72.0).abs() < 0.01 && (p.y - 72.0).abs() < 0.01);
1739 }
1740
1741 #[test]
1742 fn tj_offsets_shift_the_pen_and_generate_spaces() {
1743 let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -500 (B)] TJ ET");
1745 let page = doc.page(0).unwrap();
1746 assert_eq!(page.text(), "A B");
1747 assert!(page.glyphs[1].is_generated_space);
1748
1749 let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -20 (B)] TJ ET");
1751 assert_eq!(doc.page(0).unwrap().text(), "AB");
1752 }
1753
1754 #[test]
1761 fn a_compressed_word_space_is_still_a_word_space() {
1762 let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -190 (B)] TJ ET");
1763 assert_eq!(
1764 doc.page(0).unwrap().text(),
1765 "A B",
1766 "0.19 em is a squeezed space"
1767 );
1768
1769 let doc = doc_with("BT /F1 12 Tf 72 720 Td [(A) -80 (B)] TJ ET");
1771 assert_eq!(doc.page(0).unwrap().text(), "AB", "0.08 em is not a space");
1772 }
1773
1774 #[test]
1782 fn a_substituted_glyph_reports_the_documents_advance() {
1783 let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1784 let page = doc.page(0).unwrap();
1785
1786 for glyph in &page.glyphs {
1787 let width = glyph.bbox.x1 - glyph.bbox.x0;
1788 assert!(
1789 (width - glyph.advance).abs() < 0.01,
1790 "{:?}: box {width} should span the advance {}",
1791 glyph.text,
1792 glyph.advance
1793 );
1794 }
1795
1796 let gap = page.glyphs[1].bbox.x0 - page.glyphs[0].bbox.x1;
1798 assert!(
1799 gap.abs() < 0.01,
1800 "adjacent letters must not show an ink gap"
1801 );
1802 }
1803
1804 #[test]
1805 fn text_state_operators_apply() {
1806 let doc = doc_with("BT /F1 12 Tf 50 Tz 72 720 Td (Hi) Tj ET");
1808 let page = doc.page(0).unwrap();
1809 assert!((page.glyphs[1].origin.x - (72.0 + 8.664 / 2.0)).abs() < 0.01);
1810
1811 let doc = doc_with("BT /F1 12 Tf 14 TL 72 720 Td (A) Tj T* (B) Tj ET");
1813 let page = doc.page(0).unwrap();
1814 let b = page.glyphs.iter().find(|g| g.text == "B").unwrap();
1815 assert!((b.origin.y - 706.0).abs() < 0.01);
1816 }
1817
1818 #[test]
1819 fn a_scaled_text_matrix_does_not_manufacture_spaces() {
1820 let doc = doc_with("BT /F1 12 Tf 2 0 0 2 72 720 Tm (Hi) Tj ET");
1824 let page = doc.page(0).unwrap();
1825 assert_eq!(page.text(), "Hi");
1826 assert!(!page.glyphs.iter().any(|g| g.is_generated_space));
1827 assert!((page.glyphs[1].origin.x - (72.0 + 0.722 * 12.0 * 2.0)).abs() < 0.05);
1829 }
1830
1831 #[test]
1832 fn an_oversized_advance_does_not_swallow_word_breaks() {
1833 let doc = doc_with("BT /F1 12 Tf 0.5 0 0 0.5 72 720 Tm (a) Tj 40 0 Td (b) Tj ET");
1836 let page = doc.page(0).unwrap();
1837 assert!(
1838 page.glyphs.iter().any(|g| g.is_generated_space),
1839 "a 20pt gap at 6pt effective size is a word break: {:?}",
1840 page.text()
1841 );
1842 }
1843
1844 #[test]
1845 fn text_breaks_lines_instead_of_fusing_words() {
1846 let doc = doc_with("BT /F1 12 Tf 72 720 Td (of) Tj 0 -14 Td (the) Tj ET");
1849 assert_eq!(doc.page(0).unwrap().text(), "of\nthe");
1850 }
1851
1852 #[test]
1853 fn a_wrapped_line_returning_to_the_margin_breaks() {
1854 let doc =
1856 doc_with("BT /F1 12 Tf 1 0 0 1 300 720 Tm (end) Tj 1 0 0 1 72 720 Tm (start) Tj ET");
1857 assert_eq!(doc.page(0).unwrap().text(), "end\nstart");
1858 }
1859
1860 #[test]
1861 fn a_page_sized_image_with_no_text_reads_as_scanned() {
1862 let doc =
1863 doc_with("q 612 0 0 792 0 0 cm BI /W 4 /H 4 /BPC 8 /CS /G ID 0123456789abcdef EI Q");
1864 assert!(doc.page(0).unwrap().is_likely_scanned());
1865 }
1866
1867 #[test]
1868 fn text_over_an_image_is_not_scanned() {
1869 let body = (0..40)
1871 .map(|i| {
1872 format!(
1873 "BT /F1 9 Tf 72 {} Td (line of real text) Tj ET",
1874 700 - i * 12
1875 )
1876 })
1877 .collect::<Vec<_>>()
1878 .join(" ");
1879 let doc = doc_with(&format!(
1880 "q 612 0 0 792 0 0 cm BI /W 4 /H 4 /BPC 8 /CS /G ID 0123456789abcdef EI Q {body}"
1881 ));
1882 assert!(!doc.page(0).unwrap().is_likely_scanned());
1883 }
1884
1885 #[test]
1886 fn an_ordinary_text_page_is_not_scanned() {
1887 let doc = doc_with("BT /F1 12 Tf 72 720 Td (Hi) Tj ET");
1888 assert!(!doc.page(0).unwrap().is_likely_scanned());
1889 }
1890
1891 #[test]
1892 fn cells_in_separate_text_objects_stay_separate_words() {
1893 let doc =
1897 doc_with("BT /F1 10 Tf 72 700 Td (alpha) Tj ET BT /F1 10 Tf 300 700 Td (beta) Tj ET");
1898 let text = doc.page(0).unwrap().text();
1899 assert!(
1900 text.contains("alpha") && text.contains("beta") && !text.contains("alphabeta"),
1901 "cells fused: {text:?}"
1902 );
1903 }
1904
1905 #[test]
1906 fn a_superscript_stays_on_its_line() {
1907 let doc = doc_with("BT /F1 10 Tf 72 700 Td (x) Tj 4 Ts /F1 7 Tf (2) Tj ET");
1911 assert_eq!(doc.page(0).unwrap().text(), "x2");
1912 }
1913
1914 #[test]
1915 fn ordinary_spacing_within_a_line_is_not_a_break() {
1916 let doc = doc_with("BT /F1 12 Tf 72 720 Td (a) Tj (b) Tj ET");
1917 assert_eq!(doc.page(0).unwrap().text(), "ab");
1918 }
1919
1920 #[test]
1921 fn invisible_text_is_kept_but_flagged() {
1922 let doc = doc_with("BT /F1 12 Tf 3 Tr 72 720 Td (Hi) Tj ET");
1923 let page = doc.page(0).unwrap();
1924 assert_eq!(page.text(), "Hi");
1925 assert!(!page.glyphs[0].is_visible());
1926 }
1927
1928 #[test]
1929 fn paths_are_recorded_in_user_space() {
1930 let doc = doc_with("0 0 1 rg 10 20 100 50 re f");
1931 let page = doc.page(0).unwrap();
1932 assert_eq!(page.paths.len(), 1);
1933 let p = &page.paths[0];
1934 assert_eq!(p.bbox, Rect::from_corners(10.0, 20.0, 110.0, 70.0));
1935 assert_eq!(p.fill, Some([0.0, 0.0, 1.0]));
1936 assert_eq!(p.stroke, None);
1937 }
1938
1939 #[test]
1940 fn cm_transforms_geometry_and_q_restores_it() {
1941 let doc = doc_with("q 2 0 0 2 5 5 cm 0 0 10 10 re f Q 0 0 10 10 re S");
1942 let page = doc.page(0).unwrap();
1943 assert_eq!(page.paths.len(), 2);
1944 assert_eq!(page.paths[0].bbox, Rect::from_corners(5.0, 5.0, 25.0, 25.0));
1946 assert_eq!(page.paths[1].bbox, Rect::from_corners(0.0, 0.0, 10.0, 10.0));
1948 assert!(page.paths[1].fill.is_none() && page.paths[1].stroke.is_some());
1949 }
1950
1951 #[test]
1952 fn cmyk_and_gray_colors_convert() {
1953 let doc = doc_with("0.5 g 0 0 1 1 re f 0 1 1 0 k 0 0 1 1 re f");
1954 let page = doc.page(0).unwrap();
1955 assert_eq!(page.paths[0].fill, Some([0.5, 0.5, 0.5]));
1956 assert_eq!(page.paths[1].fill, Some([1.0, 0.0, 0.0]));
1958 }
1959
1960 #[test]
1961 fn inline_images_are_recorded() {
1962 let doc =
1963 doc_with("q 100 0 0 50 10 10 cm BI /W 2 /H 2 /BPC 8 /CS /G ID \x01\x02\x03\x04 EI Q");
1964 let page = doc.page(0).unwrap();
1965 assert_eq!(page.images.len(), 1);
1966 let img = &page.images[0];
1967 assert_eq!((img.width, img.height), (2, 2));
1968 assert_eq!(img.bbox, Rect::from_corners(10.0, 10.0, 110.0, 60.0));
1969 assert_eq!((img.components, img.bits_per_component), (1, 8));
1970 match img.decode(&doc).unwrap() {
1972 ImageData::Raw { data, .. } => assert_eq!(data.len(), 4),
1973 other => panic!("expected raw samples, got {other:?}"),
1974 }
1975 }
1976
1977 #[test]
1978 fn malformed_content_does_not_panic() {
1979 for junk in [
1980 "BT (unterminated",
1981 "q q q q 1 0 0 1 cm",
1982 "BT /Missing 12 Tf (x) Tj ET",
1983 "[(A) (B)] TJ",
1984 ") ] >> garbage f",
1985 "BT /F1 0 Tf 72 720 Td (Hi) Tj ET",
1986 ] {
1987 let page = doc_with(junk).page(0).unwrap();
1988 let _ = page.text();
1989 }
1990 }
1991}
1992
1993#[cfg(test)]
1994mod thread_safety {
1995 const _: fn() = || {
1998 fn assert_send_sync<T: Send + Sync>() {}
1999 assert_send_sync::<crate::Document>();
2000 assert_send_sync::<crate::Font>();
2001 assert_send_sync::<crate::Page>();
2002 };
2003}
2004
2005#[cfg(test)]
2006mod colorspace_tests {
2007 use super::tests::doc_with;
2008
2009 #[test]
2010 fn scn_reads_operands_through_the_declared_space() {
2011 let doc = doc_with("/DeviceRGB cs 1 0 0 scn 0 0 1 1 re f");
2013 assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([1.0, 0.0, 0.0]));
2014
2015 let doc = doc_with("/DeviceGray cs 0.25 scn 0 0 1 1 re f");
2017 assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([0.25, 0.25, 0.25]));
2018 }
2019
2020 #[test]
2021 fn pattern_name_operand_leaves_the_color_alone() {
2022 let doc = doc_with("1 0 0 rg /P0 scn 0 0 1 1 re f");
2025 assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([1.0, 0.0, 0.0]));
2026 }
2027
2028 #[test]
2029 fn cs_resets_to_the_spaces_initial_color() {
2030 let doc = doc_with("1 0 0 rg /DeviceRGB cs 0 0 1 1 re f");
2033 assert_eq!(doc.page(0).unwrap().paths[0].fill, Some([0.0, 0.0, 0.0]));
2034 }
2035}