https://github.com/trananhtung/parse-css-font
Parse the CSS font shorthand into its components (style, variant, weight, stretch, size, line-height, family). A faithful Rust port of parse-css-font. Zero deps, no_std.
https://github.com/trananhtung/parse-css-font
css font no-std parser rust
Last synced: about 13 hours ago
JSON representation
Parse the CSS font shorthand into its components (style, variant, weight, stretch, size, line-height, family). A faithful Rust port of parse-css-font. Zero deps, no_std.
- Host: GitHub
- URL: https://github.com/trananhtung/parse-css-font
- Owner: trananhtung
- License: apache-2.0
- Created: 2026-06-22T05:56:52.000Z (7 days ago)
- Default Branch: master
- Last Pushed: 2026-06-22T14:46:53.000Z (7 days ago)
- Last Synced: 2026-06-25T15:37:14.086Z (4 days ago)
- Topics: css, font, no-std, parser, rust
- Language: Rust
- Size: 15.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# parse-css-font
[](#contributors-)
[](https://crates.io/crates/parse-css-font)
[](https://docs.rs/parse-css-font)
[](https://github.com/trananhtung/parse-css-font/actions/workflows/ci.yml)
[](#license)
**Parse the CSS [`font`](https://developer.mozilla.org/en-US/docs/Web/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`](https://www.npmjs.com/package/parse-css-font) npm package.
Zero dependencies and `#![no_std]`.
```rust
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.
```toml
[dependencies]
parse-css-font = "0.1"
```
## API
| Item | Purpose |
| --- | --- |
| `parse(value)` | `Result` |
| `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` |
| `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](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/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](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.