[][src]Crate fonterator

Fonterator

Load fonts as vector graphics in pure Rust with advanced text layout. When you want to render text, fonterator gives you an iterator over footile PathOps, which you can easily pass right into footile.

Simple Example

use fonterator as font; // For parsing font file.
use footile::{FillRule, Plotter, Raster, Rgba8}; // For rendering font text.
use png_pong::{RasterBuilder, EncoderBuilder}; // For saving PNG

const FONT_SIZE: f32 = 32.0;

fn main() {
    // Example Text
    let english = "Raster Text With Font";
    let korean = "글꼴로 래스터 텍스트 사용";
    let japanese = "フォント付きラスタテキスト";

    // Init font, and paths.
    let font = font::monospace_font();

    // Init rendering.
    let mut p = Plotter::new(512, 512);
    let mut r = Raster::new(p.width(), p.height());

    // Render English Left Aligned.
    let path = font.render(
        english,
        (64.0, 0.0, 512.0 - 64.0, 512.0 - FONT_SIZE),
        (FONT_SIZE, FONT_SIZE),
        font::TextAlign::Left
    ).0;
    let path: Vec<font::PathOp> = path.collect();
    r.over(p.fill(&path, FillRule::NonZero), Rgba8::rgb(0, 0, 0));

    // Render Korean Vertically
    let path = font.render(
        korean,
        (0.0, 0.0, 512.0, 512.0 - 32.0 * 7.0),
        (FONT_SIZE, FONT_SIZE),
        font::TextAlign::Vertical
    ).0;
    let path: Vec<font::PathOp> = path.collect();
    r.over(p.fill(&path, FillRule::NonZero), Rgba8::rgb(0, 0, 0));

    // Render Japanese Vertically
    let path = font.render(
        japanese,
        (32.0, 0.0, 512.0, 512.0 - 32.0 * 7.0),
        (FONT_SIZE, FONT_SIZE),
        font::TextAlign::Vertical
    ).0;
    let path: Vec<font::PathOp> = path.collect();
    r.over(p.fill(&path, FillRule::NonZero), Rgba8::rgb(0, 0, 0));

    // Save PNG
    let raster = RasterBuilder::new()
        .with_u8_buffer(512, 512, r.as_u8_slice());
    let mut out_data = Vec::new();
    let mut encoder = EncoderBuilder::new();
    let mut encoder = encoder.encode_rasters(&mut out_data);
    encoder.add_frame(&raster, 0).expect("Failed to add frame");
    std::fs::write("out.png", out_data).expect("Failed to save image");
}

Re-exports

pub use footile;

Structs

Font

A collection of TTF/OTF fonts used as a single font.

TextPathIterator

Iterator that generates path from characters.

Enums

PathOp

Path operation.

TextAlign

Text alignment.

Constants

BOLD

Bold

ITALIC

Italic

NONE

No emphasis

Functions

licenses

Get a text string of the licenses that must be included in a binary program for using the font. Assumes BSL-1.0 for fonterator, so fonterator license does not need to be included. Requires either feature = "monospace-font" or feature = "normal-font"

monospace_font

Get a monospace font. Requires feature = "monospace-font", enabled by default.

normal_font

Get a monospace font. Requires feature = "normal-font".