Implement XeTeX contextual interword spacing and shipout shaping
This commit is contained in:
1 parent
7e5428ec35
commit
4a77540e46
23 files changed
+491
-42
No files matched your search
@@ -0,0 +1,227 @@
|
||||
//! Shape full native word sequences after horizontal glue has been set.
|
||||
|
||||
use super::super::paragraph::interword::invisible;
|
||||
use super::*;
|
||||
use crate::{opentype::OpenTypeRun, provenance::PaintSource};
|
||||
use std::{ops::Range, sync::Arc};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub(in crate::execution) struct Run {
|
||||
pub(super) nodes: Range<usize>,
|
||||
pub(super) word: Arc<OpenTypeRun>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub(super) effects: Vec<usize>,
|
||||
}
|
||||
|
||||
impl NativeBox {
|
||||
pub(in crate::execution) fn prepare_interword_shipout(
|
||||
&mut self,
|
||||
limit: usize,
|
||||
cancellation: Option<&Cancellation>,
|
||||
) -> Result<usize, String> {
|
||||
let mut remaining = limit;
|
||||
prepare(self, &mut remaining, cancellation)?;
|
||||
Ok(limit - remaining)
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare(
|
||||
packed: &mut NativeBox,
|
||||
remaining: &mut usize,
|
||||
cancellation: Option<&Cancellation>,
|
||||
) -> Result<(), String> {
|
||||
if cancellation.is_some_and(Cancellation::is_cancelled) {
|
||||
return Err("native compilation cancelled".into());
|
||||
}
|
||||
if packed.kind.is_unset() {
|
||||
return Ok(());
|
||||
}
|
||||
match &mut packed.content {
|
||||
BoxContent::Horizontal(nodes) => {
|
||||
packed.interword_runs = shape_row(nodes, packed.setting, remaining, cancellation)?;
|
||||
for node in nodes {
|
||||
let child = match node {
|
||||
TextNode::Box(item)
|
||||
| TextNode::MathChar(item)
|
||||
| TextNode::Glyph { glyph: item, .. } => item.native_box.as_deref_mut(),
|
||||
TextNode::Leaders(LeaderGlue {
|
||||
pattern: LeaderPattern::Box(item),
|
||||
..
|
||||
}) => Some(item.as_mut()),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(child) = child {
|
||||
prepare(child, remaining, cancellation)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
BoxContent::Vertical(nodes) => {
|
||||
for node in nodes {
|
||||
let child = match node {
|
||||
NativeVerticalNode::Box(item)
|
||||
| NativeVerticalNode::Line(item)
|
||||
| NativeVerticalNode::Leaders(LeaderGlue {
|
||||
pattern: LeaderPattern::Box(item),
|
||||
..
|
||||
}) => item,
|
||||
_ => continue,
|
||||
};
|
||||
prepare(child, remaining, cancellation)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn shape_row(
|
||||
nodes: &[TextNode],
|
||||
setting: GlueSetting,
|
||||
remaining: &mut usize,
|
||||
cancellation: Option<&Cancellation>,
|
||||
) -> Result<Vec<Run>, String> {
|
||||
let next = |start: usize| {
|
||||
let offset = nodes[start..].iter().position(|node| !invisible(node));
|
||||
offset.map_or(nodes.len(), |offset| start + offset)
|
||||
};
|
||||
let mut runs = Vec::new();
|
||||
let mut moved_suffix_effects = false;
|
||||
let mut index = 0;
|
||||
while let Some(node) = nodes.get(index) {
|
||||
let start = index;
|
||||
index += 1;
|
||||
let TextNode::OpenType(first) = node else {
|
||||
continue;
|
||||
};
|
||||
let mut text = String::new();
|
||||
let mut sources = Vec::new();
|
||||
let mut units = 0;
|
||||
loop {
|
||||
let mut word_index = next(index);
|
||||
let space = match nodes.get(word_index) {
|
||||
Some(TextNode::FontSpace(_, font, origin)) => {
|
||||
Some((Some(*origin), *font == first.font.id))
|
||||
}
|
||||
Some(TextNode::Glue(_)) => Some((None, false)),
|
||||
_ => None,
|
||||
};
|
||||
if let Some((_, canonical)) = space {
|
||||
let adjustment = matches!(
|
||||
nodes.get(word_index + 1),
|
||||
Some(TextNode::SpaceAdjustment(_))
|
||||
);
|
||||
if !canonical && !adjustment {
|
||||
break;
|
||||
}
|
||||
word_index = next(word_index + 1 + usize::from(adjustment));
|
||||
}
|
||||
let Some(TextNode::OpenType(word)) = nodes.get(word_index) else {
|
||||
break;
|
||||
};
|
||||
if word.font.id != first.font.id {
|
||||
break;
|
||||
}
|
||||
if index == start + 1 {
|
||||
text.push_str(&first.text);
|
||||
sources.extend(first.sources.iter().cloned());
|
||||
units = first.text.encode_utf16().count();
|
||||
}
|
||||
if let Some((origin, _)) = space {
|
||||
if let Some(origin) = origin {
|
||||
sources.push((text.len(), PaintSource::token(origin)));
|
||||
}
|
||||
text.push(' ');
|
||||
units += 1;
|
||||
}
|
||||
units += word.text.encode_utf16().count();
|
||||
if units > usize::from(u16::MAX) {
|
||||
return Err("native justified OpenType word capacity exceeded".into());
|
||||
}
|
||||
let offset = text.len();
|
||||
text.push_str(&word.text);
|
||||
sources.extend(
|
||||
word.sources
|
||||
.iter()
|
||||
.map(|(at, source)| (offset + at, source.clone())),
|
||||
);
|
||||
index = word_index + 1;
|
||||
}
|
||||
if index == start + 1 {
|
||||
continue;
|
||||
}
|
||||
if cancellation.is_some_and(Cancellation::is_cancelled) {
|
||||
return Err("native compilation cancelled".into());
|
||||
}
|
||||
let mut word = first.font.shape_mapped(text, sources, false)?;
|
||||
*remaining = remaining
|
||||
.checked_sub(word.glyphs.len())
|
||||
.ok_or("native justified OpenType shaping step limit exceeded")?;
|
||||
let target = nodes[start..index]
|
||||
.iter()
|
||||
.map(|node| shipout_width(node, setting))
|
||||
.sum();
|
||||
word.actual_text = first.actual_text;
|
||||
justify(&mut word, target)?;
|
||||
let mut effects = Vec::new();
|
||||
if !moved_suffix_effects
|
||||
&& let Some(offset) = nodes[start + 1..index].iter().position(invisible)
|
||||
{
|
||||
// XDV relinks the first invisible node in the old run. The scan
|
||||
// then follows its new link into the remaining horizontal list.
|
||||
effects.push(start + 1 + offset);
|
||||
effects.extend(
|
||||
nodes[index..]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(offset, node)| invisible(node).then_some(index + offset)),
|
||||
);
|
||||
moved_suffix_effects = true;
|
||||
}
|
||||
runs.push(Run {
|
||||
nodes: start..index,
|
||||
word: Arc::new(word),
|
||||
effects,
|
||||
});
|
||||
}
|
||||
Ok(runs)
|
||||
}
|
||||
|
||||
fn shipout_width(node: &TextNode, setting: GlueSetting) -> i64 {
|
||||
match node {
|
||||
TextNode::Glue(glue) | TextNode::FontSpace(glue, ..) => {
|
||||
// XeTeX rounds each consumed space independently before joining.
|
||||
let (mut cumulative, mut previous) = (0, 0);
|
||||
i64::from(glue.width) + setting.adjustment(*glue, &mut cumulative, &mut previous)
|
||||
}
|
||||
TextNode::OpenType(word) => word.width,
|
||||
TextNode::SpaceAdjustment(width) => i64::from(*width),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn justify(word: &mut OpenTypeRun, target: i64) -> Result<(), String> {
|
||||
let difference = target - word.width;
|
||||
word.width = target;
|
||||
if difference == 0 || word.glyphs.len() < 2 {
|
||||
return Ok(());
|
||||
}
|
||||
let space = word
|
||||
.font
|
||||
.face()?
|
||||
.glyph_index(' ')
|
||||
.map_or(0, |glyph| glyph.0);
|
||||
let spaces = word.glyphs.iter().filter(|glyph| glyph.id == space).count();
|
||||
let total = if spaces == 0 {
|
||||
word.glyphs.len() - 1
|
||||
} else {
|
||||
spaces
|
||||
};
|
||||
let mut consumed = 0;
|
||||
for glyph in &mut word.glyphs {
|
||||
glyph.x =
|
||||
(glyph.x as f64 + difference as f64 * consumed as f64 / total as f64).round() as i64;
|
||||
if spaces == 0 || glyph.id == space {
|
||||
consumed += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user