pdf-interpret 0.5.9

A crate for interpreting PDF files.
Documentation
use crate::font::Glyph;
use crate::soft_mask::SoftMask;
use crate::{BlendMode, ClipPath, Image};
use crate::{GlyphDrawMode, Paint, PathDrawMode};
use kurbo::{Affine, BezPath, Rect, Shape};

/// A trait for a device that can be used to process PDF drawing instructions.
pub trait Device<'a> {
    /// Set the properties for future stroking operations.
    /// Set a soft mask to be used for future drawing instructions.
    fn set_soft_mask(&mut self, mask: Option<SoftMask<'a>>);
    /// Set the blend mode that should be used for rendering operations.
    fn set_blend_mode(&mut self, blend_mode: BlendMode);
    /// Draw a path.
    fn draw_path(
        &mut self,
        path: &BezPath,
        transform: Affine,
        paint: &Paint<'a>,
        draw_mode: &PathDrawMode,
    );
    /// Push a new clip path to the clip stack.
    fn push_clip_path(&mut self, clip_path: &ClipPath);
    /// Push a new transparency group to the blend stack.
    fn push_transparency_group(
        &mut self,
        opacity: f32,
        mask: Option<SoftMask<'a>>,
        blend_mode: BlendMode,
    );
    /// Draw a glyph.
    fn draw_glyph(
        &mut self,
        glyph: &Glyph<'a>,
        transform: Affine,
        glyph_transform: Affine,
        paint: &Paint<'a>,
        // TODO: Move this into outline glyph.
        draw_mode: &GlyphDrawMode,
    );
    /// Draw an image.
    fn draw_image(&mut self, image: Image<'a, '_>, transform: Affine);
    /// Pop the last clip path from the clip stack.
    fn pop_clip_path(&mut self);
    /// Pop the last transparency group from the blend stack.
    fn pop_transparency_group(&mut self);
    /// Draw a rectangle directly, without going through the general path pipeline.
    fn draw_rect(
        &mut self,
        rect: &Rect,
        transform: Affine,
        paint: &Paint<'a>,
        draw_mode: &PathDrawMode,
    ) {
        self.draw_path(&rect.to_path(0.1), transform, paint, draw_mode);
    }
    /// Called at the beginning of a marked content sequence (BMC/BDC).
    ///
    /// The tag is the marked content tag (e.g. b"P", b"Span"). The mcid is
    /// the marked content identifier from the properties dict, if present.
    ///
    /// `actual_text` is the sequence's `/ActualText` (PDF 32000-1 §14.9.4)
    /// already decoded from its text-string encoding: the characters the
    /// producer says this sequence *is*, whatever its glyphs decode to. A
    /// text extractor must prefer it over the glyphs; a rendering device has
    /// no use for it and the default implementation ignores it.
    fn begin_marked_content(
        &mut self,
        _tag: &[u8],
        _mcid: Option<i32>,
        _actual_text: Option<&str>,
    ) {
    }
    /// Called at the end of a marked content sequence (EMC).
    fn end_marked_content(&mut self) {}
    /// Text the page carries at a known place with no glyph drawn for it.
    ///
    /// `rect` is where that text sits, already in the device's own coordinate
    /// system. The one producer today is a form field whose widget has no
    /// appearance stream to draw: the value is in `/V` and nothing paints it,
    /// so a device that reads glyphs would never see it (#549). A rendering
    /// device has nothing to draw either -- synthesising the missing
    /// appearance is a different job -- and the default implementation
    /// ignores it.
    fn draw_placed_text(&mut self, _text: &str, _rect: Rect) {}
    /// Called when a TJ-array numeric adjustment is encountered between
    /// substrings. Positive values shift text backward, negative values shift
    /// it forward (1/1000 em units, per PDF §9.4.3). Text extractors use this
    /// as a high-confidence word-boundary signal.
    // ANN[r17/TEX1] Raw TJ offset surfaced so TextExtractionDevice can use it
    // as the highest-confidence signal in the multi-signal space-detection
    // consensus. Default is no-op to stay transparent to rendering devices.
    fn text_adjustment(&mut self, _amount: f32) {}
}

/// A device that discards all drawing operations.
pub struct DummyDevice;

impl Device<'_> for DummyDevice {
    fn set_soft_mask(&mut self, _: Option<SoftMask<'_>>) {}
    fn set_blend_mode(&mut self, _: BlendMode) {}
    fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'_>, _: &PathDrawMode) {}
    fn push_clip_path(&mut self, _: &ClipPath) {}
    fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'_>>, _: BlendMode) {}
    fn draw_glyph(
        &mut self,
        _: &Glyph<'_>,
        _: Affine,
        _: Affine,
        _: &Paint<'_>,
        _: &GlyphDrawMode,
    ) {
    }
    fn draw_image(&mut self, _: Image<'_, '_>, _: Affine) {}
    fn pop_clip_path(&mut self) {}
    fn pop_transparency_group(&mut self) {}
}