Implement XeTeX native word ActualText across shaping and shipout
This commit is contained in:
1 parent
97319d751b
commit
7e5428ec35
15 files changed
+143
-24
No files matched your search
@@ -63,7 +63,8 @@ pub(super) fn rejoin(
|
||||
text.push_str(&run.text);
|
||||
}
|
||||
}
|
||||
let word = first.font.shape_mapped(text, sources, glyph_metrics)?;
|
||||
let mut word = first.font.shape_mapped(text, sources, glyph_metrics)?;
|
||||
word.actual_text = first.actual_text;
|
||||
if let Some((steps, limit)) = &mut shaping_budget {
|
||||
**steps = steps
|
||||
.checked_add(word.glyphs.len())
|
||||
|
||||
@@ -174,7 +174,9 @@ impl NativeShipout {
|
||||
let y = self.device.vertical_origin + self.box_node.height + p.y;
|
||||
if let MathItem::OpenType(run) = p.item {
|
||||
run.paint(x, y, |value| value, |item, source| {
|
||||
page.sources.insert(page.items.len(), source);
|
||||
if !matches!(item, DisplayItem::OpenTypeActualText(_)) {
|
||||
page.sources.insert(page.items.len(), source);
|
||||
}
|
||||
page.items.push(item);
|
||||
});
|
||||
return;
|
||||
|
||||
@@ -389,6 +389,7 @@ impl Executor {
|
||||
if let Some(font) = opentype {
|
||||
if self.state.input_profile() == InputProfile::XeTeX {
|
||||
self.reads.insert(Dependency::Parameter(Parameter::XeTeXuseglyphmetrics));
|
||||
self.reads.insert(Dependency::Parameter(Parameter::XeTeXgenerateactualtext));
|
||||
if !self.restricted() {
|
||||
self.reads.insert(Dependency::Parameter(Parameter::XeTeXdashbreakstate));
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 71;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 38] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, SCHEMA,
|
||||
const SCHEMA: u32 = 72;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 39] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
@@ -273,12 +273,10 @@ impl Builder {
|
||||
|
||||
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);
|
||||
return self.push_opentype(&run.font, text, sources, state);
|
||||
}
|
||||
|
||||
let dash_breaks = state.parameter(Parameter::XeTeXdashbreakstate).width() > 0;
|
||||
@@ -295,7 +293,7 @@ impl Builder {
|
||||
});
|
||||
let mut ends = ends.peekable();
|
||||
if ends.peek().is_none() {
|
||||
self.push_opentype(&run.font, text, sources, use_glyph_metrics)?;
|
||||
self.push_opentype(&run.font, text, sources, state)?;
|
||||
} else {
|
||||
let mut sources = sources.into_iter().peekable();
|
||||
let mut start = 0;
|
||||
@@ -308,7 +306,7 @@ impl Builder {
|
||||
&run.font,
|
||||
text[start..end].to_owned(),
|
||||
fragment_sources,
|
||||
use_glyph_metrics,
|
||||
state,
|
||||
)?;
|
||||
if end != text.len() {
|
||||
self.push_explicit_hyphen_break(run.font.id);
|
||||
@@ -325,14 +323,52 @@ impl Builder {
|
||||
fn push_opentype(
|
||||
&mut self,
|
||||
font: &Arc<OpenTypeFont>,
|
||||
text: String,
|
||||
sources: Vec<(usize, PaintSource)>,
|
||||
use_glyph_metrics: bool,
|
||||
mut text: String,
|
||||
mut sources: Vec<(usize, PaintSource)>,
|
||||
state: &State,
|
||||
) -> Result<(), String> {
|
||||
let shaped = font.shape_mapped(text, sources, use_glyph_metrics)?;
|
||||
let xetex = state.input_profile() == InputProfile::XeTeX;
|
||||
let use_glyph_metrics =
|
||||
xetex && state.parameter(Parameter::XeTeXuseglyphmetrics).width() > 0;
|
||||
// A fresh word takes the current subtype, including when the scanner
|
||||
// replaces the preceding word. Packing preserves the first subtype.
|
||||
let previous = match self.nodes.as_slice() {
|
||||
[.., preceding, TextNode::OpenType(run)]
|
||||
if xetex && run.font.id == font.id
|
||||
&& !matches!(preceding, TextNode::Glyph { ligature: false, .. }
|
||||
| TextNode::Discretionary { .. }) =>
|
||||
{
|
||||
Some(run)
|
||||
}
|
||||
_ => None,
|
||||
}.filter(|_| {
|
||||
!self.nodes.iter().rev().skip(1).take(usize::from(u8::MAX))
|
||||
.enumerate().any(|(offset, node)| {
|
||||
matches!(node, TextNode::Discretionary { replacement_count, .. }
|
||||
if usize::from(*replacement_count) > offset)
|
||||
})
|
||||
});
|
||||
if let Some(previous) = previous {
|
||||
let offset = previous.text.len();
|
||||
let mut joined = previous.text.clone();
|
||||
joined.push_str(&text);
|
||||
if joined.encode_utf16().count() > usize::from(u16::MAX) {
|
||||
return Err("native OpenType word capacity exceeded".into());
|
||||
}
|
||||
text = joined;
|
||||
let mut joined_sources = previous.sources.clone();
|
||||
joined_sources.extend(sources.into_iter().map(|(at, source)| (at + offset, source)));
|
||||
sources = joined_sources;
|
||||
}
|
||||
let replaced = previous.is_some();
|
||||
let mut shaped = font.shape_mapped(text, sources, use_glyph_metrics)?;
|
||||
shaped.actual_text = xetex && state.parameter(Parameter::XeTeXgenerateactualtext).width() > 0;
|
||||
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")?;
|
||||
if replaced {
|
||||
self.nodes.pop();
|
||||
}
|
||||
self.nodes.push(TextNode::OpenType(Arc::new(shaped)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ pub(super) fn add_hyphens(
|
||||
let max_length = super::super::hyphenation::max_word_length(state);
|
||||
let uppercase = state.parameter(Parameter::Uchyph).width() > 0;
|
||||
let use_glyph_metrics = state.parameter(Parameter::XeTeXuseglyphmetrics).width() > 0;
|
||||
let mapped_hyphen_actual_text = state.parameter(Parameter::XeTeXgenerateactualtext).width() > 0;
|
||||
let mut after_glue = false;
|
||||
let mut auto_breaking = true;
|
||||
let mut edits = Vec::new();
|
||||
@@ -123,6 +124,7 @@ pub(super) fn add_hyphens(
|
||||
run.text[from..to].to_owned(),
|
||||
fragment_sources,
|
||||
use_glyph_metrics,
|
||||
run.actual_text,
|
||||
steps,
|
||||
step_limit,
|
||||
)?);
|
||||
@@ -145,6 +147,7 @@ pub(super) fn add_hyphens(
|
||||
text,
|
||||
sources,
|
||||
use_glyph_metrics,
|
||||
run.font.has_mapping() && mapped_hyphen_actual_text,
|
||||
steps,
|
||||
step_limit,
|
||||
)?],
|
||||
@@ -210,10 +213,12 @@ fn shape(
|
||||
text: String,
|
||||
sources: Vec<(usize, PaintSource)>,
|
||||
use_glyph_metrics: bool,
|
||||
actual_text: bool,
|
||||
steps: &mut usize,
|
||||
step_limit: usize,
|
||||
) -> Result<TextNode, String> {
|
||||
let run = run.font.shape_mapped(text, sources, use_glyph_metrics)?;
|
||||
let mut run = run.font.shape_mapped(text, sources, use_glyph_metrics)?;
|
||||
run.actual_text = actual_text;
|
||||
*steps = steps
|
||||
.checked_add(run.glyphs.len())
|
||||
.filter(|steps| *steps <= step_limit)
|
||||
|
||||
@@ -126,6 +126,7 @@ pub enum Parameter {
|
||||
XeTeXinterchartokenstate,
|
||||
XeTeXprotrudechars,
|
||||
XeTeXhyphenatablelength,
|
||||
XeTeXgenerateactualtext,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum ParameterValue {
|
||||
@@ -158,13 +159,14 @@ impl Parameter {
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) const XETEX: [Self; 6] = [
|
||||
pub(super) const XETEX: [Self; 7] = [
|
||||
Self::XeTeXuseglyphmetrics,
|
||||
Self::XeTeXdashbreakstate,
|
||||
Self::Suppressfontnotfounderror,
|
||||
Self::XeTeXinterchartokenstate,
|
||||
Self::XeTeXprotrudechars,
|
||||
Self::XeTeXhyphenatablelength,
|
||||
Self::XeTeXgenerateactualtext,
|
||||
];
|
||||
|
||||
pub const ALL: &'static [Self] = &[
|
||||
@@ -289,6 +291,7 @@ impl Parameter {
|
||||
Self::XeTeXinterchartokenstate,
|
||||
Self::XeTeXprotrudechars,
|
||||
Self::XeTeXhyphenatablelength,
|
||||
Self::XeTeXgenerateactualtext,
|
||||
];
|
||||
pub fn name(self) -> String {
|
||||
match self {
|
||||
@@ -298,6 +301,7 @@ impl Parameter {
|
||||
Self::XeTeXinterchartokenstate => "XeTeXinterchartokenstate".into(),
|
||||
Self::XeTeXprotrudechars => "XeTeXprotrudechars".into(),
|
||||
Self::XeTeXhyphenatablelength => "XeTeXhyphenatablelength".into(),
|
||||
Self::XeTeXgenerateactualtext => "XeTeXgenerateactualtext".into(),
|
||||
_ => format!("{self:?}").to_ascii_lowercase(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,6 +337,10 @@ impl OpenTypeFont {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn has_mapping(&self) -> bool {
|
||||
self.mapping.is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn shape_mapped(
|
||||
self: &Arc<Self>,
|
||||
text: String,
|
||||
@@ -423,6 +427,7 @@ impl OpenTypeFont {
|
||||
width: x_origin,
|
||||
sources,
|
||||
glyph_extents,
|
||||
actual_text: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -533,6 +538,8 @@ pub struct OpenTypeRun {
|
||||
pub sources: Vec<(usize, PaintSource)>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
glyph_extents: Option<[i64; 2]>,
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub(crate) actual_text: bool,
|
||||
}
|
||||
|
||||
impl OpenTypeRun {
|
||||
@@ -592,6 +599,13 @@ impl OpenTypeRun {
|
||||
scale: impl Fn(i64) -> i64,
|
||||
mut emit: impl FnMut(crate::render::DisplayItem, PaintSource),
|
||||
) {
|
||||
let marked = self.actual_text && (!self.text.is_empty() || !self.glyphs.is_empty());
|
||||
if marked {
|
||||
emit(
|
||||
crate::render::DisplayItem::OpenTypeActualText(Some(self.text.clone())),
|
||||
PaintSource::default(),
|
||||
);
|
||||
}
|
||||
for glyph in &self.glyphs {
|
||||
let first = self
|
||||
.sources
|
||||
@@ -620,6 +634,12 @@ impl OpenTypeRun {
|
||||
source.derived(crate::provenance::Derivation::FontProgram),
|
||||
);
|
||||
}
|
||||
if marked {
|
||||
emit(
|
||||
crate::render::DisplayItem::OpenTypeActualText(None),
|
||||
PaintSource::default(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -238,6 +238,7 @@ pub(crate) fn painted_glyph(
|
||||
width: advance,
|
||||
sources: vec![(0, source.clone())],
|
||||
glyph_extents: Some([height, depth]),
|
||||
actual_text: false,
|
||||
};
|
||||
(
|
||||
MathGeometry {
|
||||
|
||||
@@ -851,6 +851,10 @@ fn page_content(
|
||||
let px = |x| pt(x - h);
|
||||
let py = |y| pt(height - y - v);
|
||||
match item {
|
||||
DisplayItem::OpenTypeActualText(text) => match text {
|
||||
Some(text) => opentype_fonts::begin_actual_text(&mut content, text, true),
|
||||
None => content.push_str("EMC\n"),
|
||||
},
|
||||
DisplayItem::OpenTypeGlyph { x, y, size, font, glyph, text, .. } => {
|
||||
opentype[&font.id].paint(
|
||||
&mut opentype_text, &mut content, font.id, *glyph, text,
|
||||
|
||||
@@ -263,6 +263,16 @@ fn utf16_hex(value: &str) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn begin_actual_text(output: &mut PageContent, text: &str, xdv: bool) {
|
||||
// XDV uses PDFDocEncoding where it can use Unicode's low byte directly.
|
||||
let encoded = if xdv && text.chars().all(|c| matches!(c as u32, 0..=0x7F | 0xA1..=0xFF)) {
|
||||
text.chars().map(|c| format!("{:02X}", c as u8)).collect()
|
||||
} else {
|
||||
format!("FEFF{}", utf16_hex(text))
|
||||
};
|
||||
output.push_str(&format!("/Span << /ActualText <{encoded}> >> BDC\n"));
|
||||
}
|
||||
|
||||
impl EmbeddedFont {
|
||||
pub(super) fn paint(
|
||||
&self,
|
||||
@@ -291,10 +301,7 @@ impl EmbeddedFont {
|
||||
TextRun::finish(run, output);
|
||||
}
|
||||
if override_text {
|
||||
output.push_str(&format!(
|
||||
"/Span << /ActualText <FEFF{}> >> BDC\n",
|
||||
utf16_hex(text)
|
||||
));
|
||||
begin_actual_text(output, text, false);
|
||||
}
|
||||
let state = run.get_or_insert_with(|| {
|
||||
let coordinates = [x, y].map(|value| {
|
||||
|
||||
@@ -60,6 +60,8 @@ pub enum DisplayItem {
|
||||
glyph: u16,
|
||||
text: String,
|
||||
},
|
||||
/// Word-level PDF replacement text. None closes the current word.
|
||||
OpenTypeActualText(Option<String>),
|
||||
/// A native math glyph. `face` preserves the optical master independently
|
||||
/// of the painted size; `slot` preserves CMEX next-larger selection.
|
||||
MathGlyph {
|
||||
@@ -344,7 +346,8 @@ impl DisplayList {
|
||||
fn svg_paint_items(&self) -> impl Iterator<Item = &DisplayItem> {
|
||||
self.items
|
||||
.iter()
|
||||
.filter(|item| !matches!(item, DisplayItem::PdfImageObject(_)))
|
||||
.filter(|item| !matches!(item,
|
||||
DisplayItem::PdfImageObject(_) | DisplayItem::OpenTypeActualText(_)))
|
||||
}
|
||||
|
||||
/// Compare the inputs consumed by SVG rendering. Font data for glyphs not
|
||||
@@ -431,7 +434,8 @@ impl DisplayList {
|
||||
*y += dy;
|
||||
}
|
||||
DisplayItem::Fill(_) | DisplayItem::Background(_) | DisplayItem::PdfColor(_) => {}
|
||||
DisplayItem::Special(_) | DisplayItem::PdfImageObject(_) => {}
|
||||
DisplayItem::Special(_) | DisplayItem::PdfImageObject(_)
|
||||
| DisplayItem::OpenTypeActualText(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -715,7 +719,8 @@ impl DisplayList {
|
||||
*thickness as f64 / 65_536.0
|
||||
));
|
||||
}
|
||||
DisplayItem::Link { .. } | DisplayItem::PdfTextSpace { .. } => {}
|
||||
DisplayItem::Link { .. } | DisplayItem::PdfTextSpace { .. }
|
||||
| DisplayItem::OpenTypeActualText(_) => {}
|
||||
DisplayItem::Special(content) => {
|
||||
out.push_str(&format!(
|
||||
"<!-- special: {} -->
|
||||
|
||||
Reference in new issue
Block a user