Debian Codegraph @

third_party/rust/aa-stroke/examples/simple.rs

raw
1 use aa_stroke::{StrokeStyle, LineCap, LineJoin, Point, Stroker, tri_rasterize::rasterize_to_mask};
2
3
4
5 fn write_image(data: &[u8], path: &str, width: u32, height: u32) {
6 use std::path::Path;
7 use std::fs::File;
8 use std::io::BufWriter;
9
10 /*let mut png_data: Vec<u8> = vec![0; (width * height * 3) as usize];
11 let mut i = 0;
12 for pixel in data {
13 png_data[i] = ((pixel >> 16) & 0xff) as u8;
14 png_data[i + 1] = ((pixel >> 8) & 0xff) as u8;
15 png_data[i + 2] = ((pixel >> 0) & 0xff) as u8;
16 i += 3;
17 }*/
18
19
20 let path = Path::new(path);
21 let file = File::create(path).unwrap();
22 let w = &mut BufWriter::new(file);
23
24 let mut encoder = png::Encoder::new(w, width, height); // Width is 2 pixels and height is 1.
25 encoder.set_color(png::ColorType::Grayscale);
26 encoder.set_depth(png::BitDepth::Eight);
27 let mut writer = encoder.write_header().unwrap();
28
29 writer.write_image_data(&data).unwrap(); // Save
30 }
31
32
33 // WpfGfx uses CShapeBase which has a set of Figures
34 // Figures have FigureFlags which include FigureFlagClosed
35 // so that we can know ahead of time whether the figure/subpath
36 // is closed
37
38 // How do we handle transformed paths? D2D seems to only support transforms that
39 // can be applied before stroking. (one's with uniform scale?)
40 fn main() {
41 let mut stroker = Stroker::new(&StrokeStyle{
42 cap: LineCap::Round,
43 join: LineJoin::Bevel,
44 width: 20.,
45 ..Default::default()});
46 stroker.move_to(Point::new(20., 20.), false);
47 stroker.line_to(Point::new(100., 100.));
48 stroker.line_to_capped(Point::new(110., 20.));
49
50
51 stroker.move_to(Point::new(120., 20.), true);
52 stroker.line_to(Point::new(120., 50.));
53 stroker.line_to(Point::new(140., 50.));
54 stroker.close();
55
56 stroker.move_to(Point::new(20., 160.), true);
57 stroker.curve_to(Point::new(100., 160.), Point::new(100., 180.), Point::new(20., 180.));
58 stroker.close();
59
60 let stroked = stroker.finish();
61 dbg!(&stroked);
62
63 println!("num of vertices {}", stroked.len());
64 let mask = rasterize_to_mask(&stroked, 200, 200);
65 write_image(&mask,"out.png", 200, 200);
66 /*
67 struct Target;
68 impl CFlatteningSink for Target {
69 fn AcceptPointAndTangent(&mut self,
70 pt: &GpPointR,
71 // The point
72 vec: &GpPointR,
73 // The tangent there
74 fLast: bool
75 // Is this the last point on the curve?
76 ) -> HRESULT {
77 todo!()
78 }
79
80 fn AcceptPoint(&mut self,
81 pt: &GpPointR,
82 // The point
83 t: f64,
84 // Parameter we're at
85 fAborted: &mut bool) -> HRESULT {
86 println!("{} {}", pt.x, pt.y);
87 return S_OK;
88 }
89 }
90 let bezier = CBezier::new([GpPointR { x: 0., y: 0. },
91 GpPointR { x: 0., y: 0. },
92 GpPointR { x: 0., y: 0. },
93 GpPointR { x: 100., y: 100. }]);
94 let mut t = Target{};
95 let mut f = CBezierFlattener::new(&bezier, &mut t, 0.1);
96 f.Flatten(false);*/
97
98 }