Use shaped glyph edges for XeTeX protrusion and line breaking
This commit is contained in:
1 parent
5df90f113c
commit
6826efbeb1
12 files changed
+231
-39
No files matched your search
@@ -1,4 +1,4 @@
|
||||
//! The same font-slot widths drive break search and the owned margin kerns.
|
||||
//! The same shaped edges drive break search and the owned margin kerns.
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -6,12 +6,19 @@ enum Edge {
|
||||
Skip,
|
||||
Solid,
|
||||
Glyph(FontId, u8),
|
||||
OpenType(FontId, u16),
|
||||
}
|
||||
|
||||
impl Edge {
|
||||
fn character(node: &TextNode) -> Self {
|
||||
fn opentype(run: &OpenTypeRun, right: bool) -> Self {
|
||||
let glyph = if right { run.glyphs.last() } else { run.glyphs.first() };
|
||||
glyph.map_or(Self::Solid, |glyph| Self::OpenType(run.font.id, glyph.id))
|
||||
}
|
||||
|
||||
fn character(node: &TextNode, right: bool) -> Self {
|
||||
match node {
|
||||
TextNode::Glyph { font, slot, .. } => Self::Glyph(*font, *slot),
|
||||
TextNode::OpenType(run) => Self::opentype(run, right),
|
||||
TextNode::MathChar(glyph) => {
|
||||
use crate::math_engine_tex::MathItem;
|
||||
match glyph
|
||||
@@ -21,6 +28,7 @@ impl Edge {
|
||||
{
|
||||
Some([item]) => match &item.item {
|
||||
MathItem::NativeGlyph { font, slot, .. } => Self::Glyph(font.id, *slot),
|
||||
MathItem::OpenType(run) => Self::opentype(run, right),
|
||||
_ => Self::Solid,
|
||||
},
|
||||
_ => Self::Solid,
|
||||
@@ -44,7 +52,7 @@ impl Edge {
|
||||
_ => Self::Solid,
|
||||
},
|
||||
_ if skippable(node) => Self::Skip,
|
||||
_ => Self::character(node),
|
||||
_ => Self::character(node, right),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,25 +71,38 @@ impl Edge {
|
||||
Self::Skip
|
||||
}
|
||||
|
||||
fn width(self, state: &State, right: bool) -> i32 {
|
||||
let Self::Glyph(font, slot) = self else {
|
||||
return 0;
|
||||
fn width(self, state: &State, right: bool) -> Result<i32, String> {
|
||||
let xe = state.input_profile() == InputProfile::XeTeX;
|
||||
let (font, code) = match self {
|
||||
Self::Glyph(font, slot) if !xe => (font, state.fonts.protrusion_code(font, slot, right)),
|
||||
Self::Glyph(font, slot) => (font, state.fonts.xe_protrusion_code(font, i32::from(slot), right)),
|
||||
Self::OpenType(font, glyph) if xe => (font, state.fonts.xe_protrusion_code(font, i32::from(glyph), right)),
|
||||
_ => return Ok(0),
|
||||
};
|
||||
let code = state.fonts.protrusion_code(font, slot, right);
|
||||
if code == 0 {
|
||||
return 0;
|
||||
return Ok(0);
|
||||
}
|
||||
let quad = state
|
||||
.fonts
|
||||
.font(font)
|
||||
.expect("marginal glyph's font is live")
|
||||
.quad();
|
||||
let quad = state.fonts.font(font)?.quad();
|
||||
let scaled = i64::from(quad) * i64::from(code);
|
||||
// Round to the nearest sp, taking half a unit away from zero.
|
||||
(scaled.signum() * ((scaled.abs() + 500) / 1000)) as i32
|
||||
// XeTeX's signed remainder does not round up for negative factors.
|
||||
let rounding = if xe && code < 0 { 0 } else { 500 };
|
||||
let width = scaled.signum() * ((scaled.abs() + rounding) / 1000);
|
||||
if width.abs() > 1073741823 {
|
||||
return Err("native character protrusion exceeds dimension capacity".into());
|
||||
}
|
||||
Ok(width as i32)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn level(state: &State, parameters: &BTreeMap<Parameter, ParameterValue>) -> i32 {
|
||||
let parameter = if state.input_profile() == InputProfile::XeTeX {
|
||||
Parameter::XeTeXprotrudechars
|
||||
} else {
|
||||
Parameter::Pdfprotrudechars
|
||||
};
|
||||
parameters[¶meter].width()
|
||||
}
|
||||
|
||||
fn skippable(node: &TextNode) -> bool {
|
||||
match node {
|
||||
TextNode::Box(boxed) => matches!(
|
||||
@@ -141,22 +162,24 @@ pub(super) fn margin_kern(
|
||||
nodes: &[TextNode],
|
||||
right: bool,
|
||||
direct: bool,
|
||||
) -> Option<TextNode> {
|
||||
) -> Result<Option<TextNode>, String> {
|
||||
let edge = if direct {
|
||||
nodes.last().map_or(Edge::Solid, Edge::character)
|
||||
nodes.last().map_or(Edge::Solid, |node| Edge::character(node, right))
|
||||
} else {
|
||||
Edge::list(nodes, right)
|
||||
};
|
||||
let Edge::Glyph(font, slot) = edge else {
|
||||
return None;
|
||||
let (font, slot) = match edge {
|
||||
Edge::Glyph(font, slot) => (font, u16::from(slot)),
|
||||
Edge::OpenType(font, glyph) => (font, glyph),
|
||||
_ => return Ok(None),
|
||||
};
|
||||
let width = -edge.width(state, right);
|
||||
(width != 0).then_some(TextNode::MarginKern {
|
||||
let width = -edge.width(state, right)?;
|
||||
Ok((width != 0).then_some(TextNode::MarginKern {
|
||||
width,
|
||||
right,
|
||||
font,
|
||||
slot,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
@@ -185,7 +208,7 @@ impl Search {
|
||||
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 width = edge.width(state, right)?;
|
||||
let mut expanding = None;
|
||||
if expand && let Edge::Glyph(font, slot) = edge {
|
||||
let key = (font, slot);
|
||||
@@ -210,9 +233,9 @@ impl Search {
|
||||
};
|
||||
if let TextNode::Discretionary { pre_break, post_break, .. } = node {
|
||||
widths[i].pre = pre_break.last()
|
||||
.map(|node| project(Edge::character(node), true)).transpose()?;
|
||||
.map(|node| project(Edge::character(node, true), true)).transpose()?;
|
||||
widths[i].post = post_break.first()
|
||||
.map(|node| project(Edge::character(node), false)).transpose()?;
|
||||
.map(|node| project(Edge::character(node, false), false)).transpose()?;
|
||||
}
|
||||
}
|
||||
for (i, node) in nodes.iter().enumerate().rev() {
|
||||
|
||||
Reference in new issue
Block a user