use std::{collections::BTreeMap, fmt, io, ops::{Add, Deref, DerefMut, Mul}};
use crate::write::{Formatter, PdfName, Serialize};
#[derive(Clone, Eq, PartialEq)]
pub struct PdfString(Vec<u8>);
impl PdfString {
pub fn new<S: AsRef<[u8]>>(string: S) -> Self {
Self(string.as_ref().to_vec())
}
}
impl PdfString {
pub fn as_bytes(&self) -> &[u8] {
self.0.as_ref()
}
pub fn into_bytes(self) -> Vec<u8> {
self.0
}
}
impl fmt::Debug for PdfString {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
todo!()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ObjRef {
pub id: u64,
pub gen: u16,
}
#[derive(Debug, Copy, Clone)]
pub enum BaseEncoding {
MacRomanEncoding,
WinAnsiEncoding,
MacExpertEncoding,
}
impl Serialize for BaseEncoding {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
match self {
Self::MacRomanEncoding => PdfName("MacRomanEncoding").write(f),
Self::WinAnsiEncoding => PdfName("WinAnsiEncoding").write(f),
Self::MacExpertEncoding => PdfName("MacExpertEncoding").write(f),
}
}
}
#[derive(Debug, Clone)]
pub enum PageLabelKind {
Decimal,
RomanLower,
RomanUpper,
AlphaLower,
AlphaUpper,
}
impl Serialize for PageLabelKind {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
match self {
Self::Decimal => PdfName("D").write(f),
Self::RomanLower => PdfName("r").write(f),
Self::RomanUpper => PdfName("R").write(f),
Self::AlphaLower => PdfName("a").write(f),
Self::AlphaUpper => PdfName("A").write(f),
}
}
}
#[derive(Debug, Clone)]
pub struct PageLabel {
pub prefix: PdfString,
pub kind: Option<PageLabelKind>,
pub start: u32,
}
impl Serialize for PageLabel {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
let mut dict = f.pdf_dict();
dict.field("Type", &PdfName("PageLabel"))?;
dict.opt_field("S", &self.kind)?;
dict.field("St", &self.start)?;
if !self.prefix.as_bytes().is_empty() {
dict.field("P", &self.prefix)?;
}
dict.finish()
}
}
struct BTreeSer<'a, A, B>(&'a BTreeMap<A, B>);
impl<'a, A, B> Serialize for BTreeSer<'a, A, B>
where
A: Serialize,
B: Serialize,
{
fn write(&self, f: &mut Formatter) -> io::Result<()> {
let mut arr = f.pdf_arr();
for (key, value) in self.0 {
arr.entry(key)?;
arr.entry(value)?;
}
arr.finish()
}
}
pub struct NumberTree<T> {
inner: BTreeMap<usize, T>,
}
impl<T> Default for NumberTree<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> From<BTreeMap<usize, T>> for NumberTree<T> {
fn from(tree: BTreeMap<usize, T>) -> Self {
Self { inner: tree }
}
}
impl<T> NumberTree<T> {
pub fn new() -> Self {
Self {
inner: BTreeMap::new(),
}
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn insert(&mut self, key: usize, value: T) -> Option<T> {
self.inner.insert(key, value)
}
}
impl<T: Serialize> Serialize for NumberTree<T> {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
f.pdf_dict().field("Nums", &BTreeSer(&self.inner))?.finish()
}
}
#[derive(Debug, Clone)]
pub struct SparseSet<T> {
inner: Vec<Option<T>>,
}
impl<T> Default for SparseSet<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Deref for SparseSet<T> {
type Target = Vec<Option<T>>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for SparseSet<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<T> SparseSet<T> {
pub fn new() -> Self {
Self { inner: vec![] }
}
}
impl<T: Clone> SparseSet<T> {
pub fn with_size(size: usize) -> Self {
Self {
inner: vec![None; size],
}
}
}
impl<T: Serialize> Serialize for SparseSet<T> {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
let mut arr = f.pdf_arr();
let mut needs_number = true;
for (index, entry) in self.inner.iter().enumerate() {
if let Some(value) = entry {
if needs_number {
arr.entry(&index)?;
needs_number = false;
}
arr.entry(value)?;
} else {
needs_number = true;
}
}
arr.finish()
}
}
#[derive(Debug, Clone)]
pub struct Encoding<'a> {
pub base_encoding: Option<BaseEncoding>,
pub differences: Option<SparseSet<PdfName<'a>>>,
}
impl Serialize for Encoding<'_> {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
f.pdf_dict()
.field("Type", &PdfName("Encoding"))?
.opt_field("BaseEncoding", &self.base_encoding)?
.opt_field("Differences", &self.differences)?
.finish()
}
}
#[derive(Debug, Copy, Clone)]
pub struct Point<P> {
pub x: P,
pub y: P,
}
impl<P: Default> Default for Point<P> {
fn default() -> Self {
Self {
x: P::default(),
y: P::default(),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Rectangle<P> {
pub ll: Point<P>,
pub ur: Point<P>,
}
impl Rectangle<i32> {
pub fn a4_media_box() -> Self {
Rectangle {
ll: Point { x: 0, y: 0 },
ur: Point { x: 592, y: 842 },
}
}
}
impl<P: Serialize> Serialize for Rectangle<P> {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
f.pdf_arr()
.entry(&self.ll.x)?
.entry(&self.ll.y)?
.entry(&self.ur.x)?
.entry(&self.ur.y)?
.finish()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Matrix<P> {
pub a: P,
pub b: P,
pub c: P,
pub d: P,
pub e: P,
pub f: P,
}
impl<P> Mul for Matrix<P>
where
P: Copy + Mul<Output = P> + Add<Output = P>,
{
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
a: self.a * rhs.a + self.b * rhs.c,
b: self.a * rhs.b + self.b * rhs.d,
c: self.c * rhs.a + self.d * rhs.c,
d: self.c * rhs.b + self.d * rhs.d,
e: self.e * rhs.a + self.f * rhs.c + rhs.e,
f: self.e * rhs.b + self.f * rhs.d + rhs.f,
}
}
}
impl Matrix<f32> {
pub fn identity() -> Self {
Self::scale(1.0, 1.0)
}
pub fn inverse_y() -> Self {
Self::scale(1.0, -1.0)
}
pub fn inverse_x() -> Self {
Self::scale(-1.0, 1.0)
}
pub fn inverse_xy() -> Self {
Self::scale(-1.0, -1.0)
}
pub fn translate(x: f32, y: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: x,
f: y,
}
}
pub fn scale(x: f32, y: f32) -> Self {
Self {
a: x,
b: 0.0,
c: 0.0,
d: y,
e: 0.0,
f: 0.0,
}
}
pub fn default_glyph() -> Self {
Self::scale(0.0001, 0.0001)
}
}
impl<P: Serialize> Serialize for Matrix<P> {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
f.pdf_arr()
.entry(&self.a)?
.entry(&self.b)?
.entry(&self.c)?
.entry(&self.d)?
.entry(&self.e)?
.entry(&self.f)?
.finish()
}
}
pub type Dict<P> = BTreeMap<String, P>;
impl<P: Serialize> Serialize for Dict<P> {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
let mut dict = f.pdf_dict();
for (key, value) in self {
dict.field(key, value)?;
}
dict.finish()
}
}
pub enum ProcSet {
PDF,
Text,
ImageB,
ImageC,
ImageI,
}
impl Serialize for ProcSet {
fn write(&self, f: &mut Formatter) -> io::Result<()> {
match self {
Self::PDF => PdfName("PDF").write(f),
Self::Text => PdfName("Text").write(f),
Self::ImageB => PdfName("ImageB").write(f),
Self::ImageC => PdfName("ImageC").write(f),
Self::ImageI => PdfName("ImageI").write(f),
}
}
}
#[derive(Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum Trapped {
True,
False,
Unknown,
}
impl Default for Trapped {
fn default() -> Self {
Trapped::Unknown
}
}
impl Serialize for Trapped {
fn write(&self, _f: &mut Formatter) -> io::Result<()> {
todo!()
}
}