Skip to content

Commit

Permalink
Add small test case
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 18, 2024
1 parent 6182c75 commit 37cefd2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::serialize::{SerializeSettings, SerializerContext};
use crate::surface::PageBuilder;
use tiny_skia_path::Size;
use crate::object::page::PageLabel;

pub struct Document {
serializer_context: SerializerContext,
Expand All @@ -21,6 +22,10 @@ impl Document {
PageBuilder::new(&mut self.serializer_context, size)
}

pub fn start_page_with(&mut self, size: Size, page_label: PageLabel) -> PageBuilder {
PageBuilder::new_with(&mut self.serializer_context, size, page_label)
}

pub fn finish(self) -> Vec<u8> {
self.serializer_context.finish().finish()
}
Expand Down
38 changes: 33 additions & 5 deletions src/object/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ impl Object for Page {

impl RegisterableObject for Page {}

// TODO: Make sure that page 0 is always included.

/// A page label.
#[derive(Debug, Hash, Eq, PartialEq, Default, Clone)]
pub struct PageLabel {
Expand All @@ -80,12 +78,12 @@ impl PageLabel {
pub fn new(
style: Option<NumberingStyle>,
prefix: Option<String>,
offset: Option<NonZeroU32>,
offset: NonZeroU32,
) -> Self {
Self {
style,
prefix,
offset,
offset: Some(offset),
}
}

Expand Down Expand Up @@ -186,6 +184,7 @@ mod tests {
use pdf_writer::types::NumberingStyle;
use std::num::{NonZeroI32, NonZeroU32};
use tiny_skia_path::{PathBuilder, Rect, Size};
use crate::document::Document;

#[test]
fn simple_page() {
Expand Down Expand Up @@ -243,11 +242,40 @@ mod tests {
let page_label = PageLabel::new(
Some(NumberingStyle::Arabic),
Some("P".to_string()),
NonZeroU32::new(2),
NonZeroU32::new(2).unwrap(),
);

sc.add(page_label);

check_snapshot("page/page_label", sc.finish().as_bytes());
}

// TODO: Fix issues with not being able to create empty pages with just start_page_with.
// TODO: Fix issue with two duplicate pages not showing up.

#[test]
fn page_label_complex() {
let mut db = Document::new(SerializeSettings::default_test());
let mut page = db.start_page_with(Size::from_wh(200.0, 200.0).unwrap(), PageLabel::default());
let mut surface = page.surface();
surface.finish();
page.finish();

let mut page = db.start_page_with(Size::from_wh(250.0, 200.0).unwrap(), PageLabel::default());
let mut surface = page.surface();
surface.finish();
page.finish();

let mut page = db.start_page_with(Size::from_wh(200.0, 200.0).unwrap(), PageLabel::new(
Some(NumberingStyle::LowerRoman),
None,
NonZeroU32::new(2).unwrap()
));
let mut surface = page.surface();
surface.finish();
page.finish();


check_snapshot("page/page_label_complex", &db.finish());
}
}
11 changes: 9 additions & 2 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::resource::{ColorSpaceEnum, FontResource};
use crate::stream::PdfFont;
use crate::util::NameExt;
use fontdb::{Database, ID};
use pdf_writer::{Chunk, Filter, Finish, Pdf, Ref};
use pdf_writer::{Chunk, Filter, Finish, Name, Pdf, Ref};
use siphasher::sip128::{Hasher128, SipHasher13};
use skrifa::instance::Location;
use std::borrow::Cow;
Expand Down Expand Up @@ -80,6 +80,7 @@ pub struct SerializerContext {
font_map: HashMap<Font, FontContainer>,
catalog_ref: Ref,
page_tree_ref: Ref,
page_labels_ref: Option<Ref>,
page_refs: Vec<Ref>,
page_labels: Vec<PageLabel>,
cached_mappings: HashMap<u128, Ref>,
Expand Down Expand Up @@ -115,6 +116,7 @@ impl SerializerContext {
chunks: Vec::new(),
page_tree_ref,
catalog_ref,
page_labels_ref: None,
page_refs: vec![],
page_labels: vec![],
chunks_len: 0,
Expand Down Expand Up @@ -277,7 +279,7 @@ impl SerializerContext {
// Always needs to be called.
pub fn finish(mut self) -> Pdf {
if let Some(container) = PageLabelContainer::new(self.page_labels.clone()) {
self.add(container);
self.page_labels_ref = Some(self.add(container));
}

// Write fonts
Expand Down Expand Up @@ -317,6 +319,11 @@ impl SerializerContext {

let mut catalog = pdf.catalog(self.catalog_ref);
catalog.pages(self.page_tree_ref);

if let Some(plr) = self.page_labels_ref {
catalog.pair(Name(b"PageLabels"), plr);
}

catalog.finish();

pdf.extend(&page_tree_chunk);
Expand Down

0 comments on commit 37cefd2

Please sign in to comment.