#font #glyph #sdf #graphics

sdf_glyph_renderer

从位图图像生成SDF字体符号的工具

11个版本 (2个稳定版)

1.0.1 2024年2月13日
1.0.0 2023年7月15日
0.5.1 2023年3月15日
0.4.0 2022年9月5日
0.1.2 2021年2月18日

#12 in 数据格式

Download history 207/week @ 2024-04-06 192/week @ 2024-04-13 72/week @ 2024-04-20 122/week @ 2024-04-27 169/week @ 2024-05-04 137/week @ 2024-05-11 105/week @ 2024-05-18 174/week @ 2024-05-25 123/week @ 2024-06-01 148/week @ 2024-06-08 222/week @ 2024-06-15 151/week @ 2024-06-22 170/week @ 2024-06-29 152/week @ 2024-07-06 122/week @ 2024-07-13 138/week @ 2024-07-20

每月596次下载
用于 4 个crate(2个直接使用)

BSD-3-Clause

23KB
274

SDF符号渲染器

该crate是Valve和Mapbox展示的符号距离场生成技术(此处)的Rust实现。通用的接口与任何位图一起工作,当启用可选的freetype功能时,高级接口可轻松与FreeType面交互。

该crate采用的方法与TinySDF类似;它从光栅位图而不是直接从矢量轮廓开始工作。这使crate简单,并允许它与任何位图通用。SDF使用Felzenszwalb & Huttenlocher在此论文中描述的相同算法进行计算。

而不是为字体重新发明光栅化过程,此crate依赖于FreeType生成位图。这相当快(我们谈论µs/glyph),并且结果几乎总是与sdf-glyph-foundry的更复杂的基于矢量的方法不可区分。

该crate由pbf_font_tools用于从任何FreeType可读字体生成SDF符号。如果您正在寻找批处理生成工具,请查看build_pbf_glyphs

示例使用

// The alpha raster is a Vec<u8> expressing the level of opacity at each pixel.
// In this case, the following map is for the # symbol from Open Sans Light.
// This bitmap is pre-buffered with a 3px border around it, which we note below.
let alpha = Vec::from([
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 75, 91, 55, 2, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 3, 141, 246, 196, 180, 231, 205, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 117, 236, 42, 0, 0, 10, 180, 207, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    203, 130, 0, 0, 0, 0, 46, 255, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215, 107, 0,
    0, 0, 0, 33, 255, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 163, 169, 0, 0, 0, 0, 113,
    221, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 247, 71, 0, 0, 65, 240, 71, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 241, 69, 138, 231, 71, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 8, 199, 255, 191, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 60, 222, 186, 136, 242, 58, 0, 0, 0, 0, 0, 8, 32, 0, 0, 0, 0, 0, 0, 0, 0, 85,
    240, 92, 0, 0, 117, 241, 56, 0, 0, 0, 0, 105, 175, 0, 0, 0, 0, 0, 0, 0, 17, 240, 79, 0,
    0, 0, 0, 118, 240, 54, 0, 0, 0, 199, 81, 0, 0, 0, 0, 0, 0, 0, 100, 210, 0, 0, 0, 0, 0,
    0, 118, 239, 52, 0, 73, 230, 6, 0, 0, 0, 0, 0, 0, 0, 134, 173, 0, 0, 0, 0, 0, 0, 0,
    119, 238, 64, 226, 84, 0, 0, 0, 0, 0, 0, 0, 0, 123, 196, 0, 0, 0, 0, 0, 0, 0, 0, 120,
    255, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 54, 252, 45, 0, 0, 0, 0, 0, 0, 7, 163, 230, 235,
    46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159, 230, 92, 4, 0, 0, 14, 90, 218, 189, 17, 120, 235,
    44, 0, 0, 0, 0, 0, 0, 0, 0, 3, 118, 231, 251, 222, 227, 251, 197, 91, 2, 0, 0, 125,
    233, 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 34, 55, 49, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
]);

// Construct a new bitmap using the alpha bitmap, and the raw metrics: a width of 16px,
// a height of 19px, and a 3px buffer.
let bitmap = BitmapGlyph::new(alpha, 16, 19, 3);

// Generate the signed distance field from the bitmap
let sdf = render_sdf(&bitmap, 8);

依赖项

~0.3–1.9MB
~40K SLoC