Apply TeX page offsets in the route-D PDF endpoint
This commit is contained in:
1 parent
5db7acaff2
commit
8d319b3b0c
5 files changed
+75
-11
No files matched your search
+50
-6
@@ -89,6 +89,50 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
/// Pull `\hoffset`/`\voffset` assignments out of the source and return the
|
||||
/// remaining body. Both commands take a TeX dimension.
|
||||
fn strip_page_offsets(source: &str) -> (String, PageOrigin) {
|
||||
let mut origin = PageOrigin::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;
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "voffset") {
|
||||
origin.v_offset = value;
|
||||
} else {
|
||||
body.push_str(line);
|
||||
body.push('\n');
|
||||
}
|
||||
}
|
||||
(body, origin)
|
||||
}
|
||||
|
||||
fn dimension_assignment(line: &str, name: &str) -> Option<i64> {
|
||||
let rest = line.strip_prefix('\\')?;
|
||||
let rest = rest.strip_prefix(name)?;
|
||||
let rest = rest.trim_start().strip_prefix('=')?;
|
||||
parse_dimension(rest.trim())
|
||||
}
|
||||
|
||||
fn parse_dimension(text: &str) -> Option<i64> {
|
||||
let end = text
|
||||
.find(|character: char| {
|
||||
!(character.is_ascii_digit() || character == '.' || character == '-')
|
||||
})
|
||||
.unwrap_or(text.len());
|
||||
let number: f64 = text[..end].parse().ok()?;
|
||||
let factor = match text[end..].trim() {
|
||||
"pt" => 1.0,
|
||||
"bp" => 72.27 / 72.0,
|
||||
"in" => 72.27,
|
||||
"cm" => 72.27 / 2.54,
|
||||
"mm" => 72.27 / 25.4,
|
||||
_ => return None,
|
||||
};
|
||||
Some((number * factor * 65_536.0).round() as i64)
|
||||
}
|
||||
|
||||
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
|
||||
pub fn preview_json(source: &str) -> Result<String, String> {
|
||||
let paragraphs = paragraphs(source)?;
|
||||
@@ -99,8 +143,11 @@ 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 paragraphs = paragraphs(latex_body(source))?;
|
||||
let document = build_document(¶graphs);
|
||||
let (body, origin) = strip_page_offsets(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;
|
||||
let engine = Engine::layout(&document);
|
||||
let pages: Vec<_> = engine
|
||||
.pages()
|
||||
@@ -115,10 +162,7 @@ pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
|
||||
Ok(display_lists_to_pdf_with_origin(
|
||||
&pages,
|
||||
PAGE_HEIGHT_PT,
|
||||
PageOrigin {
|
||||
h_offset: document.h_offset,
|
||||
v_offset: document.v_offset,
|
||||
},
|
||||
origin,
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user