Struct TextObject

Source
pub struct TextObject<'a> { /* private fields */ }
Expand description

A text object is where text is put on the canvas.

A TextObject should never be created directly by the user. Instead, the Canvas.text method should be called. It will create a TextObject and call a callback, before terminating the text object properly.

§Example

let serif = canvas.get_font(BuiltinFont::Times_Roman);
// t will be a TextObject
canvas.text(|t| {
    t.set_font(&serif, 14.0)?;
    t.set_leading(18.0)?;
    t.pos(10.0, 300.0)?;
    t.show("Some lines of text in what might look like a")?;
    t.show_line("paragraph of three lines. Lorem ipsum dolor")?;
    t.show_line("sit amet. Blahonga.")?;
    Ok(())
})?;

Implementations§

Source§

impl<'a> TextObject<'a>

Source

pub fn set_font(&mut self, font: &FontRef, size: f32) -> Result<()>

Set the font and font-size to be used by the following text operations.

Examples found in repository?
examples/text.rs (line 33)
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}
Source

pub fn set_leading(&mut self, leading: f32) -> Result<()>

Set leading, the vertical distance from a line of text to the next. This is important for the show_line method.

Examples found in repository?
examples/text.rs (line 34)
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}
Source

pub fn set_rise(&mut self, rise: f32) -> Result<()>

Set the rise above the baseline for coming text. Calling set_rise again with a zero argument will get back to the old baseline.

Source

pub fn set_char_spacing(&mut self, a_c: f32) -> Result<()>

Set the amount of extra space between characters, in 1/1000 text unit.

Source

pub fn set_word_spacing(&mut self, a_w: f32) -> Result<()>

Set the amount of extra space between words, in 1/1000 text unit.

Source

pub fn set_stroke_color(&mut self, color: Color) -> Result<()>

Set color for stroking operations.

Source

pub fn set_fill_color(&mut self, color: Color) -> Result<()>

Set color for non-stroking operations.

Source

pub fn pos(&mut self, x: f32, y: f32) -> Result<()>

Move text position.

The first time pos is called in a TextObject, (x, y) refers to the same point as for Canvas::move_to, after that, the point is relative to the earlier pos.

Examples found in repository?
examples/text.rs (line 35)
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}
Source

pub fn show(&mut self, text: &str) -> Result<()>

Show a text.

Examples found in repository?
examples/text.rs (line 36)
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}
Source

pub fn show_adjusted(&mut self, param: &[(&str, i32)]) -> Result<()>

Show one or more text strings, allowing individual glyph positioning.

Each item in param should contain a string to show and a number to adjust the position. The adjustment is measured in thousands of unit of text space. Positive adjustment brings letters closer, negative widens the gap.

§Example
t.show_adjusted(&[("W", 130), ("AN", -40), ("D", 0)])
Examples found in repository?
examples/text.rs (line 39)
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}
Source

pub fn show_line(&mut self, text: &str) -> Result<()>

Show a text as a line. See also set_leading.

Examples found in repository?
examples/text.rs (line 37)
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}
Source

pub fn gsave(&mut self) -> Result<()>

Push the graphics state on a stack.

Source

pub fn grestore(&mut self) -> Result<()>

Pop a graphics state from the gsave stack and restore it.

Auto Trait Implementations§

§

impl<'a> Freeze for TextObject<'a>

§

impl<'a> !RefUnwindSafe for TextObject<'a>

§

impl<'a> !Send for TextObject<'a>

§

impl<'a> !Sync for TextObject<'a>

§

impl<'a> Unpin for TextObject<'a>

§

impl<'a> !UnwindSafe for TextObject<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.