Measure native XeTeX glyphs and break after mapped dashes

This commit is contained in:
gloamxun committed 2026-11-21 12:58:25 +08:00
1 parent c548ca8e9f
commit facecbf790
9 files changed
+332 -35

No files matched your search

@@ -1,6 +1,6 @@
//! Native horizontal material and paragraph breaking. No command/string parser.
use super::*;
use crate::{HorizontalBox, Paragraph, ParagraphFont};
use crate::{HorizontalBox, Paragraph, ParagraphFont, provenance::PaintSource};
use std::{collections::BTreeMap, sync::Arc};
use crate::opentype::{OpenTypeFont, OpenTypeRun};
@@ -209,14 +209,7 @@ impl Builder {
});
if explicit_hyphen && !self.restricted {
hyphen_break = Some(self.nodes.len());
self.nodes.push(TextNode::Discretionary {
font: run.font.id,
automatic: false,
hyphen_char: None,
pre_break: Vec::new(),
post_break: Vec::new(),
replacement_count: 0,
});
self.push_explicit_hyphen_break(run.font.id);
}
}
Output::Kern(width) => self.nodes.push(TextNode::Kern {
@@ -238,11 +231,7 @@ impl Builder {
}
pub fn finish_text(&mut self, right_boundary: bool, state: &State) -> Result<(), String> {
if let Some(run) = self.opentype.take() {
let shaped = run.font.shape(run.text, run.sources)?;
self.font_steps = self.font_steps.checked_add(shaped.glyphs.len())
.filter(|steps| *steps <= self.font_step_limit)
.ok_or("native OpenType shaping step limit exceeded")?;
self.nodes.push(TextNode::OpenType(Arc::new(shaped)));
self.finish_opentype(run, state)?;
}
if let Some(mut run) = self.run.take() {
run.machine
@@ -262,6 +251,83 @@ impl Builder {
}
Ok(())
}
fn push_explicit_hyphen_break(&mut self, font: FontId) {
self.nodes.push(TextNode::Discretionary {
font,
automatic: false,
hyphen_char: None,
pre_break: Vec::new(),
post_break: Vec::new(),
replacement_count: 0,
});
}
fn finish_opentype(&mut self, run: PendingOpenType, state: &State) -> Result<(), String> {
let xetex = state.input_profile() == InputProfile::XeTeX;
let use_glyph_metrics =
xetex && state.parameter(Parameter::XeTeXuseglyphmetrics).width() > 0;
let original_end = run.text.chars().next_back();
let (text, sources) = run.font.map_text(run.text, run.sources)?;
if self.restricted || !xetex {
return self.push_opentype(&run.font, text, sources, use_glyph_metrics);
}
let dash_breaks = state.parameter(Parameter::XeTeXdashbreakstate).width() > 0;
let hyphen = u32::try_from(state.font_integers(run.font.id).hyphen)
.ok()
.and_then(char::from_u32);
let is_hyphen = |c| Some(c) == hyphen || (dash_breaks && matches!(c, '–' | '—'));
// XeTeX tests the end of the original word for the final discretionary,
// and the characters of the mapped word for interior breakpoints.
let trailing_break = original_end.is_some_and(is_hyphen);
let ends = text.char_indices().filter_map(|(at, c)| {
let end = at + c.len_utf8();
(end < text.len() && is_hyphen(c)).then_some(end)
});
let mut ends = ends.peekable();
if ends.peek().is_none() {
self.push_opentype(&run.font, text, sources, use_glyph_metrics)?;
} else {
let mut sources = sources.into_iter().peekable();
let mut start = 0;
for end in ends.chain(std::iter::once(text.len())) {
let mut fragment_sources = Vec::new();
while let Some((offset, origin)) = sources.next_if(|(at, _)| *at < end) {
fragment_sources.push((offset - start, origin));
}
self.push_opentype(
&run.font,
text[start..end].to_owned(),
fragment_sources,
use_glyph_metrics,
)?;
if end != text.len() {
self.push_explicit_hyphen_break(run.font.id);
}
start = end;
}
}
if trailing_break {
self.push_explicit_hyphen_break(run.font.id);
}
Ok(())
}
fn push_opentype(
&mut self,
font: &Arc<OpenTypeFont>,
text: String,
sources: Vec<(usize, PaintSource)>,
use_glyph_metrics: bool,
) -> Result<(), String> {
let shaped = font.shape_mapped(text, sources, use_glyph_metrics)?;
self.font_steps = self.font_steps.checked_add(shaped.glyphs.len())
.filter(|steps| *steps <= self.font_step_limit)
.ok_or("native OpenType shaping step limit exceeded")?;
self.nodes.push(TextNode::OpenType(Arc::new(shaped)));
Ok(())
}
pub fn opentype_character(
&mut self, c: char, origin: TokenOrigin, state: &State, font: Arc<OpenTypeFont>,
) -> Result<(), String> {