#font #truetype #opentype #ttf #otf

fonterator

Load fonts as vector graphics in pure Rust with advanced text layout

11 unstable releases (5 breaking)

✓ Uses Rust 2018 edition

0.6.0 Oct 24, 2019
0.5.1 Aug 11, 2019
0.4.2 Jul 15, 2019
0.4.0 May 13, 2019
0.1.0 May 25, 2018

#24 in Rendering

Download history 50/week @ 2019-07-11 19/week @ 2019-07-18 3/week @ 2019-07-25 34/week @ 2019-08-01 51/week @ 2019-08-08 9/week @ 2019-08-15 68/week @ 2019-08-22 34/week @ 2019-08-29 33/week @ 2019-09-05 34/week @ 2019-09-12 94/week @ 2019-09-19 26/week @ 2019-09-26 20/week @ 2019-10-03 3/week @ 2019-10-10 47/week @ 2019-10-17

152 downloads per month
Used in 3 crates (2 directly)

MIT / BSL-1.0

2MB
519 lines

Fonterator

docs.rs build status crates.io discord

About | Source | Changelog | Cala Blog

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
    );
    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
    );
    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
    );
    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");
}

Features

  • Load TTF/OTF fonts and font collections.
  • Automatic kerning and font layout.
  • Horizontal and vertical text layout.
  • Left-to-right and right-to-left text layout.
  • Use fallback fonts if a character is not available from one font.
  • Align text left/center/right/vertical
  • Line Wrapping

TODO

Contributing

Contributors are always welcome! Whether it is a bug report, bug fix, feature request, feature implementation or whatever. Don't be shy about getting involved. I always make time to fix bugs, so usually a patched version of the library will be out soon after a report. Features take me longer, though. I'll also always listen to any design critiques you have. If you have any questions you can email me at jeronlau@plopgrizzly.com. Otherwise, here's a link to the issues on GitHub.

And, as always, make sure to always follow the code of conduct. Happy coding!

License

This repository is licensed under either of the following:

at your option.

Contribution Licensing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above without any additional terms or conditions.

Dependencies