Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,144 @@
|
||||
//! Page preview updates produced from the same published native layout as PDF.
|
||||
use hayro_svg::{
|
||||
RenderCache, SvgRenderSettings, hayro_interpret::InterpreterSettings, hayro_syntax::Pdf,
|
||||
};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use texrra_tex_core::render::DisplayList;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
|
||||
pub struct PreviewPage {
|
||||
pub index: usize,
|
||||
pub svg: Arc<str>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
|
||||
pub struct PreviewUpdate {
|
||||
/// None means replace the entire preview. Otherwise the receiver must be
|
||||
/// displaying this exact revision before applying the update.
|
||||
pub base_revision: Option<u64>,
|
||||
pub revision: u64,
|
||||
/// Remove any displayed pages at or beyond this index.
|
||||
pub page_count: usize,
|
||||
/// Zero-based page indices to replace or append.
|
||||
pub pages: Vec<PreviewPage>,
|
||||
}
|
||||
|
||||
type PageSvg = OnceLock<Result<Arc<str>, String>>;
|
||||
|
||||
struct PdfPreview<'a> {
|
||||
bytes: Option<&'a Arc<[u8]>>,
|
||||
document: OnceLock<Result<Pdf, String>>,
|
||||
}
|
||||
|
||||
impl PdfPreview<'_> {
|
||||
fn render_page(&self, index: usize) -> Result<Arc<str>, String> {
|
||||
let document = self
|
||||
.document
|
||||
.get_or_init(|| {
|
||||
let bytes = self
|
||||
.bytes
|
||||
.ok_or("embedded PDF preview has no published PDF")?;
|
||||
Pdf::new(Arc::new(bytes.clone()))
|
||||
.map_err(|error| format!("could not read the published preview PDF: {error:?}"))
|
||||
})
|
||||
.as_ref()
|
||||
.map_err(Clone::clone)?;
|
||||
let page = document
|
||||
.pages()
|
||||
.iter()
|
||||
.nth(index)
|
||||
.ok_or("preview PDF page is missing")?;
|
||||
Ok(Arc::from(hayro_svg::convert(
|
||||
page,
|
||||
&RenderCache::new(),
|
||||
&InterpreterSettings::default(),
|
||||
&SvgRenderSettings::default(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct PreviewState {
|
||||
previous_revision: Option<u64>,
|
||||
changed: Vec<usize>,
|
||||
slots: Vec<Arc<PageSvg>>,
|
||||
}
|
||||
|
||||
impl PreviewState {
|
||||
pub(crate) fn new(
|
||||
pages: &[DisplayList],
|
||||
previous: Option<(u64, &[DisplayList], &Self)>,
|
||||
pdf_unchanged: bool,
|
||||
) -> Self {
|
||||
let mut changed = Vec::new();
|
||||
let mut slots = Vec::with_capacity(pages.len());
|
||||
for (index, page) in pages.iter().enumerate() {
|
||||
if let Some((_, old_pages, old)) = previous
|
||||
&& (pdf_unchanged || !page.has_embedded_pdf())
|
||||
&& old_pages
|
||||
.get(index)
|
||||
.is_some_and(|old_page| old_page.same_svg_paint(page))
|
||||
{
|
||||
slots.push(old.slots[index].clone());
|
||||
} else {
|
||||
changed.push(index);
|
||||
slots.push(Arc::new(OnceLock::new()));
|
||||
}
|
||||
}
|
||||
Self {
|
||||
previous_revision: previous.map(|(revision, _, _)| revision),
|
||||
changed,
|
||||
slots,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn render(
|
||||
&self,
|
||||
revision: u64,
|
||||
pages: &[DisplayList],
|
||||
pdf: Option<&Arc<[u8]>>,
|
||||
visible: Option<u64>,
|
||||
) -> Result<PreviewUpdate, String> {
|
||||
let pdf = PdfPreview {
|
||||
bytes: pdf,
|
||||
document: OnceLock::new(),
|
||||
};
|
||||
let mut rendered = Vec::new();
|
||||
let base_revision = if visible == Some(revision) {
|
||||
visible
|
||||
} else if visible.is_some() && visible == self.previous_revision {
|
||||
for &index in &self.changed {
|
||||
rendered.push(self.render_page(index, &pages[index], &pdf)?);
|
||||
}
|
||||
visible
|
||||
} else {
|
||||
for (index, page) in pages.iter().enumerate() {
|
||||
rendered.push(self.render_page(index, page, &pdf)?);
|
||||
}
|
||||
None
|
||||
};
|
||||
Ok(PreviewUpdate {
|
||||
base_revision,
|
||||
revision,
|
||||
page_count: pages.len(),
|
||||
pages: rendered,
|
||||
})
|
||||
}
|
||||
|
||||
fn render_page(
|
||||
&self,
|
||||
index: usize,
|
||||
page: &DisplayList,
|
||||
pdf: &PdfPreview<'_>,
|
||||
) -> Result<PreviewPage, String> {
|
||||
let svg = self.slots[index]
|
||||
.get_or_init(|| {
|
||||
if page.has_embedded_pdf() {
|
||||
pdf.render_page(index)
|
||||
} else {
|
||||
page.try_to_svg().map(Arc::from)
|
||||
}
|
||||
})
|
||||
.clone()?;
|
||||
Ok(PreviewPage { index, svg })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user