buffer_glyphs_from_bytes

Function buffer_glyphs_from_bytes 

Source
pub fn buffer_glyphs_from_bytes(
    font_data: &[u8],
) -> Result<BufferGlyphs<'_>, ()>
Expand description

Create a BufferGlyphs from some font data

Examples found in repository?
examples/text.rs (line 10)
4fn main() {
5    // Initalize the buffer
6    let mut buffer = RenderBuffer::new(150, 40);
7    buffer.clear([0.0, 0.0, 0.0, 1.0]);
8
9    // Load the font and initialize glyphs
10    let mut glyphs = buffer_glyphs_from_bytes(include_bytes!("roboto.ttf")).unwrap();
11
12    // Draw text
13    text(
14        [1.0; 4],
15        30,
16        "Oh boy!",
17        &mut glyphs,
18        IDENTITY.trans(10.0, 30.0),
19        &mut buffer,
20    )
21    .unwrap();
22
23    // Save the image
24    buffer.save("text.png").unwrap();
25}
More examples
Hide additional examples
examples/window.rs (line 12)
7fn main() {
8    // Load Matt Damon
9    let matt = RenderBuffer::decode_from_bytes(include_bytes!("matt.jpg")).unwrap();
10
11    // Load the font and initialize glyphs
12    let mut glyphs = buffer_glyphs_from_bytes(include_bytes!("roboto.ttf")).unwrap();
13
14    // Initalize the buffer
15    let mut buffer = RenderBuffer::new(matt.width(), matt.height());
16    buffer.clear([0.0, 0.0, 0.0, 1.0]);
17
18    // Draw Matt to the buffer
19    image(&matt, IDENTITY, &mut buffer);
20
21    // Give Matt red eyes
22    const RED: [f32; 4] = [1.0, 0.0, 0.0, 0.7];
23    const DIAMETER: f64 = 40.0;
24    ellipse(
25        RED,
26        [115.0, 175.0, DIAMETER, DIAMETER],
27        IDENTITY,
28        &mut buffer,
29    );
30    ellipse(
31        RED,
32        [210.0, 195.0, DIAMETER, DIAMETER],
33        IDENTITY,
34        &mut buffer,
35    );
36
37    // Let people know he is woke
38    text(
39        [0.0, 1.0, 0.0, 1.0],
40        70,
41        "# w o k e",
42        &mut glyphs,
43        IDENTITY.trans(0.0, 70.0),
44        &mut buffer,
45    )
46    .unwrap();
47
48    // Create a window
49    let mut window: PistonWindow = WindowSettings::new(
50        "piston_window texture example",
51        (matt.height(), matt.height()),
52    )
53    .exit_on_esc(true)
54    .build()
55    .unwrap();
56
57    // Create a texture from red-eyed Matt
58    let matt_texture = buffer
59        .to_g2d_texture(
60            &mut window.create_texture_context(),
61            &TextureSettings::new(),
62        )
63        .unwrap();
64
65    // Initialize a rotation
66    let mut rot = 0.0;
67
68    // Run the event loop
69    while let Some(event) = window.next() {
70        match event {
71            Event::Loop(Loop::Render(..)) => {
72                window.draw_2d(&event, |context, graphics, _| {
73                    // Clear window with black
74                    clear([0.0, 0.0, 0.0, 1.0], graphics);
75                    // Draw matt rotated and scaled
76                    image(
77                        &matt_texture,
78                        context
79                            .transform
80                            .trans(matt.height() as f64 / 2.0, matt.height() as f64 / 2.0)
81                            .scale(0.5, 0.5)
82                            .rot_rad(rot),
83                        graphics,
84                    );
85                });
86            }
87            // Rotate on update
88            Event::Loop(Loop::Update(UpdateArgs { dt, .. })) => rot += dt,
89            _ => (),
90        }
91    }
92
93    // Save the image
94    buffer.save("red_eyes.png").unwrap();
95}