| 1 | ab_glyph |
| 2 | [](https://crates.io/crates/ab_glyph) |
| 3 | [](https://docs.rs/ab_glyph) |
| 4 | ======== |
| 5 | Fast API for loading, scaling, positioning and rasterizing OpenType font glyphs. |
| 6 | |
| 7 | ```rust |
| 8 | use ab_glyph::{FontRef, Font, Glyph, point}; |
| 9 | |
| 10 | let font = FontRef::try_from_slice(include_bytes!("../../dev/fonts/Exo2-Light.otf"))?; |
| 11 | |
| 12 | // Get a glyph for 'q' with a scale & position. |
| 13 | let q_glyph: Glyph = font.glyph_id('q').with_scale_and_position(24.0, point(100.0, 0.0)); |
| 14 | |
| 15 | // Draw it. |
| 16 | if let Some(q) = font.outline_glyph(q_glyph) { |
| 17 | q.draw(|x, y, c| { /* draw pixel `(x, y)` with coverage: `c` */ }); |
| 18 | } |
| 19 | ``` |
| 20 | |
| 21 | ## no_std |
| 22 | no_std environments are supported using `alloc` & [`libm`](https://github.com/rust-lang/libm). |
| 23 | ```toml |
| 24 | ab_glyph = { default-features = false, features = ["libm"] } |
| 25 | ``` |
| 26 | |
| 27 | ## Comparison with [`rusttype`](https://gitlab.redox-os.org/redox-os/rusttype) |
| 28 | ab_glyph is a rewrite of rusttype made after I added .otf support for the latter and saw some performance issue's |
| 29 | with the rusttype API. |
| 30 | |
| 31 | ab_glyph is a more focussed API concentrating on high performance for both .ttf & .otf fonts. |
| 32 | |
| 33 | When laying out glyphs into paragraph, ab_glyph is faster than rusttype using .ttf fonts & |
| 34 | **much** faster for .otf fonts. |
| 35 | |
| 36 | ``` |
| 37 | group ab-glyph rusttype 0.9 |
| 38 | ----- -------- ------------ |
| 39 | layout_a_sentence (exo2-ttf) 1.00 11.1±0.08µs 1.56 17.3±0.14µs |
| 40 | layout_a_sentence (exo2-otf) 1.00 11.1±0.12µs 8.85 98.1±1.17µs |
| 41 | ``` |
| 42 | _Note: Numbers from May-2020 benchmarks, ab-glyph performance is also expected to have improved since then_. |