From 58adf1d025bd7ff149d7fd06147b0f3e069f8c7d Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Sun, 12 Feb 2023 23:32:14 +0100 Subject: Add XMP --- src/export/pdf/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/export/pdf') diff --git a/src/export/pdf/mod.rs b/src/export/pdf/mod.rs index 60cd4ea2..38433211 100644 --- a/src/export/pdf/mod.rs +++ b/src/export/pdf/mod.rs @@ -11,6 +11,7 @@ use std::hash::Hash; use pdf_writer::types::Direction; use pdf_writer::{Finish, Name, PdfWriter, Ref, TextStr}; +use xmp_writer::{LangId, RenditionClass, XmpWriter}; use self::outline::HeadingNode; use self::page::Page; @@ -115,20 +116,39 @@ fn write_catalog(ctx: &mut PdfContext) { }; // Write the document information. + let meta_ref = ctx.alloc.bump(); + let mut xmp = XmpWriter::new(); + let mut info = ctx.writer.document_info(ctx.alloc.bump()); if let Some(title) = &ctx.document.title { info.title(TextStr(title)); + xmp.title([(None, title.as_str())]); } if let Some(author) = &ctx.document.author { info.author(TextStr(author)); + xmp.creator([(author.as_str())]); } info.creator(TextStr("Typst")); + xmp.creator_tool("Typst"); + xmp.num_pages(ctx.document.pages.len() as u32); + xmp.format("application/pdf"); + xmp.language(ctx.languages.keys().map(|lang| LangId(lang.as_str()))); + xmp.rendition_class(RenditionClass::Proof); + xmp.pdf_version("1.7"); + info.finish(); + let xmp_buf = xmp.finish(None); + let mut meta_stream = ctx.writer.stream(meta_ref, &xmp_buf); + meta_stream.pair(Name(b"Type"), Name(b"Metadata")); + meta_stream.pair(Name(b"Subtype"), Name(b"XML")); + meta_stream.finish(); + // Write the document catalog. let mut catalog = ctx.writer.catalog(ctx.alloc.bump()); catalog.pages(ctx.page_tree_ref); catalog.viewer_preferences().direction(dir); + catalog.pair(Name(b"Metadata"), meta_ref); if let Some(outline_root_id) = outline_root_id { catalog.outlines(outline_root_id); -- cgit v1.2.3