use lopdf::ObjectId;
use crate::font::FontHandle;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Color {
Rgb([f32; 3]),
Cmyk([f32; 4]),
}
impl From<[f32; 3]> for Color {
fn from(c: [f32; 3]) -> Self {
Color::Rgb(c)
}
}
impl From<[f32; 4]> for Color {
fn from(c: [f32; 4]) -> Self {
Color::Cmyk(c)
}
}
pub struct TextRun {
pub text: String,
pub font: FontHandle,
pub x: f32,
pub y: f32,
pub font_size: f32,
pub color: Color,
pub render_mode: u8,
}
#[allow(dead_code)] pub(super) struct PendingText {
pub(super) font: FontHandle,
pub(super) text: String,
pub(super) x: f32,
pub(super) y: f32,
pub(super) font_size: f32,
pub(super) render_mode: u8,
pub(super) color: Color,
pub(super) opacity: f32,
pub(super) rotation_degrees: f32,
pub(super) bold: bool,
pub(super) italic: bool,
}
pub(super) enum PendingOp {
Text(PendingText),
Replace(crate::replace::TextReplaceOp),
ReplacePreserve(crate::replace::TextReplacePreserveOp),
ReplaceResubset(crate::replace::TextReplaceResubsetOp),
#[cfg(feature = "draw")]
Draw(crate::draw::DrawOp),
}
pub(super) struct PendingPage {
pub(super) page_id: ObjectId,
pub(super) ops: Vec<PendingOp>,
}
pub(super) struct PendingBookmark {
pub(super) title: String,
pub(super) page: u32,
pub(super) y: f32,
pub(super) level: u8,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct AttachmentInfo {
pub filename: String,
pub size: usize,
pub mime_type: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PdfMetadata {
pub title: Option<String>,
pub author: Option<String>,
pub subject: Option<String>,
pub keywords: Option<String>,
pub creator: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldType {
Text,
Checkbox,
Radio,
Choice,
Signature,
Unknown,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct FormField {
pub name: String,
pub field_type: FieldType,
pub value: String,
}
#[derive(Clone, Debug, Default)]
pub struct TextFieldOptions {
pub default_value: String,
pub multiline: bool,
pub read_only: bool,
}
#[non_exhaustive]
#[derive(Clone, Debug, Default)]
pub struct ReplaceOptions {
pub normalize_whitespace: bool,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct FragmentReplaceOpts {
pub font_size: Option<f32>,
pub max_width: Option<f32>,
pub y_offset: f32,
pub color: Option<Color>,
pub shrink_to_fit: bool,
pub min_font_size: f32,
pub dry_run: bool,
}
impl Default for FragmentReplaceOpts {
fn default() -> Self {
Self {
font_size: None,
max_width: None,
y_offset: 0.0,
color: None,
shrink_to_fit: false,
min_font_size: 4.0,
dry_run: false,
}
}
}
pub struct BatchEntry<'a> {
pub fragments: &'a [crate::extract::TextFragment],
pub new_text: &'a str,
pub opts: FragmentReplaceOpts,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct FitOptions {
pub shrink_to_fit: bool,
pub min_font_size: f32,
pub color: Option<Color>,
}
impl Default for FitOptions {
fn default() -> Self {
Self { shrink_to_fit: true, min_font_size: 6.0, color: None }
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct BoxFitOptions {
pub min_font_size: f32,
pub max_lines: Option<usize>,
pub wrap: bool,
pub overflow: OverflowPolicy,
}
impl Default for BoxFitOptions {
fn default() -> Self {
Self {
min_font_size: 6.0,
max_lines: None,
wrap: true,
overflow: OverflowPolicy::WrapThenShrink,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OverflowPolicy {
Shrink,
WrapThenShrink,
Truncate,
Report,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct FitResult {
pub lines: Vec<String>,
pub font_size: f32,
pub used_rect: [f32; 4],
pub overflow_horizontal: bool,
pub overflow_vertical: bool,
}
impl FitResult {
pub fn overflow(&self) -> bool {
self.overflow_horizontal || self.overflow_vertical
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FragmentReplaceFailureReason {
NoSourceInfo,
StreamIndexOutOfRange,
XObjectNotFound,
OperatorNotFound,
DecompressFailed,
}
pub(super) struct RawFont {
pub(super) ttf_bytes: Vec<u8>,
}
pub struct Document {
pub(crate) inner: lopdf::Document,
pub(super) raw_fonts: Vec<RawFont>,
pub(super) pending: Vec<PendingPage>,
pub(super) pending_bookmarks: Vec<PendingBookmark>,
pub(super) finalized: bool,
pub(super) pending_encryption: Option<(String, String, EncAlgorithm)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum EncAlgorithm {
Rc4_128,
Aes256,
}