Read vsize from route-D sources as the page goal
This commit is contained in:
1 parent
59e77f4721
commit
3b99f93ab8
4 files changed
+71
-24
No files matched your search
+36
-21
@@ -87,30 +87,39 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight` assignments out of
|
||||
/// the source and return the remaining body. Each command takes a TeX
|
||||
/// dimension.
|
||||
fn strip_page_geometry(source: &str) -> (String, PageOrigin, Option<i64>, Option<i64>) {
|
||||
let mut origin = PageOrigin::default();
|
||||
let mut paper_width = None;
|
||||
let mut paper_height = None;
|
||||
/// Page geometry pulled out of the source preamble.
|
||||
#[derive(Default)]
|
||||
struct PageGeometry {
|
||||
origin: PageOrigin,
|
||||
paper_width: Option<i64>,
|
||||
paper_height: Option<i64>,
|
||||
vsize: Option<i64>,
|
||||
}
|
||||
|
||||
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`
|
||||
/// assignments out of the source and return the remaining body. Each command
|
||||
/// takes a TeX dimension.
|
||||
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
let mut geometry = PageGeometry::default();
|
||||
let mut body = String::new();
|
||||
for line in source.lines() {
|
||||
let trimmed = line.trim();
|
||||
if let Some(value) = dimension_assignment(trimmed, "hoffset") {
|
||||
origin.h_offset = value;
|
||||
geometry.origin.h_offset = value;
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "voffset") {
|
||||
origin.v_offset = value;
|
||||
geometry.origin.v_offset = value;
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "paperwidth") {
|
||||
paper_width = Some(value);
|
||||
geometry.paper_width = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "paperheight") {
|
||||
paper_height = Some(value);
|
||||
geometry.paper_height = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "vsize") {
|
||||
geometry.vsize = Some(value);
|
||||
} else {
|
||||
body.push_str(line);
|
||||
body.push('\n');
|
||||
}
|
||||
}
|
||||
(body, origin, paper_width, paper_height)
|
||||
(body, geometry)
|
||||
}
|
||||
|
||||
fn dimension_assignment(line: &str, name: &str) -> Option<i64> {
|
||||
@@ -140,15 +149,18 @@ 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, _origin, paper_width, paper_height) = strip_page_geometry(source);
|
||||
let (body, geometry) = strip_page_geometry(source);
|
||||
let paragraphs = paragraphs(&body)?;
|
||||
let mut document = build_document(¶graphs);
|
||||
if let Some(width) = paper_width {
|
||||
if let Some(width) = geometry.paper_width {
|
||||
document.paper_width = width;
|
||||
}
|
||||
if let Some(height) = paper_height {
|
||||
if let Some(height) = geometry.paper_height {
|
||||
document.paper_height = height;
|
||||
}
|
||||
if let Some(height) = geometry.vsize {
|
||||
document.page_goal = height;
|
||||
}
|
||||
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
|
||||
@@ -161,17 +173,20 @@ 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, origin, paper_width, paper_height) = strip_page_geometry(latex_body(source));
|
||||
let (body, geometry) = strip_page_geometry(latex_body(source));
|
||||
let paragraphs = paragraphs(&body)?;
|
||||
let mut document = build_document(¶graphs);
|
||||
document.h_offset = origin.h_offset;
|
||||
document.v_offset = origin.v_offset;
|
||||
if let Some(width) = paper_width {
|
||||
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) = paper_height {
|
||||
if let Some(height) = geometry.paper_height {
|
||||
document.paper_height = height;
|
||||
}
|
||||
if let Some(height) = geometry.vsize {
|
||||
document.page_goal = height;
|
||||
}
|
||||
let engine = Engine::layout(&document);
|
||||
let pages: Vec<_> = engine
|
||||
.pages()
|
||||
@@ -188,7 +203,7 @@ pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
|
||||
&pages,
|
||||
document.paper_width,
|
||||
document.paper_height,
|
||||
origin,
|
||||
geometry.origin,
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user