text/
text.rs

1//! Example program drawing some text on a page.
2extern crate pdf_canvas;
3
4use pdf_canvas::graphicsstate::Color;
5use pdf_canvas::{BuiltinFont, Pdf};
6
7/// Create a `text.pdf` file, with a single page containg some
8/// text lines positioned in various ways on some helper lines.
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(&times, 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}