pub enum Color {
// some variants omitted
}
Expand description
Any color (or grayscale) value that this library can make PDF represent.
Implementations§
Source§impl Color
impl Color
Sourcepub fn rgb(red: u8, green: u8, blue: u8) -> Self
pub fn rgb(red: u8, green: u8, blue: u8) -> Self
Return a color from a RGB colorspace.
§Example
let white = Color::rgb(255, 255, 255);
let black = Color::rgb(0, 0, 0);
let red = Color::rgb(255, 0, 0);
let yellow = Color::rgb(255, 255, 0);
Examples found in repository?
examples/circles.rs (line 31)
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}
More examples
examples/text.rs (line 14)
9fn main() {
10 let mut document = Pdf::create("text.pdf").unwrap();
11 document.set_title("Text example");
12 document
13 .render_page(300.0, 400.0, |c| {
14 c.set_stroke_color(Color::rgb(200, 200, 255))?;
15 c.rectangle(10.0, 10.0, 280.0, 380.0)?;
16 c.line(10.0, 300.0, 290.0, 300.0)?;
17 c.line(150.0, 10.0, 150.0, 390.0)?;
18 c.stroke()?;
19 let helvetica = BuiltinFont::Helvetica;
20 c.left_text(10.0, 380.0, helvetica, 12.0, "Top left")?;
21 c.left_text(10.0, 10.0, helvetica, 12.0, "Bottom left")?;
22 c.right_text(290.0, 380.0, helvetica, 12.0, "Top right")?;
23 c.right_text(290.0, 10.0, helvetica, 12.0, "Bottom right")?;
24 c.center_text(
25 150.0,
26 330.0,
27 BuiltinFont::Times_Bold,
28 18.0,
29 "Centered",
30 )?;
31 let times = c.get_font(BuiltinFont::Times_Roman);
32 c.text(|t| {
33 t.set_font(×, 14.0)?;
34 t.set_leading(18.0)?;
35 t.pos(10.0, 300.0)?;
36 t.show("Some lines of text in what might look like a")?;
37 t.show_line("paragraph of three lines. Lorem ipsum dolor")?;
38 t.show_line("sit amet. Blahonga. ")?;
39 t.show_adjusted(&[("W", 130), ("AN", -40), ("D", 0)])?;
40 t.pos(0., -30.)?;
41 t.show_adjusted(
42 &(-19..21).map(|i| ("o", 16 * i)).collect::<Vec<_>>(),
43 )
44 })?;
45
46 // In Swedish, we use the letters å, ä, and ö
47 // in words like sloe liqueur. That is why rust-pdf
48 // uses /WinAnsiEncoding for text.
49 let times_italic = BuiltinFont::Times_Italic;
50 c.right_text(
51 290.0,
52 200.0,
53 times_italic,
54 14.0,
55 "På svenska använder vi bokstäverna å, ä & ö",
56 )?;
57 c.right_text(
58 290.0,
59 182.0,
60 times_italic,
61 14.0,
62 "i ord som slånbärslikör. Därför använder",
63 )?;
64 c.right_text(
65 290.0,
66 164.0,
67 times_italic,
68 14.0,
69 "rust-pdf /WinAnsiEncoding för text.",
70 )?;
71
72 c.center_text(
73 150.0,
74 130.0,
75 BuiltinFont::Symbol,
76 14.0,
77 "Hellas ΑΒΓΔαβγδ",
78 )?;
79 c.center_text(
80 150.0,
81 114.0,
82 BuiltinFont::Symbol,
83 14.0,
84 "∀ μ < δ : ∃ σ ∈ Σ",
85 )?;
86 c.center_text(
87 150.0,
88 90.0,
89 BuiltinFont::ZapfDingbats,
90 14.0,
91 "♥♠♦♣",
92 )?;
93 Ok(())
94 })
95 .unwrap();
96 document.finish().unwrap();
97}
Sourcepub fn gray(gray: u8) -> Self
pub fn gray(gray: u8) -> Self
Examples found in repository?
examples/mandala.rs (line 20)
10fn main() {
11 // Open our pdf document.
12 let mut document = Pdf::create("mandala.pdf").expect("Create PDF file");
13 let mut args = env::args().skip(1);
14 let n: u8 = args.next().map(|s| s.parse().expect("number")).unwrap_or(7);
15
16 // Render a page with something resembling a mandala on it.
17 document
18 .render_page(600.0, 600.0, |c| {
19 c.concat(Matrix::translate(300., 300.))?;
20 c.set_stroke_color(Color::gray(0))?;
21 let segment = 2. * PI / n as f32;
22 for _i in 0..n {
23 c.move_to(0., 33.5)?;
24 c.line_to(0., 250.)?;
25 let r = 99.;
26 c.circle(0., r, r * 1.25 * segment)?;
27 let d = 141.4;
28 let rr = 36.;
29 c.circle(0., d + rr, rr)?;
30 c.stroke()?;
31 c.concat(Matrix::rotate(segment))?;
32 }
33 c.concat(Matrix::rotate(segment / 2.))?;
34 for _i in 0..n {
35 let mut r0 = 58.66;
36 let mut r = 0.7705 * r0 * segment;
37 for _j in 0..(n + 1) / 3 {
38 c.circle(0., r0, r)?;
39 let r2 = 1.058 * r;
40 r0 = r0 + r + r2;
41 r = r2;
42 }
43 c.stroke()?;
44 c.concat(Matrix::rotate(segment))?;
45 }
46
47 Ok(())
48 })
49 .unwrap();
50 document.finish().unwrap();
51}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Color
impl RefUnwindSafe for Color
impl Send for Color
impl Sync for Color
impl Unpin for Color
impl UnwindSafe for Color
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more