use crate::build_common::{VListChild, VListElem, VListParam, make_v_list};
use crate::define_function::{FunctionDefSpec, FunctionPropSpec};
use crate::dom_tree::HtmlDomNode;
use crate::mathml_tree::{MathDomNode, MathNode, MathNodeType};
use crate::options::Options;
use crate::parser::parse_node::{NodeType, ParseNode, ParseNodeVcenter};
use crate::types::{ArgType, ParseError, ParseErrorKind};
use crate::{KatexContext, build_html, build_mathml};
pub fn define_vcenter(ctx: &mut KatexContext) {
ctx.define_function(FunctionDefSpec {
node_type: Some(NodeType::Vcenter),
names: &["\\vcenter"],
props: FunctionPropSpec {
num_args: 1,
arg_types: Some(vec![ArgType::Original]),
allowed_in_text: false,
..Default::default()
},
handler: Some(|context, args, _opt_args| {
let body = args[0].clone();
Ok(ParseNode::Vcenter(ParseNodeVcenter {
mode: context.parser.mode,
loc: context.loc(),
body: Box::new(body),
}))
}),
html_builder: Some(html_builder),
mathml_builder: Some(mathml_builder),
});
}
fn html_builder(
node: &ParseNode,
options: &Options,
ctx: &KatexContext,
) -> Result<HtmlDomNode, ParseError> {
let ParseNode::Vcenter(vcenter_node) = node else {
return Err(ParseError::new(ParseErrorKind::ExpectedNode {
node: NodeType::Vcenter,
}));
};
let body = build_html::build_group(ctx, &vcenter_node.body, options, None)?;
let axis_height = options.font_metrics().axis_height;
let dy = 0.5 * ((body.height() - axis_height) - (body.depth() + axis_height));
let children = vec![VListChild::Elem(
VListElem::builder().elem(body).build().into(),
)];
let vlist = make_v_list(
VListParam::Shift {
children,
position_data: dy,
},
options,
)?;
Ok(vlist.into())
}
fn mathml_builder(
node: &ParseNode,
options: &Options,
ctx: &KatexContext,
) -> Result<MathDomNode, ParseError> {
let ParseNode::Vcenter(vcenter_node) = node else {
return Err(ParseError::new(ParseErrorKind::ExpectedNode {
node: NodeType::Vcenter,
}));
};
let base_group = build_mathml::build_group(ctx, &vcenter_node.body, options)?;
let mut mpadded = MathNode::builder()
.node_type(MathNodeType::Mpadded)
.children(vec![base_group])
.build();
mpadded
.attributes
.insert("class".to_owned(), "vcenter".to_owned());
Ok(MathDomNode::Math(mpadded))
}