Measure external font expansion at paragraph break margins
This commit is contained in:
1 parent
91b3e6a8fe
commit
1e4c113b27
6 files changed
+242
-91
No files matched your search
@@ -158,39 +158,60 @@ pub(super) fn margin_kern(
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub(super) struct EdgeMeasure {
|
||||
pub(super) width: i32,
|
||||
expanding: Option<(FontId, u8)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
struct Projected {
|
||||
left: Option<(usize, i32)>,
|
||||
right: Option<(usize, i32)>,
|
||||
pre: Option<i32>,
|
||||
post: Option<i32>,
|
||||
left: Option<(usize, EdgeMeasure)>,
|
||||
right: Option<(usize, EdgeMeasure)>,
|
||||
pre: Option<EdgeMeasure>,
|
||||
post: Option<EdgeMeasure>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub(super) struct Search(Vec<Projected>);
|
||||
pub(super) struct Search {
|
||||
widths: Vec<Projected>,
|
||||
variations: BTreeMap<(FontId, u8), expansion::MarginVariation>,
|
||||
}
|
||||
|
||||
impl Search {
|
||||
pub(super) fn new(state: &State, nodes: &[TextNode]) -> Self {
|
||||
pub(super) fn new(state: &State, nodes: &[TextNode], expand: bool) -> Result<Self, String> {
|
||||
let mut widths = vec![Projected::default(); nodes.len() + 1];
|
||||
let mut variations = BTreeMap::new();
|
||||
let mut project = |edge: Edge, right| -> Result<EdgeMeasure, String> {
|
||||
use std::collections::btree_map::Entry;
|
||||
let width = edge.width(state, right);
|
||||
let mut expanding = None;
|
||||
if expand && let Edge::Glyph(font, slot) = edge {
|
||||
let key = (font, slot);
|
||||
match variations.entry(key) {
|
||||
Entry::Occupied(_) => expanding = Some(key),
|
||||
Entry::Vacant(entry) => {
|
||||
if let Some(variation) = expansion::MarginVariation::project(state, font, slot)? {
|
||||
entry.insert(variation);
|
||||
expanding = Some(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(EdgeMeasure { width, expanding })
|
||||
};
|
||||
for (i, node) in nodes.iter().enumerate() {
|
||||
let edge = Edge::node(node, true);
|
||||
widths[i + 1].right = if matches!(edge, Edge::Skip) {
|
||||
widths[i].right
|
||||
} else {
|
||||
Some((i, edge.width(state, true)))
|
||||
Some((i, project(edge, true)?))
|
||||
};
|
||||
if let TextNode::Discretionary {
|
||||
pre_break,
|
||||
post_break,
|
||||
..
|
||||
} = node
|
||||
{
|
||||
widths[i].pre = pre_break
|
||||
.last()
|
||||
.map(|node| Edge::character(node).width(state, true));
|
||||
widths[i].post = post_break
|
||||
.first()
|
||||
.map(|node| Edge::character(node).width(state, false));
|
||||
if let TextNode::Discretionary { pre_break, post_break, .. } = node {
|
||||
widths[i].pre = pre_break.last()
|
||||
.map(|node| project(Edge::character(node), true)).transpose()?;
|
||||
widths[i].post = post_break.first()
|
||||
.map(|node| project(Edge::character(node), false)).transpose()?;
|
||||
}
|
||||
}
|
||||
for (i, node) in nodes.iter().enumerate().rev() {
|
||||
@@ -198,36 +219,54 @@ impl Search {
|
||||
widths[i].left = if matches!(edge, Edge::Skip) {
|
||||
widths[i + 1].left
|
||||
} else {
|
||||
Some((i, edge.width(state, false)))
|
||||
Some((i, project(edge, false)?))
|
||||
};
|
||||
}
|
||||
Self(widths)
|
||||
Ok(Self { widths, variations })
|
||||
}
|
||||
|
||||
pub(super) fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
self.widths.len() + self.variations.len()
|
||||
}
|
||||
|
||||
pub(super) fn width(
|
||||
pub(super) fn needs_fonts(&self) -> bool {
|
||||
self.variations.values().any(|v| matches!(v, expansion::MarginVariation::Pending { .. }))
|
||||
}
|
||||
|
||||
pub(super) fn edges(
|
||||
&self,
|
||||
start: usize,
|
||||
end: usize,
|
||||
post: Option<usize>,
|
||||
pre: Option<usize>,
|
||||
right_visible: bool,
|
||||
) -> i64 {
|
||||
let left = post.and_then(|i| self.0[i].post).or_else(|| {
|
||||
self.0[start]
|
||||
.left
|
||||
) -> [EdgeMeasure; 2] {
|
||||
let left = post.and_then(|i| self.widths[i].post).or_else(|| {
|
||||
self.widths[start].left
|
||||
.filter(|(i, _)| *i < end)
|
||||
.map(|(_, width)| width)
|
||||
.map(|(_, edge)| edge)
|
||||
});
|
||||
let right = pre.and_then(|i| self.0[i].pre).or_else(|| {
|
||||
self.0[end]
|
||||
.right
|
||||
let right = pre.and_then(|i| self.widths[i].pre).or_else(|| {
|
||||
self.widths[end].right
|
||||
.filter(|(i, _)| right_visible && *i >= start)
|
||||
.map(|(_, width)| width)
|
||||
.map(|(_, edge)| edge)
|
||||
});
|
||||
i64::from(left.unwrap_or(0)) + i64::from(right.unwrap_or(0))
|
||||
[left.unwrap_or_default(), right.unwrap_or_default()]
|
||||
}
|
||||
|
||||
pub(super) fn variation(
|
||||
&mut self,
|
||||
edges: [EdgeMeasure; 2],
|
||||
state: &mut State,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<expansion::Capacity, String> {
|
||||
let mut total = expansion::Capacity::default();
|
||||
for edge in edges {
|
||||
if let Some(key) = edge.expanding {
|
||||
total += self.variations.get_mut(&key).expect("projected margin glyph")
|
||||
.resolve(state, key.0, files)?;
|
||||
}
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user