Lay out inline math in paragraphs
This commit is contained in:
1 parent
aae046a2f0
commit
e008741aa5
7 files changed
+346
-13
No files matched your search
@@ -469,6 +469,8 @@ the assignable TeX parameters `\\delimiterfactor` (default 901) and
|
|||||||
|
|
||||||
Route-D text faces follow the NFSS declarations and compose with the surrounding run: `\sffamily\bfseries` selects cmssbx10, `\sffamily\itshape` selects cmssi10, and bold italic sans substitutes cmssbx10 because Computer Modern has no sans bold italic. `\scshape`/`\textsc` select the real cmcsc10 small-caps face, `\slshape`/`\textsl` use cmsl10 (or cmbxsl10 when bold) rather than the italic cmti10, and `\ttfamily\itshape` reaches cmitt10. All twelve text programs (cmr10, cmbx10, cmti10, cmbxti10, cmtt10, cmss10, cmssbx10, cmssi10, cmcsc10, cmsl10, cmbxsl10, cmitt10) are embedded as Type1 outlines. Because Computer Modern has no bold small caps, a bold small-caps request falls back to cmbx10. Section-wide declarations such as `\bfseries` apply from where they appear to the end of their group, and `\em` toggles emphasis relative to the surrounding face. Paragraph base sizes come from `\fontsize{<size>}{<baselineskip>}` and the article size commands (`\tiny` through `\Huge`), scaling glyph metrics and the default interline glue while the measure, indentation and authored skips stay absolute. The class options `10pt`/`11pt`/`12pt` on `\documentclass[...]` set the default base size (and article `\baselineskip` of 12pt/13.6pt/14.5pt); a size command inside a paragraph still overrides the class default. The paragraph measure follows `\textwidth`/`\hsize` (or `\setlength{\textwidth}{...}`), and `\setlength` accepts the other supported layout lengths, including a `\parskip` with plus/minus glue. The `geometry` package's `margin`/`hmargin`/`vmargin`/`left`/`right`/`top`/`bottom`/`textwidth`/`textheight` options (or `\geometry{...}`) are applied to the page measure and height; other package code is not executed. Block environments `center`, `flushleft` and `flushright` set the paragraph alignment, and aligned short lines are centring/flushing within the full measure rather than at the natural line width. `\title`, `\author` and `\date` are captured from the preamble, and `\maketitle` expands into a centered title block at the article class sizes. `\section`, `\subsection` and `\subsubsection` advance article counters, prefix their titles with the number, and paint bold at `\Large`/`\large`/`\normalsize`; starred headings stay unnumbered and an optional `[short]` title is dropped. The `abstract` environment expands into a centered bold heading and a 9pt body, and `\maketitle` joins multiple `\and` authors and turns `\thanks` notes into title footnotes. Section labels resolve `\ref`/`\eqref` to the heading number (standalone `\label` lines attach to the previous heading, unknown references print `??`), `\appendix` restarts sectioning with letter numbers, and the `abstract` environment expands to its heading and body. In route-D, math uses Computer Modern metrics from the bundled `cmmi10`, `cmsy10`, `cmex10`, `msbm10` and `eufm10` TFMs and embeds their Type1 programs, so italic letters, relation symbols, big operators, `\mathcal` capitals, `\mathbb` capitals and `\mathfrak` Fraktur letters each paint with their own face. Fractions can be written as `\frac` or with TeX's infix forms (`\over`, `\atop`, `\choose`, `\brace`, `\brack`, `\overwithdelims`, `\atopwithdelims`); only `\over`-style fractions draw a bar and fractions never draw parentheses. `\left`/`\right` choose CMEX delimiter sizes from the body extent using TeX's delimiterfactor and shortfall, `\middle` delimiters are sized with their group, and `\big`/`\Big`/`\bigg`/`\Bigg` paint at their selected CMEX size. `align`/`align*` displays share column widths, right-align even columns at their relations, and number each row (with `\tag`/`\notag` per row); `gather`/`gather*` centre each row instead. Inter-atom spacing uses TeX's 3/4/5mu classes (`1mu` is `1/18em`), `\mid` is a relation and `\colon` punctuation, and `\mskip`/`\mkern`/`\mspace`/`\hskip`/`\kern`/`\hspace` scan TeX dimensions, taking the natural width of any `plus`/`minus` parts. `\footnote{...}` leaves a numbered marker in the paragraph and sets the note as a 9pt page insertion at the bottom of the page that mentions it. A `thebibliography` environment turns its `\bibitem` entries into numbered `[n]` paragraphs, and `\cite{...}` resolves to `[n]`/`[n, m]` (unknown keys print `[?]`). A `figure` environment becomes a float at its `[t]`/`[b]`/`[h]` placement, its `\caption` numbers as `Figure n:` and feeds `\ref`, and `\includegraphics[width=...]` reserves a rule placeholder. `table` environments likewise become floats carrying their captured `tabular` body, with `Table n:` captions that resolve from `\ref`. Table captions sit above the tabular as in LaTeX, figure captions below the graphic, and `\caption*` leaves either unnumbered. Theorem-like environments (`theorem`, `lemma`, `corollary`, `proposition`, `definition`, `example`, `remark`, `proof`) produce a bold numbered heading with an optional `[title]`, italic statements for the theorem group, and unnumbered proofs, with `\label`/`\ref` resolving to tLine truncated
|
Route-D text faces follow the NFSS declarations and compose with the surrounding run: `\sffamily\bfseries` selects cmssbx10, `\sffamily\itshape` selects cmssi10, and bold italic sans substitutes cmssbx10 because Computer Modern has no sans bold italic. `\scshape`/`\textsc` select the real cmcsc10 small-caps face, `\slshape`/`\textsl` use cmsl10 (or cmbxsl10 when bold) rather than the italic cmti10, and `\ttfamily\itshape` reaches cmitt10. All twelve text programs (cmr10, cmbx10, cmti10, cmbxti10, cmtt10, cmss10, cmssbx10, cmssi10, cmcsc10, cmsl10, cmbxsl10, cmitt10) are embedded as Type1 outlines. Because Computer Modern has no bold small caps, a bold small-caps request falls back to cmbx10. Section-wide declarations such as `\bfseries` apply from where they appear to the end of their group, and `\em` toggles emphasis relative to the surrounding face. Paragraph base sizes come from `\fontsize{<size>}{<baselineskip>}` and the article size commands (`\tiny` through `\Huge`), scaling glyph metrics and the default interline glue while the measure, indentation and authored skips stay absolute. The class options `10pt`/`11pt`/`12pt` on `\documentclass[...]` set the default base size (and article `\baselineskip` of 12pt/13.6pt/14.5pt); a size command inside a paragraph still overrides the class default. The paragraph measure follows `\textwidth`/`\hsize` (or `\setlength{\textwidth}{...}`), and `\setlength` accepts the other supported layout lengths, including a `\parskip` with plus/minus glue. The `geometry` package's `margin`/`hmargin`/`vmargin`/`left`/`right`/`top`/`bottom`/`textwidth`/`textheight` options (or `\geometry{...}`) are applied to the page measure and height; other package code is not executed. Block environments `center`, `flushleft` and `flushright` set the paragraph alignment, and aligned short lines are centring/flushing within the full measure rather than at the natural line width. `\title`, `\author` and `\date` are captured from the preamble, and `\maketitle` expands into a centered title block at the article class sizes. `\section`, `\subsection` and `\subsubsection` advance article counters, prefix their titles with the number, and paint bold at `\Large`/`\large`/`\normalsize`; starred headings stay unnumbered and an optional `[short]` title is dropped. The `abstract` environment expands into a centered bold heading and a 9pt body, and `\maketitle` joins multiple `\and` authors and turns `\thanks` notes into title footnotes. Section labels resolve `\ref`/`\eqref` to the heading number (standalone `\label` lines attach to the previous heading, unknown references print `??`), `\appendix` restarts sectioning with letter numbers, and the `abstract` environment expands to its heading and body. In route-D, math uses Computer Modern metrics from the bundled `cmmi10`, `cmsy10`, `cmex10`, `msbm10` and `eufm10` TFMs and embeds their Type1 programs, so italic letters, relation symbols, big operators, `\mathcal` capitals, `\mathbb` capitals and `\mathfrak` Fraktur letters each paint with their own face. Fractions can be written as `\frac` or with TeX's infix forms (`\over`, `\atop`, `\choose`, `\brace`, `\brack`, `\overwithdelims`, `\atopwithdelims`); only `\over`-style fractions draw a bar and fractions never draw parentheses. `\left`/`\right` choose CMEX delimiter sizes from the body extent using TeX's delimiterfactor and shortfall, `\middle` delimiters are sized with their group, and `\big`/`\Big`/`\bigg`/`\Bigg` paint at their selected CMEX size. `align`/`align*` displays share column widths, right-align even columns at their relations, and number each row (with `\tag`/`\notag` per row); `gather`/`gather*` centre each row instead. Inter-atom spacing uses TeX's 3/4/5mu classes (`1mu` is `1/18em`), `\mid` is a relation and `\colon` punctuation, and `\mskip`/`\mkern`/`\mspace`/`\hskip`/`\kern`/`\hspace` scan TeX dimensions, taking the natural width of any `plus`/`minus` parts. `\footnote{...}` leaves a numbered marker in the paragraph and sets the note as a 9pt page insertion at the bottom of the page that mentions it. A `thebibliography` environment turns its `\bibitem` entries into numbered `[n]` paragraphs, and `\cite{...}` resolves to `[n]`/`[n, m]` (unknown keys print `[?]`). A `figure` environment becomes a float at its `[t]`/`[b]`/`[h]` placement, its `\caption` numbers as `Figure n:` and feeds `\ref`, and `\includegraphics[width=...]` reserves a rule placeholder. `table` environments likewise become floats carrying their captured `tabular` body, with `Table n:` captions that resolve from `\ref`. Table captions sit above the tabular as in LaTeX, figure captions below the graphic, and `\caption*` leaves either unnumbered. Theorem-like environments (`theorem`, `lemma`, `corollary`, `proposition`, `definition`, `example`, `remark`, `proof`) produce a bold numbered heading with an optional `[title]`, italic statements for the theorem group, and unnumbered proofs, with `\label`/`\ref` resolving to tLine truncated
|
||||||
|
|
||||||
|
Inline `$...$` and `\(...\)` math is laid out inside 10pt paragraphs (larger sizes keep the delimiters literal).
|
||||||
|
|
||||||
Route-D integration can also be checked against a real TeX page: `cargo test -p texrra-tex-core --test route_d` includes `route_d_page_matches_pdftex_raster_geometry`, which renders the route-D PDF and a pdfTeX Letter page at the same scaled-point geometry, rasterizes both with `pdftoppm`, and compares ink line count plus left, right, and bottom edges. It is skipped when `pdflatex`/`pdftoppm` are unavailable. `route_d_multiline_bands_track_pdftex` extends that check to a wrapped paragraph: the pixel rows that carry ink form one band per TeX line, and every band top, bottom, left edge and (except for the last line, whose width follows the break point) right edge must land within a couple of pixels of the pdfTeX page.
|
Route-D integration can also be checked against a real TeX page: `cargo test -p texrra-tex-core --test route_d` includes `route_d_page_matches_pdftex_raster_geometry`, which renders the route-D PDF and a pdfTeX Letter page at the same scaled-point geometry, rasterizes both with `pdftoppm`, and compares ink line count plus left, right, and bottom edges. It is skipped when `pdflatex`/`pdftoppm` are unavailable. `route_d_multiline_bands_track_pdftex` extends that check to a wrapped paragraph: the pixel rows that carry ink form one band per TeX line, and every band top, bottom, left edge and (except for the last line, whose width follows the break point) right edge must land within a couple of pixels of the pdfTeX page.
|
||||||
|
|
||||||
Route-D float placement mirrors TeX fractions:
|
Route-D float placement mirrors TeX fractions:
|
||||||
|
|||||||
@@ -1270,6 +1270,27 @@ impl Unit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build a paragraph unit mixing styled runs with inline formulas.
|
||||||
|
pub fn paragraph_with_style_and_segments(
|
||||||
|
id: UnitId,
|
||||||
|
source: impl Into<String>,
|
||||||
|
style: ParagraphStyle,
|
||||||
|
segments: Vec<crate::paragraph::InlineSegment>,
|
||||||
|
) -> Self {
|
||||||
|
let source = source.into();
|
||||||
|
let paragraph = crate::paragraph::layout_paragraph_segments(&segments, &style);
|
||||||
|
Self {
|
||||||
|
id,
|
||||||
|
source,
|
||||||
|
items: vec![VerticalItem::Paragraph(paragraph)],
|
||||||
|
paragraph_style: Some(style),
|
||||||
|
paragraph_runs: None,
|
||||||
|
space_before: 0,
|
||||||
|
space_stretch: 0,
|
||||||
|
space_shrink: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Build a paragraph unit with both an explicit style and font face.
|
/// Build a paragraph unit with both an explicit style and font face.
|
||||||
pub fn paragraph_with_style_and_face(
|
pub fn paragraph_with_style_and_face(
|
||||||
id: UnitId,
|
id: UnitId,
|
||||||
|
|||||||
@@ -14,6 +14,16 @@ pub struct HorizontalBox {
|
|||||||
/// Font face for this glyph when it comes from a styled run.
|
/// Font face for this glyph when it comes from a styled run.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub font: Option<ParagraphFont>,
|
pub font: Option<ParagraphFont>,
|
||||||
|
/// The inline formula this box paints, when it came from `$...$`.
|
||||||
|
#[serde(default)]
|
||||||
|
pub math: Option<Box<crate::math::MathAtom>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A run of paragraph material: text in one face or an inline formula.
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
|
pub enum InlineSegment {
|
||||||
|
Text(ParagraphFont, String),
|
||||||
|
Math(crate::math::MathAtom),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
@@ -610,6 +620,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
|
|||||||
character: Some(character),
|
character: Some(character),
|
||||||
italic: 0,
|
italic: 0,
|
||||||
font: None,
|
font: None,
|
||||||
|
math: None,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1076,6 +1087,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
|||||||
character: Some('-'),
|
character: Some('-'),
|
||||||
italic: 0,
|
italic: 0,
|
||||||
font: None,
|
font: None,
|
||||||
|
math: None,
|
||||||
}));
|
}));
|
||||||
} else if *no_break_width > 0 {
|
} else if *no_break_width > 0 {
|
||||||
line.push(HorizontalItem::Box(HorizontalBox {
|
line.push(HorizontalItem::Box(HorizontalBox {
|
||||||
@@ -1085,6 +1097,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
|||||||
character: None,
|
character: None,
|
||||||
italic: 0,
|
italic: 0,
|
||||||
font: None,
|
font: None,
|
||||||
|
math: None,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1428,6 +1441,7 @@ pub fn text_items_with_font(
|
|||||||
character: Some(character),
|
character: Some(character),
|
||||||
italic: font.italic(character),
|
italic: font.italic(character),
|
||||||
font: None,
|
font: None,
|
||||||
|
math: None,
|
||||||
}));
|
}));
|
||||||
let boundary = character_index + 1;
|
let boundary = character_index + 1;
|
||||||
// TeX's defaults for American English are \lefthyphenmin=2 and
|
// TeX's defaults for American English are \lefthyphenmin=2 and
|
||||||
@@ -1550,6 +1564,90 @@ pub fn layout_paragraph_runs(
|
|||||||
paragraph
|
paragraph
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Lay out a paragraph mixing styled text with inline formulas.
|
||||||
|
pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphStyle) -> Paragraph {
|
||||||
|
let mut items = Vec::new();
|
||||||
|
let mut previous: Option<(ParagraphFont, String)> = None;
|
||||||
|
let mut seen_content = false;
|
||||||
|
for segment in segments {
|
||||||
|
match segment {
|
||||||
|
InlineSegment::Text(font, text) => {
|
||||||
|
if text.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let starts_with_space = text.chars().next().is_some_and(char::is_whitespace);
|
||||||
|
let boundary_space = match &previous {
|
||||||
|
Some((_previous_font, previous_text)) => {
|
||||||
|
previous_text
|
||||||
|
.chars()
|
||||||
|
.next_back()
|
||||||
|
.is_some_and(char::is_whitespace)
|
||||||
|
|| starts_with_space
|
||||||
|
}
|
||||||
|
None => seen_content && starts_with_space,
|
||||||
|
};
|
||||||
|
if boundary_space {
|
||||||
|
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(
|
||||||
|
previous.as_ref().map_or(*font, |(font, _)| *font),
|
||||||
|
);
|
||||||
|
items.push(HorizontalItem::Glue {
|
||||||
|
width: metrics.space(),
|
||||||
|
stretch: metrics.stretch(),
|
||||||
|
shrink: metrics.shrink(),
|
||||||
|
order: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
|
||||||
|
let mut run = text_items_with_font(text, style, &metrics);
|
||||||
|
for item in &mut run {
|
||||||
|
if let HorizontalItem::Box(box_) = item {
|
||||||
|
box_.font = Some(*font);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
items.extend(run);
|
||||||
|
previous = Some((*font, text.clone()));
|
||||||
|
seen_content = true;
|
||||||
|
}
|
||||||
|
InlineSegment::Math(atom) => {
|
||||||
|
if let Some((previous_font, previous_text)) = &previous
|
||||||
|
&& previous_text
|
||||||
|
.chars()
|
||||||
|
.next_back()
|
||||||
|
.is_some_and(char::is_whitespace)
|
||||||
|
{
|
||||||
|
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*previous_font);
|
||||||
|
items.push(HorizontalItem::Glue {
|
||||||
|
width: metrics.space(),
|
||||||
|
stretch: metrics.stretch(),
|
||||||
|
shrink: metrics.shrink(),
|
||||||
|
order: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let math = crate::math::layout_math(atom, &crate::cmr10::Cmr10Metrics::default());
|
||||||
|
items.push(HorizontalItem::Box(HorizontalBox {
|
||||||
|
width: math.width,
|
||||||
|
height: math.height,
|
||||||
|
depth: math.depth,
|
||||||
|
character: None,
|
||||||
|
italic: 0,
|
||||||
|
font: None,
|
||||||
|
math: Some(Box::new(atom.clone())),
|
||||||
|
}));
|
||||||
|
previous = None;
|
||||||
|
seen_content = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scale_items_for_size(&mut items, style.font_size);
|
||||||
|
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||||
|
paragraph.font = match segments.first() {
|
||||||
|
Some(InlineSegment::Text(font, _)) => *font,
|
||||||
|
_ => ParagraphFont::Roman,
|
||||||
|
};
|
||||||
|
paragraph.font_size = style.font_size;
|
||||||
|
paragraph
|
||||||
|
}
|
||||||
|
|
||||||
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
|
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
|
||||||
let mut items = text_items(text, style);
|
let mut items = text_items(text, style);
|
||||||
scale_items_for_size(&mut items, style.font_size);
|
scale_items_for_size(&mut items, style.font_size);
|
||||||
@@ -1798,6 +1896,48 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inline_math_boxes_sit_between_words() {
|
||||||
|
let style = ParagraphStyle {
|
||||||
|
line_width: 300 * 65_536,
|
||||||
|
par_indent: 0,
|
||||||
|
..ParagraphStyle::default()
|
||||||
|
};
|
||||||
|
let atom = crate::math::parse_math("x^2");
|
||||||
|
let segments = vec![
|
||||||
|
InlineSegment::Text(ParagraphFont::Roman, "Value ".into()),
|
||||||
|
InlineSegment::Math(atom.clone()),
|
||||||
|
InlineSegment::Text(ParagraphFont::Roman, " tail".into()),
|
||||||
|
];
|
||||||
|
let paragraph = layout_paragraph_segments(&segments, &style);
|
||||||
|
let boxes: Vec<&HorizontalBox> = paragraph
|
||||||
|
.lines
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.filter_map(|item| match item {
|
||||||
|
HorizontalItem::Box(box_) => Some(box_),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let math = boxes
|
||||||
|
.iter()
|
||||||
|
.find(|box_| box_.math.is_some())
|
||||||
|
.expect("math box");
|
||||||
|
assert!(math.character.is_none());
|
||||||
|
assert_eq!(
|
||||||
|
math.width,
|
||||||
|
crate::math::layout_math(&atom, &crate::cmr10::Cmr10Metrics::default()).width
|
||||||
|
);
|
||||||
|
let glues = paragraph
|
||||||
|
.lines
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.filter(|item| matches!(item, HorizontalItem::Glue { .. }))
|
||||||
|
.count();
|
||||||
|
assert!(glues >= 2, "spaces on both sides of the formula");
|
||||||
|
assert!(paragraph.font_size == 655_360);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn split_at_keeps_each_part_last_line_descent() {
|
fn split_at_keeps_each_part_last_line_descent() {
|
||||||
let boxed = |character: char, depth: i64| {
|
let boxed = |character: char, depth: i64| {
|
||||||
@@ -1808,6 +1948,7 @@ mod tests {
|
|||||||
character: Some(character),
|
character: Some(character),
|
||||||
italic: 0,
|
italic: 0,
|
||||||
font: None,
|
font: None,
|
||||||
|
math: None,
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
let items = vec![
|
let items = vec![
|
||||||
|
|||||||
@@ -529,6 +529,17 @@ impl Page {
|
|||||||
for item in line {
|
for item in line {
|
||||||
match item {
|
match item {
|
||||||
HorizontalItem::Box(box_) => {
|
HorizontalItem::Box(box_) => {
|
||||||
|
if let Some(math) = &box_.math {
|
||||||
|
let math_font = Cmr10Metrics::default();
|
||||||
|
Self::paint_math(
|
||||||
|
math,
|
||||||
|
&math_font,
|
||||||
|
x,
|
||||||
|
baseline,
|
||||||
|
size,
|
||||||
|
&mut list.items,
|
||||||
|
);
|
||||||
|
}
|
||||||
if let Some(character) = box_.character {
|
if let Some(character) = box_.character {
|
||||||
list.push(DisplayItem::Text {
|
list.push(DisplayItem::Text {
|
||||||
font: box_.font.map_or(face, paragraph_face),
|
font: box_.font.map_or(face, paragraph_face),
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
|||||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||||
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
||||||
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
||||||
PDF opt-in with `?engine=route-d`). 640 workspace tests are green and no oracle
|
PDF opt-in with `?engine=route-d`). 644 workspace tests are green and no oracle
|
||||||
fixture is ignored.
|
fixture is ignored.
|
||||||
|
|
||||||
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
|
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
|
||||||
|
|||||||
+2
-2
@@ -333,7 +333,7 @@ for a later overlaid slash.
|
|||||||
| M4 math | partial | atoms, scripts, fractions (including infix `\over`/`\atop`/`\choose`), roots, delimiters, accents, operators, classes, braces, boxed, negated, math alphabets (incl. `\mathcal`, `\mathbb` and `\mathfrak`), `align`/`gather` rows with per-row tags; no math painter/shrink wrapping |
|
| M4 math | partial | atoms, scripts, fractions (including infix `\over`/`\atop`/`\choose`), roots, delimiters, accents, operators, classes, braces, boxed, negated, math alphabets (incl. `\mathcal`, `\mathbb` and `\mathfrak`), `align`/`gather` rows with per-row tags; no math painter/shrink wrapping |
|
||||||
| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; used faces embed per-document TrueType glyf subsets that keep the original glyph IDs, include composite components, and carry a matching ToUnicode CMap; ASCII documents use the compact tier; no Base-14 dependency; SVG carries family/weight/style; route-D PDFs embed Knuth's Computer Modern Type1 programs (cmr10/cmbx10/cmti10/cmbxti10/cmtt10/cmss10/cmssbx10/cmssi10/cmcsc10/cmsl10/cmbxsl10/cmitt10) as `/Subtype /Type1` fonts with `/FontFile` (`Length1/2/3`), `/Encoding /Differences` and TFM `/Widths`, falling back per face to Liberation when the program does not encode every character |
|
| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; used faces embed per-document TrueType glyf subsets that keep the original glyph IDs, include composite components, and carry a matching ToUnicode CMap; ASCII documents use the compact tier; no Base-14 dependency; SVG carries family/weight/style; route-D PDFs embed Knuth's Computer Modern Type1 programs (cmr10/cmbx10/cmti10/cmbxti10/cmtt10/cmss10/cmssbx10/cmssi10/cmcsc10/cmsl10/cmbxsl10/cmitt10) as `/Subtype /Type1` fonts with `/FontFile` (`Length1/2/3`), `/Encoding /Differences` and TFM `/Widths`, falling back per face to Liberation when the program does not encode every character |
|
||||||
| M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; route-D reads `\documentclass` size options (10/11/12pt) into the paragraph base size and `\baselineskip`; executing class or package code remains absent |
|
| M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; route-D reads `\documentclass` size options (10/11/12pt) into the paragraph base size and `\baselineskip`; executing class or package code remains absent |
|
||||||
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints plus opt-in `?engine=route-d` project preview, content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center/flushleft/flushright mapping; editor toggle in `texrra-web`; inline math pending |
|
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints plus opt-in `?engine=route-d` project preview, content-based page hashes and section/emphasis/display-math/inline-math/itemize/enumerate/description/quote/center/flushleft/flushright mapping; editor toggle in `texrra-web`; inline `$...$`/`\(...\)` math is laid out inside 10pt paragraphs, while larger paragraphs keep delimiters literal |
|
||||||
|
|
||||||
v1 remains the production renderer; the route-D core is additive and tested independently.
|
v1 remains the production renderer; the route-D core is additive and tested independently.
|
||||||
|
|
||||||
@@ -367,7 +367,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
|
|||||||
|
|
||||||
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||||
|
|
||||||
The crate currently has 402 unit tests plus eight route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 640 tests (402 route-D unit, 88 engine binary unit, 8 route-D integration, 35 oracle, 71 v1 integration, 31 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
The crate currently has 403 unit tests plus eight route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 644 tests (403 route-D unit, 91 engine binary unit, 8 route-D integration, 35 oracle, 71 v1 integration, 31 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||||
|
|
||||||
### M0 incremental demo
|
### M0 incremental demo
|
||||||
|
|
||||||
|
|||||||
+168
-10
@@ -2278,6 +2278,102 @@ fn nfss_font_with_size(
|
|||||||
/// Handles `\textbf{...}`-style wrappers, declaration groups such as
|
/// Handles `\textbf{...}`-style wrappers, declaration groups such as
|
||||||
/// `{\bfseries ...}` and arbitrary nesting of both, including braces inside a
|
/// `{\bfseries ...}` and arbitrary nesting of both, including braces inside a
|
||||||
/// group. Plain text inherits the surrounding face.
|
/// group. Plain text inherits the surrounding face.
|
||||||
|
/// Find the closing delimiter of an inline formula.
|
||||||
|
fn math_end(text: &str, closing: &str) -> Option<usize> {
|
||||||
|
if closing == "$" {
|
||||||
|
let bytes = text.as_bytes();
|
||||||
|
let mut index = 0;
|
||||||
|
while index < bytes.len() {
|
||||||
|
match bytes[index] {
|
||||||
|
b'\\' => index += 2,
|
||||||
|
b'$' => return Some(index),
|
||||||
|
_ => index += 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
text.find(closing)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Split text into literal pieces and `$...$` / `\(...\)` formulas.
|
||||||
|
fn split_inline_math(text: &str) -> Vec<(bool, String)> {
|
||||||
|
let mut pieces = Vec::new();
|
||||||
|
let mut literal = String::new();
|
||||||
|
let mut index = 0;
|
||||||
|
while index < text.len() {
|
||||||
|
let rest = &text[index..];
|
||||||
|
if rest.starts_with("$$") {
|
||||||
|
literal.push_str("$$");
|
||||||
|
index += 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let opener = if rest.starts_with("\\(") {
|
||||||
|
Some((2, "\\)"))
|
||||||
|
} else if rest.starts_with('$') {
|
||||||
|
Some((1, "$"))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
if let Some((open_len, closing)) = opener {
|
||||||
|
let after = &rest[open_len..];
|
||||||
|
if let Some(end) = math_end(after, closing) {
|
||||||
|
if !literal.is_empty() {
|
||||||
|
pieces.push((false, std::mem::take(&mut literal)));
|
||||||
|
}
|
||||||
|
pieces.push((true, after[..end].to_string()));
|
||||||
|
index += open_len + end + closing.len();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let character = rest.chars().next().expect("non-empty tail");
|
||||||
|
if rest.starts_with('\\') {
|
||||||
|
literal.push(character);
|
||||||
|
if let Some(next) = rest[character.len_utf8()..].chars().next() {
|
||||||
|
literal.push(next);
|
||||||
|
index += character.len_utf8() + next.len_utf8();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
literal.push(character);
|
||||||
|
index += character.len_utf8();
|
||||||
|
}
|
||||||
|
if !literal.is_empty() {
|
||||||
|
pieces.push((false, literal));
|
||||||
|
}
|
||||||
|
pieces
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Build mixed text/math segments when the paragraph contains inline math.
|
||||||
|
///
|
||||||
|
/// Math metrics are measured at 10pt, so sized paragraphs keep the literal
|
||||||
|
/// delimiters until per-size math layout exists.
|
||||||
|
fn inline_math_segments(
|
||||||
|
text: &str,
|
||||||
|
base: ParagraphFont,
|
||||||
|
style: &ParagraphStyle,
|
||||||
|
) -> Option<Vec<texrra_tex_core::paragraph::InlineSegment>> {
|
||||||
|
if style.font_size != 655_360 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let runs = inline_runs(text, base).unwrap_or_else(|| vec![(base, text.to_string())]);
|
||||||
|
let mut segments = Vec::new();
|
||||||
|
let mut found = false;
|
||||||
|
for (font, run_text) in runs {
|
||||||
|
for (is_math, piece) in split_inline_math(&run_text) {
|
||||||
|
if is_math {
|
||||||
|
found = true;
|
||||||
|
segments.push(texrra_tex_core::paragraph::InlineSegment::Math(parse_math(
|
||||||
|
&piece,
|
||||||
|
)));
|
||||||
|
} else if !piece.is_empty() {
|
||||||
|
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(font, piece));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
found.then_some(segments)
|
||||||
|
}
|
||||||
|
|
||||||
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
|
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
|
||||||
let mut runs = Vec::new();
|
let mut runs = Vec::new();
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
@@ -3350,19 +3446,27 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
|||||||
space_skip: geometry.space_skip,
|
space_skip: geometry.space_skip,
|
||||||
xspace_skip: geometry.xspace_skip,
|
xspace_skip: geometry.xspace_skip,
|
||||||
};
|
};
|
||||||
let mut unit = match inline_runs(¶graph.text, paragraph.font) {
|
let mut unit = match inline_math_segments(¶graph.text, paragraph.font, &style) {
|
||||||
Some(runs) => Unit::paragraph_with_style_and_runs(
|
Some(segments) => Unit::paragraph_with_style_and_segments(
|
||||||
index as u64,
|
index as u64,
|
||||||
paragraph.text.clone(),
|
paragraph.text.clone(),
|
||||||
style,
|
style.clone(),
|
||||||
runs,
|
segments,
|
||||||
),
|
|
||||||
None => Unit::paragraph_with_style_and_face(
|
|
||||||
index as u64,
|
|
||||||
paragraph.text.clone(),
|
|
||||||
style,
|
|
||||||
paragraph.font,
|
|
||||||
),
|
),
|
||||||
|
None => match inline_runs(¶graph.text, paragraph.font) {
|
||||||
|
Some(runs) => Unit::paragraph_with_style_and_runs(
|
||||||
|
index as u64,
|
||||||
|
paragraph.text.clone(),
|
||||||
|
style,
|
||||||
|
runs,
|
||||||
|
),
|
||||||
|
None => Unit::paragraph_with_style_and_face(
|
||||||
|
index as u64,
|
||||||
|
paragraph.text.clone(),
|
||||||
|
style,
|
||||||
|
paragraph.font,
|
||||||
|
),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
unit.space_before = paragraph.space_before;
|
unit.space_before = paragraph.space_before;
|
||||||
unit.space_stretch = paragraph.space_stretch;
|
unit.space_stretch = paragraph.space_stretch;
|
||||||
@@ -4241,6 +4345,60 @@ A definition.
|
|||||||
assert_eq!(document.units[3].space_stretch, 2 * SCALE);
|
assert_eq!(document.units[3].space_stretch, 2 * SCALE);
|
||||||
assert_eq!(document.units[3].space_shrink, SCALE);
|
assert_eq!(document.units[3].space_shrink, SCALE);
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn inline_math_becomes_math_boxes() {
|
||||||
|
let blocks = paragraphs("Value $x^2$ tail.").expect("paragraphs");
|
||||||
|
let document = build_document(&blocks, &PageGeometry::default());
|
||||||
|
let paragraph = match &document.units[0].items[0] {
|
||||||
|
VerticalItem::Paragraph(paragraph) => paragraph,
|
||||||
|
_ => panic!("expected a paragraph unit"),
|
||||||
|
};
|
||||||
|
let math = paragraph
|
||||||
|
.lines
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.find_map(|item| match item {
|
||||||
|
texrra_tex_core::paragraph::HorizontalItem::Box(box_) if box_.math.is_some() => {
|
||||||
|
Some(box_)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.expect("inline math box");
|
||||||
|
assert!(math.character.is_none());
|
||||||
|
assert!(math.width > 0);
|
||||||
|
assert_eq!(document.units[0].source, "Value $x^2$ tail.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inline_math_paints_italic_glyphs() {
|
||||||
|
let json = preview_json("Value $x^2$ tail.").expect("preview");
|
||||||
|
assert!(
|
||||||
|
json.contains("font-style=") && json.contains("italic"),
|
||||||
|
"math letters use the italic face"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inline_math_stays_literal_in_sized_paragraphs() {
|
||||||
|
let blocks = paragraphs("\\large Value $x$ tail.").expect("paragraphs");
|
||||||
|
let document = build_document(&blocks, &PageGeometry::default());
|
||||||
|
let paragraph = match &document.units[0].items[0] {
|
||||||
|
VerticalItem::Paragraph(paragraph) => paragraph,
|
||||||
|
_ => panic!("expected a paragraph unit"),
|
||||||
|
};
|
||||||
|
let boxes: Vec<_> = paragraph
|
||||||
|
.lines
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.filter_map(|item| match item {
|
||||||
|
texrra_tex_core::paragraph::HorizontalItem::Box(box_) => Some(box_),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
assert!(boxes.iter().all(|box_| box_.math.is_none()));
|
||||||
|
assert!(boxes.iter().any(|box_| box_.character == Some('$')));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn text_width_sets_the_paragraph_measure() {
|
fn text_width_sets_the_paragraph_measure() {
|
||||||
let long =
|
let long =
|
||||||
|
|||||||
Reference in new issue
Block a user