Map sections and emphasis in the route-D preview
This commit is contained in:
1 parent
ec9ce3b765
commit
2c4d968659
4 files changed
+91
-20
No files matched your search
+62
-17
@@ -8,7 +8,7 @@ use std::cell::RefCell;
|
||||
use std::fmt::Write as _;
|
||||
|
||||
use texrra_tex_core::pdf::display_lists_to_pdf;
|
||||
use texrra_tex_core::{Document, Engine, ParagraphStyle, Unit};
|
||||
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem};
|
||||
|
||||
const SCALE: i64 = 65_536;
|
||||
const PAGE_HEIGHT_PT: i64 = 792;
|
||||
@@ -64,7 +64,7 @@ pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
|
||||
(next.clone(), engine)
|
||||
}
|
||||
};
|
||||
engine.edit(&next, unit as u64, paragraphs[unit].to_string());
|
||||
engine.edit(&next, unit as u64, paragraphs[unit].text.clone());
|
||||
let changed = engine.changed_pages().to_vec();
|
||||
let recomputed = engine.recomputed_pages();
|
||||
let json = render_json(&engine, Some((&changed, recomputed)));
|
||||
@@ -73,7 +73,14 @@ pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
|
||||
})
|
||||
}
|
||||
|
||||
fn paragraphs(source: &str) -> Result<Vec<&str>, String> {
|
||||
#[derive(Clone)]
|
||||
struct SourceParagraph {
|
||||
text: String,
|
||||
font: ParagraphFont,
|
||||
line_height: i64,
|
||||
}
|
||||
|
||||
fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
let paragraphs: Vec<&str> = source
|
||||
.split("\n\n")
|
||||
.map(str::trim)
|
||||
@@ -82,26 +89,64 @@ fn paragraphs(source: &str) -> Result<Vec<&str>, String> {
|
||||
if paragraphs.is_empty() {
|
||||
return Err("route-D preview needs at least one paragraph".to_string());
|
||||
}
|
||||
Ok(paragraphs)
|
||||
Ok(paragraphs
|
||||
.into_iter()
|
||||
.map(|paragraph| parse_paragraph(paragraph))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn build_document(paragraphs: &[&str]) -> Document {
|
||||
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,
|
||||
};
|
||||
fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
|
||||
let rest = text.strip_prefix(command)?.trim_start();
|
||||
let rest = rest.strip_prefix('{')?.strip_suffix('}')?;
|
||||
Some(rest.trim())
|
||||
}
|
||||
|
||||
/// Map a small, general LaTeX subset onto route-D paragraph faces. This is not
|
||||
/// a full LaTeX reader; it exists so the live endpoint can show headings and
|
||||
/// emphasis while the semantic layer is built out.
|
||||
fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
let candidates = [
|
||||
("\\section", ParagraphFont::Bold, 18),
|
||||
("\\textbf", ParagraphFont::Bold, 12),
|
||||
("\\emph", ParagraphFont::Italic, 12),
|
||||
];
|
||||
for (command, font, line_height) in candidates {
|
||||
if let Some(text) = command_body(raw, command) {
|
||||
return SourceParagraph {
|
||||
text: text.to_string(),
|
||||
font,
|
||||
line_height,
|
||||
};
|
||||
}
|
||||
}
|
||||
SourceParagraph {
|
||||
text: raw.to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_document(paragraphs: &[SourceParagraph]) -> Document {
|
||||
let units = paragraphs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, paragraph)| {
|
||||
Unit::paragraph_with_style(index as u64, (*paragraph).to_string(), style.clone())
|
||||
let style = ParagraphStyle {
|
||||
line_width: 480 * SCALE,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
par_indent: 0,
|
||||
line_height: paragraph.line_height * SCALE,
|
||||
char_width: 10 * SCALE,
|
||||
space_width: 4 * SCALE,
|
||||
space_stretch: 6 * SCALE,
|
||||
space_shrink: 2 * SCALE,
|
||||
};
|
||||
let mut unit = Unit::paragraph_with_style(index as u64, paragraph.text.clone(), style);
|
||||
if let Some(VerticalItem::Paragraph(paragraph_box)) = unit.items.first_mut() {
|
||||
paragraph_box.font = paragraph.font;
|
||||
}
|
||||
unit
|
||||
})
|
||||
.collect();
|
||||
Document::new(700 * SCALE, units)
|
||||
|
||||
Reference in new issue
Block a user