Skip to content

Commit

Permalink
chore: split meta, overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphoseer committed Jan 7, 2025
1 parent 65e07df commit 45b994c
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 52 deletions.
37 changes: 37 additions & 0 deletions crates/sdo-pdf/src/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use pdf_create::chrono::Local;
use pdf_create::common::PdfString;
use pdf_create::encoding::{pdf_doc_encode, PDFDocEncodingError};
use pdf_create::high::Info;

/// Information to add into the PDF `/Info` dictionary
pub struct MetaInfo {
/// Title
pub title: Option<String>,
/// Author
pub author: Option<String>,
/// Subject
pub subject: Option<String>,
}

pub fn prepare_info(info: &mut Info, meta: &MetaInfo) -> Result<(), PDFDocEncodingError> {
if let Some(author) = &meta.author {
let author = pdf_doc_encode(author)?;
info.author = Some(PdfString::new(author));
}
if let Some(subject) = &meta.subject {
let subject = pdf_doc_encode(subject)?;
info.subject = Some(PdfString::new(subject));
}
if let Some(title) = &meta.title {
let title = pdf_doc_encode(title)?;
info.title = Some(PdfString::new(title));
}
let creator = pdf_doc_encode("SIGNUM © 1986-93 F. Schmerbeck")?;
info.creator = Some(PdfString::new(creator));
let producer = pdf_doc_encode("Signum! Document Toolbox")?;
info.producer = Some(PdfString::new(producer));
let now = Local::now();
info.creation_date = Some(now);
info.mod_date = Some(now);
Ok(())
}
3 changes: 3 additions & 0 deletions crates/sdo-pdf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod cmap;
pub mod font;
mod info;
pub mod sdoc;

pub use info::{prepare_info, MetaInfo};
8 changes: 8 additions & 0 deletions crates/signum/src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,11 @@ pub struct DocumentInfo {
/// Decoded images embedded in the document
pub images: Vec<(String, Page)>,
}

/// Common adjustments to the graphics state for rendering
pub struct Overrides {
/// Horizontal offset for rendering
pub xoffset: i32,
/// Vertical offset for rendering
pub yoffset: i32,
}
5 changes: 3 additions & 2 deletions src/bin/sdo-batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn run(buffer: &[u8], opt: RunOpts) -> eyre::Result<()> {
// Preprare output
let mut hnd = Handle::new();

prepare_meta(&mut hnd, &script.meta)?;
prepare_meta(&mut hnd, &script.meta.to_pdf_meta())?;

let mut use_table_vec = UseTableVec::new();
for (doc, di) in &documents {
Expand All @@ -130,8 +130,9 @@ pub fn run(buffer: &[u8], opt: RunOpts) -> eyre::Result<()> {
hnd.res.fonts.push(font);
}

let overrides = script.meta.to_overrides();
for (doc, di) in &documents {
prepare_document(&mut hnd, doc, di, &script.meta, &font_info)?;
prepare_document(&mut hnd, doc, di, &overrides, &font_info)?;
}

for (key, value) in &script.page_labels {
Expand Down
36 changes: 35 additions & 1 deletion src/cli/opt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{borrow::Cow, collections::BTreeMap, fmt, io, path::PathBuf, str::FromStr};

use pdf_create::high;
use sdo_pdf::MetaInfo;
use serde::Deserialize;
use signum::chsets::FontKind;
use signum::{chsets::FontKind, docs::Overrides};
use structopt::StructOpt;
use thiserror::*;

Expand Down Expand Up @@ -175,6 +176,39 @@ pub struct Meta {
pub subject: Option<String>,
}

impl Meta {
/// Get the [MetaInfo] for PDF generation
pub fn pdf_meta_info(&self, file_name: &str) -> MetaInfo {
MetaInfo {
author: self.title.to_owned(),
title: Some(
match self.title.as_deref() {
Some(title) => title,
None => file_name,
}
.to_owned(),
),
subject: self.subject.to_owned(),
}
}

/// Get the [MetaInfo] for PDF generation
pub fn to_pdf_meta(&self) -> MetaInfo {
MetaInfo {
author: self.title.to_owned(),
title: self.title.to_owned(),
subject: self.subject.to_owned(),
}
}

pub fn to_overrides(&self) -> Overrides {
Overrides {
xoffset: self.xoffset.unwrap_or(0),
yoffset: self.yoffset.unwrap_or(0),
}
}
}

fn chsets_path() -> PathBuf {
PathBuf::from("CHSETS")
}
Expand Down
77 changes: 28 additions & 49 deletions src/cli/sdoc/pdf.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
use std::{borrow::Cow, collections::BTreeMap, fs::File, io::BufWriter, path::Path};
use std::{collections::BTreeMap, fs::File, io::BufWriter, path::Path};

use color_eyre::eyre::{self, eyre};
use color_eyre::eyre::{self, eyre, OptionExt};
use log::{debug, info};
use pdf_create::{
chrono::Local,
common::{
ColorIs, ColorSpace, ImageMetadata, OutputIntent, OutputIntentSubtype, PdfString, ProcSet,
Rectangle,
},
encoding::pdf_doc_encode,
high::{DictResource, Handle, Image, Page, Resource, Resources, XObject},
};
use sdo_pdf::{font::Fonts, sdoc::Contents};
use signum::chsets::{
cache::{ChsetCache, FontCacheInfo},
FontKind, UseTableVec,
use sdo_pdf::{font::Fonts, prepare_info, sdoc::Contents, MetaInfo};
use signum::{
chsets::{
cache::{ChsetCache, FontCacheInfo},
FontKind, UseTableVec,
},
docs::Overrides,
};

use crate::cli::opt::Meta;

use super::{Document, DocumentInfo};

pub fn prepare_meta(hnd: &mut Handle, meta: &Meta) -> eyre::Result<()> {
// Metadata
if let Some(author) = &meta.author {
let author = pdf_doc_encode(author)?;
hnd.info.author = Some(PdfString::new(author));
}
if let Some(subject) = &meta.subject {
let subject = pdf_doc_encode(subject)?;
hnd.info.subject = Some(PdfString::new(subject));
}
if let Some(title) = &meta.title {
let title = pdf_doc_encode(title)?;
hnd.info.title = Some(PdfString::new(title));
}
let creator = pdf_doc_encode("SIGNUM © 1986-93 F. Schmerbeck")?;
hnd.info.creator = Some(PdfString::new(creator));
let producer = pdf_doc_encode("Signum! Document Toolbox")?;
hnd.info.producer = Some(PdfString::new(producer));

let now = Local::now();
hnd.info.creation_date = Some(now);
hnd.info.mod_date = Some(now);
pub fn prepare_meta(hnd: &mut Handle, meta: &MetaInfo) -> eyre::Result<()> {
prepare_info(&mut hnd.info, meta)?;

// Output intents
hnd.output_intents.push(OutputIntent {
Expand All @@ -62,7 +41,7 @@ pub fn prepare_document(
hnd: &mut Handle,
doc: &Document,
di: &DocumentInfo,
meta: &Meta,
meta: &Overrides,
font_info: &Fonts,
) -> eyre::Result<()> {
let print = &di.fonts;
Expand Down Expand Up @@ -153,9 +132,9 @@ pub fn prepare_document(
let xmargin = (a4_width - width as i32) / 2;
let ymargin = (a4_height - height) / 2;

let left = xmargin as f32 + meta.xoffset.unwrap_or(0) as f32;
let left = xmargin as f32 + meta.xoffset as f32;
let left = left - page_info.format.left as f32 * 8.0 / 10.0;
let top = ymargin as f32 + meta.yoffset.unwrap_or(0) as f32;
let top = ymargin as f32 + meta.yoffset as f32;
let top = a4_height as f32 - top - 8.0;
let media_box = Rectangle::media_box(a4_width, a4_height);

Expand Down Expand Up @@ -225,19 +204,19 @@ pub fn prepare_document(
Ok(())
}

fn doc_meta<'a>(doc: &'a Document) -> eyre::Result<Cow<'a, Meta>> {
fn doc_meta(doc: &Document) -> eyre::Result<(MetaInfo, Overrides)> {
let meta = doc.opt.meta()?;
if meta.title.is_none() {
let mut meta = meta.into_owned();
let file_name = doc.opt.file.file_name().unwrap();
let title = file_name
.to_str()
.ok_or_else(|| eyre!("File name contains invalid characters"))?;
meta.title = Some(title.to_owned());
Ok(Cow::Owned(meta))
} else {
Ok(meta)
}
let file_name = doc
.opt
.file
.file_name()
.ok_or_eyre("expect file to have name")?;
let file_name = file_name
.to_str()
.ok_or_eyre("File name contains invalid characters")?;
let info = meta.pdf_meta_info(file_name);
let overrides = meta.to_overrides();
Ok((info, overrides))
}

pub fn process_doc<'a>(
Expand All @@ -248,7 +227,7 @@ pub fn process_doc<'a>(
) -> eyre::Result<Handle<'a>> {
let mut hnd = Handle::new();

let meta = doc_meta(doc)?;
let (meta, overrides) = doc_meta(doc)?;
prepare_meta(&mut hnd, &meta)?;

let use_matrix = doc.use_matrix();
Expand All @@ -269,7 +248,7 @@ pub fn process_doc<'a>(
hnd.res.fonts.push(font);
}

prepare_document(&mut hnd, doc, di, &meta, &font_info)?;
prepare_document(&mut hnd, doc, di, &overrides, &font_info)?;
Ok(hnd)
}

Expand Down

0 comments on commit 45b994c

Please sign in to comment.