summaryrefslogtreecommitdiff
path: root/src/library/mod.rs
blob: 44f8f947feb3d01b74b8816e03dca3b66699a8ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! The standard library.
//!
//! Call [`new`] to obtain a [`Scope`] containing all standard library
//! definitions.

pub mod align;
pub mod columns;
pub mod container;
pub mod deco;
pub mod flow;
pub mod grid;
pub mod heading;
pub mod hidden;
pub mod image;
pub mod link;
pub mod list;
pub mod pad;
pub mod page;
pub mod par;
pub mod place;
pub mod shape;
pub mod spacing;
pub mod stack;
pub mod table;
pub mod text;
pub mod transform;
pub mod utility;

pub use self::image::*;
pub use align::*;
pub use columns::*;
pub use container::*;
pub use deco::*;
pub use flow::*;
pub use grid::*;
pub use heading::*;
pub use hidden::*;
pub use link::*;
pub use list::*;
pub use pad::*;
pub use page::*;
pub use par::*;
pub use place::*;
pub use shape::*;
pub use spacing::*;
pub use stack::*;
pub use table::*;
pub use text::*;
pub use transform::*;
pub use utility::*;

macro_rules! prelude {
    ($($reexport:item)*) => {
        /// Helpful imports for creating library functionality.
        pub mod prelude {
            $(#[doc(no_inline)] $reexport)*
        }
    };
}

prelude! {
    pub use std::fmt::{self, Debug, Formatter};
    pub use std::num::NonZeroUsize;
    pub use std::rc::Rc;
    pub use std::hash::Hash;

    pub use typst_macros::class;

    pub use crate::diag::{At, TypResult};
    pub use crate::eval::{
        Args, Construct, EvalContext, Node, Property, Scope, Set, Smart, StyleChain,
        StyleMap, Styled, Value,
    };
    pub use crate::frame::*;
    pub use crate::geom::*;
    pub use crate::layout::{
        Constrain, Constrained, Constraints, Layout, LayoutContext, PackedNode, Regions,
    };
    pub use crate::syntax::{Span, Spanned};
    pub use crate::util::{EcoString, OptionExt};
}

use prelude::*;

/// Construct a scope containing all standard library definitions.
pub fn new() -> Scope {
    let mut std = Scope::new();

    // Structure and semantics.
    std.def_class::<PageNode>("page");
    std.def_class::<PagebreakNode>("pagebreak");
    std.def_class::<ParNode>("par");
    std.def_class::<ParbreakNode>("parbreak");
    std.def_class::<LinebreakNode>("linebreak");
    std.def_class::<TextNode>("text");
    std.def_class::<DecoNode<Underline>>("underline");
    std.def_class::<DecoNode<Strikethrough>>("strike");
    std.def_class::<DecoNode<Overline>>("overline");
    std.def_class::<LinkNode>("link");
    std.def_class::<HeadingNode>("heading");
    std.def_class::<ListNode<Unordered>>("list");
    std.def_class::<ListNode<Ordered>>("enum");
    std.def_class::<TableNode>("table");
    std.def_class::<ImageNode>("image");
    std.def_class::<ShapeNode<Rect>>("rect");
    std.def_class::<ShapeNode<Square>>("square");
    std.def_class::<ShapeNode<Ellipse>>("ellipse");
    std.def_class::<ShapeNode<Circle>>("circle");

    // Layout.
    std.def_class::<HNode>("h");
    std.def_class::<VNode>("v");
    std.def_class::<BoxNode>("box");
    std.def_class::<BlockNode>("block");
    std.def_class::<AlignNode>("align");
    std.def_class::<PadNode>("pad");
    std.def_class::<PlaceNode>("place");
    std.def_class::<TransformNode<Move>>("move");
    std.def_class::<TransformNode<Scale>>("scale");
    std.def_class::<TransformNode<Rotate>>("rotate");
    std.def_class::<HideNode>("hide");
    std.def_class::<StackNode>("stack");
    std.def_class::<GridNode>("grid");
    std.def_class::<ColumnsNode>("columns");
    std.def_class::<ColbreakNode>("colbreak");

    // Utility functions.
    std.def_func("assert", assert);
    std.def_func("type", type_);
    std.def_func("repr", repr);
    std.def_func("join", join);
    std.def_func("int", int);
    std.def_func("float", float);
    std.def_func("str", str);
    std.def_func("abs", abs);
    std.def_func("min", min);
    std.def_func("max", max);
    std.def_func("even", even);
    std.def_func("odd", odd);
    std.def_func("range", range);
    std.def_func("rgb", rgb);
    std.def_func("lower", lower);
    std.def_func("upper", upper);
    std.def_func("len", len);
    std.def_func("sorted", sorted);

    // Predefined colors.
    // TODO: More colors.
    std.def_const("white", RgbaColor::WHITE);
    std.def_const("black", RgbaColor::BLACK);
    std.def_const("eastern", RgbaColor::new(0x23, 0x9D, 0xAD, 0xFF));
    std.def_const("conifer", RgbaColor::new(0x9f, 0xEB, 0x52, 0xFF));
    std.def_const("forest", RgbaColor::new(0x43, 0xA1, 0x27, 0xFF));

    // Other constants.
    std.def_const("ltr", Dir::LTR);
    std.def_const("rtl", Dir::RTL);
    std.def_const("ttb", Dir::TTB);
    std.def_const("btt", Dir::BTT);
    std.def_const("left", Align::Left);
    std.def_const("center", Align::Center);
    std.def_const("right", Align::Right);
    std.def_const("top", Align::Top);
    std.def_const("horizon", Align::Horizon);
    std.def_const("bottom", Align::Bottom);
    std.def_const("serif", FontFamily::Serif);
    std.def_const("sans-serif", FontFamily::SansSerif);
    std.def_const("monospace", FontFamily::Monospace);

    std
}

dynamic! {
    Dir: "direction",
}

castable! {
    usize,
    Expected: "non-negative integer",
    Value::Int(int) => int.try_into().map_err(|_| "must be at least zero")?,
}

castable! {
    NonZeroUsize,
    Expected: "positive integer",
    Value::Int(int) => int
        .try_into()
        .and_then(usize::try_into)
        .map_err(|_| "must be positive")?,
}

castable! {
    Paint,
    Expected: "color",
    Value::Color(color) => Paint::Solid(color),
}

castable! {
    String,
    Expected: "string",
    Value::Str(string) => string.into(),
}

castable! {
    PackedNode,
    Expected: "node",
    Value::Node(node) => node.into_block(),
}