Skip to content

Commit 4e152f6

Browse files
committed
Add tests for to_chunk
1 parent e7372cd commit 4e152f6

7 files changed

Lines changed: 1847 additions & 1779 deletions

File tree

tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fontdb = { workspace = true }
1313
svg2pdf = { workspace = true, default-features = true }
1414
usvg = { workspace = true }
1515
pdfium-render = { workspace = true, features = ["sync"] }
16-
pdf-writer = { workspace = true }
16+
pdf-writer.workspace = true
1717
image = { workspace = true }
1818
oxipng = { workspace = true }
1919
once_cell = { workspace = true }

tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ that cover certain other edge cases and some integration tests specific to `svg2
2525

2626
# Tests
2727

28-
We use a Python script to generate the `integration.rs` file, which tests all of the svg files
28+
We use a Python script to generate the `render.rs` file, which tests all of the svg files
2929
that are part of the test suites. SVG files which don't have a corresponding reference image
3030
will be skipped. To regenerate this file, you can simply run `./scripts/gen-tests.py` and
3131
it should work out of the box.

tests/ref/api/to_chunk.png

70.9 KB
Loading

tests/scripts/gen-tests.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from common import SVG_DIR, ROOT, TestFile
66
from pathlib import Path
77

8-
OUT_PATH = ROOT / "src" / "integration.rs"
8+
OUT_PATH = ROOT / "src" / "render.rs"
99

1010
NO_RELATIVE_PATHS = "no relative paths supported"
1111
INVESTIGATE = "need to investigate"
@@ -86,7 +86,21 @@
8686
def main():
8787
test_string = f"// This file was auto-generated by `{Path(__file__).name}`, do not edit manually.\n\n"
8888
test_string += "#![allow(non_snake_case)]\n\n"
89-
test_string += "#[allow(unused_imports)]\nuse crate::run_test;\nuse svg2pdf::Options;\n"
89+
test_string += "#[allow(unused_imports)]\nuse std::path::PathBuf;\nuse crate::{run_test_impl, convert_svg};\nuse svg2pdf::Options;\n"
90+
91+
test_string += """
92+
#[allow(dead_code)]
93+
pub fn get_svg_path(test_name: &str) -> PathBuf {
94+
PathBuf::from("svg").join(String::from(test_name) + ".svg")
95+
}
96+
97+
#[allow(dead_code)]
98+
pub fn run_test(test_name: &str) -> i32 {
99+
let svg_path = get_svg_path(test_name);
100+
let (pdf, actual_image) = convert_svg(&svg_path, Options::default());
101+
run_test_impl(pdf, actual_image, test_name)
102+
}\n
103+
"""
90104

91105
for p in SVG_DIR.rglob("*"):
92106
if p.is_file() and p.suffix == ".svg":
@@ -107,7 +121,7 @@ def main():
107121

108122
test_string += "#[test] "
109123

110-
test_string += f'fn {function_name}() {{assert_eq!(run_test("{test_file.test_name()}", Options::default()), 0)}}\n'
124+
test_string += f'fn {function_name}() {{assert_eq!(run_test("{test_file.test_name()}"), 0)}}\n'
111125

112126
with open(Path(OUT_PATH), "w") as file:
113127
file.write(test_string)

tests/src/api.rs

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,84 @@
1-
use crate::run_test_impl;
2-
use std::path::Path;
3-
use svg2pdf::Options;
4-
5-
fn run_api_test(svg_path: &Path, test_name: &str, options: Options) {
6-
assert_eq!(
7-
run_test_impl(
8-
Path::new(svg_path),
9-
Path::new(&format!("ref/api/{}.png", test_name)),
10-
Path::new(&format!("diff/api/{}.png", test_name)),
11-
Path::new(&format!("pdf/api/{}.pdf", test_name)),
12-
options
13-
),
14-
0
15-
);
16-
}
1+
use std::collections::HashMap;
2+
#[allow(unused_imports)]
3+
use {
4+
crate::{convert_svg, run_test_impl},
5+
crate::{render_pdf, FONTDB},
6+
pdf_writer::{Content, Finish, Name, Pdf, Rect, Ref, Str},
7+
std::path::Path,
8+
svg2pdf::Options,
9+
};
1710

1811
#[test]
1912
fn text_to_paths() {
2013
let options = Options { embed_text: false, ..Options::default() };
2114

22-
run_api_test(
23-
Path::new("svg/resvg/text/text/simple-case.svg"),
24-
"text_to_paths",
25-
options,
26-
);
15+
let svg_path = "svg/resvg/text/text/simple-case.svg";
16+
let (pdf, actual_image) = convert_svg(Path::new(svg_path), options);
17+
let res = run_test_impl(pdf, actual_image, "api/text_to_paths");
18+
assert_eq!(res, 0);
19+
}
20+
21+
#[test]
22+
fn to_chunk() {
23+
let mut alloc = Ref::new(1);
24+
let catalog_id = alloc.bump();
25+
let page_tree_id = alloc.bump();
26+
let page_id = alloc.bump();
27+
let font_id = alloc.bump();
28+
let content_id = alloc.bump();
29+
let font_name = Name(b"F1");
30+
let svg_name = Name(b"S1");
31+
32+
let path =
33+
"svg/custom/integration/wikimedia/coat_of_the_arms_of_edinburgh_city_council.svg";
34+
let svg = std::fs::read_to_string(path).unwrap();
35+
let db = FONTDB.lock().unwrap();
36+
let tree =
37+
svg2pdf::usvg::Tree::from_str(&svg, &svg2pdf::usvg::Options::default(), &db)
38+
.unwrap();
39+
let (mut svg_chunk, svg_id) =
40+
svg2pdf::to_chunk(&tree, svg2pdf::Options::default(), &db);
41+
42+
let mut map = HashMap::new();
43+
let svg_chunk =
44+
svg_chunk.renumber(|old| *map.entry(old).or_insert_with(|| alloc.bump()));
45+
let svg_id = map.get(&svg_id).unwrap();
46+
47+
let mut pdf = Pdf::new();
48+
pdf.catalog(catalog_id).pages(page_tree_id);
49+
pdf.pages(page_tree_id).kids([page_id]).count(1);
50+
51+
let mut page = pdf.page(page_id);
52+
page.media_box(Rect::new(0.0, 0.0, 595.0, 842.0));
53+
page.parent(page_tree_id);
54+
page.contents(content_id);
55+
56+
let mut resources = page.resources();
57+
resources.x_objects().pair(svg_name, svg_id);
58+
resources.fonts().pair(font_name, font_id);
59+
resources.finish();
60+
page.finish();
61+
62+
pdf.type1_font(font_id).base_font(Name(b"Helvetica"));
63+
64+
let mut content = Content::new();
65+
content
66+
.begin_text()
67+
.set_font(font_name, 16.0)
68+
.next_line(108.0, 734.0)
69+
.show(Str(b"Look at my wonderful (distorted) vector graphic!"))
70+
.end_text();
71+
72+
content
73+
.transform([300.0, 0.0, 0.0, 225.0, 147.5, 385.0])
74+
.x_object(svg_name);
75+
76+
pdf.stream(content_id, &content.finish());
77+
pdf.extend(&svg_chunk);
78+
let pdf = pdf.finish();
79+
80+
let actual_image = render_pdf(pdf.as_slice());
81+
let res = run_test_impl(pdf, actual_image, "api/to_chunk");
82+
83+
assert_eq!(res, 0);
2784
}

tests/src/lib.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[rustfmt::skip]
2-
mod integration;
2+
mod render;
33
mod api;
44

55
use std::cmp::max;
@@ -150,10 +150,6 @@ pub fn get_diff(
150150
(diff_image, pixel_diff)
151151
}
152152

153-
pub fn get_svg_path(test_name: &str) -> PathBuf {
154-
PathBuf::from("svg").join(String::from(test_name) + ".svg")
155-
}
156-
157153
pub fn get_ref_path(test_name: &str) -> PathBuf {
158154
PathBuf::from("ref").join(String::from(test_name) + ".png")
159155
}
@@ -166,24 +162,11 @@ pub fn get_pdf_path(test_name: &str) -> PathBuf {
166162
PathBuf::from("pdf").join(String::from(test_name) + ".pdf")
167163
}
168164

169-
/// Runs a single test instance.
170-
pub fn run_test(test_name: &str, options: Options) -> i32 {
171-
let svg_path = get_svg_path(test_name);
165+
pub fn run_test_impl(pdf: Vec<u8>, actual_image: RgbaImage, test_name: &str) -> i32 {
172166
let ref_path = get_ref_path(test_name);
173167
let diff_path = get_diff_path(test_name);
174168
let pdf_path = get_pdf_path(test_name);
175169

176-
run_test_impl(&svg_path, &ref_path, &diff_path, &pdf_path, options)
177-
}
178-
pub fn run_test_impl(
179-
svg_path: &Path,
180-
ref_path: &Path,
181-
diff_path: &Path,
182-
pdf_path: &Path,
183-
options: Options,
184-
) -> i32 {
185-
let (pdf, actual_image) = convert_svg(&svg_path, options);
186-
187170
// Just as a convenience, if the test is supposed to run but there doesn't exist
188171
// a reference image yet, we create a new one. This allows us to conveniently generate
189172
// new reference images for test cases.

0 commit comments

Comments
 (0)