Align centered and flushed blocks within the measure
This commit is contained in:
1 parent
2783dfb284
commit
925c916ea5
5 files changed
+104
-5
No files matched your search
@@ -467,7 +467,7 @@ from the font's MATH assembly parts. `\\left...\\right` target sizing follows
|
||||
the assignable TeX parameters `\\delimiterfactor` (default 901) and
|
||||
`\\delimitershortfall` (default 5pt), including local and global assignments.
|
||||
|
||||
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. 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.
|
||||
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. 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.
|
||||
|
||||
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.
|
||||
|
||||
|
||||
@@ -1058,6 +1058,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
ranges.reverse();
|
||||
let mut lines = Vec::with_capacity(ranges.len());
|
||||
let mut max_width = 0;
|
||||
let mut measure = 0;
|
||||
let line_count = ranges.len();
|
||||
for (line_index, (start, end)) in ranges.into_iter().enumerate() {
|
||||
let mut line: Vec<HorizontalItem> = Vec::new();
|
||||
@@ -1100,6 +1101,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
// difference between the natural width and the measure across the
|
||||
// glue, so the shipped line (and therefore the PDF) is flush.
|
||||
let target = style.line_target(line_index);
|
||||
measure = measure.max(target);
|
||||
let (mut natural, mut stretch, mut shrink) = (0_i64, 0_i64, 0_i64);
|
||||
let mut stretch_by_order = [0_i64; 4];
|
||||
for item in &line {
|
||||
@@ -1248,7 +1250,11 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
// The page builder still budgets paragraphs on the line grid; the
|
||||
// baseline table below carries TeX's interline glue for painting.
|
||||
height: lines.len() as i64 * style.line_height,
|
||||
width: max_width,
|
||||
width: if style.centered || style.right_aligned {
|
||||
max_width.max(measure)
|
||||
} else {
|
||||
max_width
|
||||
},
|
||||
depth,
|
||||
lines,
|
||||
font: ParagraphFont::Roman,
|
||||
@@ -1595,6 +1601,45 @@ mod tests {
|
||||
assert_eq!(glyphs(&ten), glyphs(&twelve));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aligned_paragraphs_keep_the_measure_width() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let base = ParagraphStyle {
|
||||
line_width: 300 * 65_536,
|
||||
par_indent: 0,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let centered = layout_paragraph_with_font(
|
||||
"short",
|
||||
&ParagraphStyle {
|
||||
centered: true,
|
||||
..base.clone()
|
||||
},
|
||||
&font,
|
||||
);
|
||||
let right = layout_paragraph_with_font(
|
||||
"short",
|
||||
&ParagraphStyle {
|
||||
right_aligned: true,
|
||||
..base.clone()
|
||||
},
|
||||
&font,
|
||||
);
|
||||
assert_eq!(centered.width, 300 * 65_536);
|
||||
assert_eq!(right.width, 300 * 65_536);
|
||||
assert!(
|
||||
centered.lines[0]
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
HorizontalItem::Box(box_) => box_.width,
|
||||
HorizontalItem::Glue { width, .. } => *width,
|
||||
_ => 0,
|
||||
})
|
||||
.sum::<i64>()
|
||||
< centered.width
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overfull_lines_do_not_exceed_the_declared_shrink() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
|
||||
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||
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
|
||||
PDF opt-in with `?engine=route-d`). 600 workspace tests are green and no oracle
|
||||
PDF opt-in with `?engine=route-d`). 603 workspace tests are green and no oracle
|
||||
fixture is ignored.
|
||||
|
||||
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 |
|
||||
| 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 |
|
||||
| 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/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/itemize/enumerate/description/quote/center/flushleft/flushright mapping; editor toggle in `texrra-web`; inline math pending |
|
||||
|
||||
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.
|
||||
|
||||
The crate currently has 401 unit tests plus eight route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 600 tests (401 route-D unit, 53 engine binary unit, 8 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||
The crate currently has 401 unit tests plus eight route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 603 tests (402 route-D unit, 55 engine binary unit, 8 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||
|
||||
### M0 incremental demo
|
||||
|
||||
|
||||
@@ -1026,6 +1026,28 @@ fn take_labels(text: &mut String) -> Vec<String> {
|
||||
}
|
||||
|
||||
fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
for (environment, centered, right_aligned) in [
|
||||
("center", true, false),
|
||||
("flushleft", false, false),
|
||||
("flushright", false, true),
|
||||
] {
|
||||
let begin = format!("\\begin{{{environment}}}");
|
||||
let finish = format!("\\end{{{environment}}}");
|
||||
let Some(body) = raw
|
||||
.strip_prefix(&begin)
|
||||
.and_then(|text| text.strip_suffix(&finish))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let body = body.trim();
|
||||
if body.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut block = parse_paragraph(body);
|
||||
block.centered = centered;
|
||||
block.right_aligned = right_aligned;
|
||||
return vec![block];
|
||||
}
|
||||
for (environment, numbered) in [("itemize", false), ("enumerate", true)] {
|
||||
let begin = format!("\\begin{{{environment}}}");
|
||||
let finish = format!("\\end{{{environment}}}");
|
||||
@@ -2406,6 +2428,38 @@ mod source_tests {
|
||||
assert_eq!(glue.par_skip_shrink, Some(SCALE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alignment_environments_set_the_block_alignment() {
|
||||
let blocks = paragraphs("\\begin{center}\nCentered\n\\end{center}").expect("blocks");
|
||||
assert!(blocks[0].centered);
|
||||
assert!(!blocks[0].right_aligned);
|
||||
let blocks = paragraphs("\\begin{flushright}\nRight\n\\end{flushright}").expect("blocks");
|
||||
assert!(blocks[0].right_aligned);
|
||||
assert!(!blocks[0].centered);
|
||||
let blocks = paragraphs("\\begin{flushleft}\nLeft\n\\end{flushleft}").expect("blocks");
|
||||
assert!(!blocks[0].centered);
|
||||
assert!(!blocks[0].right_aligned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn centered_blocks_paint_centred_lines() {
|
||||
let wide = "\\begin{center}\nshort\n\\end{center}";
|
||||
let blocks = paragraphs(wide).expect("blocks");
|
||||
let document = build_document(&blocks, &PageGeometry::default());
|
||||
let engine = texrra_tex_core::Engine::layout_paged(&document).expect("layout");
|
||||
let page = &engine.pages()[0];
|
||||
let x = page
|
||||
.display_list()
|
||||
.items
|
||||
.into_iter()
|
||||
.find_map(|item| match item {
|
||||
texrra_tex_core::DisplayItem::Text { x, text, .. } if text == "s" => Some(x),
|
||||
_ => None,
|
||||
})
|
||||
.expect("text");
|
||||
assert!(x > 0, "centred text starts at {x}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn geometry_package_sets_margins() {
|
||||
let (_, geometry) = geometry_and_body("\\usepackage[margin=1in]{geometry}\n\nA paragraph.");
|
||||
|
||||
Reference in new issue
Block a user