mod ua_style;
use std::borrow::Cow;
use crate::host::{
LayoutCollector, MathComputedStyle, MathElement, MathFont, MathStyle, MathTree, NodeKind,
layout_math,
};
pub type NodeId = usize;
#[derive(Debug, Clone, PartialEq)]
pub struct DocNode {
pub name: String,
pub element: MathElement,
pub attributes: Vec<(String, String)>,
pub text: Option<String>,
pub children: Vec<NodeId>,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MathDocument {
nodes: Vec<DocNode>,
root: Option<NodeId>,
}
impl MathDocument {
pub fn new() -> Self {
Self::default()
}
pub fn add_element(
&mut self,
name: &str,
attributes: Vec<(String, String)>,
children: Vec<NodeId>,
) -> NodeId {
self.push(DocNode {
name: name.to_owned(),
element: MathElement::from_local_name(name),
attributes,
text: None,
children,
})
}
pub fn add_token(
&mut self,
name: &str,
attributes: Vec<(String, String)>,
text: &str,
) -> NodeId {
self.push(DocNode {
name: name.to_owned(),
element: MathElement::from_local_name(name),
attributes,
text: Some(text.to_owned()),
children: Vec::new(),
})
}
fn push(&mut self, node: DocNode) -> NodeId {
self.nodes.push(node);
let id = self.nodes.len() - 1;
self.root = Some(id);
id
}
pub fn set_root(&mut self, root: NodeId) {
self.root = Some(root);
}
pub fn root(&self) -> Option<NodeId> {
self.root
}
pub fn node(&self, id: NodeId) -> &DocNode {
&self.nodes[id]
}
pub fn len(&self) -> usize {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
pub fn attribute(&self, id: NodeId, name: &str) -> Option<&str> {
self.nodes[id]
.attributes
.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value.as_str())
}
pub fn parents(&self) -> Vec<Option<NodeId>> {
let mut parents = vec![None; self.nodes.len()];
for (id, node) in self.nodes.iter().enumerate() {
for &child in &node.children {
parents[child] = Some(id);
}
}
parents
}
pub fn style<F: MathFont + Clone>(
&self,
font: F,
options: StyleOptions,
) -> StyledDocument<'_, F> {
let (styles, texts) = ua_style::resolve(self, &font, options);
StyledDocument {
document: self,
font,
styles,
texts,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StyleOptions {
pub font_size: f32,
pub math_style: MathStyle,
}
impl Default for StyleOptions {
fn default() -> Self {
StyleOptions {
font_size: 16.0,
math_style: MathStyle::Normal,
}
}
}
pub struct StyledDocument<'d, F> {
document: &'d MathDocument,
font: F,
styles: Vec<MathComputedStyle>,
texts: Vec<Option<String>>,
}
impl<'d, F> StyledDocument<'d, F> {
pub fn document(&self) -> &'d MathDocument {
self.document
}
}
impl<'d, F: MathFont + Clone> MathTree for StyledDocument<'d, F> {
type NodeId = NodeId;
type Font = F;
fn kind(&self, node: NodeId) -> NodeKind {
NodeKind::Math(self.document.nodes[node].element)
}
fn children(&self, node: NodeId) -> impl Iterator<Item = NodeId> {
self.document.nodes[node].children.iter().copied()
}
fn child_count(&self, node: NodeId) -> usize {
self.document.nodes[node].children.len()
}
fn style(&self, node: NodeId) -> MathComputedStyle {
self.styles[node]
}
fn font(&self, _node: NodeId) -> F {
self.font.clone()
}
fn text(&self, node: NodeId) -> Option<Cow<'_, str>> {
self.texts[node].as_deref().map(Cow::Borrowed)
}
fn attribute(&self, node: NodeId, name: &str) -> Option<Cow<'_, str>> {
if let Some(value) = self.document.attribute(node, name) {
return Some(Cow::Borrowed(value));
}
if name == "columnalign" && self.document.node(node).element == MathElement::Mtd {
let inline = self.document.attribute(node, "style")?;
let align = ua_style::inline_style_property(inline, "text-align")?;
return match align {
"left" | "start" => Some(Cow::Borrowed("left")),
"right" | "end" => Some(Cow::Borrowed("right")),
"center" => Some(Cow::Borrowed("center")),
_ => None,
};
}
None
}
}
pub fn layout<F: MathFont + Clone>(
document: &MathDocument,
font: F,
options: StyleOptions,
) -> LayoutCollector<StyledDocument<'_, F>> {
let root = document.root().expect("the document has a root");
let mut collector = LayoutCollector::new(document.style(font, options));
layout_math(&mut collector, root);
collector
}