diff --git a/README.md b/README.md index 74d9aa3..04ac173 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,10 @@ and nested boxes and inline rules are preserved. Boxes that do not fit on the current line overflow like an overfull TeX hbox rather than wrapping. `\makebox[width][l|c|r]{...}` fixes the box width and aligns its content, `\raisebox{lift}{...}` lifts inline content by a dimension, and `\fbox{...}` -draws a framed inline box with 3pt padding and a 0.4pt rule. +draws a framed inline box with 3pt padding and a 0.4pt rule. The +`\begin{minipage}[t]{width}...\end{minipage}` environment lays its content out +as a width-constrained block; `\vbox{...}` and `\vtop{...}` are accepted as +full-text-width blocks. `\checkmark`, `\textdagger`, `\textdaggerdbl`, `\textperiodcentered`, `\textsection`, `\textparagraph`, `\textbrokenbar`, and `\textvisiblespace` @@ -293,8 +296,9 @@ Known gaps for full document classes: base font-size class options (`10pt`/`11pt`/`12pt`) are parsed but the engine still uses 11pt; footnotes and `\thanks` are dropped, custom `\maketitle` implementations are ignored (the engine title layout is -used), `\minipage` is not implemented yet, `\parbox` is block-level rather than -inline, and the abstract prints as body text without an "Abstract" heading. +used), `\parbox`/`\minipage` are block-level rather than inline, `\vbox` and +`\vtop` map to a full-width block box rather than true nested VBox material, and +the abstract prints as body text without an "Abstract" heading. Basic `\input{file}` imports are read relative to the main `.tex` file. Imported files participate in source mapping and frontend cache invalidation. Local style @@ -454,7 +458,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`, | Area | Implemented | Remaining | | --- | --- | --- | -| Boxes | `\rule`, inline framed `\fbox`, block `\parbox`, inline `\mbox`/`\makebox` (fixed width and alignment), `\raisebox`, `center`, `\hrule` | `\minipage`, VBox | +| Boxes | `\rule`, inline framed `\fbox`, block `\parbox`/`\minipage`, `\vbox`/`\vtop` (as full-width block), inline `\mbox`/`\makebox`, `\raisebox`, `center`, `\hrule` | true nested VBox material | | Fonts | serif/sans/mono faces, bold/italic/mono/sans declarations, group scoping, true scaled small caps | — | | Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, centering, `\parindent` (17pt default), `\hfill`/`\hfil` | `\raggedright`/`\raggedleft` | | Notes/floats | — | footnote insertion area, figure/table floats | diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index 9271f01..415059f 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -375,6 +375,11 @@ fn consume_environment_preamble( start_index } +struct MinipageBuilder { + runs: Vec, + width: Option, +} + struct InlineBoxSpec { runs: Vec, width: Option, @@ -414,6 +419,7 @@ struct Parser { box_frame: bool, box_width: Option, inline_boxes: Vec, + minipage: Option, centered: Option>, current_source: Option, paragraph_source: Option, @@ -454,6 +460,7 @@ impl Parser { box_frame: true, box_width: None, inline_boxes: Vec::new(), + minipage: None, centered: None, current_source: None, paragraph_source: None, @@ -574,6 +581,7 @@ impl Parser { self.append_text(&pending_text); } self.finish_heading(); + self.finish_minipage(); self.flush_paragraph(); if let Some(candidate) = rich_candidate && candidate.end_index == expanded.items.len() @@ -604,6 +612,7 @@ impl Parser { && self.framed_box.is_none() && self.centered.is_none() && self.inline_boxes.is_empty() + && self.minipage.is_none() && !self.bold && !self.italic && !self.monospace @@ -918,6 +927,13 @@ impl Parser { if self.table.is_some() { self.set_table_alignments(argument); } + if let Some(minipage) = &mut self.minipage + && minipage.width.is_none() + { + minipage.width = parse_dimension(argument.trim()).map(|width_mm| { + common::Scaled((width_mm / 25.4 * 72.0_f64 * 65_536.0).round() as i32) + }); + } } expanded::FrontendEventKind::BeginEnvironment { name: environment, .. @@ -1025,6 +1041,24 @@ impl Parser { }); } + fn finish_minipage(&mut self) { + let Some(mut minipage) = self.minipage.take() else { + return; + }; + trim_run_end(&mut minipage.runs); + if minipage.runs.is_empty() { + return; + } + let width = minipage + .width + .unwrap_or_else(|| common::Scaled((160.0 / 25.4 * 72.0_f64 * 65_536.0).round() as i32)); + self.push_block(Block::FramedBox { + runs: minipage.runs, + frame: false, + width: Some(width), + }); + } + fn finish_heading(&mut self) { if let Some(mut heading) = self.heading.take() { trim_run_end(&mut heading.runs); @@ -1255,6 +1289,7 @@ impl Parser { fn target_runs(&mut self) -> &mut Vec { let paragraph_target = self.inline_boxes.is_empty() + && self.minipage.is_none() && self.centered.is_none() && self.framed_box.is_none() && self.metadata_capture.is_none() @@ -1271,6 +1306,8 @@ impl Parser { } if let Some(spec) = self.inline_boxes.last_mut() { &mut spec.runs + } else if let Some(minipage) = &mut self.minipage { + &mut minipage.runs } else if let Some(runs) = &mut self.centered { runs } else if let Some(runs) = &mut self.framed_box { @@ -1370,7 +1407,9 @@ impl Parser { } fn paragraph_break(&mut self) { - if !self.lists.is_empty() || self.table.is_some() { + if self.minipage.is_some() { + self.append_text(" "); + } else if !self.lists.is_empty() || self.table.is_some() { self.append_text(" "); } else { self.flush_paragraph(); @@ -1476,6 +1515,14 @@ impl Parser { self.push_block(Block::Centered { runs }); } } + (true, "minipage") => { + self.flush_paragraph(); + self.minipage = Some(MinipageBuilder { + runs: Vec::new(), + width: None, + }); + } + (false, "minipage") => self.finish_minipage(), (true, "thebibliography") => { self.flush_paragraph(); self.bibliography = Some(BibliographyBuilder::default()); diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 285ecae..238054f 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -4,15 +4,11 @@ This file lists the concrete implementation plans for the foundational items tha are still missing. Each item names the files/functions to touch. Completed: paragraph indentation (`\parindent`) and `\hfill`/`\hfil`; sans -family and true scaled small caps (see `docs/STATUS.md`). +family and true scaled small caps; inline box geometry (`\fbox`, `\makebox`, +`\raisebox`), `\minipage`, and `\vbox`/`\vtop` block approximations (see +`docs/STATUS.md`). -## 1. `\minipage` and VBox - -- Reuse the block `FramedBox` measurement with an explicit width and vertical - stacking; add `\begin{minipage}{width}` environment parsing. -- Add VBox material for `\vbox`/`\vtop` and nested vertical lists. - -## 2. Footnotes +## 1. Footnotes - Parser: capture `\footnote{...}`/`\thanks{...}` into a per-paragraph list with a number marker run (superscript approximation via smaller raised text @@ -21,7 +17,7 @@ family and true scaled small caps (see `docs/STATUS.md`). text height and place footnotes with a separator rule at the bottom. - Paint footnotes as normal positioned lines after the page's content. -## 3. Floats +## 2. Floats - Classify figure/table blocks as floats with `[htbp]` placement hints. - Planner: maintain pending floats and try top/bottom/page placements; keep @@ -29,13 +25,13 @@ family and true scaled small caps (see `docs/STATUS.md`). - Renderer: place deferred floats at the saved slots; page keys must include float placement decisions. -## 4. Page styles / headers-footers +## 3. Page styles / headers-footers - `DocumentModel` fields for header/footer runs and `\pagestyle`/`\thispagestyle`. - Paint headers/footers in the page margins in both PDF and SVG; page numbers need a page index placeholder resolved at paint time. -## 5. Unified pagination +## 4. Unified pagination - Extract the current duplicated planner (`build_page_break_plan`) and renderer (`build_positioned_pages`) vertical logic into one page builder returning an diff --git a/docs/STATUS.md b/docs/STATUS.md index 29b8f29..6c8907a 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -13,8 +13,9 @@ the source changes; the SVG preview is refreshed per page. - Headings: numbered `\section`/`\subsection`/`\subsubsection` and starred `\section*` variants (starred headings do not advance the counter). - Box material: `\rule` (raise/width/height), inline framed `\fbox`, block - `\parbox`, inline `\mbox`, `\makebox[width][align]`, `\raisebox`, `center`, - `\hrule` with `width`/`height`. + `\parbox`/`\minipage`, `\vbox`/`\vtop` (full-width block approximation), + inline `\mbox`, `\makebox[width][align]`, `\raisebox`, `center`, `\hrule` + with `width`/`height`. - Fonts: Liberation Serif/Sans/Mono faces, bold/italic/monospace/sans declarations with group scoping, grouped text-style commands, true small caps (lowercase uppercase glyphs at 80% size), and sans/serif/mono small caps. @@ -37,7 +38,8 @@ See `docs/ROADMAP.md` for concrete implementation plans. ## Known approximations / gaps -- Boxes: `\parbox` is block-level; `\minipage` and VBox material are missing. +- Boxes: `\parbox`/`\minipage` are block-level; `\vbox`/`\vtop` map to a + full-width block rather than true nested VBox material. - Pages: no footnote insertion area, no floats, no headers/footers (fancyhdr commands are accepted as no-ops), planner and renderer are separate. - Lengths: `\textwidth`/`\textheight`/`\linewidth`/`\columnwidth` are fixed @@ -50,7 +52,7 @@ See `docs/ROADMAP.md` for concrete implementation plans. - `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to one A4 page each; the arXiv template project compiles to 3 Letter pages. -- `cargo fmt --check` passes; `cargo test --workspace` (20 tests) covers the three +- `cargo fmt --check` passes; `cargo test --workspace` (21 tests) covers the three examples, the arXiv fixture, SVG/PDF output, project API, static frontend assets, path traversal, the legacy `/texrra` endpoint, and page-hash cache stability. diff --git a/src/rust_typeset/frontend.rs b/src/rust_typeset/frontend.rs index c3ef94b..e81b653 100644 --- a/src/rust_typeset/frontend.rs +++ b/src/rust_typeset/frontend.rs @@ -26,6 +26,8 @@ const PRELUDE: &str = r#" \def\maketitle{\texrramaketitle} \def\tableofcontents{} \def\noindent{\texrranoindent} +\def\vbox#1{\parbox{\textwidth}{#1}} +\def\vtop#1{\parbox{\textwidth}{#1}} \def\hfill{\hskip0pt plus 1fil\relax} \def\hfil{\hskip0pt plus 1fil\relax} \def\allowbreak{\penalty0} diff --git a/src/rust_typeset/texlang_adapter.rs b/src/rust_typeset/texlang_adapter.rs index 0d8bd8a..7f5c229 100644 --- a/src/rust_typeset/texlang_adapter.rs +++ b/src/rust_typeset/texlang_adapter.rs @@ -5513,6 +5513,16 @@ fn begin_environment_primitive( .filter_map(|token| token.char()) .collect::(), ) + } else if name == "minipage" { + // \begin{minipage}[t]{50mm}: skip the optional position and return + // the mandatory width argument. + let _ = read_optional_bracketed_argument(input, token, "minipage position")?; + Some( + read_scanned_token_list(input)? + .into_iter() + .filter_map(|token| token.char()) + .collect::(), + ) } else { None }; diff --git a/tests/typeset.rs b/tests/typeset.rs index 694d85f..e0b5814 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -426,3 +426,38 @@ A\fbox{B}C "text around fbox missing" ); } + +#[test] +fn minipage_wraps_to_requested_width_and_vbox_wraps() { + let source = r#"\documentclass{article} +\begin{document} +\begin{minipage}[t]{50mm} +This is a minipage with enough text to wrap across several lines and demonstrate that it is block-wide. +\end{minipage} + +\vbox{A vbox wraps at text width with \textbf{bold} content.} +\end{document} +"#; + let value = fresh_preview(source, "minipage.tex"); + assert_eq!(value["status"], "ok", "{}", value["message"]); + let svg = page_svg(&value, 0); + assert!(svg.contains(">minipage"), "minipage content missing"); + assert!( + svg.contains(">block-wide."), + "wrapped minipage tail missing" + ); + let first = text_y(&svg, "This"); + let wrapped = text_y(&svg, "text"); + assert!( + wrapped > first + 3.0, + "minipage did not wrap: {first} -> {wrapped}" + ); + for word in ["This", "text", "block-wide."] { + assert!( + text_x(&svg, word) < 50.0, + "minipage content escaped its 50mm width: {word}" + ); + } + assert!(svg.contains(">vbox"), "vbox content missing"); + assert!(svg.contains(">bold"), "vbox styled content missing"); +}