Crate ttf_word_wrap[−][src]
A library for wrapping text based on a font and a given maximum line width.
A simple example using ttf_parser
as the font parser.
use ttf_parser::Face; use ttf_word_wrap::{Wrap, WhiteSpaceWordWrap, TTFParserMeasure}; // Load a TrueType font using `ttf_parser` let font_data = std::fs::read("./test_fonts/Roboto-Regular.ttf").expect("TTF should exist"); let font_face = Face::from_slice(&font_data, 0).expect("TTF should be valid"); let measure = TTFParserMeasure::new(&font_face); // Set up wrapping options, split on whitespace: let word_wrap = WhiteSpaceWordWrap::new(20000, &measure); // Use the `Wrap` trait and split the `&str` let poem = "Mary had a little lamb whose fleece was white as snow"; let lines: Vec<&str> = poem.wrap(&word_wrap).collect(); assert_eq!(lines[0], "Mary had a little lamb");
A more complicated example that returns Position
s of the glyphs.
use ttf_parser::Face; use ttf_word_wrap::{WrapWithPosition, WhiteSpaceWordWrap, TTFParserMeasure, Position}; // Load a TrueType font using `ttf_parser` let font_data = std::fs::read("./test_fonts/Roboto-Regular.ttf").expect("TTF should exist"); let font_face = Face::from_slice(&font_data, 0).expect("TTF should be valid"); let measure = TTFParserMeasure::new(&font_face); // Set up wrapping options, split on whitespace: let word_wrap = WhiteSpaceWordWrap::new(20000, &measure); // Use the `Wrap` trait and split the `&str` let poem = "Mary had a little lamb whose fleece was white as snow"; let positions: Vec<Position> = poem.wrap_with_position(&word_wrap).collect(); // offset is in the unit (em) of the TTFParserMeasure. assert_eq!(positions[0], Position { ch: 'M', line: 0, offset: 0 });
Structs
Position | A |
TTFParserMeasure | Implements measuring glyphs via |
WhiteSpaceWordWrap | WordWrap for variable-width TTF text. |
Traits
Measure | Implementing this allows overriding of how glyphs are measured. |
Wrap | Provides |
WrapWithPosition | Provides |