diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-01 00:01:17 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-01 00:10:54 +0200 |
| commit | 06dbac6efd98be5a015023c88ed3dbd9a35a4594 (patch) | |
| tree | bb3c75230098bf71d1ac23bbe7184e4ae7a6cef2 /src/syntax/func | |
| parent | 064954cf9edbb0201b6184e69978f86e93741008 (diff) | |
Port font handling to fontdock and ttf-parser 🛳
- Use fontdock for indexing fonts and querying
- Typst binary now automatically indexes and uses system fonts in addition to a fixed font folder!
- Removes subsetting support for now (was half-finished anyways, plan is to use harfbuzz for subsetting in the future)
- Adds font width configuration support
Diffstat (limited to 'src/syntax/func')
| -rw-r--r-- | src/syntax/func/values.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/syntax/func/values.rs b/src/syntax/func/values.rs index 3269f8e9..85891d5e 100644 --- a/src/syntax/func/values.rs +++ b/src/syntax/func/values.rs @@ -1,7 +1,7 @@ //! Value types for extracting function arguments. use std::fmt::{self, Display, Formatter}; -use toddle::query::{FontStyle, FontWeight}; +use fontdock::{FontStyle, FontWeight, FontWidth}; use crate::layout::prelude::*; use crate::length::{Length, ScaleLength}; @@ -148,12 +148,11 @@ impl Value for (FontWeight, bool) { match expr.v { Expr::Number(weight) => { let weight = weight.round(); - if weight >= 100.0 && weight <= 900.0 { - Ok((FontWeight(weight as i16), false)) + Ok((FontWeight(weight as u16), false)) } else { - let clamped = weight.min(900.0).max(100.0) as i16; - Ok((FontWeight(clamped), true)) + let clamped = weight.min(900.0).max(100.0); + Ok((FontWeight(clamped as u16), true)) } } Expr::Ident(id) => { @@ -168,6 +167,32 @@ impl Value for (FontWeight, bool) { } } +/// The additional boolean specifies whether a number was clamped into the range +/// 1 - 9 to make it a valid font width. +impl Value for (FontWidth, bool) { + fn parse(expr: Spanned<Expr>) -> Result<Self, Diagnostic> { + match expr.v { + Expr::Number(width) => { + let width = width.round(); + if width >= 1.0 && width <= 9.0 { + Ok((FontWidth::new(width as u16).unwrap(), false)) + } else { + let clamped = width.min(9.0).max(1.0); + Ok((FontWidth::new(clamped as u16).unwrap(), true)) + } + } + Expr::Ident(id) => { + FontWidth::from_name(id.as_str()) + .ok_or_else(|| error!("invalid font width")) + .map(|width| (width, false)) + } + other => Err( + error!("expected identifier or number, found {}", other.name()) + ), + } + } +} + impl Value for Paper { fn parse(expr: Spanned<Expr>) -> Result<Self, Diagnostic> { Paper::from_name(Ident::parse(expr)?.as_str()) |
