diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-11-01 13:07:11 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-11-02 10:39:45 +0100 |
| commit | 072e7c710c763904ad3ee72cfb05252f9f0d3929 (patch) | |
| tree | dd2ffa3f4a086ef823a9a0776dddeda5777c7b14 /src/library | |
| parent | 8ccb7d6f15a2fbb44247c143b3dd821e44e0d6eb (diff) | |
Reduce namespace pollution
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/mod.rs | 37 | ||||
| -rw-r--r-- | src/library/page.rs | 3 | ||||
| -rw-r--r-- | src/library/text.rs | 56 | ||||
| -rw-r--r-- | src/library/utility.rs | 2 |
4 files changed, 57 insertions, 41 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs index c0b6f2b8..b0a42e83 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -46,10 +46,8 @@ pub use text::*; pub use transform::*; pub use utility::*; -use std::convert::TryFrom; - use crate::eval::{Scope, Value}; -use crate::font::{FontFamily, FontStretch, FontStyle, FontWeight, VerticalFontMetric}; +use crate::font::FontFamily; use crate::geom::*; /// Construct a scope containing all standard library definitions. @@ -125,16 +123,6 @@ pub fn new() -> Scope { std.def_const("serif", FontFamily::Serif); std.def_const("sans-serif", FontFamily::SansSerif); std.def_const("monospace", FontFamily::Monospace); - std.def_const("normal", FontStyle::Normal); - std.def_const("italic", FontStyle::Italic); - std.def_const("oblique", FontStyle::Oblique); - std.def_const("regular", FontWeight::REGULAR); - std.def_const("bold", FontWeight::BOLD); - std.def_const("ascender", VerticalFontMetric::Ascender); - std.def_const("cap-height", VerticalFontMetric::CapHeight); - std.def_const("x-height", VerticalFontMetric::XHeight); - std.def_const("baseline", VerticalFontMetric::Baseline); - std.def_const("descender", VerticalFontMetric::Descender); std } @@ -151,26 +139,3 @@ dynamic! { FontFamily: "font family", Value::Str(string) => Self::Named(string.to_lowercase()), } - -dynamic! { - FontStyle: "font style", -} - -dynamic! { - FontWeight: "font weight", - Value::Int(v) => { - u16::try_from(v).map_or(Self::BLACK, Self::from_number) - }, -} - -dynamic! { - FontStretch: "font stretch", - Value::Relative(v) => Self::from_ratio(v.get() as f32), -} - -dynamic! { - VerticalFontMetric: "vertical font metric", - Value::Length(v) => Self::Linear(v.into()), - Value::Relative(v) => Self::Linear(v.into()), - Value::Linear(v) => Self::Linear(v), -} diff --git a/src/library/page.rs b/src/library/page.rs index 9dcb5ba3..b31a32a3 100644 --- a/src/library/page.rs +++ b/src/library/page.rs @@ -5,8 +5,7 @@ use crate::style::{Paper, PaperClass}; pub fn page(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { castable! { Paper: "string", - Value::Str(string) => Paper::from_name(&string) - .ok_or("invalid paper name")?, + Value::Str(string) => Paper::from_name(&string).ok_or("unknown paper")?, } let paper = args.named::<Paper>("paper")?.or_else(|| args.find()); diff --git a/src/library/text.rs b/src/library/text.rs index 17b33ca5..e55ba483 100644 --- a/src/library/text.rs +++ b/src/library/text.rs @@ -1,10 +1,14 @@ use std::borrow::Cow; +use std::convert::TryInto; use std::ops::Range; use rustybuzz::{Feature, Tag, UnicodeBuffer}; use super::prelude::*; -use crate::font::{Face, FaceId, FontFamily, FontVariant}; +use crate::font::{ + Face, FaceId, FontFamily, FontStretch, FontStyle, FontVariant, FontWeight, + VerticalFontMetric, +}; use crate::geom::{Dir, Em, Length, Point, Size}; use crate::style::{ FontFeatures, NumberPosition, NumberStyle, NumberWidth, Style, TextStyle, @@ -43,10 +47,57 @@ pub fn font(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { } castable! { + FontStyle: "string", + Value::Str(string) => match string.as_str() { + "normal" => Self::Normal, + "italic" => Self::Italic, + "oblique" => Self::Oblique, + _ => Err(r#"expected "normal", "italic" or "oblique""#)?, + }, + } + + castable! { + FontWeight: "integer or string", + Value::Int(v) => v.try_into().map_or(Self::BLACK, Self::from_number), + Value::Str(string) => match string.as_str() { + "thin" => Self::THIN, + "extralight" => Self::EXTRALIGHT, + "light" => Self::LIGHT, + "regular" => Self::REGULAR, + "medium" => Self::MEDIUM, + "semibold" => Self::SEMIBOLD, + "bold" => Self::BOLD, + "extrabold" => Self::EXTRABOLD, + "black" => Self::BLACK, + _ => Err("unknown font weight")?, + }, + } + + castable! { + FontStretch: "relative", + Value::Relative(v) => Self::from_ratio(v.get() as f32), + } + + castable! { + VerticalFontMetric: "linear or string", + Value::Length(v) => Self::Linear(v.into()), + Value::Relative(v) => Self::Linear(v.into()), + Value::Linear(v) => Self::Linear(v), + Value::Str(string) => match string.as_str() { + "ascender" => Self::Ascender, + "cap-height" => Self::CapHeight, + "x-height" => Self::XHeight, + "baseline" => Self::Baseline, + "descender" => Self::Descender, + _ => Err("unknown font metric")?, + }, + } + + castable! { StylisticSet: "none or integer", Value::None => Self(None), Value::Int(v) => match v { - 1..=20 => Self(Some(v as u8)), + 1 ..= 20 => Self(Some(v as u8)), _ => Err("must be between 1 and 20")?, }, } @@ -109,6 +160,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { let sans_serif = args.named("sans-serif")?; let monospace = args.named("monospace")?; let fallback = args.named("fallback")?; + let size = args.named::<Linear>("size")?.or_else(|| args.find()); let style = args.named("style")?; let weight = args.named("weight")?; diff --git a/src/library/utility.rs b/src/library/utility.rs index 05b7639b..69ae235d 100644 --- a/src/library/utility.rs +++ b/src/library/utility.rs @@ -90,7 +90,7 @@ pub fn rgb(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> { if let Some(string) = args.find::<Spanned<Str>>() { match RgbaColor::from_str(&string.v) { Ok(color) => color, - Err(_) => bail!(string.span, "invalid color"), + Err(_) => bail!(string.span, "invalid hex string"), } } else { let r = args.expect("red component")?; |
