use quillmark_core::{
backend::{Backend, FontFace},
document::Document,
error::{RenderError, RenderResult, Severity},
quill::{CalendarDate, Quill},
session::LiveSession,
types::{OutputFormat, RenderOptions},
};
use quillmark_content::units::{Scalars, Units};
use std::collections::HashMap;
use std::sync::Arc;
pub struct Engine {
backends: HashMap<String, Arc<dyn Backend>>,
}
impl Engine {
pub fn new() -> Self {
Self::with_fonts(Vec::<Vec<u8>>::new()).expect("an empty font library refuses nothing")
}
pub fn with_fonts<F: Into<Arc<[u8]>>>(
fonts: impl IntoIterator<Item = F>,
) -> Result<Self, RenderError> {
#[allow(unused_mut)]
let mut engine = Self {
backends: HashMap::new(),
};
#[cfg(feature = "typst")]
{
let library = Arc::new(quillmark_typst::FontLibrary::new(fonts)?);
engine.register_backend(Box::new(quillmark_typst::TypstBackend::new(library)));
}
#[cfg(not(feature = "typst"))]
let _ = fonts;
#[cfg(feature = "acroform")]
{
engine.register_backend(Box::new(quillmark_acroform::AcroformBackend));
}
Ok(engine)
}
#[cfg(any(feature = "typst", feature = "acroform"))]
fn register_backend(&mut self, backend: Box<dyn Backend>) {
let id = backend.id().to_string();
self.backends.insert(id, Arc::from(backend));
}
pub fn registered_backends(&self) -> Vec<&str> {
self.backends.keys().map(|s| s.as_str()).collect()
}
fn resolve_backend(&self, quill: &Quill) -> Result<&Arc<dyn Backend>, RenderError> {
let backend_id = quill.backend_id();
self.backends.get(backend_id).ok_or_else(|| {
RenderError::coded_hint(
"engine::backend_not_found",
format!("Backend '{}' not registered or not enabled", backend_id),
format!(
"Available backends: {}",
self.backends.keys().cloned().collect::<Vec<_>>().join(", ")
),
)
})
}
pub fn open(
&self,
quill: &Quill,
doc: &Document,
today: CalendarDate,
) -> Result<LiveSession, RenderError> {
let backend = self.resolve_backend(quill)?;
let json_data = quill.compile_checked(doc, today)?;
backend.open(quill, &json_data, today)
}
pub fn render(
&self,
quill: &Quill,
doc: &Document,
today: CalendarDate,
opts: &RenderOptions,
) -> Result<RenderResult, RenderError> {
self.render_in(quill, doc, today, opts, Scalars)
}
pub fn render_in(
&self,
quill: &Quill,
doc: &Document,
today: CalendarDate,
opts: &RenderOptions,
u: impl Units,
) -> Result<RenderResult, RenderError> {
let default_format = self
.resolve_backend(quill)?
.supported_formats()
.first()
.copied();
let session = self.open(quill, doc, today)?;
let mut resolved = opts.clone();
resolved.output_format = opts.output_format.or(default_format);
let mut result = session.render_in(&resolved, u)?;
let unclaimed = quill
.advisories(doc)
.into_iter()
.filter(|d| d.severity == Severity::Warning);
result.warnings.splice(0..0, unclaimed);
Ok(result)
}
pub fn supported_formats(&self, quill: &Quill) -> Result<&'static [OutputFormat], RenderError> {
Ok(self.resolve_backend(quill)?.supported_formats())
}
pub fn font_faces(&self, quill: &Quill) -> Result<Vec<FontFace>, RenderError> {
Ok(self.resolve_backend(quill)?.font_faces(quill))
}
}
impl Default for Engine {
fn default() -> Self {
Self::new()
}
}