Skip to main content

Crate parse_css_font

Crate parse_css_font 

Source
Expand description

§parse-css-font — parse the CSS font shorthand

Split a CSS font shorthand value into its parts — style, variant, weight, stretch, size, line-height and the font-family list — or recognise a system-font keyword. A faithful Rust port of the parse-css-font npm package (handy for canvas text, PDF generation, and layout tooling). Zero dependencies and #![no_std].

use parse_css_font::{parse, Font, LineHeight};

let Font::Shorthand(f) = parse("italic bold 12px/1.5 Arial, sans-serif").unwrap() else {
    panic!("expected a shorthand");
};
assert_eq!(f.style, "italic");
assert_eq!(f.weight, "bold");
assert_eq!(f.size, "12px");
assert_eq!(f.line_height, LineHeight::Number(1.5));
assert_eq!(f.family, ["Arial", "sans-serif"]);

// System fonts are returned as-is.
assert!(matches!(parse("caption"), Ok(Font::System(_))));

Structs§

Shorthand
The components of a parsed font shorthand. Each property defaults to "normal" when not specified, matching the CSS font shorthand.

Enums§

Font
A parsed CSS font value: either a system-font keyword or the shorthand parts.
LineHeight
A line-height value.
ParseError
An error returned when a font value cannot be parsed.
SystemFont
One of the six CSS system-font keywords.

Functions§

parse
Parse a CSS font shorthand value.