#text #render #font #word #align

pane

Align text inside of resizable rectangular panes

4 releases

0.1.3 Nov 14, 2018
0.1.2 Nov 14, 2018
0.1.1 Nov 14, 2018
0.1.0 Nov 14, 2018

#56 in Rendering

Download history 54/week @ 2018-11-19 17/week @ 2018-11-26 42/week @ 2018-12-03 18/week @ 2018-12-10

5 downloads per month

MIT license

137KB
1K SLoC

on crates.io

API Documentation

Description

This crate provides a data structure for text alignment. Rectangular Panes, which may have smaller child Panes, can be defined, and the positions of characters of text within them can be calculated.

The graphics feature, which is on by default, allows the direct rendering of a Pane with the piston2d-graphics crate.

Example

The following example creates a simple Pane tree where some nodes contain formatted text. The Pane is then drawn using my graphics_buffer crate, and the image is saved to a file.

extern crate graphics_buffer;
extern crate pane;

use graphics_buffer::*;
use pane::prelude::*;

static ROBOTO: &'static [u8] = include_bytes!("roboto.ttf");

static MESSAGE1: &'static str =
    "Somebody once told me the world is gonna role me. I ain't the sharpest tool in the shed.";

static MESSAGE2: &'static str = "She was lookin' kinda dumb with her finger and her thumb";

static MESSAGE3: &'static str = "in the shape of an 'L' on her forehead.";

fn main() {
    // Initialize the glyphs
    let mut glyphs = BufferGlyphs::from_bytes(ROBOTO).unwrap();

    // Initialize a text format
    let format = TextFormat::new(50).color(color::WHITE);

    // Create a pane
    let pane = Pane::new()
        .with_rect([0.0, 0.0, 400.0, 300.0])
        .with_color(color::BLACK)
        .with_margin(10.0)
        .with_orientation(Orientation::Horizontal)
        // Add some sub-panes
        .with_panes(vec![
            // This pane will be on the left
            Pane::new()
                .with_color(color::RED)
                .with_contents(Contents::Text(MESSAGE1.to_string(), format))
                .with_margin(5.0),
            // This pane will be on the right, but it is split into more sub-panes
            Pane::new()
                .with_color(color::WHITE)
                .with_margin(5.0)
                .with_panes(vec![
                    // This pane will be in the top-right
                    Pane::new()
                        .with_color(color::GREEN)
                        .with_contents(Contents::Text(MESSAGE2.to_string(), format.right()))
                        .with_margin(5.0),
                    // This pane will be in the bottom-right
                    Pane::new()
                        .with_color(color::BLUE)
                        .with_contents(Contents::Text(MESSAGE3.to_string(), format.centered()))
                        .with_margin(5.0),
                ]),
        ])
        .fit_text(&mut glyphs);

    // Create a RenderBuffer with the same size as the pane
    let mut buffer = RenderBuffer::new(pane.rect().width() as u32, pane.rect().height() as u32);
    buffer.clear([1.0, 1.0, 1.0, 1.0]);

    // Draw the pane to the buffer
    pane.draw(&mut glyphs, identity(), &mut buffer).unwrap();

    // Save the buffer
    buffer.save("simple.png").unwrap();
}

This example creates the image below. Notice the different text justifications.

Hey now!

Dependencies

~1MB
~19K SLoC