Write PDF metadata from hypersetup
This commit is contained in:
1 parent
8686b9a35e
commit
863e7c1a81
5 files changed
+213
-8
No files matched your search
+86
-2
@@ -12,7 +12,9 @@ use std::sync::{Mutex, OnceLock};
|
||||
use texrra_tex_core::cmr10::Cmr10Metrics;
|
||||
use texrra_tex_core::math::{AlignedRow, MathAtom, parse_math, split_math_lines};
|
||||
use texrra_tex_core::paragraph::HorizontalItem;
|
||||
use texrra_tex_core::pdf::{PageOrigin, display_lists_to_pdf_with_cm_type1};
|
||||
use texrra_tex_core::pdf::{
|
||||
PageOrigin, PdfMetadata, display_lists_to_pdf_with_cm_type1_and_metadata,
|
||||
};
|
||||
pub use texrra_tex_core::render::RasterImage;
|
||||
use texrra_tex_core::render::{DisplayItem, DisplayList, FontFace};
|
||||
use texrra_tex_core::{
|
||||
@@ -228,6 +230,10 @@ struct PageGeometry {
|
||||
author: Option<String>,
|
||||
/// `\date{...}` captured from the preamble.
|
||||
date: Option<String>,
|
||||
pdf_title: Option<String>,
|
||||
pdf_author: Option<String>,
|
||||
pdf_subject: Option<String>,
|
||||
pdf_keywords: Option<String>,
|
||||
/// `thebibliography` entries: label key and entry text, in order.
|
||||
bibliography: Vec<(String, String)>,
|
||||
/// `\newtheorem` definitions from the preamble.
|
||||
@@ -1051,6 +1057,50 @@ fn geometry_counter(geometry: &PageGeometry, name: &str) -> u32 {
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn hypersetup_values(line: &str) -> Vec<(String, String)> {
|
||||
let Some(rest) = line.strip_prefix("\\hypersetup") else {
|
||||
return Vec::new();
|
||||
};
|
||||
let Some((body, _)) = braced_group(rest.trim_start()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut values = Vec::new();
|
||||
let mut depth = 0_usize;
|
||||
let mut entry = String::new();
|
||||
for character in body.chars() {
|
||||
match character {
|
||||
'{' => {
|
||||
depth += 1;
|
||||
entry.push(character);
|
||||
}
|
||||
'}' => {
|
||||
depth = depth.saturating_sub(1);
|
||||
entry.push(character);
|
||||
}
|
||||
',' if depth == 0 => {
|
||||
values.push(entry.trim().to_string());
|
||||
entry = String::new();
|
||||
}
|
||||
_ => entry.push(character),
|
||||
}
|
||||
}
|
||||
values.push(entry.trim().to_string());
|
||||
values
|
||||
.into_iter()
|
||||
.filter_map(|entry| {
|
||||
let (key, value) = entry.split_once('=')?;
|
||||
let value = value.trim();
|
||||
let value = value
|
||||
.strip_prefix('{')
|
||||
.and_then(|inner| inner.strip_suffix('}'))
|
||||
.unwrap_or(value)
|
||||
.trim()
|
||||
.to_string();
|
||||
Some((key.trim().to_string(), value))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
let mut geometry = PageGeometry::default();
|
||||
let mut body = String::new();
|
||||
@@ -1245,6 +1295,18 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
.extend(graphics_path_groups(trimmed));
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\hypersetup") {
|
||||
for (key, value) in hypersetup_values(trimmed) {
|
||||
match key.as_str() {
|
||||
"pdftitle" => geometry.pdf_title = Some(value),
|
||||
"pdfauthor" => geometry.pdf_author = Some(value),
|
||||
"pdfsubject" => geometry.pdf_subject = Some(value),
|
||||
"pdfkeywords" => geometry.pdf_keywords = Some(value),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(value) = braced_value(trimmed, "\\title") {
|
||||
geometry.title = Some(value);
|
||||
continue;
|
||||
@@ -2107,11 +2169,24 @@ pub fn pdf_bytes_with_images(
|
||||
geometry.suppress_first_page_number,
|
||||
);
|
||||
}
|
||||
Ok(display_lists_to_pdf_with_cm_type1(
|
||||
let metadata = PdfMetadata {
|
||||
title: geometry
|
||||
.pdf_title
|
||||
.clone()
|
||||
.or_else(|| geometry.title.clone()),
|
||||
author: geometry
|
||||
.pdf_author
|
||||
.clone()
|
||||
.or_else(|| geometry.author.clone()),
|
||||
subject: geometry.pdf_subject.clone(),
|
||||
keywords: geometry.pdf_keywords.clone(),
|
||||
};
|
||||
Ok(display_lists_to_pdf_with_cm_type1_and_metadata(
|
||||
&pages,
|
||||
document.paper_width,
|
||||
document.paper_height,
|
||||
geometry.origin,
|
||||
&metadata,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -7520,6 +7595,15 @@ a & b
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[test]
|
||||
fn hypersetup_writes_the_pdf_info_dictionary() {
|
||||
let pdf =
|
||||
pdf_bytes("\\hypersetup{pdftitle={My Title},pdfauthor={Ada}}\n\nBody.").expect("pdf");
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/Title (My Title)"));
|
||||
assert!(text.contains("/Author (Ada)"));
|
||||
}
|
||||
|
||||
fn pageref_reports_the_page_of_its_heading() {
|
||||
let source = "\\pagestyle{empty}\n\n\\section{One}\n\nFirst page.\n\n\\newpage\n\n\\section{Two}\\label{sec:two}\n\nSee page \\pageref{sec:two}.";
|
||||
let json = preview_json(source).expect("preview");
|
||||
|
||||
Reference in new issue
Block a user