Honour textheight geometry and widen the page oracle
This commit is contained in:
1 parent
c6ebbea58d
commit
97b72ac5d9
5 files changed
+167
-36
No files matched your search
+50
-31
@@ -22,6 +22,7 @@ thread_local! {
|
||||
|
||||
struct ProjectSession {
|
||||
paragraphs: Vec<SourceParagraph>,
|
||||
geometry: PageGeometry,
|
||||
engine: Engine,
|
||||
}
|
||||
|
||||
@@ -42,17 +43,19 @@ fn latex_body(source: &str) -> &str {
|
||||
/// `Engine::edit`, starting from the earliest changed paragraph. Structural
|
||||
/// edits fall back to a full layout. The v1 project preview remains the default.
|
||||
pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, String> {
|
||||
let paragraphs = paragraphs(latex_body(source))?;
|
||||
let document = build_document(¶graphs);
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let paragraphs = paragraphs(&body)?;
|
||||
let mut document = build_document(¶graphs);
|
||||
apply_geometry(&mut document, &geometry);
|
||||
let mut sessions = project_sessions()
|
||||
.lock()
|
||||
.map_err(|_| "route-D session lock".to_string())?;
|
||||
let previous = sessions.remove(project_id);
|
||||
let (engine, changed, recomputed) = match previous {
|
||||
Some(mut session) => {
|
||||
if session.paragraphs == paragraphs {
|
||||
if session.paragraphs == paragraphs && session.geometry == geometry {
|
||||
(session.engine, Vec::new(), 0)
|
||||
} else {
|
||||
} else if session.geometry == geometry {
|
||||
let prefix = session
|
||||
.paragraphs
|
||||
.iter()
|
||||
@@ -70,6 +73,11 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
||||
let recomputed = changed.len();
|
||||
(engine, changed, recomputed)
|
||||
}
|
||||
} else {
|
||||
let engine = Engine::layout(&document);
|
||||
let changed: Vec<usize> = (0..engine.pages().len()).collect();
|
||||
let recomputed = changed.len();
|
||||
(engine, changed, recomputed)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
@@ -82,23 +90,29 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
||||
let json = render_json(&document, &engine, Some((&changed, recomputed)));
|
||||
sessions.insert(
|
||||
project_id.to_string(),
|
||||
ProjectSession { paragraphs, engine },
|
||||
ProjectSession {
|
||||
paragraphs,
|
||||
geometry,
|
||||
engine,
|
||||
},
|
||||
);
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
/// Page geometry pulled out of the source preamble.
|
||||
#[derive(Default)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
struct PageGeometry {
|
||||
origin: PageOrigin,
|
||||
paper_width: Option<i64>,
|
||||
paper_height: Option<i64>,
|
||||
vsize: Option<i64>,
|
||||
textheight: Option<i64>,
|
||||
}
|
||||
|
||||
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`
|
||||
/// assignments out of the source and return the remaining body. Each command
|
||||
/// takes a TeX dimension.
|
||||
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`/
|
||||
/// `\textheight` assignments out of the source and return the remaining body.
|
||||
/// Each command takes a TeX dimension. LaTeX resets `\vsize` from
|
||||
/// `\textheight` at every page, so `\textheight` wins when both appear.
|
||||
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
let mut geometry = PageGeometry::default();
|
||||
let mut body = String::new();
|
||||
@@ -114,6 +128,8 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
geometry.paper_height = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "vsize") {
|
||||
geometry.vsize = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "textheight") {
|
||||
geometry.textheight = Some(value);
|
||||
} else {
|
||||
body.push_str(line);
|
||||
body.push('\n');
|
||||
@@ -122,6 +138,27 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
(body, geometry)
|
||||
}
|
||||
|
||||
/// Strip geometry assignments from the whole source (preamble included) and
|
||||
/// return the LaTeX body with them removed.
|
||||
fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
let (stripped, geometry) = strip_page_geometry(source);
|
||||
(latex_body(&stripped).to_string(), geometry)
|
||||
}
|
||||
|
||||
fn apply_geometry(document: &mut Document, geometry: &PageGeometry) {
|
||||
document.h_offset = geometry.origin.h_offset;
|
||||
document.v_offset = geometry.origin.v_offset;
|
||||
if let Some(width) = geometry.paper_width {
|
||||
document.paper_width = width;
|
||||
}
|
||||
if let Some(height) = geometry.paper_height {
|
||||
document.paper_height = height;
|
||||
}
|
||||
if let Some(height) = geometry.textheight.or(geometry.vsize) {
|
||||
document.page_goal = height;
|
||||
}
|
||||
}
|
||||
|
||||
fn dimension_assignment(line: &str, name: &str) -> Option<i64> {
|
||||
let rest = line.strip_prefix('\\')?;
|
||||
let rest = rest.strip_prefix(name)?;
|
||||
@@ -149,18 +186,10 @@ fn parse_dimension(text: &str) -> Option<i64> {
|
||||
|
||||
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
|
||||
pub fn preview_json(source: &str) -> Result<String, String> {
|
||||
let (body, geometry) = strip_page_geometry(source);
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let paragraphs = paragraphs(&body)?;
|
||||
let mut document = build_document(¶graphs);
|
||||
if let Some(width) = geometry.paper_width {
|
||||
document.paper_width = width;
|
||||
}
|
||||
if let Some(height) = geometry.paper_height {
|
||||
document.paper_height = height;
|
||||
}
|
||||
if let Some(height) = geometry.vsize {
|
||||
document.page_goal = height;
|
||||
}
|
||||
apply_geometry(&mut document, &geometry);
|
||||
let engine = Engine::layout(&document);
|
||||
let json = render_json(&document, &engine, None);
|
||||
// Keep the layout so a following `/edit` request can diff against it and
|
||||
@@ -173,20 +202,10 @@ pub fn preview_json(source: &str) -> Result<String, String> {
|
||||
|
||||
/// Render the same checkpointed document as a route-D PDF.
|
||||
pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
|
||||
let (body, geometry) = strip_page_geometry(latex_body(source));
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let paragraphs = paragraphs(&body)?;
|
||||
let mut document = build_document(¶graphs);
|
||||
document.h_offset = geometry.origin.h_offset;
|
||||
document.v_offset = geometry.origin.v_offset;
|
||||
if let Some(width) = geometry.paper_width {
|
||||
document.paper_width = width;
|
||||
}
|
||||
if let Some(height) = geometry.paper_height {
|
||||
document.paper_height = height;
|
||||
}
|
||||
if let Some(height) = geometry.vsize {
|
||||
document.page_goal = height;
|
||||
}
|
||||
apply_geometry(&mut document, &geometry);
|
||||
let engine = Engine::layout(&document);
|
||||
let pages: Vec<_> = engine
|
||||
.pages()
|
||||
|
||||
Reference in new issue
Block a user