Honour per-class insertion page limits

This commit is contained in:
gloamxun committed 2026-10-20 23:02:30 +08:00
1 parent ff3fff343d
commit a8e7499ce3
3 files changed
+69 -7

No files matched your search

+1 -1
View File
@@ -566,7 +566,7 @@ default until the new core reaches layout parity. See `docs/TEX_CORE.md`.
The first M7 service integration is available as `POST /api/route-d/preview`, with PDF export at `POST /api/route-d/pdf`.
It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph`/display-math/list subset, lays it out with the route-D page
builder, and returns the v1-compatible preview shape plus per-page hashes and
SVG. Route-D paragraphs are laid out with CMR10 advances, kerns and ligatures, and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. The v1 `/preview` and project APIs are unchanged and remain the default.
SVG. Route-D paragraphs are laid out with CMR10 advances, kerns and ligatures, and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. `Document::insertion_classes` caps each insertion class to a page fraction and defers overflow to the next page. The v1 `/preview` and project APIs are unchanged and remain the default.
`POST /api/route-d/edit/{unit}` applies one paragraph edit to the last route-D
document and returns the same page list plus `changedPages` and
+64 -5
View File
@@ -512,6 +512,14 @@ impl Unit {
}
}
/// A TeX insertion class (`\insert` class) and its maximum share of the
/// page. A value of 100 means the insertion may use the whole page.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct InsertionClass {
pub number: u8,
pub fraction_percent: u8,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Document {
pub units: Vec<Unit>,
@@ -525,6 +533,9 @@ pub struct Document {
pub text_fraction_percent: u8,
/// `\floatpagefraction` equivalent for dedicated float pages.
pub float_page_fraction_percent: u8,
/// Per-class insertion limits. Missing classes use the full page.
#[serde(default)]
pub insertion_classes: Vec<InsertionClass>,
}
fn default_text_fraction() -> u8 {
@@ -539,9 +550,19 @@ impl Document {
top_skip: 0,
text_fraction_percent: default_text_fraction(),
float_page_fraction_percent: 50,
insertion_classes: Vec::new(),
}
}
/// Maximum height available to one insertion class on a page.
pub fn insertion_class_limit(&self, class: u8) -> i64 {
self.insertion_classes
.iter()
.find(|entry| entry.number == class)
.map_or(self.page_goal, |entry| {
self.page_goal * i64::from(entry.fraction_percent) / 100
})
}
pub fn with_top_skip(mut self, top_skip: i64) -> Self {
self.top_skip = top_skip;
self
@@ -832,9 +853,21 @@ impl Engine {
}
}
VerticalItem::Insertion(insertion) => {
let page_capacity = document.page_goal.saturating_sub(document.top_skip);
if insertion.height > page_capacity && !insertion.items.is_empty() {
if !current.items.is_empty() {
let class_limit = document
.insertion_class_limit(insertion.class)
.min(document.page_goal.saturating_sub(document.top_skip))
.max(1);
let class_height: i64 = current
.insertions
.iter()
.filter(|entry| entry.class == insertion.class)
.map(|entry| entry.height)
.sum();
if (class_height + insertion.height > class_limit
|| insertion.height > class_limit)
&& !insertion.items.is_empty()
{
if !current.items.is_empty() || !current.insertions.is_empty() {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
@@ -843,7 +876,7 @@ impl Engine {
let mut chunk_height = 0;
for sub_item in &insertion.items {
let sub_height = item_height(sub_item);
if chunk_height + sub_height > page_capacity && !chunk.is_empty() {
if chunk_height + sub_height > class_limit && !chunk.is_empty() {
current.insertions.push(Insertion {
class: insertion.class,
height: chunk_height,
@@ -867,7 +900,10 @@ impl Engine {
}
} else {
let goal = document.page_goal - insertion_height;
if !current.items.is_empty() && used + insertion.height > goal {
if (!current.items.is_empty() || !current.insertions.is_empty())
&& (used + insertion.height > goal
|| class_height + insertion.height > class_limit)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
@@ -1503,6 +1539,29 @@ mod tests {
assert_eq!(Engine::layout(&document).pages().len(), 1);
}
#[test]
fn insertion_class_limit_ships_the_overflow_to_the_next_page() {
let rule = VerticalItem::Rule {
width: 65_536,
height: 200,
};
let mut document = Document::new(
1000,
vec![
Unit::insertion(1, 1, 200, vec![rule.clone()]),
Unit::insertion(2, 1, 200, vec![rule]),
],
);
document.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 30,
}];
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
assert_eq!(layout.pages()[0].insertions.len(), 1);
assert_eq!(layout.pages()[1].insertions.len(), 1);
}
#[test]
fn a_large_top_float_gets_a_float_page() {
let mut document = Document::new(100, Vec::new());
+4 -1
View File
@@ -167,6 +167,9 @@ a single cost function.
- `Unit::vertical` and `Unit::insertion` build arbitrary vertical material and
reserved-height page insertions; insertion height is deducted from the
usable page goal and the material is attached to `Page::insertions`.
`Document::insertion_classes` sets a per-class page fraction; an insertion
that would exceed the class limit ships the page and continues on the next
one, splitting at item boundaries when the insertion itself is too tall.
Insertions taller than a page are split at item boundaries across pages.
- Page breaking handles vertical boxes/glue, `Document::top_skip` and forced
vertical penalties (`penalty <= -10000`); positive penalties are recorded
@@ -264,7 +267,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 141 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 236 tests (141 route-D unit, 1 route-D integration, 71 v1 integration, 18 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
The crate currently has 142 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 237 tests (142 route-D unit, 1 route-D integration, 71 v1 integration, 18 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo