Center route-D paragraphs and preview center blocks

This commit is contained in:
gloamxun committed 2026-10-20 22:12:39 +08:00
1 parent 611e6fec96
commit 472f778e75
5 files changed
+56 -3

No files matched your search

+8
View File
@@ -42,6 +42,9 @@ pub struct ParagraphStyle {
pub left_skip: i64,
/// Right skip reserved at the end of every line (`\rightskip`).
pub right_skip: i64,
/// Center each line in the measure instead of justifying it.
#[serde(default)]
pub centered: bool,
pub par_indent: i64,
pub line_height: i64,
pub char_width: i64,
@@ -56,6 +59,7 @@ impl Default for ParagraphStyle {
line_width: 400,
left_skip: 0,
right_skip: 0,
centered: false,
par_indent: 0,
line_height: 12,
char_width: 10,
@@ -91,6 +95,8 @@ pub struct Paragraph {
pub font: ParagraphFont,
#[serde(default)]
pub left_skip: i64,
#[serde(default)]
pub centered: bool,
}
impl Paragraph {
@@ -150,6 +156,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
width: 0,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
centered: style.centered,
};
}
let mut breakpoints = vec![0_usize];
@@ -326,6 +333,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
lines,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
centered: style.centered,
}
}
+14 -1
View File
@@ -378,7 +378,20 @@ impl Page {
let size = ((line_height as f64) * 0.7) as i64;
for line in &paragraph.lines {
let baseline = y + ((line_height as f64) * 0.8) as i64;
let mut x = paragraph.left_skip;
let natural: i64 = line
.iter()
.map(|item| match item {
HorizontalItem::Box(box_) => box_.width,
HorizontalItem::Glue { width, .. } => *width,
HorizontalItem::Penalty { .. }
| HorizontalItem::Discretionary { .. } => 0,
})
.sum();
let mut x = if paragraph.centered {
paragraph.left_skip + ((paragraph.width - natural) / 2).max(0)
} else {
paragraph.left_skip
};
for item in line {
match item {
HorizontalItem::Box(box_) => {
+2 -2
View File
@@ -234,7 +234,7 @@ for a later overlaid slash.
| M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping |
| M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; all five text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and `/W` glyph widths; no Base-14 dependency; SVG carries family/weight/style |
| M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent |
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints with content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote mapping; editor integration and inline math pending |
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints with content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center mapping; editor integration and inline math pending |
v1 remains the production renderer; the route-D core is additive and tested independently.
@@ -264,7 +264,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
The crate currently has 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 231 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 16 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
The crate currently has 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 232 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 17 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo
+8
View File
@@ -78,6 +78,7 @@ pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
#[derive(Clone)]
struct SourceParagraph {
text: String,
centered: bool,
font: ParagraphFont,
line_height: i64,
math: bool,
@@ -130,6 +131,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
line_height: 12,
math: false,
left_skip: 12,
centered: false,
}
})
.collect();
@@ -149,6 +151,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
line_height: 12,
math: false,
left_skip: 12,
centered: false,
}];
}
}
@@ -177,6 +180,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
line_height: 12,
math: false,
left_skip: 12,
centered: false,
}
})
.collect();
@@ -205,6 +209,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
line_height: 12,
math: true,
left_skip: 0,
centered: false,
};
}
let candidates = [
@@ -220,6 +225,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
line_height,
math: false,
left_skip: 0,
centered: false,
};
}
}
@@ -229,6 +235,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
line_height: 12,
math: false,
left_skip: 0,
centered: false,
}
}
@@ -247,6 +254,7 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
let style = ParagraphStyle {
line_width: 480 * SCALE,
left_skip: paragraph.left_skip * SCALE,
centered: paragraph.centered,
right_skip: 0,
par_indent: 0,
line_height: paragraph.line_height * SCALE,
+24
View File
@@ -530,6 +530,30 @@ fn edits_route_d_list_units() {
assert!(changed.contains(&0), "{changed:?}");
}
#[test]
fn route_d_renders_center_blocks() {
let server = TestServer::start("route-d-center");
let response = server.request(
"POST",
"/api/route-d/preview",
b"\\begin{center}Centered\\end{center}",
"text/plain",
);
assert_eq!(response.status, 200, "{}", response.text());
let value: Value = serde_json::from_slice(&response.body).expect("route-D JSON");
let svg = value["pages"][0]["svg"].as_str().unwrap();
let index = svg.find(">C</text>").expect("centered C");
let prefix = &svg[..index];
let start = prefix.rfind("<text x=\"").expect("text x");
let x: f64 = prefix[start + 9..]
.split('"')
.next()
.unwrap()
.parse()
.unwrap();
assert!(x > 50.0, "{x}");
}
#[test]
fn edits_route_d_preview_pages() {
let server = TestServer::start("route-d-edit");