144 lines
4.1 KiB
Rust
144 lines
4.1 KiB
Rust
//! Actual PDF device state shared by execution, painting and serialization.
|
|
#[derive(Clone, Copy)]
|
|
pub struct PdfInfo<'a> {
|
|
pub entries: &'a [u8],
|
|
pub date: Option<&'a str>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub(crate) struct PdfHeaderOptions {
|
|
pub major: i32,
|
|
pub minor: u8,
|
|
pub object_compression: u8,
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub draft_mode: bool,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub struct PdfFileOptions {
|
|
pub major: i32,
|
|
pub minor: u8,
|
|
pub object_compression: u8,
|
|
pub decimal_digits: u8,
|
|
pub pk_resolution: u32,
|
|
}
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum PdfDriver {
|
|
#[default]
|
|
PdfTeX,
|
|
Xdv,
|
|
}
|
|
|
|
impl PdfDriver {
|
|
fn is_pdftex(&self) -> bool {
|
|
*self == Self::PdfTeX
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub struct PdfPageData {
|
|
#[serde(default, skip_serializing_if = "PdfDriver::is_pdftex")]
|
|
pub driver: PdfDriver,
|
|
pub file: PdfFileOptions,
|
|
pub stream_compression: u8,
|
|
#[serde(default, skip_serializing_if = "is_zero")]
|
|
pub omit_procset: i32,
|
|
}
|
|
|
|
fn is_zero(value: &i32) -> bool {
|
|
*value == 0
|
|
}
|
|
|
|
impl PdfPageData {
|
|
pub(crate) fn writes_procset(&self) -> bool {
|
|
self.driver == PdfDriver::PdfTeX
|
|
&& (self.omit_procset < 0 || (self.omit_procset == 0 && self.file.major < 2))
|
|
}
|
|
}
|
|
|
|
/// TeX round_xn_over_d, including signed halfway cases.
|
|
pub fn magnify(value: i64, mag: i32) -> i64 {
|
|
let n = i128::from(value) * i128::from(mag);
|
|
if n < 0 {
|
|
-((-n + 500) / 1000) as i64
|
|
} else {
|
|
((n + 500) / 1000) as i64
|
|
}
|
|
}
|
|
/// Convert a physical dimension to the logical coordinates of a magnified page.
|
|
pub(crate) fn unmagnify(value: i64, mag: i32) -> i64 {
|
|
let mag = i64::from(mag);
|
|
let n = value * 1000;
|
|
n.signum() * ((n.abs() + mag / 2) / mag)
|
|
}
|
|
|
|
pub fn divide_scaled(value: i64, denominator: i64, digits: u8) -> (i64, i64) {
|
|
let scale = 10i128.pow(u32::from(digits));
|
|
let n = i128::from(value).abs();
|
|
let d = i128::from(denominator);
|
|
let mut q = n * scale / d;
|
|
let mut rem = n * scale % d;
|
|
if 2 * rem >= d {
|
|
q += 1;
|
|
rem -= d;
|
|
}
|
|
let out = n - rem / scale;
|
|
let sign = if value < 0 { -1 } else { 1 };
|
|
((q as i64) * sign, (out as i64) * sign)
|
|
}
|
|
pub fn print_decimal(value: i64, digits: u8) -> String {
|
|
let scale = 10i64.pow(u32::from(digits));
|
|
let sign = if value < 0 { "-" } else { "" };
|
|
let n = value.abs();
|
|
if digits == 0 {
|
|
format!("{value}")
|
|
} else {
|
|
format!(
|
|
"{sign}{}.{:0width$}",
|
|
n / scale,
|
|
n % scale,
|
|
width = usize::from(digits)
|
|
)
|
|
}
|
|
}
|
|
/// pdfTeX divide_scaled(s, one_hundred_bp, digits+2), printed as bp.
|
|
pub fn print_sp(value: i64, digits: u8) -> String {
|
|
let scale = 10i128.pow(u32::from(digits));
|
|
let n = i128::from(value).abs() * 100 * scale;
|
|
let rounded = (n + 3_289_088) / 6_578_176;
|
|
let sign = if value < 0 && rounded != 0 { "-" } else { "" };
|
|
if digits == 0 {
|
|
format!("{sign}{rounded}")
|
|
} else {
|
|
format!(
|
|
"{sign}{}.{:0width$}",
|
|
rounded / scale,
|
|
rounded % scale,
|
|
width = usize::from(digits)
|
|
)
|
|
}
|
|
}
|
|
pub(crate) mod cmap;
|
|
mod color;
|
|
pub(crate) mod xdv_annotation;
|
|
pub(crate) mod xdv_color;
|
|
pub(crate) mod xdv_destination;
|
|
pub(crate) mod xdv_document;
|
|
pub(crate) mod xdv_image;
|
|
pub(crate) mod xdv_navigation;
|
|
pub(crate) mod xdv_object;
|
|
pub(crate) mod xdv_paper;
|
|
pub mod forms;
|
|
mod graphics;
|
|
pub(crate) use color::{ColorResult, PdfColorStacks, ShippedColor};
|
|
pub use color::{PdfColorProgram, PdfColorStackAction};
|
|
pub(crate) use graphics::{Affine, DeviceOrigin, xdv_page_origin};
|
|
pub use graphics::{PdfGraphics, PdfLiteralMode, PdfMatrix};
|
|
pub mod catalog;
|
|
pub mod text_spaces;
|
|
pub(crate) mod page_attributes;
|
|
pub mod links;
|
|
|
|
#[doc(hidden)]
|
|
pub mod page_resources;
|