Expose route-D pages through a preview endpoint
This commit is contained in:
1 parent
12288b51ee
commit
875f8a7600
8 files changed
+151
-5
No files matched your search
Executable
+91
@@ -0,0 +1,91 @@
|
||||
//! Route-D preview endpoint: a small, independent TeX-style page pipeline.
|
||||
//!
|
||||
//! This is intentionally separate from the v1 `DocumentModel` preview. It
|
||||
//! demonstrates checkpointed, page-hashed output from the route-D core. The
|
||||
//! v1 preview remains the default and the fallback.
|
||||
|
||||
use std::fmt::Write as _;
|
||||
|
||||
use texrra_tex_core::{Document, Engine, ParagraphStyle, Unit};
|
||||
|
||||
const SCALE: i64 = 65_536;
|
||||
const PAGE_HEIGHT_PT: i64 = 792;
|
||||
|
||||
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
|
||||
///
|
||||
/// Each paragraph is a route-D unit, so `Engine` produces per-page checkpoints
|
||||
/// and hashes. The response shape deliberately mirrors the useful parts of the
|
||||
/// v1 preview without pretending to parse LaTeX yet.
|
||||
pub fn preview_json(source: &str) -> Result<String, String> {
|
||||
let paragraphs: Vec<&str> = source
|
||||
.split("\n\n")
|
||||
.map(str::trim)
|
||||
.filter(|paragraph| !paragraph.is_empty())
|
||||
.collect();
|
||||
if paragraphs.is_empty() {
|
||||
return Err("route-D preview needs at least one paragraph".to_string());
|
||||
}
|
||||
|
||||
let style = ParagraphStyle {
|
||||
line_width: 480 * SCALE,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
par_indent: 0,
|
||||
line_height: 12 * SCALE,
|
||||
char_width: 10 * SCALE,
|
||||
space_width: 4 * SCALE,
|
||||
space_stretch: 6 * SCALE,
|
||||
space_shrink: 2 * SCALE,
|
||||
};
|
||||
let units = paragraphs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, paragraph)| {
|
||||
Unit::paragraph_with_style(index as u64, (*paragraph).to_string(), style.clone())
|
||||
})
|
||||
.collect();
|
||||
let document = Document::new(700 * SCALE, units);
|
||||
let engine = Engine::layout(&document);
|
||||
|
||||
let mut json = String::new();
|
||||
json.push_str("{\"status\":\"ok\",\"pageWidthMm\":215.90,\"pageHeightMm\":279.40,\"pages\":[");
|
||||
let mut page_count = 0usize;
|
||||
for page in engine.pages() {
|
||||
let svg = page.display_list().to_svg();
|
||||
if page_count > 0 {
|
||||
json.push(',');
|
||||
}
|
||||
write!(
|
||||
json,
|
||||
"{{\"index\":{page_count},\"hash\":\"{:016x}\",\"svg\":\"{}\"}}",
|
||||
page.hash,
|
||||
escape_json(&svg)
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
page_count += 1;
|
||||
}
|
||||
write!(
|
||||
json,
|
||||
"],\"pageCount\":{page_count},\"pageHeightPt\":{PAGE_HEIGHT_PT}}}"
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
fn escape_json(value: &str) -> String {
|
||||
let mut escaped = String::with_capacity(value.len());
|
||||
for character in value.chars() {
|
||||
match character {
|
||||
'"' => escaped.push_str("\\\""),
|
||||
'\\' => escaped.push_str("\\\\"),
|
||||
'\n' => escaped.push_str("\\n"),
|
||||
'\r' => escaped.push_str("\\r"),
|
||||
'\t' => escaped.push_str("\\t"),
|
||||
character if character < '\u{20}' => {
|
||||
let _ = write!(escaped, "\\u{:04x}", character as u32);
|
||||
}
|
||||
character => escaped.push(character),
|
||||
}
|
||||
}
|
||||
escaped
|
||||
}
|
||||
Reference in new issue
Block a user