circles/circles.rs
1//! Example program drawing circles on a page.
2extern crate pdf_canvas;
3
4use pdf_canvas::graphicsstate::Color;
5use pdf_canvas::Pdf;
6use std::f32::consts::PI;
7
8/// Create a `circles.pdf` file, with a single page containg a circle
9/// stroked in black, overwritten with a circle in a finer yellow
10/// stroke.
11/// The black circle is drawn using the `Canvas.circle` method,
12/// which approximates a circle with four bezier curves.
13/// The yellow circle is drawn as a 200-sided polygon.
14fn main() {
15 // Open our pdf document.
16 let mut document = Pdf::create("circles.pdf").unwrap();
17
18 // Add a 400x400 pt page.
19
20 // Render-page writes the pdf file structure for a page and
21 // creates a Canvas which is sent to the function that is the last
22 // argument of the render_page method.
23 // That function then puts content on the page by calling methods
24 // on the canvas.
25 document
26 .render_page(400.0, 400.0, |c| {
27 let (x, y) = (200.0, 200.0);
28 let r = 190.0;
29
30 // Set a wide black pen and stroke a circle
31 c.set_stroke_color(Color::rgb(0, 0, 0))?;
32 c.set_line_width(2.0)?;
33 c.circle(x, y, r)?;
34 c.stroke()?;
35
36 // Set a finer yellow pen and stroke a 200-sided polygon
37 c.set_stroke_color(Color::rgb(255, 230, 150))?;
38 c.set_line_width(1.0)?;
39 c.move_to(x + r, y)?;
40 let sides: u8 = 200;
41 for n in 1..sides {
42 let phi = f32::from(n) * 2.0 * PI / f32::from(sides);
43 c.line_to(x + r * phi.cos(), y + r * phi.sin())?;
44 }
45 c.close_and_stroke()
46 })
47 .unwrap();
48 document.finish().unwrap();
49}