Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,286 @@
|
||||
//! 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))]),
|
||||
metrics.width,
|
||||
metrics.height,
|
||||
metrics.depth,
|
||||
))
|
||||
}
|
||||
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 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);
|
||||
let Ok(id) = self.state.fonts.glyph_font(font) else {
|
||||
continue;
|
||||
};
|
||||
let m = id.metrics();
|
||||
let mut slot = c.slot;
|
||||
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(char::from(slot)).is_some() {
|
||||
best = Some((id, slot));
|
||||
break 'search;
|
||||
}
|
||||
let extent = m.height(char::from(slot)) + m.depth(char::from(slot));
|
||||
if extent > largest {
|
||||
largest = extent;
|
||||
best = Some((id, slot));
|
||||
if extent >= target {
|
||||
break 'search;
|
||||
}
|
||||
}
|
||||
let Some(next) = m.next_larger(char::from(slot)) else {
|
||||
break;
|
||||
};
|
||||
slot = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
let result = if let Some((id, slot)) = best {
|
||||
if let Some(recipe) = id.metrics().extensible_recipe(char::from(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),
|
||||
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(Vec::new()),
|
||||
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 thickness = params.rule_thickness;
|
||||
let mut clearance = thickness
|
||||
+ if style.display() {
|
||||
params.x_height.abs()
|
||||
} else {
|
||||
thickness.abs()
|
||||
} / 4;
|
||||
let sign_source = source.paint.clone().derived(Derivation::Radical);
|
||||
let mut sign = self.delimiter_box(
|
||||
delimiter,
|
||||
style,
|
||||
extent + clearance + thickness,
|
||||
&sign_source,
|
||||
)?;
|
||||
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,
|
||||
setting: GlueSetting::default(),
|
||||
badness: 0,
|
||||
diagnostic_eligible: false,
|
||||
excess: 0,
|
||||
span: Span::default(),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user