parse-css-font
Parse the CSS font
shorthand 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.
Zero dependencies and #![no_std].
use ;
let Shorthand = parse.unwrap else ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
Why parse-css-font?
The font shorthand packs up to seven properties into one string with an order-
independent prefix, an optional size/line-height, and a quoted, comma-separated
family list. Canvas text rendering, PDF generation, and layout tools all need to
pull those apart. This is the small, dependency-free piece that does exactly that —
matching the canonical JS implementation.
[]
= "0.1"
API
| Item | Purpose |
|---|---|
parse(value) |
Result<Font, ParseError> |
Font::System(SystemFont) |
A system-font keyword (caption, icon, menu, …) |
Font::Shorthand(Shorthand) |
The parsed parts |
Shorthand { style, variant, weight, stretch, size, line_height, family } |
Strings, with family: Vec<String> |
LineHeight |
Normal, Number(f64) (unitless), or Other(String) (e.g. "14px") |
Behavior
style,variant,weight, andstretchmay appear in any order before the size, and each defaults to"normal".- The
font-sizeis required; a/line-heightafter it is optional. A unitless line-height becomesLineHeight::Number, anormalone becomesNormal, and any other (e.g.14px) becomesOther. font-familyis required, split on top-level commas, with surrounding quotes removed.- System-font keywords are matched exactly (case-sensitive) and returned as
Font::System. - Missing size/family, an empty string, or a repeated property returns a
ParseError.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of Apache-2.0 or MIT at your option.