From 06dbac6efd98be5a015023c88ed3dbd9a35a4594 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 1 Aug 2020 00:01:17 +0200 Subject: =?UTF-8?q?Port=20font=20handling=20to=20fontdock=20and=20ttf-pars?= =?UTF-8?q?er=20=F0=9F=9B=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/syntax/func/values.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'src/syntax/func') 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) -> Result { + 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) -> Result { Paper::from_name(Ident::parse(expr)?.as_str()) -- cgit v1.2.3