diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-01-23 15:03:10 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-01-23 15:23:52 +0100 |
| commit | 4653ffebb43d733a3cff873d0903c7d00aaeb499 (patch) | |
| tree | 6a97b2e6a6903b3198547d6f3d0a7e3d2eb023cd /library/src/lib.rs | |
| parent | 84c6c8b0e6b17996a603ec88b7490107154f38f3 (diff) | |
Math module
Diffstat (limited to 'library/src/lib.rs')
| -rw-r--r-- | library/src/lib.rs | 230 |
1 files changed, 116 insertions, 114 deletions
diff --git a/library/src/lib.rs b/library/src/lib.rs index c2f51ee4..c4b6710f 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -11,144 +11,146 @@ pub mod text; pub mod visualize; use typst::geom::{Align, Color, Dir, GenAlign}; -use typst::model::{LangItems, Library, Node, NodeId, Scope, StyleMap}; +use typst::model::{LangItems, Library, Module, Node, NodeId, Scope, StyleMap}; use self::layout::LayoutRoot; /// Construct the standard library. pub fn build() -> Library { - Library { scope: scope(), styles: styles(), items: items() } + let math = math::module(); + let global = global(math.clone()); + Library { global, math, styles: styles(), items: items() } } -/// Construct the standard scope. -fn scope() -> Scope { - let mut std = Scope::new(); +/// Construct the module with global definitions. +fn global(math: Module) -> Module { + let mut global = Scope::deduplicating(); // Basics. - std.def_func::<basics::HeadingNode>("heading"); - std.def_func::<basics::ListNode>("list"); - std.def_func::<basics::EnumNode>("enum"); - std.def_func::<basics::TermsNode>("terms"); - std.def_func::<basics::TableNode>("table"); + global.def_func::<basics::HeadingNode>("heading"); + global.def_func::<basics::ListNode>("list"); + global.def_func::<basics::EnumNode>("enum"); + global.def_func::<basics::TermsNode>("terms"); + global.def_func::<basics::TableNode>("table"); // Text. - std.def_func::<text::TextNode>("text"); - std.def_func::<text::LinebreakNode>("linebreak"); - std.def_func::<text::SymbolNode>("symbol"); - std.def_func::<text::SmartQuoteNode>("smartquote"); - std.def_func::<text::StrongNode>("strong"); - std.def_func::<text::EmphNode>("emph"); - std.def_func::<text::LowerFunc>("lower"); - std.def_func::<text::UpperFunc>("upper"); - std.def_func::<text::SmallcapsFunc>("smallcaps"); - std.def_func::<text::SubNode>("sub"); - std.def_func::<text::SuperNode>("super"); - std.def_func::<text::UnderlineNode>("underline"); - std.def_func::<text::StrikeNode>("strike"); - std.def_func::<text::OverlineNode>("overline"); - std.def_func::<text::RawNode>("raw"); + global.def_func::<text::TextNode>("text"); + global.def_func::<text::LinebreakNode>("linebreak"); + global.def_func::<text::SymbolNode>("symbol"); + global.def_func::<text::SmartQuoteNode>("smartquote"); + global.def_func::<text::StrongNode>("strong"); + global.def_func::<text::EmphNode>("emph"); + global.def_func::<text::LowerFunc>("lower"); + global.def_func::<text::UpperFunc>("upper"); + global.def_func::<text::SmallcapsFunc>("smallcaps"); + global.def_func::<text::SubNode>("sub"); + global.def_func::<text::SuperNode>("super"); + global.def_func::<text::UnderlineNode>("underline"); + global.def_func::<text::StrikeNode>("strike"); + global.def_func::<text::OverlineNode>("overline"); + global.def_func::<text::RawNode>("raw"); // Math. - math::define(&mut std); + global.define("math", math); // Layout. - std.def_func::<layout::PageNode>("page"); - std.def_func::<layout::PagebreakNode>("pagebreak"); - std.def_func::<layout::VNode>("v"); - std.def_func::<layout::ParNode>("par"); - std.def_func::<layout::ParbreakNode>("parbreak"); - std.def_func::<layout::HNode>("h"); - std.def_func::<layout::BoxNode>("box"); - std.def_func::<layout::BlockNode>("block"); - std.def_func::<layout::StackNode>("stack"); - std.def_func::<layout::GridNode>("grid"); - std.def_func::<layout::ColumnsNode>("columns"); - std.def_func::<layout::ColbreakNode>("colbreak"); - std.def_func::<layout::PlaceNode>("place"); - std.def_func::<layout::AlignNode>("align"); - std.def_func::<layout::PadNode>("pad"); - std.def_func::<layout::RepeatNode>("repeat"); - std.def_func::<layout::MoveNode>("move"); - std.def_func::<layout::ScaleNode>("scale"); - std.def_func::<layout::RotateNode>("rotate"); - std.def_func::<layout::HideNode>("hide"); + global.def_func::<layout::PageNode>("page"); + global.def_func::<layout::PagebreakNode>("pagebreak"); + global.def_func::<layout::VNode>("v"); + global.def_func::<layout::ParNode>("par"); + global.def_func::<layout::ParbreakNode>("parbreak"); + global.def_func::<layout::HNode>("h"); + global.def_func::<layout::BoxNode>("box"); + global.def_func::<layout::BlockNode>("block"); + global.def_func::<layout::StackNode>("stack"); + global.def_func::<layout::GridNode>("grid"); + global.def_func::<layout::ColumnsNode>("columns"); + global.def_func::<layout::ColbreakNode>("colbreak"); + global.def_func::<layout::PlaceNode>("place"); + global.def_func::<layout::AlignNode>("align"); + global.def_func::<layout::PadNode>("pad"); + global.def_func::<layout::RepeatNode>("repeat"); + global.def_func::<layout::MoveNode>("move"); + global.def_func::<layout::ScaleNode>("scale"); + global.def_func::<layout::RotateNode>("rotate"); + global.def_func::<layout::HideNode>("hide"); // Visualize. - std.def_func::<visualize::ImageNode>("image"); - std.def_func::<visualize::LineNode>("line"); - std.def_func::<visualize::RectNode>("rect"); - std.def_func::<visualize::SquareNode>("square"); - std.def_func::<visualize::EllipseNode>("ellipse"); - std.def_func::<visualize::CircleNode>("circle"); + global.def_func::<visualize::ImageNode>("image"); + global.def_func::<visualize::LineNode>("line"); + global.def_func::<visualize::RectNode>("rect"); + global.def_func::<visualize::SquareNode>("square"); + global.def_func::<visualize::EllipseNode>("ellipse"); + global.def_func::<visualize::CircleNode>("circle"); // Meta. - std.def_func::<meta::DocumentNode>("document"); - std.def_func::<meta::RefNode>("ref"); - std.def_func::<meta::LinkNode>("link"); - std.def_func::<meta::OutlineNode>("outline"); + global.def_func::<meta::DocumentNode>("document"); + global.def_func::<meta::RefNode>("ref"); + global.def_func::<meta::LinkNode>("link"); + global.def_func::<meta::OutlineNode>("outline"); // Compute. - std.def_func::<compute::TypeFunc>("type"); - std.def_func::<compute::ReprFunc>("repr"); - std.def_func::<compute::AssertFunc>("assert"); - std.def_func::<compute::EvalFunc>("eval"); - std.def_func::<compute::IntFunc>("int"); - std.def_func::<compute::FloatFunc>("float"); - std.def_func::<compute::LumaFunc>("luma"); - std.def_func::<compute::RgbFunc>("rgb"); - std.def_func::<compute::CmykFunc>("cmyk"); - std.def_func::<compute::StrFunc>("str"); - std.def_func::<compute::LabelFunc>("label"); - std.def_func::<compute::RegexFunc>("regex"); - std.def_func::<compute::RangeFunc>("range"); - std.def_func::<compute::AbsFunc>("abs"); - std.def_func::<compute::MinFunc>("min"); - std.def_func::<compute::MaxFunc>("max"); - std.def_func::<compute::EvenFunc>("even"); - std.def_func::<compute::OddFunc>("odd"); - std.def_func::<compute::ModFunc>("mod"); - std.def_func::<compute::ReadFunc>("read"); - std.def_func::<compute::CsvFunc>("csv"); - std.def_func::<compute::JsonFunc>("json"); - std.def_func::<compute::XmlFunc>("xml"); - std.def_func::<compute::LoremFunc>("lorem"); - std.def_func::<compute::NumberingFunc>("numbering"); + global.def_func::<compute::TypeFunc>("type"); + global.def_func::<compute::ReprFunc>("repr"); + global.def_func::<compute::AssertFunc>("assert"); + global.def_func::<compute::EvalFunc>("eval"); + global.def_func::<compute::IntFunc>("int"); + global.def_func::<compute::FloatFunc>("float"); + global.def_func::<compute::LumaFunc>("luma"); + global.def_func::<compute::RgbFunc>("rgb"); + global.def_func::<compute::CmykFunc>("cmyk"); + global.def_func::<compute::StrFunc>("str"); + global.def_func::<compute::LabelFunc>("label"); + global.def_func::<compute::RegexFunc>("regex"); + global.def_func::<compute::RangeFunc>("range"); + global.def_func::<compute::AbsFunc>("abs"); + global.def_func::<compute::MinFunc>("min"); + global.def_func::<compute::MaxFunc>("max"); + global.def_func::<compute::EvenFunc>("even"); + global.def_func::<compute::OddFunc>("odd"); + global.def_func::<compute::ModFunc>("mod"); + global.def_func::<compute::ReadFunc>("read"); + global.def_func::<compute::CsvFunc>("csv"); + global.def_func::<compute::JsonFunc>("json"); + global.def_func::<compute::XmlFunc>("xml"); + global.def_func::<compute::LoremFunc>("lorem"); + global.def_func::<compute::NumberingFunc>("numbering"); // Colors. - std.define("black", Color::BLACK); - std.define("gray", Color::GRAY); - std.define("silver", Color::SILVER); - std.define("white", Color::WHITE); - std.define("navy", Color::NAVY); - std.define("blue", Color::BLUE); - std.define("aqua", Color::AQUA); - std.define("teal", Color::TEAL); - std.define("eastern", Color::EASTERN); - std.define("purple", Color::PURPLE); - std.define("fuchsia", Color::FUCHSIA); - std.define("maroon", Color::MAROON); - std.define("red", Color::RED); - std.define("orange", Color::ORANGE); - std.define("yellow", Color::YELLOW); - std.define("olive", Color::OLIVE); - std.define("green", Color::GREEN); - std.define("lime", Color::LIME); + global.define("black", Color::BLACK); + global.define("gray", Color::GRAY); + global.define("silver", Color::SILVER); + global.define("white", Color::WHITE); + global.define("navy", Color::NAVY); + global.define("blue", Color::BLUE); + global.define("aqua", Color::AQUA); + global.define("teal", Color::TEAL); + global.define("eastern", Color::EASTERN); + global.define("purple", Color::PURPLE); + global.define("fuchsia", Color::FUCHSIA); + global.define("maroon", Color::MAROON); + global.define("red", Color::RED); + global.define("orange", Color::ORANGE); + global.define("yellow", Color::YELLOW); + global.define("olive", Color::OLIVE); + global.define("green", Color::GREEN); + global.define("lime", Color::LIME); // Other constants. - std.define("ltr", Dir::LTR); - std.define("rtl", Dir::RTL); - std.define("ttb", Dir::TTB); - std.define("btt", Dir::BTT); - std.define("start", GenAlign::Start); - std.define("end", GenAlign::End); - std.define("left", GenAlign::Specific(Align::Left)); - std.define("center", GenAlign::Specific(Align::Center)); - std.define("right", GenAlign::Specific(Align::Right)); - std.define("top", GenAlign::Specific(Align::Top)); - std.define("horizon", GenAlign::Specific(Align::Horizon)); - std.define("bottom", GenAlign::Specific(Align::Bottom)); - - std + global.define("ltr", Dir::LTR); + global.define("rtl", Dir::RTL); + global.define("ttb", Dir::TTB); + global.define("btt", Dir::BTT); + global.define("start", GenAlign::Start); + global.define("end", GenAlign::End); + global.define("left", GenAlign::Specific(Align::Left)); + global.define("center", GenAlign::Specific(Align::Center)); + global.define("right", GenAlign::Specific(Align::Right)); + global.define("top", GenAlign::Specific(Align::Top)); + global.define("horizon", GenAlign::Specific(Align::Horizon)); + global.define("bottom", GenAlign::Specific(Align::Bottom)); + + Module::new("global").with_scope(global) } /// Construct the standard style map. @@ -187,7 +189,7 @@ fn items() -> LangItems { term_item: |term, description| { layout::ListItem::Term(basics::TermItem { term, description }).pack() }, - math: |body, block| math::MathNode { body, block }.pack(), + math_formula: |body, block| math::FormulaNode { body, block }.pack(), math_atom: |atom| math::AtomNode(atom).pack(), math_delimited: |body| math::LrNode(body).pack(), math_script: |base, sub, sup| math::ScriptNode { base, sub, sup }.pack(), |
