|
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 | +}; |
17 | 10 |
|
18 | 11 | #[test] |
19 | 12 | fn text_to_paths() { |
20 | 13 | let options = Options { embed_text: false, ..Options::default() }; |
21 | 14 |
|
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); |
27 | 84 | } |
0 commit comments