main.rs (3143B)
1 use std::cmp;
2 use std::io;
3 use std::path::Path;
4 use std::vec::Vec;
5
6 use resvg::usvg;
7 use svg::node::element::Polygon;
8 use svg::node::element::Rectangle;
9 use svg::Document;
10
11 fn print_points(points: &Vec<(f64, f64)>) -> String {
12 let mut result = String::new();
13 for point in points.iter() {
14 result.push_str(&format!("{},{} ", point.0, point.1));
15 }
16 result
17 }
18
19 fn main() -> io::Result<()> {
20 // Color Pallette
21 let primary_color = "#700000";
22 let secondary_color = "#707070";
23
24 // Output Path
25 let svg_path = "logo.svg";
26 let png_path = "logo.png";
27
28 // Parse user inputs.
29 let mut input = String::new();
30 println!("Desired width:");
31 io::stdin().read_line(&mut input)?;
32 let width = input.trim().parse::<i32>().unwrap();
33 input.clear();
34 println!("Desired height:");
35 io::stdin().read_line(&mut input)?;
36 let height = input.trim().parse::<i32>().unwrap();
37
38 // Draw logo and save svg.
39 let unit = (cmp::min(width, height) as f64) / 10.;
40 let center = ((width as f64) / 2., (height as f64) / 2.);
41 let top_touch = center.0 + 2. / 3. * center.1 - 2. * unit;
42 let right_touch = center.1 + 2. * unit - 2. / 3. * center.0;
43
44 let flare_points: Vec<(f64, f64)> = vec![
45 (center.0 - 3. * unit, center.1),
46 (center.0 - 6. / 5. * unit, center.1 - 6. / 5. * unit),
47 (
48 top_touch.min(width as f64),
49 (top_touch - width as f64).max(0.),
50 ),
51 (width as f64, 0.),
52 (width as f64 + right_touch.min(0.), right_touch.max(0.)),
53 (center.0 + 6. / 5. * unit, center.1 + 6. / 5. * unit),
54 (center.0, center.1 + 3. * unit),
55 (center.0 - 6. / 5. * unit, center.1 + 6. / 5. * unit),
56 ];
57
58 let star_points: Vec<(f64, f64)> = vec![
59 (center.0 - 2. * unit, center.1),
60 (center.0 - 2. / 3. * unit, center.1 - 2. / 3. * unit),
61 (center.0, center.1 - 2. * unit),
62 (center.0 + 2. / 3. * unit, center.1 - 2. / 3. * unit),
63 (center.0 + 2. * unit, center.1),
64 (center.0 + 2. / 3. * unit, center.1 + 2. / 3. * unit),
65 (center.0, center.1 + 2. * unit),
66 (center.0 - 2. / 3. * unit, center.1 + 2. / 3. * unit),
67 ];
68
69 let sky = Rectangle::new()
70 .set("width", width)
71 .set("height", height)
72 .set("fill", primary_color);
73
74 let flare = Polygon::new()
75 .set("points", print_points(&flare_points))
76 .set("fill", secondary_color);
77
78 let star = Polygon::new()
79 .set("points", print_points(&star_points))
80 .set("fill", primary_color);
81
82 let document = Document::new()
83 .set("width", width)
84 .set("height", height)
85 .add(sky)
86 .add(flare)
87 .add(star);
88
89 svg::save(svg_path, &document).unwrap();
90
91 // Convert to png.
92 resvg::init();
93 let mut opt = resvg::Options::default();
94 opt.usvg.path = Some(svg_path.clone().into());
95 let rtree = usvg::Tree::from_file(svg_path, &opt.usvg).unwrap();
96 let backend = resvg::default_backend();
97 let img = backend.render_to_image(&rtree, &opt).unwrap();
98 img.save(Path::new(png_path));
99
100 Ok(())
101 }