use crate::document::{
MAX_INLINE_TEXT_BYTES,
content::{ElementText, GeneratedText},
counters::{CounterSnapshot, Counters},
navigation::FragmentIndex,
};
use crate::dom::{DocumentTree, Element, ElementId};
use crate::style::{
CounterProperties, GeneratedContent, GeneratedPseudoElement, NamedStringRequest,
PageStyleTarget, Style, StyleError, TargetRequest, TargetValue,
};
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
rc::Rc,
};
const UNPAGINATED_PAGE: i128 = 1;
const UNPAGINATED_TOTAL: usize = 1;
#[derive(Clone, Default, PartialEq, Eq)]
pub(crate) struct GeneratedValues {
targets: HashMap<TargetRequest, String>,
strings: HashMap<(PageStyleTarget, NamedStringRequest), String>,
paged: Box<PagedValues>,
}
#[derive(Clone, Default, PartialEq, Eq)]
struct PagedValues {
pages: HashMap<PageStyleTarget, i128>,
total: Option<usize>,
starts: HashMap<ElementId, Vec<PageStart>>,
running: HashMap<PageStyleTarget, Vec<PageNumbers>>,
inline_footnotes: InlineFootnotePlacement,
}
#[derive(Clone, Default, PartialEq, Eq)]
pub(crate) struct InlineFootnotePlacement {
pub starts: HashSet<ElementId>,
pub pages: HashMap<ElementId, crate::style::PageRequest>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct PageNumbers {
pub page: Option<i128>,
pub total: Option<usize>,
}
impl PageNumbers {
pub(crate) fn matches(self, page: i128, total: usize) -> bool {
self.page.is_none_or(|value| value == page) && self.total.is_none_or(|value| value == total)
}
}
#[derive(Clone, Copy, Default)]
struct ShownCounters {
page: bool,
total: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PageStart {
pub page: usize,
pub inside: bool,
pub footnote: CounterProperties,
pub automatic: Vec<(std::sync::Arc<str>, i128)>,
}
pub(crate) struct Paginated<'a> {
pub pages: usize,
pub numbers: &'a dyn Fn(usize) -> i128,
pub context_page: &'a dyn Fn(PageStyleTarget) -> Option<usize>,
pub strings:
&'a mut dyn FnMut(PageStyleTarget, &NamedStringRequest) -> Result<String, StyleError>,
pub starts: Vec<(ElementId, PageStart)>,
pub running: Vec<(PageStyleTarget, i128)>,
pub inline_footnotes: InlineFootnotePlacement,
}
#[derive(Default)]
struct TargetElement {
counters: CounterSnapshot,
before: GeneratedText,
after: GeneratedText,
}
#[derive(Clone)]
pub(crate) struct GeneratedBindings {
index: Rc<FragmentIndex>,
values: Rc<GeneratedValues>,
strings: Rc<RefCell<HashSet<(PageStyleTarget, NamedStringRequest)>>>,
pages: Rc<RefCell<HashSet<PageStyleTarget>>>,
requests: Rc<RefCell<HashMap<TargetRequest, ElementId>>>,
running: Rc<RefCell<HashMap<PageStyleTarget, ShownCounters>>>,
copy: Option<PageNumbers>,
degradations: Rc<crate::style::Degradations>,
}
impl GeneratedBindings {
pub(crate) fn bind<'a>(
&self,
content: &'a GeneratedContent,
context: Option<PageStyleTarget>,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
let bound = self.bind_references(content, context)?;
if let Some(context) = context
&& content.has_page_counters()
{
self.pages.borrow_mut().insert(context);
return Ok(Self::bind_page(
bound,
PageNumbers {
page: self.values.paged.pages.get(&context).copied(),
total: self.values.paged.total,
},
));
}
Ok(bound)
}
fn bind_running<'a>(
&self,
content: &'a GeneratedContent,
context: PageStyleTarget,
owner: PageStyleTarget,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
let bound = self.bind_references(content, Some(context))?;
if !content.has_page_counters() {
return Ok(bound);
}
let numbers = self.copy.unwrap_or_else(|| {
let mut running = self.running.borrow_mut();
let shown = running.entry(owner).or_default();
shown.page |= content.shows_page();
shown.total |= content.shows_pages();
PageNumbers::default()
});
Ok(Self::bind_page(bound, numbers))
}
fn bind_page(
content: std::borrow::Cow<'_, GeneratedContent>,
numbers: PageNumbers,
) -> std::borrow::Cow<'_, GeneratedContent> {
if !content.has_page_counters() {
return content;
}
std::borrow::Cow::Owned(content.bind_page_counters(
MAX_INLINE_TEXT_BYTES,
numbers.page.unwrap_or(UNPAGINATED_PAGE),
numbers.total.unwrap_or(UNPAGINATED_TOTAL),
))
}
fn bind_references<'a>(
&self,
content: &'a GeneratedContent,
context: Option<PageStyleTarget>,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
let mut bound = std::borrow::Cow::Borrowed(content);
if content.target_requests().next().is_some() {
bound = std::borrow::Cow::Owned(content.bind_targets(
MAX_INLINE_TEXT_BYTES,
&mut |request| {
let owner = match self.index.target(&request.reference) {
Ok(owner) => owner,
Err(error) => {
self.degradations
.record(crate::style::Degradation::UnresolvedTarget {
reference: request.reference.to_string(),
reason: error.to_string(),
});
return Ok(String::new());
}
};
self.requests.borrow_mut().insert(request.clone(), owner);
Ok(self
.values
.targets
.get(request)
.cloned()
.unwrap_or_default())
},
)?);
}
if let Some(context) = context
&& content.named_string_requests().next().is_some()
{
bound = std::borrow::Cow::Owned(bound.bind_named_strings(
MAX_INLINE_TEXT_BYTES,
&mut |request| {
let key = (context, request.clone());
self.strings.borrow_mut().insert(key.clone());
Ok(self.values.strings.get(&key).cloned().unwrap_or_default())
},
)?);
}
Ok(bound)
}
}
pub(crate) struct GeneratedPass {
bindings: GeneratedBindings,
elements: HashMap<ElementId, TargetElement>,
page_initials: Vec<(usize, std::sync::Arc<str>, i128)>,
}
impl GeneratedPass {
pub(crate) fn new(
index: Rc<FragmentIndex>,
values: GeneratedValues,
degradations: Rc<crate::style::Degradations>,
) -> Self {
Self {
bindings: GeneratedBindings {
index,
values: Rc::new(values),
strings: Rc::default(),
pages: Rc::default(),
requests: Rc::default(),
running: Rc::default(),
copy: None,
degradations,
},
elements: HashMap::new(),
page_initials: Vec::new(),
}
}
pub(crate) fn detached(&self) -> Self {
Self {
bindings: self.bindings.clone(),
elements: HashMap::new(),
page_initials: Vec::new(),
}
}
pub(crate) fn running_copy(&self, numbers: PageNumbers) -> Self {
let mut pass = self.detached();
pass.bindings.copy = Some(numbers);
pass
}
pub(crate) fn formats_copy(&self) -> bool {
self.bindings.copy.is_some()
}
pub(crate) fn running_pages(&self, owner: PageStyleTarget) -> &[PageNumbers] {
self.bindings
.values
.paged
.running
.get(&owner)
.map_or(&[], Vec::as_slice)
}
pub(crate) fn bind_running<'a>(
&self,
content: &'a GeneratedContent,
context: PageStyleTarget,
owner: PageStyleTarget,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
self.bindings.bind_running(content, context, owner)
}
pub(crate) fn bind_copy<'a>(
&self,
content: &'a GeneratedContent,
context: PageStyleTarget,
numbers: PageNumbers,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
Ok(GeneratedBindings::bind_page(
self.bindings.bind_references(content, Some(context))?,
numbers,
))
}
pub(crate) fn page_initials(&mut self, initials: Vec<(usize, std::sync::Arc<str>, i128)>) {
self.page_initials = initials;
}
pub(crate) fn bindings(&self) -> GeneratedBindings {
self.bindings.clone()
}
pub(crate) fn starts_inline_footnotes(&self, element: ElementId) -> bool {
self.bindings
.values
.paged
.inline_footnotes
.starts
.contains(&element)
}
pub(crate) fn inline_footnote_page(
&self,
element: ElementId,
) -> Option<&crate::style::PageRequest> {
self.bindings
.values
.paged
.inline_footnotes
.pages
.get(&element)
}
pub(crate) fn page_starts(&self, element: ElementId) -> &[PageStart] {
self.bindings
.values
.paged
.starts
.get(&element)
.map_or(&[], Vec::as_slice)
}
pub(crate) fn bind<'a>(
&self,
content: &'a GeneratedContent,
context: PageStyleTarget,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
self.bindings.bind(content, Some(context))
}
pub(crate) fn capture(&mut self, element: Element<'_>, counters: &Counters) {
if self.bindings.index.contains(element.id()) {
self.elements.entry(element.id()).or_default().counters = counters.capture();
}
}
pub(crate) fn generated(
&mut self,
element: Element<'_>,
pseudo: GeneratedPseudoElement,
text: &str,
style: &Style,
) {
let Some(target) = self.elements.get_mut(&element.id()) else {
return;
};
let generated = GeneratedText::capture(text, style);
match pseudo {
GeneratedPseudoElement::Before => target.before = generated,
GeneratedPseudoElement::After => target.after = generated,
GeneratedPseudoElement::FootnoteCall
| GeneratedPseudoElement::FootnoteMarker
| GeneratedPseudoElement::Checkmark
| GeneratedPseudoElement::PickerIcon
| GeneratedPseudoElement::ScrollMarkerGroup
| GeneratedPseudoElement::ScrollMarker => {}
}
}
pub(crate) fn finish(self) -> GeneratedReferences {
GeneratedReferences {
bindings: self.bindings,
elements: self.elements,
page_initials: self.page_initials,
}
}
}
pub(crate) struct GeneratedReferences {
bindings: GeneratedBindings,
elements: HashMap<ElementId, TargetElement>,
page_initials: Vec<(usize, std::sync::Arc<str>, i128)>,
}
impl GeneratedReferences {
pub(crate) fn degradations(&self) -> &crate::style::Degradations {
&self.bindings.degradations
}
pub(crate) fn bind<'a>(
&self,
content: &'a GeneratedContent,
context: PageStyleTarget,
) -> Result<std::borrow::Cow<'a, GeneratedContent>, StyleError> {
self.bindings.bind(content, Some(context))
}
pub(crate) fn resolve(
&self,
document: &DocumentTree,
element_page: &mut dyn FnMut(ElementId) -> Option<usize>,
paginated: Paginated<'_>,
) -> Result<GeneratedValues, StyleError> {
let Paginated {
pages,
numbers,
context_page,
strings,
starts,
running,
mut inline_footnotes,
} = paginated;
let mut values = HashMap::new();
for (request, owner) in self.bindings.requests.borrow().iter() {
let text = match (self.elements.get(owner), &request.value) {
(_, TargetValue::Counter(counter)) if counter.is_page_counter() => {
element_page(*owner).map(|page| {
counter
.page_text(numbers(page), pages)
.expect("page counter")
})
}
(Some(target), TargetValue::Counter(counter)) => {
Some(target.counters.text(counter).into_text())
}
(Some(target), TargetValue::Text(source)) => Some(
ElementText::new(
document
.element(*owner)
.expect("target belongs to this document"),
None,
&target.before,
&target.after,
None,
)
.resolve(*source),
),
(None, _) => None,
};
let text = text.unwrap_or_else(|| {
self.bindings
.degradations
.record(crate::style::Degradation::UnresolvedTarget {
reference: request.reference.to_string(),
reason: crate::style::TargetReferenceError::NoBox.to_string(),
});
String::new()
});
values.insert(request.clone(), text);
}
let mut named = HashMap::new();
for (context, request) in self.bindings.strings.borrow().iter() {
named.insert((*context, request.clone()), strings(*context, request)?);
}
let contexts = self.bindings.pages.borrow();
let mut page_numbers = HashMap::new();
for context in contexts.iter() {
let page = context_page(*context).ok_or_else(|| {
StyleError::Invalid("page counter content has no committed page".into())
})?;
page_numbers.insert(*context, numbers(page));
}
let mut page_starts: HashMap<_, Vec<_>> = HashMap::new();
for (element, mut start) in starts {
start.automatic = self
.page_initials
.iter()
.filter(|(page, ..)| *page == start.page)
.map(|(_, name, value)| (name.clone(), *value))
.collect();
page_starts.entry(element).or_default().push(start);
}
let shown = self.bindings.running.borrow();
let mut running_pages: HashMap<_, Vec<_>> = HashMap::new();
for (owner, page) in running {
let Some(shown) = shown.get(&owner) else {
continue;
};
let numbers = PageNumbers {
page: shown.page.then_some(page),
total: shown.total.then_some(pages),
};
let keys = running_pages.entry(owner).or_default();
if !keys.contains(&numbers) {
keys.push(numbers);
}
}
Ok(GeneratedValues {
targets: values,
strings: named,
paged: Box::new(PagedValues {
pages: page_numbers,
total: (!contexts.is_empty()).then_some(pages),
starts: page_starts,
running: running_pages,
inline_footnotes: {
inline_footnotes
.starts
.extend(&self.bindings.values.paged.inline_footnotes.starts);
inline_footnotes
},
}),
})
}
pub(crate) fn matches(&self, values: &GeneratedValues) -> bool {
self.bindings.values.as_ref() == values
}
pub(crate) fn values(&self) -> GeneratedValues {
self.bindings.values.as_ref().clone()
}
}