9个版本 (4个重大更改)
0.5.0 | 2021年4月7日 |
---|---|
0.4.2 | 2021年2月21日 |
0.3.0 | 2021年2月12日 |
0.2.2 | 2021年2月12日 |
0.1.0 | 2021年2月10日 |
#1547 in 文本处理
每月下载量 39次
105KB
2K SLoC
支持变量宽度字体的文本换行。
可以提供每行的拆分线或字符位置。
行拆分
use ttf_parser::Face;
use ttf_word_wrap::{TTFParserMeasure, WhiteSpaceWordWrap, Wrap};
// 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");
字符位置
use ttf_parser::Face;
use ttf_word_wrap::{WrapWithPosition, WhiteSpaceWordWrap, TTFParserMeasure, CharPosition,
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<CharPosition> = poem.wrap_with_position(&word_wrap).collect();
// offset is in the unit (em) of the TTFParserMeasure.
// If the font does not have the given char, `CharPosition::Unknown('M')` is returned.
assert!(matches!(positions[0], CharPosition::Known(Position { ch: 'M', line: 0, offset: 0, width: 1788 })));
依赖项
~1.5MB
~26K SLoC