Skip to main content

Crate lovyangfx_fonts

Crate lovyangfx_fonts 

Source
Expand description

no_std runtime for the bitmap fonts that ship inside LovyanGFX (u8g2 wire format), plus a tool that extracts those glyph blobs out of LovyanGFX’ C sources.

Two halves:

  • Font — a zero-copy decoder over a &'static [u8] blob. No heap, no framebuffer, no extern C: hand it a glyph blob and a plot(x, y) closure and it rasterises text for you. Metrics are baseline-corrected, so mixed CJK / ASCII lines line up.

  • cstr + the lgyf-gen binary — LovyanGFX stores its fonts as PROGMEM const uint8_t lgfx_efont_cn_14[262233] = "\0\376\6..." ;, i.e. as octal-escaped C string literals. This crate decodes that into a raw .bin you can include_bytes!.

  • fonts module — zero-copy handles over the bundled eFont CN blobs (feature efont-cn-*). The data lives in the companion crate lovyangfx-fonts-efont-cn; default features carry no fonts, so decoder-only users download nothing extra.

§Getting a blob

Three routes, pick one:

  • Bundled (0.2+) — enable a font feature and call fonts::efont_cn_14() (see fonts). The blobs live in the companion data crate, byte-identical to the LovyanGFX arrays, with the upstream notices shipped alongside.
  • Extract it yourself:
cargo install --path .            # or: cargo run --bin lgyf-gen -- ...
lgyf-gen lgfx_efont_cn.c -o fonts --only lgfx_efont_cn_14 --report
  • Your own u8g2 blob from any source — Font::new(your_blob).

Blobs you extract from a LovyanGFX checkout (PlatformIO keeps one under .pio/libdeps/<env>/LovyanGFX/, Arduino under its libraries folder) carry the same upstream notices as the source. See NOTICE and licenses/ in the repository.

§Using it on a device

use lovyangfx_fonts::Font;

// 嵌入式侧一般是 `static FONT: &[u8] = include_bytes!("fonts/efont_cn_14.bin");`
// (blob 由 lgyf-gen 从你自己的 LovyanGFX 检出里生成,本 crate 不附带字库)
let blob = std::fs::read("fonts/efont_cn_14.bin").unwrap();
let font = Font::new(&blob).expect("blob too short to be a u8g2 font");

let width = font.text_width("你好AI");
font.for_each_pixel("你好AI", 232, |x, y| {
    /* draw_pixel(x, y); */
});

§What this crate will not do

It does not talk to a display, does not do font fallback across multiple blobs (loop over your own list and use Font::has_glyph), and does not handle anti-aliased or outline fonts — u8g2 blobs are 1-bit.

Modules§

cstr
把 LovyanGFX 字体 .c 源里的字模数组还原成裸字节。
fonts
捆绑字体句柄(feature efont-cn-*)。

Structs§

Font
一个字库 blob 的解码入口。'a 就是 blob 的生命周期,全程零拷贝。
Glyph
一个字形。数据切片从字形自身开始,延伸到 blob 末尾——按位读到哪儿算哪儿, 靠位预算判定越界。
Metric
一个字的度量,已按 blob 里的位宽字段解出。

Enums§

FontError
blob 不是合法字库。

Constants§

BLOB_HEADER
blob 头长度(字节)。字形表从 BLOB_HEADER 之后开始。