339 lines
13 KiB
Rust
339 lines
13 KiB
Rust
//! TeX var_delimiter using actual family masters, TFM lists and recipes.
|
|
use super::{math::*, *};
|
|
use crate::provenance::{Derivation, PaintSource};
|
|
use crate::{
|
|
math::MathBox,
|
|
math_engine_tex::{MathGeometry, MathItem, MathStyle, PositionedItem},
|
|
};
|
|
fn character(
|
|
id: &std::sync::Arc<crate::native_font::NativeFont>,
|
|
slot: u8,
|
|
source: &PaintSource,
|
|
) -> Result<MathGeometry, String> {
|
|
if !id.has_slot(slot) {
|
|
return Err("missing native extensible delimiter component".into());
|
|
}
|
|
let m = id.metrics();
|
|
let c = char::from(slot);
|
|
Ok(MathGeometry {
|
|
metrics: MathBox {
|
|
width: m.width(c) + m.italic(c),
|
|
height: m.height(c),
|
|
depth: m.depth(c),
|
|
},
|
|
items: vec![PositionedItem {
|
|
source: source.clone(),
|
|
x: 0,
|
|
y: 0,
|
|
item: MathItem::NativeGlyph {
|
|
character: char::from(slot),
|
|
font: id.clone(),
|
|
slot,
|
|
size: id.size_sp(),
|
|
},
|
|
}],
|
|
})
|
|
}
|
|
impl Converter<'_> {
|
|
fn delimiter_character_box(
|
|
&self,
|
|
id: &std::sync::Arc<crate::native_font::NativeFont>,
|
|
slot: u8,
|
|
source: &PaintSource,
|
|
) -> Result<NativeBox, String> {
|
|
let mut g = character(id, slot, source)?;
|
|
let metrics = g.metrics;
|
|
g.metrics.width -= id.metrics().italic(char::from(slot));
|
|
Ok(delimiter_native_box(
|
|
BoxContent::Horizontal(vec![TextNode::MathChar(super::math::math_box(g))].into()),
|
|
metrics.width,
|
|
metrics.height,
|
|
metrics.depth,
|
|
))
|
|
}
|
|
pub(super) fn reserve_delimiter_glyphs(&self, n: usize) -> Result<(), String> {
|
|
let used = self
|
|
.delimiter_glyphs
|
|
.get()
|
|
.checked_add(n)
|
|
.ok_or("native delimiter component capacity exceeded")?;
|
|
if used > self.delimiter_glyph_limit {
|
|
return Err("native delimiter component capacity exceeded".into());
|
|
}
|
|
self.delimiter_glyphs.set(used);
|
|
Ok(())
|
|
}
|
|
pub(super) fn delimiter(
|
|
&self,
|
|
value: Delimiter,
|
|
style: MathStyle,
|
|
target: i64,
|
|
source: &PaintSource,
|
|
) -> Result<NativeBox, String> {
|
|
let source = source.clone().derived(Derivation::Delimiter);
|
|
let mut result = self.delimiter_owned_box(value, style, target, &source)?;
|
|
let shift =
|
|
(result.height - result.depth + 1).div_euclid(2) - self.params(style)?.axis_height;
|
|
result.shift = shift;
|
|
Ok(result)
|
|
}
|
|
fn delimiter_box(
|
|
&self,
|
|
value: Delimiter,
|
|
style: MathStyle,
|
|
target: i64,
|
|
source: &PaintSource,
|
|
) -> Result<MathGeometry, String> {
|
|
self.delimiter_owned_box(value, style, target, source)?
|
|
.geometry()
|
|
}
|
|
fn delimiter_owned_box(
|
|
&self,
|
|
value: Delimiter,
|
|
style: MathStyle,
|
|
target: i64,
|
|
source: &PaintSource,
|
|
) -> Result<NativeBox, String> {
|
|
let mut best = None;
|
|
let mut opentype_best = None;
|
|
let mut largest = 0;
|
|
'search: for c in [value.small, value.large] {
|
|
// (family 0, slot 0) is the null delimiter, not a Gamma request.
|
|
if c.family == 0 && c.slot == 0 {
|
|
continue;
|
|
}
|
|
for size in (0..=style.size_index() as u8).rev() {
|
|
let font = self.state.math_font(size, c.family);
|
|
if font == FontId::NULL {
|
|
continue;
|
|
}
|
|
if let Some(face) = self.state.fonts.font(font)?.opentype() {
|
|
let Some(character) = char::from_u32(c.slot) else { continue };
|
|
let Some(glyph) = face.face()?.glyph_index(character) else { continue };
|
|
let shaped = crate::opentype::stretch_glyph(
|
|
face, glyph.0, character, source, crate::opentype::Stretch::Vertical, target,
|
|
self.delimiter_glyph_limit.saturating_sub(self.delimiter_glyphs.get()),
|
|
)?;
|
|
let metrics = shaped.geometry.metrics;
|
|
let enough = shaped.sufficient;
|
|
largest = metrics.height + metrics.depth;
|
|
best = None;
|
|
opentype_best = Some(shaped.geometry);
|
|
if enough {
|
|
break 'search;
|
|
}
|
|
continue;
|
|
}
|
|
let id = self.state.fonts.glyph_font(font)?;
|
|
let m = self.state.fonts.program(font)?;
|
|
let Ok(mut slot) = u8::try_from(c.slot) else {
|
|
continue;
|
|
};
|
|
let mut visited = std::collections::BTreeSet::new();
|
|
while id.has_slot(slot) {
|
|
if !visited.insert(slot) {
|
|
return Err("cyclic native delimiter size list".into());
|
|
}
|
|
if m.extensible_recipe(slot).is_some() {
|
|
opentype_best = None;
|
|
best = Some((id, slot));
|
|
break 'search;
|
|
}
|
|
let extent = m.height(char::from(slot)) + m.depth(char::from(slot));
|
|
if extent > largest {
|
|
largest = extent;
|
|
opentype_best = None;
|
|
best = Some((id, slot));
|
|
if extent >= target {
|
|
break 'search;
|
|
}
|
|
}
|
|
let Some(next) = m.next_larger(slot) else {
|
|
break;
|
|
};
|
|
slot = next;
|
|
}
|
|
}
|
|
}
|
|
let result = if let Some(geometry) = opentype_best {
|
|
self.reserve_delimiter_glyphs(geometry.items.len())?;
|
|
let metrics = geometry.metrics;
|
|
delimiter_native_box(
|
|
BoxContent::Horizontal(vec![TextNode::Box(super::math::math_box(geometry))].into()),
|
|
metrics.width, metrics.height, metrics.depth,
|
|
)
|
|
} else if let Some((id, slot)) = best {
|
|
if let Some(recipe) = self.state.fonts.program(id.id)?.extensible_recipe(slot) {
|
|
let repeat = character(id, recipe.repeat, source)?;
|
|
let unit = repeat.metrics.height + repeat.metrics.depth;
|
|
let mut extent = 0;
|
|
for p in [recipe.top, recipe.middle, recipe.bottom] {
|
|
if p != 0 {
|
|
let g = character(id, p, source)?;
|
|
extent += g.metrics.height + g.metrics.depth;
|
|
}
|
|
}
|
|
let step = if recipe.middle != 0 { 2 * unit } else { unit };
|
|
let n = if step > 0 && extent < target {
|
|
(target - extent + step - 1) / step
|
|
} else {
|
|
0
|
|
};
|
|
extent += n * step;
|
|
let total = i64::from(recipe.top != 0)
|
|
+ i64::from(recipe.middle != 0)
|
|
+ i64::from(recipe.bottom != 0)
|
|
+ n * if recipe.middle != 0 { 2 } else { 1 };
|
|
self.reserve_delimiter_glyphs(total as usize)?;
|
|
let mut parts = Vec::new();
|
|
if recipe.top != 0 {
|
|
parts.push(recipe.top);
|
|
}
|
|
parts.extend(std::iter::repeat_n(recipe.repeat, n as usize));
|
|
if recipe.middle != 0 {
|
|
parts.push(recipe.middle);
|
|
parts.extend(std::iter::repeat_n(recipe.repeat, n as usize));
|
|
}
|
|
if recipe.bottom != 0 {
|
|
parts.push(recipe.bottom);
|
|
}
|
|
let height = parts
|
|
.first()
|
|
.map(|p| id.metrics().height(char::from(*p)))
|
|
.unwrap_or(0);
|
|
let nodes = parts
|
|
.iter()
|
|
.map(|p| {
|
|
self.delimiter_character_box(id, *p, source)
|
|
.map(|b| NativeVerticalNode::Box(Box::new(b)))
|
|
})
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
delimiter_native_box(
|
|
BoxContent::Vertical(nodes.into()),
|
|
repeat.metrics.width,
|
|
height,
|
|
extent - height,
|
|
)
|
|
} else {
|
|
self.reserve_delimiter_glyphs(1)?;
|
|
self.delimiter_character_box(id, slot, source)?
|
|
}
|
|
} else {
|
|
delimiter_native_box(
|
|
BoxContent::Horizontal(Default::default()),
|
|
self.state.parameter(Parameter::Nulldelimiterspace).width() as i64,
|
|
0,
|
|
0,
|
|
)
|
|
};
|
|
Ok(result)
|
|
}
|
|
|
|
pub(super) fn radical(
|
|
&self,
|
|
delimiter: Delimiter,
|
|
nucleus: &MathField,
|
|
style: MathStyle,
|
|
source: &FieldSource,
|
|
) -> Result<MathGeometry, String> {
|
|
let params = self.params(style)?;
|
|
let mut body = self.field(
|
|
nucleus,
|
|
style.cramped_style(),
|
|
source.radical.as_deref().unwrap_or(&FieldSource::UNKNOWN),
|
|
)?;
|
|
let extent = body.metrics.height + body.metrics.depth;
|
|
let id = self.state.math_font(style.size_index() as u8, delimiter.small.family);
|
|
let selected = self.state.fonts.font(id)?;
|
|
let constants = selected.opentype().map(|font| crate::opentype::MathConstants::read(font))
|
|
.transpose()?.flatten();
|
|
let (thickness, mut clearance) = if let Some(math) = &constants {
|
|
let gap = if style.display() {
|
|
math.values.radical_display_style_vertical_gap()
|
|
} else {
|
|
math.values.radical_vertical_gap()
|
|
};
|
|
(math.measure(math.values.radical_rule_thickness()), math.measure(gap))
|
|
} else {
|
|
let thickness = params.rule_thickness;
|
|
let clearance = thickness + if style.display() {
|
|
params.x_height.abs()
|
|
} else {
|
|
thickness.abs()
|
|
} / 4;
|
|
(thickness, clearance)
|
|
};
|
|
let sign_source = source.paint.clone().derived(Derivation::Radical);
|
|
let mut sign = self.delimiter_box(
|
|
delimiter,
|
|
style,
|
|
extent + clearance + thickness,
|
|
&sign_source,
|
|
)?;
|
|
if constants.is_some() {
|
|
let baseline = sign.metrics.height - thickness;
|
|
for item in &mut sign.items {
|
|
item.y += baseline;
|
|
}
|
|
sign.metrics.depth += baseline;
|
|
sign.metrics.height = thickness;
|
|
}
|
|
let excess = sign.metrics.depth - extent - clearance;
|
|
if excess > 0 {
|
|
clearance += (excess + 1) / 2;
|
|
}
|
|
let lift = body.metrics.height + clearance;
|
|
let rule_height = sign.metrics.height;
|
|
let sign_width = sign.metrics.width;
|
|
for item in &mut sign.items {
|
|
item.y -= lift;
|
|
}
|
|
for item in &mut body.items {
|
|
item.x += sign_width;
|
|
}
|
|
if body.metrics.width > 0 && rule_height > 0 {
|
|
sign.items.push(PositionedItem {
|
|
source: sign_source,
|
|
x: sign_width,
|
|
y: -lift - rule_height,
|
|
item: MathItem::Rule {
|
|
width: body.metrics.width,
|
|
height: rule_height,
|
|
},
|
|
});
|
|
}
|
|
sign.items.extend(body.items);
|
|
sign.metrics = MathBox {
|
|
width: sign_width + body.metrics.width,
|
|
height: (lift + rule_height + rule_height).max(lift + sign.metrics.height),
|
|
depth: body.metrics.depth.max(sign.metrics.depth - lift),
|
|
};
|
|
Ok(sign)
|
|
}
|
|
}
|
|
|
|
fn delimiter_native_box(content: BoxContent, width: i64, height: i64, depth: i64) -> NativeBox {
|
|
let (kind, diagnostic) = match &content {
|
|
BoxContent::Horizontal(nodes) => {
|
|
(BoxKind::Horizontal, super::diagnostics::text_nodes(nodes))
|
|
}
|
|
BoxContent::Vertical(_) => (BoxKind::Vertical, Vec::new()),
|
|
};
|
|
NativeBox {
|
|
kind,
|
|
display: false,
|
|
width,
|
|
height,
|
|
depth,
|
|
shift: 0,
|
|
content,
|
|
diagnostic: diagnostic.into(),
|
|
setting: GlueSetting::default(),
|
|
interword_runs: Vec::new(),
|
|
badness: 0,
|
|
diagnostic_eligible: false,
|
|
excess: 0,
|
|
span: Span::default(),
|
|
}
|
|
}
|