Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,342 @@
|
||||
//! Line search depends on geometry and break classes. The selected indices
|
||||
//! always refer to the current job's nodes, including its paint and effects.
|
||||
use super::*;
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
pub(super) const SEARCH_LIMIT: usize = 1_000_000;
|
||||
const RETAINED_ENTRIES: usize = 128;
|
||||
const RETAINED_NODES: usize = 65_536;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct LineBreakWork {
|
||||
pub solved_passes: u64,
|
||||
pub reused_passes: u64,
|
||||
pub examined_edges: u64,
|
||||
pub reused_edges: u64,
|
||||
}
|
||||
|
||||
impl LineBreakWork {
|
||||
pub fn since(self, before: Self) -> Self {
|
||||
Self {
|
||||
solved_passes: self.solved_passes - before.solved_passes,
|
||||
reused_passes: self.reused_passes - before.reused_passes,
|
||||
examined_edges: self.examined_edges - before.examined_edges,
|
||||
reused_edges: self.reused_edges - before.reused_edges,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct LineBreakCache {
|
||||
state: Mutex<CacheState>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CacheState {
|
||||
entries: VecDeque<Entry>,
|
||||
retained_nodes: usize,
|
||||
work: LineBreakWork,
|
||||
}
|
||||
|
||||
type Path = Option<Vec<(Break, i32)>>;
|
||||
|
||||
struct Entry {
|
||||
hash: u64,
|
||||
nodes: Vec<BreakNode>,
|
||||
parameters: BTreeMap<Parameter, ParameterValue>,
|
||||
shape: Vec<ParagraphShapeLine>,
|
||||
previous_lines: usize,
|
||||
pass: u8,
|
||||
path: Path,
|
||||
edges: usize,
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
fn retained_nodes(&self) -> usize {
|
||||
self.nodes.len() + self.shape.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl LineBreakCache {
|
||||
pub fn work(&self) -> LineBreakWork {
|
||||
self.state
|
||||
.lock()
|
||||
.expect("native line-break cache poisoned")
|
||||
.work
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub(super) struct Widths {
|
||||
natural: i64,
|
||||
stretch: [i64; 4],
|
||||
shrink: i64,
|
||||
}
|
||||
|
||||
impl Widths {
|
||||
fn add(&mut self, other: Self) {
|
||||
self.natural += other.natural;
|
||||
for (left, right) in self.stretch.iter_mut().zip(other.stretch) {
|
||||
*left += right;
|
||||
}
|
||||
self.shrink += other.shrink;
|
||||
}
|
||||
|
||||
fn glue(glue: Glue) -> Self {
|
||||
let mut stretch = [0; 4];
|
||||
stretch[usize::from(glue.stretch.order)] = i64::from(glue.stretch.amount);
|
||||
Self {
|
||||
natural: i64::from(glue.width),
|
||||
stretch,
|
||||
shrink: i64::from(glue.shrink.amount),
|
||||
}
|
||||
}
|
||||
|
||||
fn of_branch(nodes: &[TextNode]) -> Option<Self> {
|
||||
if nodes.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (natural, stretch, shrink) = metrics(nodes, Glue::default(), Glue::default());
|
||||
Some(Self {
|
||||
natural,
|
||||
stretch,
|
||||
shrink,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub(super) enum BreakNode {
|
||||
Content(i64),
|
||||
Glue(Glue),
|
||||
Kern {
|
||||
width: i32,
|
||||
explicit: bool,
|
||||
},
|
||||
MathBoundary {
|
||||
width: i32,
|
||||
after: bool,
|
||||
},
|
||||
Penalty(i32),
|
||||
Discretionary {
|
||||
automatic: bool,
|
||||
pre_break: Option<Widths>,
|
||||
post_break: Option<Widths>,
|
||||
replacement_count: u8,
|
||||
},
|
||||
}
|
||||
|
||||
impl BreakNode {
|
||||
fn project(node: &TextNode) -> Self {
|
||||
match node {
|
||||
TextNode::Language(_)
|
||||
| TextNode::Glyph { .. }
|
||||
| TextNode::Box(_)
|
||||
| TextNode::Rule(_)
|
||||
| TextNode::Mark(_)
|
||||
| TextNode::Adjustment(_)
|
||||
| TextNode::Insertion(_)
|
||||
| TextNode::Output(_)
|
||||
| TextNode::MathChar(_)
|
||||
| TextNode::Indent(_) => Self::Content(width(node)),
|
||||
TextNode::Glue(glue) | TextNode::NamedGlue(glue, _) => Self::Glue(*glue),
|
||||
TextNode::Kern { width, explicit } => Self::Kern {
|
||||
width: *width,
|
||||
explicit: *explicit,
|
||||
},
|
||||
TextNode::MathBoundary { width, after } => Self::MathBoundary {
|
||||
width: *width,
|
||||
after: *after,
|
||||
},
|
||||
TextNode::Penalty(penalty) => Self::Penalty(*penalty),
|
||||
TextNode::Discretionary {
|
||||
automatic,
|
||||
pre_break,
|
||||
post_break,
|
||||
replacement_count,
|
||||
..
|
||||
} => Self::Discretionary {
|
||||
automatic: *automatic,
|
||||
pre_break: Widths::of_branch(pre_break),
|
||||
post_break: Widths::of_branch(post_break),
|
||||
replacement_count: *replacement_count,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn widths(self) -> Widths {
|
||||
match self {
|
||||
Self::Glue(glue) => Widths::glue(glue),
|
||||
Self::Penalty(_) | Self::Discretionary { .. } => Widths::default(),
|
||||
Self::Content(natural) => Widths {
|
||||
natural,
|
||||
..Default::default()
|
||||
},
|
||||
Self::Kern { width, .. } | Self::MathBoundary { width, .. } => Widths {
|
||||
natural: i64::from(width),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn has_post(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Discretionary {
|
||||
post_break: Some(_),
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn discardable(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Glue(_) | Self::Kern { .. } | Self::MathBoundary { .. } | Self::Penalty(_)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn prefix_widths(nodes: &[BreakNode]) -> Vec<Widths> {
|
||||
let mut prefix = Vec::with_capacity(nodes.len() + 1);
|
||||
let mut widths = Widths::default();
|
||||
prefix.push(widths);
|
||||
for node in nodes {
|
||||
widths.add(node.widths());
|
||||
prefix.push(widths);
|
||||
}
|
||||
prefix
|
||||
}
|
||||
|
||||
pub(super) fn candidate_widths(
|
||||
nodes: &[BreakNode],
|
||||
widths: &[Widths],
|
||||
start: usize,
|
||||
end: usize,
|
||||
post: Option<usize>,
|
||||
pre: Option<usize>,
|
||||
left: Glue,
|
||||
right: Glue,
|
||||
) -> (i64, [i64; 4], i64) {
|
||||
let end = widths[end];
|
||||
let start = widths[start];
|
||||
let mut result = Widths {
|
||||
natural: end.natural - start.natural,
|
||||
stretch: std::array::from_fn(|order| end.stretch[order] - start.stretch[order]),
|
||||
shrink: end.shrink - start.shrink,
|
||||
};
|
||||
if let Some(index) = post
|
||||
&& let BreakNode::Discretionary {
|
||||
post_break: Some(post),
|
||||
..
|
||||
} = nodes[index]
|
||||
{
|
||||
result.add(post);
|
||||
}
|
||||
if let Some(index) = pre
|
||||
&& let BreakNode::Discretionary {
|
||||
pre_break: Some(pre),
|
||||
..
|
||||
} = nodes[index]
|
||||
{
|
||||
result.add(pre);
|
||||
}
|
||||
result.add(Widths::glue(left));
|
||||
result.add(Widths::glue(right));
|
||||
(result.natural, result.stretch, result.shrink)
|
||||
}
|
||||
|
||||
pub(super) fn solve(
|
||||
source: &[TextNode],
|
||||
parameters: &BTreeMap<Parameter, ParameterValue>,
|
||||
shape: &[ParagraphShapeLine],
|
||||
pass: u8,
|
||||
work: &mut usize,
|
||||
previous_lines: usize,
|
||||
trace: &mut super::super::paragraph_trace::Tracer<'_>,
|
||||
cache: Option<&LineBreakCache>,
|
||||
) -> Result<Path, String> {
|
||||
let nodes: Vec<_> = source.iter().map(BreakNode::project).collect();
|
||||
let retained = nodes.len() + shape.len();
|
||||
let mut hash = None;
|
||||
if let Some(cache) = cache
|
||||
&& retained <= RETAINED_NODES
|
||||
&& parameters[&Parameter::Tracingparagraphs].width() <= 0
|
||||
{
|
||||
let mut hasher = DefaultHasher::new();
|
||||
(&nodes, parameters, shape, pass, previous_lines).hash(&mut hasher);
|
||||
let key = hasher.finish();
|
||||
hash = Some(key);
|
||||
let mut cache = cache
|
||||
.state
|
||||
.lock()
|
||||
.expect("native line-break cache poisoned");
|
||||
if let Some(index) = cache.entries.iter().rposition(|entry| {
|
||||
entry.hash == key
|
||||
&& entry.pass == pass
|
||||
&& entry.previous_lines == previous_lines
|
||||
&& entry.parameters == *parameters
|
||||
&& entry.shape == shape
|
||||
&& entry.nodes == nodes
|
||||
}) {
|
||||
let entry = cache.entries.remove(index).unwrap();
|
||||
*work += entry.edges;
|
||||
let result = if *work > SEARCH_LIMIT {
|
||||
Err("native paragraph break work limit exceeded".into())
|
||||
} else {
|
||||
Ok(entry.path.clone())
|
||||
};
|
||||
cache.work.reused_passes += 1;
|
||||
cache.work.reused_edges += entry.edges as u64;
|
||||
cache.entries.push_back(entry);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
let before = *work;
|
||||
let result = break_lines(
|
||||
source,
|
||||
&nodes,
|
||||
parameters,
|
||||
shape,
|
||||
pass,
|
||||
work,
|
||||
previous_lines,
|
||||
trace,
|
||||
);
|
||||
let edges = *work - before;
|
||||
if let Some(cache) = cache {
|
||||
let mut cache = cache
|
||||
.state
|
||||
.lock()
|
||||
.expect("native line-break cache poisoned");
|
||||
cache.work.solved_passes += 1;
|
||||
cache.work.examined_edges += edges as u64;
|
||||
if let (Some(hash), Ok(path)) = (hash, &result) {
|
||||
while cache.entries.len() >= RETAINED_ENTRIES
|
||||
|| cache.retained_nodes + retained > RETAINED_NODES
|
||||
{
|
||||
let old = cache
|
||||
.entries
|
||||
.pop_front()
|
||||
.expect("line-break cache has retained entries");
|
||||
cache.retained_nodes -= old.retained_nodes();
|
||||
}
|
||||
cache.retained_nodes += retained;
|
||||
cache.entries.push_back(Entry {
|
||||
hash,
|
||||
nodes,
|
||||
parameters: parameters.clone(),
|
||||
shape: shape.to_vec(),
|
||||
previous_lines,
|
||||
pass,
|
||||
path: path.clone(),
|
||||
edges,
|
||||
});
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
Reference in new issue
Block a user