mod font;
mod geometry;
mod output;
mod style;
mod tree;
use std::borrow::Cow;
use std::collections::HashMap;
pub use self::font::{
CornerPosition, Extents, GlyphCode, MathConstant, MathFont, MathGlyph, ShapeOptions, Vector,
};
pub use self::geometry::{Edges, Point, Rect, Size};
pub use self::output::{ForeignMetrics, Fragment, NodeLayout, PositionedGlyph};
pub use self::style::{
Direction, Form, MathComputedStyle, MathShift, MathStyle, OperatorOverrides,
parse_math_length, script_scale_factor,
};
pub use self::tree::{
MathElement, MathLayoutHost, MathTree, NodeKind, default_operator_overrides,
};
pub use crate::typesetting::engine::{intrinsic_inline_size, layout_math};
pub fn axis_height<T: MathTree + ?Sized>(tree: &T, node: T::NodeId) -> f32 {
let font = tree.font(node);
let scale = tree.style(node).font_size / font.units_per_em().max(1) as f32;
font.constant(MathConstant::AxisHeight) as f32 * scale
}
#[derive(Debug)]
pub struct LayoutCollector<T: MathTree> {
pub tree: T,
pub layouts: HashMap<T::NodeId, NodeLayout>,
}
impl<T: MathTree> LayoutCollector<T> {
pub fn new(tree: T) -> Self {
LayoutCollector {
tree,
layouts: HashMap::new(),
}
}
pub fn get(&self, node: T::NodeId) -> Option<&NodeLayout> {
self.layouts.get(&node)
}
}
impl<T: MathTree> MathTree for LayoutCollector<T> {
type NodeId = T::NodeId;
type Font = T::Font;
fn kind(&self, node: Self::NodeId) -> NodeKind {
self.tree.kind(node)
}
fn children(&self, node: Self::NodeId) -> impl Iterator<Item = Self::NodeId> {
self.tree.children(node)
}
fn child_count(&self, node: Self::NodeId) -> usize {
self.tree.child_count(node)
}
fn style(&self, node: Self::NodeId) -> MathComputedStyle {
self.tree.style(node)
}
fn font(&self, node: Self::NodeId) -> Self::Font {
self.tree.font(node)
}
fn text(&self, node: Self::NodeId) -> Option<Cow<'_, str>> {
self.tree.text(node)
}
fn attribute(&self, node: Self::NodeId, name: &str) -> Option<Cow<'_, str>> {
self.tree.attribute(node, name)
}
fn operator_overrides(&self, node: Self::NodeId) -> OperatorOverrides {
self.tree.operator_overrides(node)
}
}
impl<T: MathTree> MathLayoutHost for LayoutCollector<T> {
fn set_layout(&mut self, node: Self::NodeId, layout: NodeLayout) {
self.layouts.insert(node, layout);
}
}