Skip to content

Commit

Permalink
Add two test cases and fix two bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Nov 14, 2024
1 parent 749fe13 commit 34f6b8c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,73 @@ impl Deref for Buf {
&self.inner
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{Chunk, Content, Finish, Name, Rect, Ref, Str, TextStr};

#[test]
fn test_content_limits() {
let mut limits = Limits::default();

let mut content = Content::new();
content.cubic_to(14.3, 16.2, 22.6, 30.9, 50.1, 40.0);
content.show(Str(b"Some text"));
content.set_font(Name(b"NotoSans"), 10.0);
let buf = content.finish();
limits.merge(buf.limits());

let mut content = Content::new();
content.line_to(55.0, -75.3);
content.set_font(Name(b"Noto"), 10.0);
content
.show_positioned()
.items()
.show(Str(b"A"))
.show(Str(b"B"))
.adjust(32.0);
content
.marked_content_point_with_properties(Name(b"Hi"))
.properties()
.actual_text(TextStr("text"));
let buf = content.finish();
limits.merge(buf.limits());

assert_eq!(
limits,
Limits {
int: 55,
real: 75.3,
name_len: 10,
str_len: 9,
array_len: 3,
dict_entries: 1,
}
)
}

#[test]
fn test_chunk_limits() {
let mut limits = Limits::default();

let mut chunk = Chunk::new();
let mut x_object = chunk.form_xobject(Ref::new(1), &[]);
x_object.bbox(Rect::new(4.0, 6.0, 22.1, 31.0));
x_object.finish();

limits.merge(chunk.limits());

assert_eq!(
limits,
Limits {
int: 31,
real: 22.1,
name_len: 7,
str_len: 0,
array_len: 4,
dict_entries: 4,
}
)
}
}
5 changes: 5 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ impl Primitive for Rect {
buf.push(b' ');
buf.push_val(self.y2);
buf.push(b']');

buf.limits.register_array_len(4);
}
}

Expand Down Expand Up @@ -860,6 +862,9 @@ impl<'a> Stream<'a> {

impl Drop for Stream<'_> {
fn drop(&mut self) {
let dict_len = self.dict.len as usize;
self.dict.buf.limits.register_dict_entries(dict_len);

self.dict.buf.extend_slice(b"\n>>");
self.dict.buf.extend_slice(b"\nstream\n");
self.dict.buf.extend_slice(self.data.as_ref());
Expand Down

0 comments on commit 34f6b8c

Please sign in to comment.