Skip to main content

ratex_parser/functions/
sizing.rs

1use std::collections::HashMap;
2
3use crate::error::ParseResult;
4use crate::functions::{define_function_full, FunctionContext, FunctionSpec};
5use crate::parse_node::ParseNode;
6
7static SIZE_FUNCS: &[&str] = &[
8    "\\tiny", "\\sixptsize", "\\scriptsize", "\\footnotesize", "\\small",
9    "\\normalsize", "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge",
10];
11
12pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
13    define_function_full(
14        map,
15        SIZE_FUNCS,
16        "sizing",
17        0, 0, None,
18        false,
19        true, // allowed_in_text
20        true,
21        false, false,
22        handle_sizing,
23    );
24}
25
26fn handle_sizing(
27    ctx: &mut FunctionContext,
28    _args: Vec<ParseNode>,
29    _opt_args: Vec<Option<ParseNode>>,
30) -> ParseResult<ParseNode> {
31    let break_on = ctx.break_on_token_text.clone();
32    let body = ctx.parser.parse_expression(false, break_on.as_deref())?;
33
34    let size = SIZE_FUNCS
35        .iter()
36        .position(|&f| f == ctx.func_name)
37        .map(|i| i + 1)
38        .unwrap_or(6) as u8;
39
40    Ok(ParseNode::Sizing {
41        mode: ctx.parser.mode,
42        size,
43        body,
44        loc: None,
45    })
46}