use super::Node;
use crate::pack::Spec;
use std::cell::RefCell;
pub type ListFilter = Box<dyn FnMut(&mut Vec<Node>, &Context)>;
pub type Notice = Box<dyn FnMut(&str)>;
pub type FontDefiner = Box<
dyn FnMut(&mut crate::expand::Engine, &mut crate::lexer::Lexer, &str, i64) -> Option<Defined>,
>;
pub enum Defined {
Font(Box<crate::stomach::font::Font>),
Existing(u16),
}
#[derive(Clone, Debug, Default)]
pub struct Context {
pub groupcode: &'static str,
pub size: i64,
pub packtype: &'static str,
pub maxdepth: i64,
}
#[derive(Default)]
pub struct Hooks {
pub buildpage_filter: Option<Notice>,
pub contribute_filter: Option<Notice>,
pub pre_linebreak_filter: Option<ListFilter>,
pub linebreak_filter: Option<ListFilter>,
pub post_linebreak_filter: Option<ListFilter>,
pub hpack_filter: Option<ListFilter>,
pub vpack_filter: Option<ListFilter>,
pub pre_output_filter: Option<ListFilter>,
pub hyphenate: Option<ListFilter>,
pub ligaturing: Option<ListFilter>,
pub kerning: Option<ListFilter>,
pub define_font: Option<FontDefiner>,
}
thread_local! {
static HOOKS: RefCell<Hooks> = RefCell::new(Hooks::default());
}
pub fn set(f: impl FnOnce(&mut Hooks)) {
HOOKS.with(|h| f(&mut h.borrow_mut()));
}
pub fn clear() {
set(|h| *h = Hooks::default());
}
fn packtype(spec: Spec) -> (&'static str, i64) {
match spec {
Spec::Exactly(w) => ("exactly", w),
Spec::Additional(w) => ("additional", w),
}
}
fn filter(which: fn(&mut Hooks) -> &mut Option<ListFilter>, list: &mut Vec<Node>, cx: &Context) {
let Some(mut hook) = HOOKS.with(|h| which(&mut h.borrow_mut()).take()) else {
return;
};
hook(list, cx);
HOOKS.with(|h| *which(&mut h.borrow_mut()) = Some(hook));
}
fn notice(which: fn(&mut Hooks) -> &mut Option<Notice>, info: &str) {
let Some(mut hook) = HOOKS.with(|h| which(&mut h.borrow_mut()).take()) else {
return;
};
hook(info);
HOOKS.with(|h| *which(&mut h.borrow_mut()) = Some(hook));
}
pub fn hpack_filter(list: &mut Vec<Node>, spec: Spec) {
let (packtype, size) = packtype(spec);
let cx = Context {
groupcode: "hbox",
size,
packtype,
maxdepth: 0,
};
filter(|h| &mut h.hpack_filter, list, &cx);
}
pub fn vpack_filter(list: &mut Vec<Node>, spec: Spec, groupcode: &'static str, maxdepth: i64) {
let (packtype, size) = packtype(spec);
let cx = Context {
groupcode,
size,
packtype,
maxdepth,
};
filter(|h| &mut h.vpack_filter, list, &cx);
}
pub fn pre_linebreak_filter(list: &mut Vec<Node>, groupcode: &'static str) {
let cx = Context {
groupcode,
..Context::default()
};
filter(|h| &mut h.pre_linebreak_filter, list, &cx);
}
pub fn post_linebreak_filter(list: &mut Vec<Node>, groupcode: &'static str) {
let cx = Context {
groupcode,
..Context::default()
};
filter(|h| &mut h.post_linebreak_filter, list, &cx);
}
pub fn pre_output_filter(list: &mut Vec<Node>, size: i64, maxdepth: i64) {
let cx = Context {
groupcode: "output",
size,
packtype: "exactly",
maxdepth,
};
filter(|h| &mut h.pre_output_filter, list, &cx);
}
pub fn buildpage_filter(info: &str) {
notice(|h| &mut h.buildpage_filter, info);
}
pub fn contribute_filter(info: &str) {
notice(|h| &mut h.contribute_filter, info);
}
pub fn define_font(
eng: &mut crate::expand::Engine,
lx: &mut crate::lexer::Lexer,
name: &str,
size: i64,
) -> Option<Defined> {
let mut hook = HOOKS.with(|h| h.borrow_mut().define_font.take())?;
let answer = hook(eng, lx, name, size);
HOOKS.with(|h| h.borrow_mut().define_font = Some(hook));
answer
}
pub fn has_linebreak_filter() -> bool {
HOOKS.with(|h| h.borrow().linebreak_filter.is_some())
}
pub fn linebreak_filter(list: &mut Vec<Node>) {
filter(|h| &mut h.linebreak_filter, list, &Context::default());
}
pub fn character_passes(list: &mut Vec<Node>) {
let cx = Context::default();
filter(|h| &mut h.hyphenate, list, &cx);
filter(|h| &mut h.ligaturing, list, &cx);
filter(|h| &mut h.kerning, list, &cx);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::node::BoxNode;
#[test]
fn a_registered_filter_edits_the_list_in_place() {
clear();
let mut list = vec![Node::Penalty(1)];
hpack_filter(&mut list, crate::pack::NATURAL);
assert_eq!(list.len(), 1);
set(|h| {
h.hpack_filter = Some(Box::new(|l: &mut Vec<Node>, cx: &Context| {
assert_eq!(cx.packtype, "additional");
l.push(Node::Box(BoxNode::null()));
}))
});
hpack_filter(&mut list, crate::pack::NATURAL);
hpack_filter(&mut list, crate::pack::NATURAL);
assert_eq!(list.len(), 3);
clear();
}
}