use super::{BlockSource, Measurer};
use crate::layout::LayoutError;
use crate::layout::measure::Failure;
use crate::text::IntrinsicWidths;
use std::collections::HashMap;
const MAX_KEPT_WIDTHS: usize = 1 << 16;
#[derive(Default)]
pub(in crate::layout) struct KeptWidths {
paragraphs: HashMap<(usize, Vec<u32>), IntrinsicWidths>,
boxes: HashMap<(usize, usize), IntrinsicWidths>,
contextual: bool,
}
impl KeptWidths {
fn make_room(&mut self) {
if self.paragraphs.len() + self.boxes.len() >= MAX_KEPT_WIDTHS {
self.paragraphs.clear();
self.boxes.clear();
}
}
}
fn box_key(block: BlockSource<'_>) -> (usize, usize) {
(
std::ptr::from_ref(block.block) as usize,
std::ptr::from_ref(block.style()) as usize,
)
}
impl Measurer<'_> {
pub(crate) fn intrinsic_widths(
&self,
paragraph: &crate::document::Paragraph,
atomics: &[IntrinsicWidths],
first_line_atomics: &[IntrinsicWidths],
) -> Result<IntrinsicWidths, LayoutError> {
let key = (
std::ptr::from_ref(paragraph) as usize,
atomics
.iter()
.chain(first_line_atomics)
.flat_map(|widths| [widths.min.to_bits(), widths.max.to_bits()])
.collect(),
);
if let Some(widths) = self.kept_widths.borrow().paragraphs.get(&key) {
self.work.spend(1)?;
return Ok(*widths);
}
self.work.spend(paragraph.text().len().max(1))?;
let widths = self
.text
.intrinsic_widths(paragraph, atomics, first_line_atomics)?;
let mut kept = self.kept_widths.borrow_mut();
kept.make_room();
kept.paragraphs.insert(key, widths);
Ok(widths)
}
#[inline(never)]
pub(in crate::layout) fn kept_content_widths(
&self,
block: BlockSource<'_>,
) -> Option<IntrinsicWidths> {
self.kept_widths
.borrow()
.boxes
.get(&box_key(block))
.copied()
}
#[inline(never)]
pub(in crate::layout) fn start_content_widths(&self) -> bool {
crate::work::count(crate::work::Work::ContentSizes, 1);
std::mem::take(&mut self.kept_widths.borrow_mut().contextual)
}
#[inline(never)]
pub(in crate::layout) fn keep_content_widths(
&self,
block: BlockSource<'_>,
outer: bool,
widths: &Result<IntrinsicWidths, Failure>,
) {
let mut kept = self.kept_widths.borrow_mut();
if kept.contextual {
return;
}
kept.contextual = outer;
if let Ok(widths) = widths {
kept.make_room();
kept.boxes.insert(box_key(block), *widths);
}
}
pub(in crate::layout) fn contextual_widths(&self) {
self.kept_widths.borrow_mut().contextual = true;
}
}