Track textwidth and textheight from page geometry
This commit is contained in:
1 parent
ed1da06685
commit
269433abdb
7 files changed
+153
-28
No files matched your search
@@ -165,6 +165,12 @@ options/`\geometry{...}` settings, including `paperwidth`, `paperheight`,
|
||||
`textheight` sizes it below the current top margin. The chosen page size drives
|
||||
the PDF pages, the positioned layout, and the SVG preview.
|
||||
|
||||
`\textwidth`, `\textheight`, `\linewidth`, and `\columnwidth` are live named
|
||||
length registers: they start at the A4 text block and are updated by
|
||||
`\documentclass` paper options, `\geometry`, and `\newgeometry`, so
|
||||
`\parbox{\textwidth}`, `\rule{\textwidth}{...}`, and
|
||||
`\includegraphics[width=0.5\textwidth]` all follow the current page setup.
|
||||
|
||||
Page styles are painted in both the PDF and the SVG preview:
|
||||
`\pagestyle{empty|plain|fancy}` sets the document default,
|
||||
`\thispagestyle{...}` overrides one page, and fancyhdr's `\fancyhead`/
|
||||
|
||||
@@ -2071,7 +2071,15 @@ fn normalize_whitespace(text: &str) -> String {
|
||||
}
|
||||
|
||||
fn apply_geometry(document: &mut DocumentModel, settings: &str) {
|
||||
let page = &mut document.page;
|
||||
apply_geometry_settings(&mut document.page, &mut document.margins, settings);
|
||||
}
|
||||
|
||||
/// Apply comma-separated `geometry` settings to a page size and margins.
|
||||
///
|
||||
/// This is shared by the document parser and by the frontend's live length
|
||||
/// tracker, so `\textwidth`/`\textheight` expanded during macro processing
|
||||
/// match the layout that the parser will produce.
|
||||
pub fn apply_geometry_settings(page: &mut PageSize, margins: &mut PageMargins, settings: &str) {
|
||||
// Paper size first: explicit margins in the same list must be measured
|
||||
// against the final page dimensions.
|
||||
for setting in settings.split(',') {
|
||||
@@ -2105,7 +2113,6 @@ fn apply_geometry(document: &mut DocumentModel, settings: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
let margins = &mut document.margins;
|
||||
for setting in settings.split(',') {
|
||||
let Some((key, value)) = setting.split_once('=') else {
|
||||
continue;
|
||||
@@ -2140,9 +2147,8 @@ fn apply_geometry(document: &mut DocumentModel, settings: &str) {
|
||||
|
||||
// Text-block dimensions are resolved after explicit margins so that top
|
||||
// influences the computed bottom margin in the same settings list.
|
||||
let page_width = document.page.width_mm;
|
||||
let page_height = document.page.height_mm;
|
||||
let margins = &mut document.margins;
|
||||
let page_width = page.width_mm;
|
||||
let page_height = page.height_mm;
|
||||
for setting in settings.split(',') {
|
||||
let Some((key, value)) = setting.split_once('=') else {
|
||||
continue;
|
||||
|
||||
+4
-6
@@ -6,8 +6,9 @@ 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; inline box geometry (`\fbox`, `\makebox`,
|
||||
`\raisebox`), `\minipage`, `\vbox`/`\vtop` block approximations;
|
||||
footnotes/`\thanks`; figure/table `[htbp]` placement approximations; and page
|
||||
styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`).
|
||||
footnotes/`\thanks`; figure/table `[htbp]` placement approximations; page
|
||||
styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`);
|
||||
and geometry-aware `\textwidth`/`\textheight` named length registers.
|
||||
|
||||
## 1. Unified pagination
|
||||
|
||||
@@ -16,11 +17,8 @@ styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`).
|
||||
ordered list of placed items, then use it for both page-break decisions and
|
||||
final painting. This is the prerequisite for a true deferred float queue.
|
||||
|
||||
## 2. Lengths and base font size
|
||||
## 2. Base font size
|
||||
|
||||
- Emit geometry changes into named length registers so `\textwidth`,
|
||||
`\textheight`, `\linewidth`, and `\columnwidth` track `\geometry` and
|
||||
paper-size options instead of fixed A4 defaults.
|
||||
- Thread the `10pt`/`11pt`/`12pt` class option through the parser, line metrics,
|
||||
headings, and the PDF so the base size changes consistently.
|
||||
|
||||
|
||||
+3
-4
@@ -23,7 +23,8 @@ the source changes; the SVG preview is refreshed per page.
|
||||
centering, first-line `\parindent` (17pt default, group-scoped, `\setlength`/
|
||||
`\addtolength`/`\noindent` aware), and `\hfill`/`\hfil` infinite glue.
|
||||
- Pages: A4/A5/B5/Letter/Legal/Executive, landscape, geometry margins and
|
||||
text block, automatic pagination, `\newpage`/`\pagebreak`; figures/tables
|
||||
text block, geometry-aware `\textwidth`/`\textheight`/`\linewidth`/
|
||||
`\columnwidth` (as live named length registers), automatic pagination, `\newpage`/`\pagebreak`; figures/tables
|
||||
honor `[htbp]` hints with top/bottom/page/here approximations. Page styles:
|
||||
default `plain` page numbers, `\pagestyle{empty|plain|fancy}`,
|
||||
`\thispagestyle`, fancyhdr `\fancyhead`/`\fancyfoot` (L/C/R) and
|
||||
@@ -53,8 +54,6 @@ See `docs/ROADMAP.md` for concrete implementation plans.
|
||||
- Notes: footnotes inside captions, floats, or arbitrary boxes are not
|
||||
collected; the insertion area currently follows the page where the paragraph
|
||||
ends rather than the exact reference line.
|
||||
- Lengths: `\textwidth`/`\textheight`/`\linewidth`/`\columnwidth` are fixed
|
||||
A4-content defaults, not geometry-aware.
|
||||
- Miscellaneous: `\tableofcontents`/list commands are no-ops; base font-size
|
||||
class options (10/11/12pt) are parsed but unused; rules are painted with 1x1
|
||||
images in PDF; a document-level `\renewcommand{\maketitle}` is ignored in
|
||||
@@ -64,7 +63,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 --offline` (26 tests: 22 compiler
|
||||
- `cargo fmt --check` passes; `cargo test --offline` (28 tests: 24 compiler
|
||||
integration tests + 4 server tests) covers the three examples, the arXiv
|
||||
fixture, floats/newpage, page styles and `\thispagestyle`, SVG/PDF output,
|
||||
project API, static frontend assets, path traversal, the legacy
|
||||
|
||||
@@ -327,8 +327,10 @@ const PRELUDE: &str = r#"
|
||||
\def\medskip{\vskip6pt}
|
||||
\def\smallskip{\vskip3pt}
|
||||
\def\bigskip{\vskip12pt}
|
||||
\def\textwidth{160mm}
|
||||
\def\textheight{247mm}
|
||||
\skipdef\textwidth=126
|
||||
\textwidth=160mm\relax
|
||||
\skipdef\textheight=127
|
||||
\textheight=247mm\relax
|
||||
\def\linewidth{\textwidth}
|
||||
\def\columnwidth{\textwidth}
|
||||
\def\smallbreak{\smallskip}
|
||||
|
||||
@@ -56,6 +56,9 @@ const DEFAULT_JOT: common::Scaled = common::Scaled(3 * 65_536);
|
||||
|
||||
/// Index of the `\parindent` skip register defined by the compatibility prelude.
|
||||
const PARINDENT_REGISTER_INDEX: usize = 100;
|
||||
/// Indices of the geometry-aware `\textwidth`/`\textheight` skip registers.
|
||||
const TEXTWIDTH_REGISTER_INDEX: usize = 126;
|
||||
const TEXTHEIGHT_REGISTER_INDEX: usize = 127;
|
||||
|
||||
static PARAGRAPH_BREAK_TAG: texlang::command::StaticTag = texlang::command::StaticTag::new();
|
||||
static LINE_BREAK_TAG: texlang::command::StaticTag = texlang::command::StaticTag::new();
|
||||
@@ -515,6 +518,11 @@ struct CompilerState {
|
||||
edef_tokens: Option<VecDeque<token::Token>>,
|
||||
#[serde(skip)]
|
||||
dependencies: Rc<RefCell<Vec<SourceDependency>>>,
|
||||
/// Live page geometry used by `\textwidth`/`\textheight` expansion.
|
||||
#[serde(skip)]
|
||||
geometry_page: texrra_core::document::PageSize,
|
||||
#[serde(skip)]
|
||||
geometry_margins: texrra_core::document::PageMargins,
|
||||
}
|
||||
|
||||
struct TexlangInputTrace {
|
||||
@@ -4134,11 +4142,9 @@ fn load_document_class(
|
||||
merged_options.extend(split_options(&text));
|
||||
}
|
||||
if !merged_options.is_empty() {
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::Geometry(merged_options.join(",")),
|
||||
);
|
||||
let settings = merged_options.join(",");
|
||||
note_geometry_settings(input, &settings);
|
||||
write_event(input, token, FrontendEventKind::Geometry(settings));
|
||||
}
|
||||
|
||||
let class_file = format!("{class}.cls");
|
||||
@@ -4429,11 +4435,9 @@ fn use_package(
|
||||
|
||||
if is_builtin_package(package) {
|
||||
if package == "geometry" && !merged_options.is_empty() {
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::Geometry(merged_options.join(",")),
|
||||
);
|
||||
let settings = merged_options.join(",");
|
||||
note_geometry_settings(input, &settings);
|
||||
write_event(input, token, FrontendEventKind::Geometry(settings));
|
||||
}
|
||||
queue_package_continuation(input, token, command_name, options.as_deref(), remaining);
|
||||
return Ok(());
|
||||
@@ -6077,7 +6081,40 @@ fn geometry_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
argument_event_primitive(token, input, FrontendEventKind::Geometry)
|
||||
let settings = read_scanned_token_list(input)?
|
||||
.into_iter()
|
||||
.filter_map(|token| token.char())
|
||||
.collect::<String>()
|
||||
.trim()
|
||||
.to_string();
|
||||
note_geometry_settings(input, &settings);
|
||||
write_event(input, token, FrontendEventKind::Geometry(settings));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn note_geometry_settings(input: &mut vm::ExecutionInput<CompilerState>, settings: &str) {
|
||||
let state = input.state_mut();
|
||||
texrra_core::document_parser::apply_geometry_settings(
|
||||
&mut state.geometry_page,
|
||||
&mut state.geometry_margins,
|
||||
settings,
|
||||
);
|
||||
let width =
|
||||
(state.geometry_page.width_mm - state.geometry_margins.left - state.geometry_margins.right)
|
||||
.max(0.0);
|
||||
let height = (state.geometry_page.height_mm
|
||||
- state.geometry_margins.top
|
||||
- state.geometry_margins.bottom)
|
||||
.max(0.0);
|
||||
set_geometry_register(state, TEXTWIDTH_REGISTER_INDEX, width);
|
||||
set_geometry_register(state, TEXTHEIGHT_REGISTER_INDEX, height);
|
||||
}
|
||||
|
||||
fn set_geometry_register(state: &mut CompilerState, index: usize, millimetres: f64) {
|
||||
let register = state.engine.skip_registers.get_index_mut_ref(index);
|
||||
register.width = common::Scaled((millimetres * 72.27 / 25.4 * 65_536.0).round() as i32);
|
||||
register.stretch = common::Scaled::ZERO;
|
||||
register.shrink = common::Scaled::ZERO;
|
||||
}
|
||||
|
||||
fn text_style_argument(
|
||||
|
||||
@@ -194,6 +194,43 @@ fn text_x(svg: &str, needle: &str) -> f64 {
|
||||
.unwrap_or_else(|| panic!("text element for `{needle}` has no x coordinate"))
|
||||
}
|
||||
|
||||
fn first_black_rect_width(svg: &str) -> f64 {
|
||||
for fragment in svg.split("<rect ").skip(1) {
|
||||
let tag = fragment.split('>').next().unwrap_or(fragment);
|
||||
if !tag.contains("fill=\"#000000\"") {
|
||||
continue;
|
||||
}
|
||||
let value = tag
|
||||
.split("width=\"")
|
||||
.nth(1)
|
||||
.and_then(|rest| rest.split('"').next())
|
||||
.and_then(|value| value.parse::<f64>().ok());
|
||||
if let Some(width) = value {
|
||||
return width;
|
||||
}
|
||||
}
|
||||
panic!("no filled rectangle in SVG: {svg}");
|
||||
}
|
||||
|
||||
fn max_black_rect_height(svg: &str) -> f64 {
|
||||
let mut maximum: Option<f64> = None;
|
||||
for fragment in svg.split("<rect ").skip(1) {
|
||||
let tag = fragment.split('>').next().unwrap_or(fragment);
|
||||
if !tag.contains("fill=\"#000000\"") {
|
||||
continue;
|
||||
}
|
||||
let value = tag
|
||||
.split("height=\"")
|
||||
.nth(1)
|
||||
.and_then(|rest| rest.split('"').next())
|
||||
.and_then(|value| value.parse::<f64>().ok());
|
||||
if let Some(height) = value {
|
||||
maximum = Some(maximum.map_or(height, |current| current.max(height)));
|
||||
}
|
||||
}
|
||||
maximum.unwrap_or_else(|| panic!("no filled rectangle in SVG: {svg}"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_indent_tracks_setlength_groups_and_noindent() {
|
||||
let source = r#"\documentclass{article}
|
||||
@@ -682,3 +719,43 @@ fn default_plain_style_prints_a_page_number() {
|
||||
"default page number is not in the footer"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn geometry_updates_text_width_and_text_height() {
|
||||
let source = r#"\documentclass{article}
|
||||
\geometry{textwidth=100mm,textheight=200mm}
|
||||
\begin{document}
|
||||
\noindent\rule{\textwidth}{2pt}
|
||||
\noindent\rule{2pt}{\textheight}
|
||||
\end{document}
|
||||
"#;
|
||||
let value = fresh_preview(source, "geometry-lengths.tex");
|
||||
assert_eq!(value["status"], "ok", "{}", value["message"]);
|
||||
let svg = page_svg(&value, 0);
|
||||
let width = first_black_rect_width(&svg);
|
||||
assert!(
|
||||
(width - 100.0).abs() < 1.0,
|
||||
"textwidth did not follow geometry: {width}"
|
||||
);
|
||||
let height = max_black_rect_height(&svg);
|
||||
assert!(
|
||||
(height - 200.0).abs() < 1.0,
|
||||
"textheight did not follow geometry: {height}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn letter_paper_updates_text_width_length() {
|
||||
let source = r#"\documentclass[letterpaper]{article}
|
||||
\begin{document}
|
||||
\noindent\rule{\textwidth}{2pt}
|
||||
\end{document}
|
||||
"#;
|
||||
let value = fresh_preview(source, "letter-lengths.tex");
|
||||
assert_eq!(value["status"], "ok", "{}", value["message"]);
|
||||
let width = first_black_rect_width(&page_svg(&value, 0));
|
||||
assert!(
|
||||
(width - 165.9).abs() < 1.0,
|
||||
"letter textwidth is wrong: {width}"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user