mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
Hide subengines and improve Compiler API 🕳
This commit is contained in:
parent
67b1945034
commit
9b3386f8a3
42
src/lib.rs
42
src/lib.rs
@ -29,31 +29,32 @@ mod engine;
|
|||||||
mod pdf;
|
mod pdf;
|
||||||
mod utility;
|
mod utility;
|
||||||
|
|
||||||
pub use crate::parsing::{Tokens, Parser, ParseError};
|
pub use crate::parsing::{Tokens, ParseError};
|
||||||
pub use crate::engine::{Engine, TypesetError};
|
pub use crate::engine::TypesetError;
|
||||||
pub use crate::pdf::{PdfCreator, PdfWritingError};
|
pub use crate::pdf::PdfWritingError;
|
||||||
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use crate::parsing::Parser;
|
||||||
use crate::syntax::SyntaxTree;
|
use crate::syntax::SyntaxTree;
|
||||||
|
use crate::engine::Engine;
|
||||||
use crate::doc::{Document, Style};
|
use crate::doc::{Document, Style};
|
||||||
|
use crate::pdf::PdfCreator;
|
||||||
|
|
||||||
|
|
||||||
/// Emits various compiled intermediates from source code.
|
/// Compiles source code into typesetted documents allowing to
|
||||||
pub struct Compiler<'s> {
|
/// retrieve results at various stages.
|
||||||
/// The source code of the document.
|
pub struct Compiler {
|
||||||
source: &'s str,
|
|
||||||
/// Style for typesetting.
|
/// Style for typesetting.
|
||||||
style: Style,
|
style: Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> Compiler<'s> {
|
impl Compiler {
|
||||||
/// Create a new compiler from a document.
|
/// Create a new compiler from a document.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(source: &'s str) -> Compiler<'s> {
|
pub fn new() -> Compiler {
|
||||||
Compiler {
|
Compiler {
|
||||||
source,
|
|
||||||
style: Style::default(),
|
style: Style::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,26 +68,27 @@ impl<'s> Compiler<'s> {
|
|||||||
|
|
||||||
/// Return an iterator over the tokens of the document.
|
/// Return an iterator over the tokens of the document.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn tokenize(&self) -> Tokens<'s> {
|
pub fn tokenize<'s>(&self, source: &'s str) -> Tokens<'s> {
|
||||||
Tokens::new(self.source)
|
Tokens::new(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the abstract syntax tree representation of the document.
|
/// Return the abstract syntax tree representation of the document.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parse(&self) -> Result<SyntaxTree<'s>, Error> {
|
pub fn parse<'s>(&self, source: &'s str) -> Result<SyntaxTree<'s>, Error> {
|
||||||
Parser::new(self.tokenize()).parse().map_err(Into::into)
|
Parser::new(self.tokenize(source)).parse().map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the abstract typesetted representation of the document.
|
/// Return the abstract typesetted representation of the document.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn typeset(&self) -> Result<Document, Error> {
|
pub fn typeset(&self, source: &str) -> Result<Document, Error> {
|
||||||
let tree = self.parse()?;
|
let tree = self.parse(source)?;
|
||||||
Engine::new(&tree, self.style.clone()).typeset().map_err(Into::into)
|
Engine::new(&tree, self.style.clone()).typeset().map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write the document as a _PDF_, returning how many bytes were written.
|
/// Write the document as a _PDF_, returning how many bytes were written.
|
||||||
pub fn write_pdf<W: Write>(&self, target: &mut W) -> Result<usize, Error> {
|
pub fn write_pdf<W: Write>(&self, source: &str, target: &mut W) -> Result<usize, Error> {
|
||||||
PdfCreator::new(target, &self.typeset()?)?.write().map_err(Into::into)
|
let document = self.typeset(source)?;
|
||||||
|
PdfCreator::new(&document, target)?.write().map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,11 +156,11 @@ mod test {
|
|||||||
fn test(name: &str, src: &str) {
|
fn test(name: &str, src: &str) {
|
||||||
let path = format!("../target/typeset-pdf-{}.pdf", name);
|
let path = format!("../target/typeset-pdf-{}.pdf", name);
|
||||||
let mut file = std::fs::File::create(path).unwrap();
|
let mut file = std::fs::File::create(path).unwrap();
|
||||||
Compiler::new(src).write_pdf(&mut file).unwrap();
|
Compiler::new().write_pdf(src, &mut file).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pdfs() {
|
fn small() {
|
||||||
test("unicode", "∑mbe∂∂ed font with Unicode!");
|
test("unicode", "∑mbe∂∂ed font with Unicode!");
|
||||||
test("parentheses", "Text with ) and ( or (enclosed) works.");
|
test("parentheses", "Text with ) and ( or (enclosed) works.");
|
||||||
test("composite-glyph", "Composite character‼");
|
test("composite-glyph", "Composite character‼");
|
||||||
|
@ -31,7 +31,7 @@ struct Offsets {
|
|||||||
|
|
||||||
impl<'a, W: Write> PdfCreator<'a, W> {
|
impl<'a, W: Write> PdfCreator<'a, W> {
|
||||||
/// Create a new _PDF_ Creator.
|
/// Create a new _PDF_ Creator.
|
||||||
pub fn new(target: &'a mut W, doc: &'a Document) -> PdfResult<PdfCreator<'a, W>> {
|
pub fn new(doc: &'a Document, target: &'a mut W) -> PdfResult<PdfCreator<'a, W>> {
|
||||||
// Calculate a unique id for all object to come
|
// Calculate a unique id for all object to come
|
||||||
let catalog = 1;
|
let catalog = 1;
|
||||||
let page_tree = catalog + 1;
|
let page_tree = catalog + 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user