From 287acf7ad6945f55509e65d547b0a9b33751858b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 28 Oct 2026 10:52:53 +0000 Subject: [PATCH] Refactor pdf for strict Clippy checks --- crates/pdf/README.md | 33 +++++++++++ crates/pdf/src/lib.rs | 71 +++++++++++++++++------- crates/pdf/src/tests.rs | 120 ++++++++++++++++++++++------------------ 3 files changed, 151 insertions(+), 73 deletions(-) create mode 100644 crates/pdf/README.md diff --git a/crates/pdf/README.md b/crates/pdf/README.md new file mode 100644 index 0000000000..cdb6308eb2 --- /dev/null +++ b/crates/pdf/README.md @@ -0,0 +1,33 @@ +# gridcraft-pdf + +A small, dependency-free PDF 1.7 writer for vector drawing and RGBA images. + +## API migration + +The geometry-taking methods now use `Point` and `Rect` values to name their coordinates, and +`image_rgba` groups its pixel dimensions into an array. Update calls to the four changed methods as +follows: + +```rust +use gridcraft_pdf::{Font, PdfDoc, Point, Rect}; + +let mut doc = PdfDoc::new(); +let page = doc.add_page(612.0, 792.0); + +// Before: page.link_page(20.0, 30.0, 80.0, 16.0, 1, 40.0, 50.0); +page.link_page(Rect::new(20.0, 30.0, 80.0, 16.0), 1, Point::new(40.0, 50.0)); + +// Before: page.stroke_line_dashed(0.0, 0.0, 100.0, 40.0, 1.0, [0, 0, 0], &[4.0, 2.0]); +page.stroke_line_dashed(Point::new(0.0, 0.0), Point::new(100.0, 40.0), 1.0, [0, 0, 0], &[4.0, 2.0]); + +// Before: page.text_rotated(20.0, 40.0, 12.0, Font::Helvetica, [0, 0, 0], "Label", 0.2); +page.text_rotated(Point::new(20.0, 40.0), 12.0, Font::Helvetica, [0, 0, 0], "Label", 0.2); + +let rgba = [255, 0, 0, 255]; +// Before: page.image_rgba(10.0, 10.0, 24.0, 24.0, &rgba, 1, 1); +page.image_rgba(Rect::new(10.0, 10.0, 24.0, 24.0), &rgba, [1, 1]); +``` + +`Point::new(x, y)` uses the page's top-left origin, where y increases downward. `Rect::new(x, y, +width, height)` describes the destination or link rectangle in the same coordinate system. The +`image_rgba` pixel-size array is `[width, height]`. diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index e00b4fcc3c..5270d43116 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -20,6 +20,34 @@ use std::fmt::Write as _; /// RGB colour, 0–255 per channel. pub type Rgb = [u8; 3]; +/// A point in page coordinates, measured from the top-left corner. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Point { + pub x: f32, + pub y: f32, +} + +impl Point { + pub const fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +/// A page-space rectangle described by its top-left corner and extent. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Rect { + pub x: f32, + pub y: f32, + pub width: f32, + pub height: f32, +} + +impl Rect { + pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self { + Self { x, y, width, height } + } +} + /// The standard 14 fonts minus Symbol and ZapfDingbats. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum Font { @@ -353,11 +381,12 @@ impl Page { } } - fn link_rect(&self, x: f32, y: f32, w: f32, h: f32) -> Option<[f32; 4]> { - if !finite(&[x, y, w, h]) || !extent(w) || !extent(h) || self.links.len() >= MAX_PAGE_LINKS { + fn link_rect(&self, rect: Rect) -> Option<[f32; 4]> { + let Rect { x, y, width, height } = rect; + if !finite(&[x, y, width, height]) || !extent(width) || !extent(height) || self.links.len() >= MAX_PAGE_LINKS { return None; } - let (right, bottom) = (x + w, y + h); + let (right, bottom) = (x + width, y + height); if !finite(&[right, bottom]) { return None; } @@ -374,18 +403,18 @@ impl Page { if !allowed || uri.len() > MAX_LINK_URI_BYTES || uri.chars().any(char::is_control) { return; } - if let Some(rect) = self.link_rect(x, y, w, h) { + if let Some(rect) = self.link_rect(Rect::new(x, y, w, h)) { self.links.push(LinkAnnotation { rect, target: LinkTarget::Uri(uri.to_string()) }); } } /// Adds a link to a location on another page of this document (zero-based page index). - pub fn link_page(&mut self, x: f32, y: f32, w: f32, h: f32, page: usize, target_x: f32, target_y: f32) { - if !finite(&[target_x, target_y]) { + pub fn link_page(&mut self, rect: Rect, page: usize, target: Point) { + if !finite(&[target.x, target.y]) { return; } - if let Some(rect) = self.link_rect(x, y, w, h) { - self.links.push(LinkAnnotation { rect, target: LinkTarget::Page { index: page, x: target_x, y: target_y } }); + if let Some(rect) = self.link_rect(rect) { + self.links.push(LinkAnnotation { rect, target: LinkTarget::Page { index: page, x: target.x, y: target.y } }); } } @@ -424,12 +453,12 @@ impl Page { } pub fn stroke_line(&mut self, x0: f32, y0: f32, x1: f32, y1: f32, width: f32, color: Rgb) { - self.stroke_line_dashed(x0, y0, x1, y1, width, color, &[]); + self.stroke_line_dashed(Point::new(x0, y0), Point::new(x1, y1), width, color, &[]); } /// A line with a dash pattern (`[on, off, …]` in points; empty = solid). - pub fn stroke_line_dashed(&mut self, x0: f32, y0: f32, x1: f32, y1: f32, width: f32, color: Rgb, dash: &[f32]) { - if !finite(&[x0, y0, x1, y1]) { + pub fn stroke_line_dashed(&mut self, start: Point, end: Point, width: f32, color: Rgb, dash: &[f32]) { + if !finite(&[start.x, start.y, end.x, end.y]) { return; } let s = format!( @@ -437,10 +466,10 @@ impl Page { rgb_op(color, true), n(stroke_width(width)), dash_values(dash), - n(x0), - n(self.y(y0)), - n(x1), - n(self.y(y1)) + n(start.x), + n(self.y(start.y)), + n(end.x), + n(self.y(end.y)) ); self.op(&s); } @@ -543,20 +572,20 @@ impl Page { /// Text with its baseline starting at `(x, y)`. pub fn text(&mut self, x: f32, y: f32, size: f32, font: Font, color: Rgb, text: &str) { - self.text_rotated(x, y, size, font, color, text, 0.0); + self.text_rotated(Point::new(x, y), size, font, color, text, 0.0); } /// Text rotated by `angle` radians (clockwise on the page, like the y-down coordinate /// system) about its baseline origin. - pub fn text_rotated(&mut self, x: f32, y: f32, size: f32, font: Font, color: Rgb, text: &str, angle: f32) { - if text.is_empty() || !extent(size) || !finite(&[x, y]) { + pub fn text_rotated(&mut self, origin: Point, size: f32, font: Font, color: Rgb, text: &str, angle: f32) { + if text.is_empty() || !extent(size) || !finite(&[origin.x, origin.y]) { return; } let fi = font.index(); let size = n(size.min(10_000.0)); let (sn, cs) = if angle.is_finite() { angle.sin_cos() } else { (0.0, 1.0) }; // Clockwise in y-down = counter-clockwise negated in PDF's y-up space. - let mut s = format!("BT\n{}\n{} {} {} {} {} {} Tm\n", rgb_op(color, false), n(cs), n(-sn), n(sn), n(cs), n(x), n(self.y(y))); + let mut s = format!("BT\n{}\n{} {} {} {} {} {} Tm\n", rgb_op(color, false), n(cs), n(-sn), n(sn), n(cs), n(origin.x), n(self.y(origin.y))); // Runs of WinAnsi text use `font`; runs it cannot encode use the CID font. Each `Tj` // advances the text position, so the runs follow each other on the baseline. let mut rest = text; @@ -577,7 +606,9 @@ impl Page { /// Draws an RGBA image (`pw`×`ph` pixels, row-major, straight alpha) into the box /// `(x, y, w, h)`. Malformed or oversized buffers are ignored. - pub fn image_rgba(&mut self, x: f32, y: f32, w: f32, h: f32, rgba: &[u8], pw: u32, ph: u32) { + pub fn image_rgba(&mut self, destination: Rect, rgba: &[u8], pixel_size: [u32; 2]) { + let Rect { x, y, width: w, height: h } = destination; + let [pw, ph] = pixel_size; let px = pw as u64 * ph as u64; if pw == 0 || ph == 0 || px > MAX_IMAGE_PIXELS || (rgba.len() as u64) < px * 4 || !(extent(w) && extent(h) && finite(&[x, y])) { return; diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index e540685d01..bd1ad73a8a 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -1,4 +1,6 @@ use super::*; +use std::error::Error; +use std::io; fn find(hay: &[u8], needle: &[u8]) -> Option { hay.windows(needle.len()).position(|w| w == needle) @@ -8,22 +10,23 @@ fn rfind(hay: &[u8], needle: &[u8]) -> Option { } /// Parses the xref table back and checks each in-use entry points at `N 0 obj`. -fn check_xref(pdf: &[u8]) -> usize { - let sx = rfind(pdf, b"startxref\n").unwrap(); - let rest = std::str::from_utf8(&pdf[sx + 10..]).unwrap(); - let off: usize = rest.lines().next().unwrap().trim().parse().unwrap(); +fn check_xref(pdf: &[u8]) -> Result> { + let sx = rfind(pdf, b"startxref\n").ok_or_else(|| io::Error::other("missing startxref"))?; + let rest = std::str::from_utf8(&pdf[sx + 10..])?; + let off: usize = rest.lines().next().ok_or_else(|| io::Error::other("missing xref offset"))?.trim().parse()?; assert!(pdf[off..].starts_with(b"xref\n"), "startxref points at xref"); - let table = std::str::from_utf8(&pdf[off..sx]).unwrap(); + let table = std::str::from_utf8(&pdf[off..sx])?; let mut lines = table.lines(); assert_eq!(lines.next(), Some("xref")); - let hdr: Vec = lines.next().unwrap().split(' ').map(|x| x.parse().unwrap()).collect(); - assert_eq!(hdr[0], 0); - let count = hdr[1]; + let hdr: Vec = + lines.next().ok_or_else(|| io::Error::other("missing xref header"))?.split(' ').map(str::parse).collect::>()?; + assert_eq!(hdr.first(), Some(&0)); + let count = *hdr.get(1).ok_or_else(|| io::Error::other("missing xref count"))?; let mut checked = 0; for i in 0..count { - let e = lines.next().unwrap(); + let e = lines.next().ok_or_else(|| io::Error::other("missing xref entry"))?; assert_eq!(e.len(), 19, "entry `{e}` (20 bytes with newline)"); - let o: usize = e[..10].parse().unwrap(); + let o: usize = e.get(..10).ok_or_else(|| io::Error::other("short xref offset"))?.parse()?; if e.trim_end().ends_with('n') { let want = format!("{i} 0 obj"); assert!(pdf[o..].starts_with(want.as_bytes()), "object {i} at {o}"); @@ -32,38 +35,39 @@ fn check_xref(pdf: &[u8]) -> usize { } assert!(table.contains("trailer")); assert!(table.contains(&format!("/Size {count}"))); - checked + Ok(checked) } #[test] -fn minimal_document() { +fn minimal_document() -> Result<(), Box> { let pdf = PdfDoc::new().finish(); assert!(pdf.starts_with(b"%PDF-1.7")); assert!(pdf.ends_with(b"%%EOF\n")); - assert!(check_xref(&pdf) >= 5); + assert!(check_xref(&pdf)? >= 5); + Ok(()) } #[test] -fn drawing_and_text() { +fn drawing_and_text() -> Result<(), Box> { let mut d = PdfDoc::new(); d.info.title = "Report (Q1)".into(); let p = d.add_page(612.0, 792.0); p.fill_rect(10.0, 10.0, 100.0, 20.0, [255, 255, 0]); p.rect_stroke(10.0, 10.0, 100.0, 20.0, 0.5, [0, 0, 0]); p.stroke_line(0.0, 0.0, 612.0, 792.0, 1.0, [255, 0, 0]); - p.stroke_line_dashed(0.0, 5.0, 100.0, 5.0, 1.0, [0, 0, 0], &[3.0, 2.0]); + p.stroke_line_dashed(Point::new(0.0, 5.0), Point::new(100.0, 5.0), 1.0, [0, 0, 0], &[3.0, 2.0]); p.polygon(&[[0.0, 0.0], [10.0, 0.0], [5.0, 8.0]], Some([0, 0, 255]), Some(([0, 0, 0], 1.0))); p.circle(300.0, 300.0, 20.0, Some([0, 128, 0]), None); p.save(); p.clip_rect(0.0, 0.0, 50.0, 50.0); p.text(72.0, 72.0, 12.0, Font::Helvetica, [0, 0, 0], "Hello (world) \\ café €5"); p.restore(); - p.text_rotated(100.0, 100.0, 9.0, Font::TimesBold, [0, 0, 0], "Up", -std::f32::consts::FRAC_PI_2); + p.text_rotated(Point::new(100.0, 100.0), 9.0, Font::TimesBold, [0, 0, 0], "Up", -std::f32::consts::FRAC_PI_2); let p2 = d.add_page(842.0, 595.0); p2.text(10.0, 20.0, 10.0, Font::CourierBold, [0, 0, 0], "Second"); assert_eq!(d.page_count(), 2); let pdf = d.finish(); - check_xref(&pdf); + check_xref(&pdf)?; assert!(find(&pdf, b"(Hello \\(world\\) \\\\ caf\\351 \\2005) Tj").is_some()); assert!(find(&pdf, b"(Second) Tj").is_some()); assert!(find(&pdf, b"/BaseFont /Helvetica /Encoding /WinAnsiEncoding").is_some()); @@ -74,21 +78,22 @@ fn drawing_and_text() { assert!(find(&pdf, b"/Title (Report \\(Q1\\))").is_some()); // Top-left origin: a line from (0,0) starts at the top of the page. assert!(find(&pdf, b"0 792 m 612 0 l S").is_some()); + Ok(()) } #[test] -fn uri_and_page_links_are_real_pdf_annotations() { +fn uri_and_page_links_are_real_pdf_annotations() -> Result<(), Box> { let mut d = PdfDoc::new(); let first = d.add_page(200.0, 300.0); first.link_uri(10.0, 20.0, 80.0, 30.0, "https://example.org/a(b)?q=£"); - first.link_page(100.0, 40.0, 50.0, 20.0, 1, 35.0, 60.0); + first.link_page(Rect::new(100.0, 40.0, 50.0, 20.0), 1, Point::new(35.0, 60.0)); first.link_uri(-20.0, -20.0, 40.0, 40.0, "mailto:a@example.org"); first.link_uri(0.0, 0.0, f32::NAN, 10.0, "https://bad.example"); first.link_uri(0.0, 0.0, 10.0, 10.0, "javascript:alert(1)"); let second = d.add_page(100.0, 200.0); - second.link_page(0.0, 0.0, 10.0, 10.0, 99, 0.0, 0.0); + second.link_page(Rect::new(0.0, 0.0, 10.0, 10.0), 99, Point::new(0.0, 0.0)); let pdf = d.finish(); - check_xref(&pdf); + check_xref(&pdf)?; let s = String::from_utf8_lossy(&pdf); assert_eq!(s.matches("/Subtype /Link").count(), 3); assert!(s.contains("/Annots [")); @@ -97,25 +102,27 @@ fn uri_and_page_links_are_real_pdf_annotations() { assert!(s.contains("/URI (https://example.org/a\\(b\\)?q=%C2%A3)")); assert!(s.contains("/XYZ 35 140 null")); assert!(!s.contains("https://bad.example")); + Ok(()) } #[test] -fn images_with_alpha() { +fn images_with_alpha() -> Result<(), Box> { let mut d = PdfDoc::new(); let p = d.add_page(200.0, 200.0); let mut px = Vec::new(); for i in 0..16u8 { px.extend_from_slice(&[i * 10, 0, 0, if i < 8 { 255 } else { 128 }]); } - p.image_rgba(10.0, 10.0, 40.0, 40.0, &px, 4, 4); - p.image_rgba(10.0, 10.0, 40.0, 40.0, &[255; 16], 2, 2); // opaque: no mask - p.image_rgba(0.0, 0.0, 1.0, 1.0, &[1, 2, 3], 4, 4); // too short: ignored + p.image_rgba(Rect::new(10.0, 10.0, 40.0, 40.0), &px, [4, 4]); + p.image_rgba(Rect::new(10.0, 10.0, 40.0, 40.0), &[255; 16], [2, 2]); // opaque: no mask + p.image_rgba(Rect::new(0.0, 0.0, 1.0, 1.0), &[1, 2, 3], [4, 4]); // too short: ignored let pdf = d.finish(); - check_xref(&pdf); + check_xref(&pdf)?; assert!(find(&pdf, b"/SMask").is_some()); assert!(find(&pdf, b"/Im1 Do").is_some()); assert!(find(&pdf, b"/Im2 Do").is_some()); assert!(find(&pdf, b"/Im3").is_none()); + Ok(()) } #[test] @@ -166,7 +173,7 @@ fn widths() { } #[test] -fn hostile_inputs_never_panic() { +fn hostile_inputs_never_panic() -> Result<(), Box> { let mut d = PdfDoc::new(); let p = d.add_page(f32::NAN, -5.0); p.text(f32::INFINITY, f32::NAN, 1e30, Font::Helvetica, [0, 0, 0], "x"); @@ -176,9 +183,10 @@ fn hostile_inputs_never_panic() { p.restore(); p.restore(); p.save(); - p.image_rgba(0.0, 0.0, 10.0, 10.0, &[], u32::MAX, u32::MAX); + p.image_rgba(Rect::new(0.0, 0.0, 10.0, 10.0), &[], [u32::MAX, u32::MAX]); let pdf = d.finish(); - check_xref(&pdf); + check_xref(&pdf)?; + Ok(()) } fn page_content(d: &PdfDoc) -> &str { @@ -198,9 +206,9 @@ fn text_width_does_not_overflow() { fn all_zero_dash_is_drawn_solid() { let mut d = PdfDoc::new(); let p = d.add_page(100.0, 100.0); - p.stroke_line_dashed(0.0, 0.0, 50.0, 0.0, 1.0, [0, 0, 0], &[0.0, 0.0]); + p.stroke_line_dashed(Point::new(0.0, 0.0), Point::new(50.0, 0.0), 1.0, [0, 0, 0], &[0.0, 0.0]); p.polyline(&[[0.0, 0.0], [5.0, 5.0]], 1.0, [0, 0, 0], &[0.0, 0.0001, f32::NAN]); - p.stroke_line_dashed(0.0, 0.0, 50.0, 0.0, 1.0, [0, 0, 0], &[3.0, 2.0]); + p.stroke_line_dashed(Point::new(0.0, 0.0), Point::new(50.0, 0.0), 1.0, [0, 0, 0], &[3.0, 2.0]); let c = page_content(&d); assert_eq!(c.matches("[] 0 d").count(), 2); assert!(!c.contains("[0 0]")); @@ -226,15 +234,15 @@ fn non_finite_image_size_stores_nothing() { let p = d.add_page(200.0, 200.0); let px = [255u8; 16]; for (w, h) in [(f32::NAN, 10.0), (10.0, f32::NAN), (f32::INFINITY, 10.0), (-1.0, 10.0)] { - p.image_rgba(0.0, 0.0, w, h, &px, 2, 2); + p.image_rgba(Rect::new(0.0, 0.0, w, h), &px, [2, 2]); } - p.image_rgba(f32::NAN, 0.0, 10.0, 10.0, &px, 2, 2); + p.image_rgba(Rect::new(f32::NAN, 0.0, 10.0, 10.0), &px, [2, 2]); assert!(p.images.is_empty()); assert!(p.content.is_empty()); } #[test] -fn every_draw_method_survives_hostile_numbers() { +fn every_draw_method_survives_hostile_numbers() -> Result<(), Box> { let bad = [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -7.0, 0.0, 1.0e30, -1.0e30]; let px = [200u8; 16]; let mut d = PdfDoc::new(); @@ -245,41 +253,45 @@ fn every_draw_method_survives_hostile_numbers() { p.fill_rect(a, b, b, a, [1, 2, 3]); p.rect_stroke(a, b, a, b, a, [1, 2, 3]); p.stroke_line(a, b, b, a, a, [1, 2, 3]); - p.stroke_line_dashed(a, b, b, a, b, [1, 2, 3], &[a, b, 0.0]); + p.stroke_line_dashed(Point::new(a, b), Point::new(b, a), b, [1, 2, 3], &[a, b, 0.0]); p.polyline(&[[a, b], [b, a], [1.0, 1.0]], a, [1, 2, 3], &[b, a]); p.polygon(&[[a, b], [b, a], [1.0, 1.0]], Some([1, 2, 3]), Some(([4, 5, 6], a))); p.circle(a, b, b, Some([1, 2, 3]), Some(([4, 5, 6], a))); p.text(a, b, a, Font::Helvetica, [0, 0, 0], "x"); - p.text_rotated(a, b, 10.0, Font::TimesRoman, [0, 0, 0], "y", b); - p.image_rgba(a, b, b, a, &px, 2, 2); + p.text_rotated(Point::new(a, b), 10.0, Font::TimesRoman, [0, 0, 0], "y", b); + p.image_rgba(Rect::new(a, b, b, a), &px, [2, 2]); } } - p.stroke_line_dashed(0.0, 0.0, 1.0, 1.0, 1.0, [0, 0, 0], &[0.0; 100]); + p.stroke_line_dashed(Point::new(0.0, 0.0), Point::new(1.0, 1.0), 1.0, [0, 0, 0], &[0.0; 100]); let c = page_content(&d); assert!(!c.contains("NaN") && !c.contains("inf")); let pdf = d.finish(); - check_xref(&pdf); + check_xref(&pdf)?; + Ok(()) } /// Decodes the `` hex string following `key` in the file bytes. -fn utf16_entry(pdf: &[u8], key: &str) -> String { - let at = find(pdf, key.as_bytes()).unwrap() + key.len(); - let rest = std::str::from_utf8(&pdf[at..at + 9000.min(pdf.len() - at)]).unwrap_or(""); - let hex = rest.trim_start().strip_prefix("').unwrap()]; - let units: Vec = (0..hex.len()).step_by(4).map(|i| u16::from_str_radix(&hex[i..i + 4], 16).unwrap()).collect(); - String::from_utf16(&units).unwrap() +fn utf16_entry(pdf: &[u8], key: &str) -> Result> { + let at = find(pdf, key.as_bytes()).ok_or_else(|| io::Error::other("missing PDF info key"))? + key.len(); + let end = at + 9000.min(pdf.len() - at); + let rest = std::str::from_utf8(pdf.get(at..end).ok_or_else(|| io::Error::other("short PDF info value"))?)?; + let hex = rest.trim_start().strip_prefix("').ok_or_else(|| io::Error::other("missing UTF-16 terminator"))?; + let hex = hex.get(..end).ok_or_else(|| io::Error::other("invalid UTF-16 range"))?; + let units: Vec = (0..hex.len()).step_by(4).map(|i| u16::from_str_radix(hex.get(i..i + 4).unwrap_or(""), 16)).collect::>()?; + Ok(String::from_utf16(&units)?) } #[test] -fn info_strings_with_non_ascii_are_utf16be() { +fn info_strings_with_non_ascii_are_utf16be() -> Result<(), Box> { let mut doc = PdfDoc::new(); doc.add_page(200.0, 200.0); doc.info.title = "Übersicht €5 – “Q1” 日本 😀".into(); doc.info.author = "Müller & Søn".into(); let pdf = doc.finish(); - assert_eq!(utf16_entry(&pdf, "/Title"), "Übersicht €5 – “Q1” 日本 😀"); - assert_eq!(utf16_entry(&pdf, "/Author"), "Müller & Søn"); + assert_eq!(utf16_entry(&pdf, "/Title")?, "Übersicht €5 – “Q1” 日本 😀"); + assert_eq!(utf16_entry(&pdf, "/Author")?, "Müller & Søn"); + Ok(()) } #[test] @@ -293,22 +305,23 @@ fn ascii_info_strings_stay_literal_and_escaped() { } #[test] -fn info_strings_are_capped() { +fn info_strings_are_capped() -> Result<(), Box> { let mut doc = PdfDoc::new(); doc.add_page(200.0, 200.0); doc.info.title = "日".repeat(100_000); let pdf = doc.finish(); - assert_eq!(utf16_entry(&pdf, "/Title").chars().count(), MAX_INFO_CHARS); + assert_eq!(utf16_entry(&pdf, "/Title")?.chars().count(), MAX_INFO_CHARS); + Ok(()) } #[test] -fn cjk_text_is_not_replaced_by_question_marks() { +fn cjk_text_is_not_replaced_by_question_marks() -> Result<(), Box> { let mut doc = PdfDoc::new(); let p = doc.add_page(612.0, 792.0); p.text(10.0, 20.0, 10.0, Font::Helvetica, [0, 0, 0], "签字清单"); p.text(10.0, 40.0, 10.0, Font::Helvetica, [0, 0, 0], "No. 编号 2"); let pdf = doc.finish(); - check_xref(&pdf); + check_xref(&pdf)?; assert!(find(&pdf, b"(????) Tj").is_none(), "CJK text written as question marks"); assert!(find(&pdf, b"<7B7E5B576E055355> Tj").is_some()); assert!(find(&pdf, b"(No. ) Tj").is_some()); @@ -317,4 +330,5 @@ fn cjk_text_is_not_replaced_by_question_marks() { assert!(find(&pdf, b"/Encoding /UniGB-UTF16-H").is_some()); // Measured as full-width glyphs, matching the CID font's default width. assert_eq!(text_width(Font::Helvetica, 10.0, "签字"), 20.0); + Ok(()) }