Debian Codegraph @

examples/proportional.rs

raw
1 use cairo;
2 use rsvg;
3
4 use std::fs::File;
5 use std::io::BufWriter;
6 use std::process;
7
8 fn main() {
9 let args = std::env::args_os();
10
11 if args.len() != 5 {
12 eprintln!("usage: render <input.svg> <width> <height> <output.png>");
13 process::exit(1);
14 }
15
16 let mut args = args.skip(1);
17
18 let input = args.next().unwrap();
19 let width_os = args.next().unwrap();
20 let height_os = args.next().unwrap();
21 let output = args.next().unwrap();
22
23 let width_s = width_os.to_str().unwrap();
24 let height_s = height_os.to_str().unwrap();
25
26 let width: i32 = width_s.parse().unwrap();
27 let height: i32 = height_s.parse().unwrap();
28
29 assert!(width > 0 && height > 0);
30
31 let handle = match rsvg::Loader::new().read_path(input) {
32 Ok(handle) => handle,
33
34 Err(e) => {
35 eprintln!("loading error: {}", e);
36 process::exit(1);
37 }
38 };
39
40 let renderer = rsvg::CairoRenderer::new(&handle);
41
42 let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width, height).unwrap();
43 let cr = cairo::Context::new(&surface).expect("Failed to create a cairo context");
44
45 let res = renderer.render_document(
46 &cr,
47 &cairo::Rectangle::new(0.0, 0.0, f64::from(width), f64::from(height)),
48 );
49
50 match res {
51 Ok(()) => {
52 let mut file = BufWriter::new(File::create(output).unwrap());
53
54 surface.write_to_png(&mut file).unwrap();
55 }
56
57 Err(e) => {
58 eprintln!("rendering error: {}", e);
59 process::exit(1);
60 }
61 }
62 }