Skip to main content

ratex_parser/functions/
spacing.rs

1use std::collections::HashMap;
2
3use crate::error::ParseResult;
4use crate::functions::{define_function_full, FunctionContext, FunctionSpec};
5use crate::parse_node::{Measurement, ParseNode};
6
7pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
8    define_function_full(
9        map,
10        &[
11            "\\quad", "\\qquad", "\\enspace", "\\thinspace", "\\medspace",
12            "\\thickspace", "\\negthinspace", "\\negmedspace", "\\negthickspace",
13            "\\nobreakspace",
14        ],
15        "spacing",
16        0, 0, None,
17        true,  // allowed_in_argument
18        true,  // allowed_in_text
19        true,
20        false, false,
21        handle_spacing,
22    );
23
24    define_function_full(
25        map,
26        &["\\hspace"],
27        "kern",
28        1, 0,
29        Some(vec![crate::functions::ArgType::Size]),
30        false,
31        true,
32        true,
33        false, false,
34        handle_hspace,
35    );
36
37    // \hfill — produces a kern that fills available space
38    define_function_full(
39        map,
40        &["\\hfill"],
41        "spacing",
42        0, 0, None,
43        false, true, true, false, false,
44        handle_spacing,
45    );
46}
47
48fn handle_spacing(
49    ctx: &mut FunctionContext,
50    _args: Vec<ParseNode>,
51    _opt_args: Vec<Option<ParseNode>>,
52) -> ParseResult<ParseNode> {
53    Ok(ParseNode::SpacingNode {
54        mode: ctx.parser.mode,
55        text: ctx.func_name.clone(),
56        loc: None,
57    })
58}
59
60fn handle_hspace(
61    ctx: &mut FunctionContext,
62    args: Vec<ParseNode>,
63    _opt_args: Vec<Option<ParseNode>>,
64) -> ParseResult<ParseNode> {
65    let dimension = if let ParseNode::Size { value, .. } = &args[0] {
66        value.clone()
67    } else {
68        Measurement {
69            number: 0.0,
70            unit: "em".to_string(),
71        }
72    };
73
74    Ok(ParseNode::Kern {
75        mode: ctx.parser.mode,
76        dimension,
77        loc: None,
78    })
79}