#text #align #render #font #word


Align text inside of resizable rectangular panes

9 unstable releases (3 breaking)

0.4.0 Aug 2, 2019
0.3.0 Apr 11, 2019
0.2.1 Feb 6, 2019
0.2.0 Jan 12, 2019
0.1.4 Nov 19, 2018

#36 in Rendering

43 downloads per month

MIT license



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.

API Documentation


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.

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

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

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

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

const MESSAGE3: &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, 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, format.right()))
                    // This pane will be in the bottom-right
                        .with_contents(Contents::text(MESSAGE3, format.centered()))
        // Call this at the end
        .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!


~23K SLoC