#text #render #font #word #align


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


on crates.io

API Documentation


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.


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])
        // Add some sub-panes
            // This pane will be on the left
                .with_contents(Contents::Text(MESSAGE1.to_string(), format))
            // This pane will be on the right, but it is split into more sub-panes
                    // This pane will be in the top-right
                        .with_contents(Contents::Text(MESSAGE2.to_string(), format.right()))
                    // This pane will be in the bottom-right
                        .with_contents(Contents::Text(MESSAGE3.to_string(), format.centered()))
        .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

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

Hey now!


~19K SLoC