Skip to content

Commit cf01e1f

Browse files
committed
episode 25 vector graphics processing
1 parent 4836428 commit cf01e1f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import cairo
2+
3+
4+
def draw(context):
5+
x, y, x1, y1 = 0.1, 0.5, 0.4, 0.9
6+
x2, y2, x3, y3 = 0.6, 0.1, 0.9, 0.5
7+
context.scale(200, 200)
8+
context.set_line_width(0.04)
9+
context.move_to(x, y)
10+
context.curve_to(x1, y1, x2, y2, x3, y3)
11+
context.stroke()
12+
context.set_source_rgba(1, 0.2, 0.2, 0.6)
13+
context.set_line_width(0.02)
14+
context.move_to(x, y)
15+
context.line_to(x1, y1)
16+
context.move_to(x2, y2)
17+
context.line_to(x3, y3)
18+
context.stroke()
19+
20+
21+
def draw_box(context, x, y, x2, y2):
22+
context.set_source_rgb(0.258, 0.525, 0.956)
23+
context.new_path()
24+
context.move_to(x, y)
25+
context.line_to(x2, y)
26+
context.line_to(x, y2)
27+
context.move_to(x2, y2)
28+
context.line_to(x2, y)
29+
context.line_to(x, y2)
30+
context.close_path()
31+
context.fill()
32+
33+
34+
if __name__ == "__main__":
35+
with cairo.SVGSurface("example.svg", 200, 200) as surface:
36+
context = cairo.Context(surface)
37+
draw_box(context, 0., 0., 32., 32.)
38+
draw(context)
39+
with cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200) as surface:
40+
context = cairo.Context(surface)
41+
draw_box(context, 0., 0., 32., 32.)
42+
draw(context)
43+
surface.write_to_png("output.png")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "cairodraw"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
cairo-rs = { version = "0.14", features = ["png", "svg"] }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
fn draw(context: &cairo::Context) -> Result<(), Box<dyn std::error::Error>> {
2+
let (x, y, x1, y1) = (0.1, 0.5, 0.4, 0.9);
3+
let (x2, y2, x3, y3) = (0.6, 0.1, 0.9, 0.5);
4+
context.scale(200., 200.);
5+
context.set_line_width(0.04);
6+
context.move_to(x, y);
7+
context.curve_to(x1, y1, x2, y2, x3, y3);
8+
context.stroke()?;
9+
context.set_source_rgba(1., 0.2, 0.2, 0.6);
10+
context.set_line_width(0.02);
11+
context.move_to(x, y);
12+
context.line_to(x1, y1);
13+
context.move_to(x2, y2);
14+
context.line_to(x3, y3);
15+
context.stroke()?;
16+
Ok(())
17+
}
18+
19+
fn draw_box(
20+
context: &cairo::Context,
21+
x: f64,
22+
y: f64,
23+
x2: f64,
24+
y2: f64,
25+
) -> Result<(), Box<dyn std::error::Error>> {
26+
context.set_source_rgb(0.258, 0.525, 0.956);
27+
context.new_path();
28+
context.move_to(x, y);
29+
context.line_to(x2, y);
30+
context.line_to(x, y2);
31+
context.move_to(x2, y2);
32+
context.line_to(x2, y);
33+
context.line_to(x, y2);
34+
context.close_path();
35+
context.fill()?;
36+
Ok(())
37+
}
38+
39+
fn main() -> Result<(), Box<dyn std::error::Error>> {
40+
let png_output_fn = "output.png";
41+
let img_surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 200, 200).unwrap();
42+
let svg_fn = std::path::Path::new("output.svg");
43+
let svg_surface = cairo::SvgSurface::new(200., 200., Some(svg_fn)).unwrap();
44+
let img_ctx = cairo::Context::new(&img_surface)?;
45+
let svg_ctx = cairo::Context::new(&svg_surface)?;
46+
47+
draw_box(&img_ctx, 0., 0., 32., 32.)?;
48+
draw_box(&svg_ctx, 0., 0., 32., 32.)?;
49+
draw(&img_ctx)?;
50+
draw(&svg_ctx)?;
51+
let mut file = std::fs::File::create(png_output_fn)?;
52+
img_surface.write_to_png(&mut file).unwrap();
53+
Ok(())
54+
}

0 commit comments

Comments
 (0)