summaryrefslogtreecommitdiff
path: root/src/export
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-26 23:42:40 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-26 23:52:01 +0100
commit6bafc6391061d4b589dea835705a08b25a4df9f8 (patch)
tree4add85f17fc56da341acfb58a223ea20d80c280a /src/export
parent0579fd4409375aaa9fd8e87a06fd59097b5fcd97 (diff)
Document metadata
Diffstat (limited to 'src/export')
-rw-r--r--src/export/pdf/mod.rs30
-rw-r--r--src/export/pdf/page.rs8
-rw-r--r--src/export/render.rs2
3 files changed, 24 insertions, 16 deletions
diff --git a/src/export/pdf/mod.rs b/src/export/pdf/mod.rs
index 2547ddbf..7a530f04 100644
--- a/src/export/pdf/mod.rs
+++ b/src/export/pdf/mod.rs
@@ -14,21 +14,21 @@ use pdf_writer::{Finish, Name, PdfWriter, Ref, TextStr};
use self::outline::{Heading, HeadingNode};
use self::page::Page;
+use crate::doc::{Document, Lang, Metadata};
use crate::font::Font;
-use crate::frame::{Frame, Lang};
use crate::geom::{Abs, Dir, Em};
use crate::image::Image;
-/// Export a collection of frames into a PDF file.
+/// Export a document into a PDF file.
///
/// This creates one page per frame. In addition to the frames, you need to pass
/// in the context used during compilation so that fonts and images can be
/// included in the PDF.
///
/// Returns the raw bytes making up the PDF file.
-pub fn pdf(frames: &[Frame]) -> Vec<u8> {
- let mut ctx = PdfContext::new();
- page::construct_pages(&mut ctx, frames);
+pub fn pdf(document: &Document) -> Vec<u8> {
+ let mut ctx = PdfContext::new(&document.metadata);
+ page::construct_pages(&mut ctx, &document.pages);
font::write_fonts(&mut ctx);
image::write_images(&mut ctx);
page::write_page_tree(&mut ctx);
@@ -41,7 +41,8 @@ const SRGB: Name<'static> = Name(b"srgb");
const D65_GRAY: Name<'static> = Name(b"d65gray");
/// Context for exporting a whole PDF document.
-pub struct PdfContext {
+pub struct PdfContext<'a> {
+ metadata: &'a Metadata,
writer: PdfWriter,
pages: Vec<Page>,
page_heights: Vec<f32>,
@@ -57,11 +58,12 @@ pub struct PdfContext {
heading_tree: Vec<HeadingNode>,
}
-impl PdfContext {
- fn new() -> Self {
+impl<'a> PdfContext<'a> {
+ fn new(metadata: &'a Metadata) -> Self {
let mut alloc = Ref::new(1);
let page_tree_ref = alloc.bump();
Self {
+ metadata,
writer: PdfWriter::new(),
pages: vec![],
page_heights: vec![],
@@ -117,7 +119,15 @@ fn write_catalog(ctx: &mut PdfContext) {
};
// Write the document information.
- ctx.writer.document_info(ctx.alloc.bump()).creator(TextStr("Typst"));
+ let mut info = ctx.writer.document_info(ctx.alloc.bump());
+ if let Some(title) = &ctx.metadata.title {
+ info.title(TextStr(title));
+ }
+ if let Some(author) = &ctx.metadata.author {
+ info.author(TextStr(author));
+ }
+ info.creator(TextStr("Typst"));
+ info.finish();
// Write the document catalog.
let mut catalog = ctx.writer.catalog(ctx.alloc.bump());
@@ -131,8 +141,6 @@ fn write_catalog(ctx: &mut PdfContext) {
if let Some(lang) = lang {
catalog.lang(TextStr(lang.as_str()));
}
-
- catalog.finish();
}
/// Compress data with the DEFLATE algorithm.
diff --git a/src/export/pdf/page.rs b/src/export/pdf/page.rs
index 3167989c..7c479425 100644
--- a/src/export/pdf/page.rs
+++ b/src/export/pdf/page.rs
@@ -5,8 +5,8 @@ use pdf_writer::{Content, Filter, Finish, Name, Rect, Ref, Str};
use super::{
deflate, AbsExt, EmExt, Heading, HeadingNode, PdfContext, RefExt, D65_GRAY, SRGB,
};
+use crate::doc::{Destination, Element, Frame, Group, Role, Text};
use crate::font::Font;
-use crate::frame::{Destination, Element, Frame, Group, Role, Text};
use crate::geom::{
self, Abs, Color, Em, Geometry, Numeric, Paint, Point, Ratio, Shape, Size, Stroke,
Transform,
@@ -155,8 +155,8 @@ pub struct Page {
}
/// An exporter for the contents of a single PDF page.
-struct PageContext<'a> {
- parent: &'a mut PdfContext,
+struct PageContext<'a, 'b> {
+ parent: &'a mut PdfContext<'b>,
page_ref: Ref,
content: Content,
state: State,
@@ -177,7 +177,7 @@ struct State {
stroke_space: Option<Name<'static>>,
}
-impl<'a> PageContext<'a> {
+impl PageContext<'_, '_> {
fn save_state(&mut self) {
self.saves.push(self.state.clone());
self.content.save_state();
diff --git a/src/export/render.rs b/src/export/render.rs
index 7cff7ad8..14654b9b 100644
--- a/src/export/render.rs
+++ b/src/export/render.rs
@@ -8,7 +8,7 @@ use tiny_skia as sk;
use ttf_parser::{GlyphId, OutlineBuilder};
use usvg::FitTo;
-use crate::frame::{Element, Frame, Group, Text};
+use crate::doc::{Element, Frame, Group, Text};
use crate::geom::{
self, Abs, Geometry, Paint, PathElement, Shape, Size, Stroke, Transform,
};