Add ability to set document keywords. (#2234)

This commit is contained in:
qj 2023-09-26 10:35:18 +02:00 committed by GitHub
parent 34b3f72370
commit e33017042d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -27,6 +27,9 @@ pub struct DocumentElem {
/// The document's authors.
pub author: Author,
/// The document's keywords.
pub keywords: Keywords,
/// The page runs.
#[internal]
#[variadic]
@ -77,6 +80,7 @@ impl LayoutRoot for DocumentElem {
pages,
title: self.title(styles),
author: self.author(styles).0,
keywords: self.keywords(styles).0,
})
}
}
@ -91,3 +95,14 @@ cast! {
v: EcoString => Self(vec![v]),
v: Array => Self(v.into_iter().map(Value::cast).collect::<StrResult<_>>()?),
}
/// A list of keywords.
#[derive(Debug, Default, Clone, Hash)]
pub struct Keywords(Vec<EcoString>);
cast! {
Keywords,
self => self.0.into_value(),
v: EcoString => Self(vec![v]),
v: Array => Self(v.into_iter().map(Value::cast).collect::<StrResult<_>>()?),
}

View File

@ -28,6 +28,8 @@ pub struct Document {
pub title: Option<EcoString>,
/// The document's author.
pub author: Vec<EcoString>,
/// The document's keywords.
pub keywords: Vec<EcoString>,
}
/// A finished layout with items at fixed positions.

View File

@ -132,6 +132,14 @@ fn write_catalog(ctx: &mut PdfContext) {
info.author(TextStr(&authors.join(", ")));
xmp.creator(authors.iter().map(|s| s.as_str()));
}
let keywords = &ctx.document.keywords;
if !keywords.is_empty() {
let joined = keywords.join(", ");
info.keywords(TextStr(&joined));
xmp.pdf_keywords(&joined);
}
info.creator(TextStr("Typst"));
info.finish();
xmp.creator_tool("Typst");