From 49bb7f9a2bd2404fa6e14208041ef6a94068c1ec Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 17 Mar 2021 23:14:19 +0100 Subject: [PATCH] =?UTF-8?q?Switch=20from=20name=20to=20ratio=20for=20font?= =?UTF-8?q?=20stretch=20parameter=20=F0=9F=93=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/font.rs | 26 +++++++++++++------------- src/library/mod.rs | 11 +---------- tests/typ/library/font.typ | 5 ++++- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/library/font.rs b/src/library/font.rs index 439d98055..79cc85188 100644 --- a/src/library/font.rs +++ b/src/library/font.rs @@ -12,10 +12,10 @@ use super::*; /// # Named arguments /// - Font Style: `style`, of type `font-style`. /// - Font Weight: `weight`, of type `font-weight`. -/// - Font Stretch: `stretch`, of type `font-stretch`. /// - Serif family definition: `serif`, of type `font-families`. /// - Sans-serif family definition: `sans-serif`, of type `font-families`. /// - Monospace family definition: `monospace`, of type `font-families`. +/// - Font Stretch: `stretch`, of type `relative`, between 0.5 and 2.0. /// /// # Relevant types and constants /// - Type `font-families` @@ -42,16 +42,6 @@ use super::*; /// - `extrabold` (800) /// - `black` (900) /// - coerces from `integer` -/// - Type `font-stretch` -/// - `ultra-condensed` -/// - `extra-condensed` -/// - `condensed` -/// - `semi-condensed` -/// - `normal` -/// - `semi-expanded` -/// - `expanded` -/// - `extra-expanded` -/// - `ultra-expanded` pub fn font(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value { let size = args.find::(ctx); let list: Vec<_> = args.filter::(ctx).map(|f| f.to_string()).collect(); @@ -160,7 +150,7 @@ typify! { } typify! { - FontStyle: "font style" + FontStyle: "font style", } typify! { @@ -179,5 +169,15 @@ typify! { } typify! { - FontStretch: "font stretch" + FontStretch: "font stretch", + Value::Relative(relative) => { + let f = |stretch: Self| Relative::new(stretch.to_ratio()); + let [min, max] = [f(Self::UltraCondensed), f(Self::UltraExpanded)]; + let value = Self::from_ratio(relative.get()); + return if relative < min || relative > max { + CastResult::Warn(value, format!("should be between {} and {}", min, max)) + } else { + CastResult::Ok(value) + }; + }, } diff --git a/src/library/mod.rs b/src/library/mod.rs index 7e9689941..09fda20f7 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -23,7 +23,7 @@ pub use spacing::*; use std::fmt::{self, Display, Formatter}; -use fontdock::{FontStretch, FontStyle, FontWeight}; +use fontdock::{FontStyle, FontWeight}; use crate::eval::{Scope, ValueAny, ValueFunc}; use crate::exec::Softness; @@ -81,15 +81,6 @@ pub fn new() -> Scope { set!(any: "bold", FontWeight::BOLD); set!(any: "extrabold", FontWeight::EXTRABOLD); set!(any: "black", FontWeight::BLACK); - set!(any: "ultra-condensed", FontStretch::UltraCondensed); - set!(any: "extra-condensed", FontStretch::ExtraCondensed); - set!(any: "condensed", FontStretch::Condensed); - set!(any: "semi-condensed", FontStretch::SemiCondensed); - set!(any: "normal", FontStretch::Normal); - set!(any: "semi-expanded", FontStretch::SemiExpanded); - set!(any: "expanded", FontStretch::Expanded); - set!(any: "extra-expanded", FontStretch::ExtraExpanded); - set!(any: "ultra-expanded", FontStretch::UltraExpanded); std } diff --git a/tests/typ/library/font.typ b/tests/typ/library/font.typ index 566f2e877..c00bff5af 100644 --- a/tests/typ/library/font.typ +++ b/tests/typ/library/font.typ @@ -18,7 +18,7 @@ #font(weight: bold)[Bold] // Set stretch (not available, matching closest). -#font(stretch: ultra-condensed)[Condensed] +#font(stretch: 50%)[Condensed] // Set family. #font("PT Sans")[Sans serif] @@ -45,5 +45,8 @@ Emoji: 🐪, 🌋, 🏞 // Warning: 15-19 should be between 100 and 900 #font(weight: 2700) +// Warning: 16-21 should be between 50% and 200% +#font(stretch: 1000%) + // Error: 7-27 unexpected argument #font(something: "invalid")