parse-css-font 0.1.1

Parse the CSS `font` shorthand into its components (style, variant, weight, stretch, size, line-height, family). Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    32 out of 32 items documented2 out of 8 items with examples
  • Size
  • Source code size: 38.75 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 470.06 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/parse-css-font
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

parse-css-font

All Contributors

Crates.io Documentation CI License

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 parse_css_font::{parse, Font, LineHeight};

let Font::Shorthand(f) = parse("italic bold 12px/1.5 Arial, sans-serif").unwrap() else {
    unreachable!()
};
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"]);

assert!(matches!(parse("caption"), Ok(Font::System(_))));

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.

[dependencies]
parse-css-font = "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, and stretch may appear in any order before the size, and each defaults to "normal".
  • The font-size is required; a /line-height after it is optional. A unitless line-height becomes LineHeight::Number, a normal one becomes Normal, and any other (e.g. 14px) becomes Other.
  • font-family is 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.