diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-01-04 21:29:15 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-01-04 21:29:15 +0100 |
| commit | 2e77b1c836220766398e379ae0157736fb448874 (patch) | |
| tree | 70fc7d28f44193f26d880b5315ce55ed951af73c /src/library | |
| parent | 77c06ebc24ab3a43dc2268763ff8f10963f875b4 (diff) | |
Better value representations, type function 🌐
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/extend.rs | 16 | ||||
| -rw-r--r-- | src/library/insert.rs | 4 | ||||
| -rw-r--r-- | src/library/layout.rs | 20 | ||||
| -rw-r--r-- | src/library/mod.rs | 97 |
4 files changed, 89 insertions, 48 deletions
diff --git a/src/library/extend.rs b/src/library/extend.rs new file mode 100644 index 00000000..e388c9c8 --- /dev/null +++ b/src/library/extend.rs @@ -0,0 +1,16 @@ +use crate::prelude::*; + +/// `type`: Find out the name of a value's type. +/// +/// # Positional arguments +/// - Any value. +/// +/// # Return value +/// The name of the value's type as a string. +pub fn type_(ctx: &mut EvalContext, args: &mut Args) -> Value { + if let Some(value) = args.require::<Value>(ctx, "value") { + value.type_name().into() + } else { + Value::Error + } +} diff --git a/src/library/insert.rs b/src/library/insert.rs index 7d95afe3..8d60927a 100644 --- a/src/library/insert.rs +++ b/src/library/insert.rs @@ -83,8 +83,8 @@ impl Layout for NodeImage { } } -impl From<NodeImage> for Node { +impl From<NodeImage> for NodeAny { fn from(image: NodeImage) -> Self { - Self::any(image) + Self::new(image) } } diff --git a/src/library/layout.rs b/src/library/layout.rs index e469c9be..774e6781 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -1,3 +1,5 @@ +use std::fmt::{self, Display, Formatter}; + use crate::eval::Softness; use crate::layout::{Expansion, NodeFixed, NodeSpacing, NodeStack}; use crate::paper::{Paper, PaperClass}; @@ -153,7 +155,19 @@ impl Switch for Alignment { } impl_type! { - Alignment: "alignment" + Alignment: "alignment", +} + +impl Display for Alignment { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.pad(match self { + Self::Left => "left", + Self::Center => "center", + Self::Right => "right", + Self::Top => "top", + Self::Bottom => "bottom", + }) + } } /// `box`: Layout content into a box. @@ -161,7 +175,7 @@ impl_type! { /// # Named arguments /// - Width of the box: `width`, of type `linear` relative to parent width. /// - Height of the box: `height`, of type `linear` relative to parent height. -pub fn boxed(ctx: &mut EvalContext, args: &mut Args) -> Value { +pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value { let snapshot = ctx.state.clone(); let width = args.get(ctx, "width"); @@ -189,7 +203,7 @@ pub fn boxed(ctx: &mut EvalContext, args: &mut Args) -> Value { ctx.push(NodeFixed { width, height, - child: Node::any(NodeStack { dirs, align, expansion, children }), + child: NodeStack { dirs, align, expansion, children }.into(), }); ctx.state = snapshot; diff --git a/src/library/mod.rs b/src/library/mod.rs index 1cc7b9e9..f9047fc6 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -1,67 +1,78 @@ //! The standard library. +mod extend; mod insert; mod layout; mod style; +pub use extend::*; pub use insert::*; pub use layout::*; pub use style::*; use fontdock::{FontStretch, FontStyle, FontWeight}; -use crate::eval::Scope; +use crate::eval::{Scope, ValueAny, ValueFunc}; use crate::geom::Dir; /// The scope containing the standard library. pub fn _std() -> Scope { let mut std = Scope::new(); + macro_rules! set { + (func: $name:expr, $func:expr) => { + std.set($name, ValueFunc::new($name, $func)) + }; + (any: $var:expr, $any:expr) => { + std.set($var, ValueAny::new($any)) + }; + } // Functions. - std.set("align", align); - std.set("box", boxed); - std.set("font", font); - std.set("h", h); - std.set("image", image); - std.set("page", page); - std.set("pagebreak", pagebreak); - std.set("rgb", rgb); - std.set("v", v); + set!(func: "align", align); + set!(func: "box", box_); + set!(func: "font", font); + set!(func: "h", h); + set!(func: "image", image); + set!(func: "page", page); + set!(func: "pagebreak", pagebreak); + set!(func: "rgb", rgb); + set!(func: "type", type_); + set!(func: "v", v); // Constants. - std.set("left", Alignment::Left); - std.set("center", Alignment::Center); - std.set("right", Alignment::Right); - std.set("top", Alignment::Top); - std.set("bottom", Alignment::Bottom); - std.set("ltr", Dir::LTR); - std.set("rtl", Dir::RTL); - std.set("ttb", Dir::TTB); - std.set("btt", Dir::BTT); - std.set("serif", FontFamily::Serif); - std.set("sans-serif", FontFamily::SansSerif); - std.set("monospace", FontFamily::Monospace); - std.set("normal", FontStyle::Normal); - std.set("italic", FontStyle::Italic); - std.set("oblique", FontStyle::Oblique); - std.set("thin", FontWeight::THIN); - std.set("extralight", FontWeight::EXTRALIGHT); - std.set("light", FontWeight::LIGHT); - std.set("regular", FontWeight::REGULAR); - std.set("medium", FontWeight::MEDIUM); - std.set("semibold", FontWeight::SEMIBOLD); - std.set("bold", FontWeight::BOLD); - std.set("extrabold", FontWeight::EXTRABOLD); - std.set("black", FontWeight::BLACK); - std.set("ultra-condensed", FontStretch::UltraCondensed); - std.set("extra-condensed", FontStretch::ExtraCondensed); - std.set("condensed", FontStretch::Condensed); - std.set("semi-condensed", FontStretch::SemiCondensed); - std.set("normal", FontStretch::Normal); - std.set("semi-expanded", FontStretch::SemiExpanded); - std.set("expanded", FontStretch::Expanded); - std.set("extra-expanded", FontStretch::ExtraExpanded); - std.set("ultra-expanded", FontStretch::UltraExpanded); + set!(any: "left", Alignment::Left); + set!(any: "center", Alignment::Center); + set!(any: "right", Alignment::Right); + set!(any: "top", Alignment::Top); + set!(any: "bottom", Alignment::Bottom); + set!(any: "ltr", Dir::LTR); + set!(any: "rtl", Dir::RTL); + set!(any: "ttb", Dir::TTB); + set!(any: "btt", Dir::BTT); + set!(any: "serif", FontFamily::Serif); + set!(any: "sans-serif", FontFamily::SansSerif); + set!(any: "monospace", FontFamily::Monospace); + set!(any: "normal", FontStyle::Normal); + set!(any: "italic", FontStyle::Italic); + set!(any: "oblique", FontStyle::Oblique); + set!(any: "thin", FontWeight::THIN); + set!(any: "extralight", FontWeight::EXTRALIGHT); + set!(any: "light", FontWeight::LIGHT); + set!(any: "regular", FontWeight::REGULAR); + set!(any: "medium", FontWeight::MEDIUM); + set!(any: "semibold", FontWeight::SEMIBOLD); + 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 } |
