Skip to content

Commit

Permalink
Don't write empty resource dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 18, 2024
1 parent 8bb340f commit f339688
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
image = { version = "0.25.1", default-features = false, features = ["jpeg", "png", "gif", "webp"] }
miniz_oxide = "0.7.4"
once_cell = "1.19.0"
pdf-writer = {git = "https://github.com/LaurenzV/pdf-writer", rev = "5aa89f5"}
pdf-writer = {path = "../pdf-writer"}
resvg = {git = "https://github.com/LaurenzV/resvg", rev = "1c2b6bd0"}
siphasher = "1.0.1"
skrifa = {git="https://github.com/LaurenzV/fontations", rev="0ed7955"}
Expand Down
2 changes: 1 addition & 1 deletion src/object/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Object for Page {
let mut page = chunk.page(root_ref);
self.stream
.resource_dictionary
.to_pdf_resources(sc, &mut page.resources());
.to_pdf_resources(sc, &mut page);

page.media_box(self.media_box.to_pdf_rect());
page.parent(sc.page_tree_ref());
Expand Down
2 changes: 1 addition & 1 deletion src/object/tiling_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Object for TilingPattern {

self.stream
.resource_dictionary
.to_pdf_resources(sc, &mut tiling_pattern.resources());
.to_pdf_resources(sc, &mut tiling_pattern);

let final_bbox = pdf_writer::Rect::new(0.0, 0.0, self.width.get(), self.height.get());

Expand Down
2 changes: 1 addition & 1 deletion src/object/type3_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Object for Type3Font {
font_descriptor.finish();

let mut type3_font = chunk.type3_font(root_ref);
resource_dictionary.to_pdf_resources(sc, &mut type3_font.resources());
resource_dictionary.to_pdf_resources(sc, &mut type3_font);

type3_font.bbox(bbox.to_pdf_rect());
type3_font.to_unicode(cmap_ref);
Expand Down
2 changes: 1 addition & 1 deletion src/object/xobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Object for XObject {

self.stream
.resource_dictionary
.to_pdf_resources(sc, &mut x_object.resources());
.to_pdf_resources(sc, &mut x_object);
x_object.bbox(self.custom_bbox.unwrap_or(self.stream.bbox).to_pdf_rect());

if self.isolated || self.transparency_group_color_space {
Expand Down
34 changes: 27 additions & 7 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::object::tiling_pattern::TilingPattern;
use crate::object::xobject::XObject;
use crate::serialize::{hash_item, Object, RegisterableObject, SerializerContext};
use crate::util::NameExt;
use pdf_writer::traits::ResourcesExt;
use pdf_writer::writers::Resources;
use pdf_writer::{Chunk, Dict, Finish, Ref};
use std::collections::HashMap;
Expand Down Expand Up @@ -217,13 +218,28 @@ pub(crate) struct ResourceDictionary {
}

impl ResourceDictionary {
pub fn to_pdf_resources(self, sc: &mut SerializerContext, resources: &mut Resources) {
write_resource_type(sc, resources, self.color_spaces, false);
write_resource_type(sc, resources, self.ext_g_states, false);
write_resource_type(sc, resources, self.patterns, false);
write_resource_type(sc, resources, self.x_objects, false);
write_resource_type(sc, resources, self.shadings, false);
write_resource_type(sc, resources, self.fonts, true);
pub fn to_pdf_resources<T>(self, sc: &mut SerializerContext, parent: &mut T)
where
T: ResourcesExt,
{
if !self.is_empty() {
let resources = &mut parent.resources();
write_resource_type(sc, resources, self.color_spaces, false);
write_resource_type(sc, resources, self.ext_g_states, false);
write_resource_type(sc, resources, self.patterns, false);
write_resource_type(sc, resources, self.x_objects, false);
write_resource_type(sc, resources, self.shadings, false);
write_resource_type(sc, resources, self.fonts, true);
}
}

pub fn is_empty(&self) -> bool {
self.color_spaces.is_empty()
&& self.ext_g_states.is_empty()
&& self.patterns.is_empty()
&& self.x_objects.is_empty()
&& self.shadings.is_empty()
&& self.fonts.is_empty()
}
}

Expand Down Expand Up @@ -266,6 +282,10 @@ where
self.entries.len() as u32
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

fn name_from_number(num: ResourceNumber) -> String {
format!("{}{}", T::get_prefix(), num)
}
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/ext_g_state/all_set.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/Length 0
/Type /XObject
/Subtype /Form
/Resources <<>>
/BBox [0 0 0 0]
/Group <<
/Type /Group
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/page/simple_page.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ endobj
3 0 obj
<<
/Type /Page
/Resources <<>>
/MediaBox [0 0 200 200]
/Parent 2 0 R
/Contents 4 0 R
Expand Down

0 comments on commit f339688

Please sign in to comment.