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
fontshorthand. Each property defaults to"normal"when not specified, matching the CSSfontshorthand.
Enums§
- Font
- A parsed CSS
fontvalue: either a system-font keyword or the shorthand parts. - Line
Height - A
line-heightvalue. - Parse
Error - An error returned when a
fontvalue cannot be parsed. - System
Font - One of the six CSS system-font keywords.
Functions§
- parse
- Parse a CSS
fontshorthand value.