Skip to main content

Module package

Module package 

Source
Expand description

Theme packages: a token file with the font files it uses, so that a theme naming its own faces travels whole.

A theme that uses font files is written as a folder, or as a .zip archive holding the same files:

my-theme/
  my-theme.tokens.json   the token file
  fonts/Inter-Regular.ttf
  fonts/Inter-Bold.ttf

The token file lists its faces in its root $extensions, each by its path relative to the token file, with the family a theme names it by and its weight (FONTS_EXTENSION):

{ "$extensions": { "org.egui-pop.fonts": [
    { "file": "fonts/Inter-Regular.ttf", "family": "Inter", "weight": 400 }
] } }

ThemePackage::read takes a folder, a .zip archive or a lone token file (its faces then found beside it, by the same relative paths). Only a package without a readable token file is an error; a face that is missing or not a font is a Warning::Font, and the families naming it fall back.

use egui_pop_tokens::TokenFile;
use egui_pop_tokens::package::ThemePackage;

let mut package = ThemePackage::new(TokenFile::new("mine"));
package.add_font("Brand", 400, "Brand-Regular.ttf", b"\0\x01\0\0 a font".to_vec());
let dir = std::env::temp_dir().join(format!("egui-pop-package-doc-{}", std::process::id()));
let file = package.write_dir(&dir).unwrap();
assert!(file.ends_with("mine.tokens.json"));
let (back, warnings) = ThemePackage::read(&dir).unwrap();
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(back.fonts[0].family, "Brand");

Archives need the zip feature (zip, MIT, with deflate only); without it, a folder or a lone file still reads and writes.

Structs§

PackageFont
A face a theme package ships.
ThemePackage
A token file and the faces it uses.

Constants§

FONTS_DIR
The folder of a package the faces are written to.
FONTS_EXTENSION
The root $extensions key listing a token file’s font files.
MAX_UNPACKED
The most an archive may hold once unpacked (256 MiB): an archive claiming more is refused, so a crafted file cannot fill memory.
PACKAGE_TOKEN_FILE
The ending of the token file a package is written with.

Functions§

is_zip
Whether a path names a .zip archive, by its ending.