//! Typst's standard library. pub mod basics; pub mod compute; pub mod layout; pub mod math; pub mod meta; pub mod prelude; pub mod shared; pub mod text; pub mod visualize; use typst::geom::{Align, Color, Dir, GenAlign}; use typst::model::{LangItems, Library, Module, Node, NodeId, Scope, StyleMap}; use self::layout::LayoutRoot; /// Construct the standard library. pub fn build() -> Library { let sym = text::sym(); let math = math::module(&sym); let calc = compute::calc(); let global = global(sym, math.clone(), calc); Library { global, math, styles: styles(), items: items() } } /// Construct the module with global definitions. fn global(sym: Module, math: Module, calc: Module) -> Module { let mut global = Scope::deduplicating(); // Basics. global.def_func::("heading"); global.def_func::("list"); global.def_func::("enum"); global.def_func::("terms"); global.def_func::("table"); // Text. global.def_func::("text"); global.def_func::("linebreak"); global.def_func::("smartquote"); global.def_func::("strong"); global.def_func::("emph"); global.def_func::("lower"); global.def_func::("upper"); global.def_func::("smallcaps"); global.def_func::("sub"); global.def_func::("super"); global.def_func::("underline"); global.def_func::("strike"); global.def_func::("overline"); global.def_func::("raw"); global.define("sym", sym); global.define("emoji", text::emoji()); // Math. global.define("math", math); // Layout. global.def_func::("page"); global.def_func::("pagebreak"); global.def_func::("v"); global.def_func::("par"); global.def_func::("parbreak"); global.def_func::("h"); global.def_func::("box"); global.def_func::("block"); global.def_func::("stack"); global.def_func::("grid"); global.def_func::("columns"); global.def_func::("colbreak"); global.def_func::("place"); global.def_func::("align"); global.def_func::("pad"); global.def_func::("repeat"); global.def_func::("move"); global.def_func::("scale"); global.def_func::("rotate"); global.def_func::("hide"); // Visualize. global.def_func::("image"); global.def_func::("line"); global.def_func::("rect"); global.def_func::("square"); global.def_func::("ellipse"); global.def_func::("circle"); // Meta. global.def_func::("document"); global.def_func::("ref"); global.def_func::("link"); global.def_func::("outline"); // Compute. global.def_func::("type"); global.def_func::("repr"); global.def_func::("assert"); global.def_func::("eval"); global.def_func::("int"); global.def_func::("float"); global.def_func::("luma"); global.def_func::("rgb"); global.def_func::("cmyk"); global.def_func::("symbol"); global.def_func::("str"); global.def_func::("label"); global.def_func::("regex"); global.def_func::("range"); global.def_func::("read"); global.def_func::("csv"); global.def_func::("json"); global.def_func::("xml"); global.def_func::("lorem"); global.def_func::("numbering"); // Calc. global.define("calc", calc); // Colors. 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. 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. fn styles() -> StyleMap { StyleMap::new() } /// Construct the standard lang item mapping. fn items() -> LangItems { LangItems { layout: |world, content, styles| content.layout_root(world, styles), em: |styles| styles.get(text::TextNode::SIZE), dir: |styles| styles.get(text::TextNode::DIR), space: || text::SpaceNode.pack(), linebreak: || text::LinebreakNode { justify: false }.pack(), text: |text| text::TextNode(text).pack(), text_id: NodeId::of::(), text_str: |content| Some(&content.to::()?.0), smart_quote: |double| text::SmartQuoteNode { double }.pack(), parbreak: || layout::ParbreakNode.pack(), strong: |body| text::StrongNode(body).pack(), emph: |body| text::EmphNode(body).pack(), raw: |text, lang, block| { let content = text::RawNode { text, block }.pack(); match lang { Some(_) => content.styled(text::RawNode::LANG, lang), None => content, } }, link: |url| meta::LinkNode::from_url(url).pack(), ref_: |target| meta::RefNode(target).pack(), heading: |level, body| basics::HeadingNode { level, title: body }.pack(), list_item: |body| layout::ListItem::List(body).pack(), enum_item: |number, body| layout::ListItem::Enum(number, body).pack(), term_item: |term, description| { layout::ListItem::Term(basics::TermItem { term, description }).pack() }, formula: |body, block| math::FormulaNode { body, block }.pack(), math_align_point: || math::AlignPointNode.pack(), math_delimited: |open, body, close| { math::LrNode { body: open + body + close, size: None }.pack() }, math_attach: |base, bottom, top| math::AttachNode { base, bottom, top }.pack(), math_accent: |base, accent| math::AccentNode { base, accent }.pack(), math_frac: |num, denom| math::FracNode { num, denom }.pack(), } }