Find optimal fontsize
This commit is contained in:
parent
0b2e397d56
commit
46386c8e46
|
@ -4,28 +4,69 @@ use std::process::Command;
|
|||
use anyhow::{bail, Result};
|
||||
use regex::Regex;
|
||||
|
||||
pub fn linear(content: &str, output: PathBuf, metadata: Vec<(String, String)>) -> Result<u8> {
|
||||
pub fn linear(content: &str, output: PathBuf, basesize: &u8, metadata: Vec<(String, String)>) -> Result<u8> {
|
||||
let mut pandoc = pandoc::new();
|
||||
pandoc.set_input(pandoc::InputKind::Pipe(content.to_string()));
|
||||
|
||||
pandoc.set_output(pandoc::OutputKind::File(output.clone()));
|
||||
|
||||
pandoc.set_input_format(pandoc::InputFormat::Commonmark, Vec::new());
|
||||
pandoc.set_output_format(pandoc::OutputFormat::Latex, Vec::new());
|
||||
pandoc.add_option(pandoc::PandocOption::PdfEngine("pdflatex".into()));
|
||||
// TODO: configurable template
|
||||
pandoc.set_latex_template("eisvogel");
|
||||
//pandoc.set_variable("links-as-notes", "true");
|
||||
//pandoc.set_variable("fontsize", "12pt");
|
||||
for (key, value) in metadata {
|
||||
pandoc.set_variable(&key, &value);
|
||||
}
|
||||
|
||||
let mut current_size = basesize.clone();
|
||||
let mut sheets: Option<u8> = None;
|
||||
while current_size <= basesize + 10 {
|
||||
println!("DOING SIZE {}", current_size);
|
||||
pandoc.set_variable("fontsize", &format!("{}pt", current_size));
|
||||
let current_output = sized_output(&output, ¤t_size);
|
||||
let pages = make_linear(&mut pandoc, ¤t_output)?;
|
||||
let current_sheets = match pages % 4 {
|
||||
0 => pages / 4 + 1,
|
||||
_ => pages / 4
|
||||
};
|
||||
|
||||
match sheets {
|
||||
None => {
|
||||
sheets = Some(current_sheets);
|
||||
},
|
||||
Some(s) => {
|
||||
if s != current_sheets {
|
||||
// We have grown too big, remove current iteration
|
||||
// and place the previous one to output
|
||||
std::fs::remove_file(current_output)?;
|
||||
current_size = current_size - 1;
|
||||
let current_output = sized_output(&output, ¤t_size);
|
||||
std::fs::rename(¤t_output, &output)?;
|
||||
return count_pages(&output);
|
||||
} else {
|
||||
// We haven't grown too big, remove the previous iteration output
|
||||
let previous_size = current_size - 1;
|
||||
std::fs::remove_file(sized_output(&output, &previous_size))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_size = current_size + 1;
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
/*
|
||||
pandoc.execute()?;
|
||||
|
||||
let pages_count = count_pages(&output)?;
|
||||
|
||||
Ok(pages_count)
|
||||
*/
|
||||
}
|
||||
|
||||
fn make_linear(pandoc: &mut pandoc::Pandoc, output: &Path) -> Result<u8> {
|
||||
pandoc.set_output(pandoc::OutputKind::File(output.to_path_buf()));
|
||||
pandoc.clone().execute()?;
|
||||
Ok(count_pages(&output)?)
|
||||
}
|
||||
|
||||
pub fn book(input: &Path, output: &Path) -> Result<()> {
|
||||
|
@ -63,3 +104,9 @@ pub fn count_pages(pdf_file: &Path) -> Result<u8> {
|
|||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
fn sized_output(output: &Path, size: &u8) -> PathBuf {
|
||||
let mut sized_output = output.to_path_buf();
|
||||
sized_output.set_extension(&format!("{}.pdf", size));
|
||||
sized_output
|
||||
}
|
||||
|
|
|
@ -54,8 +54,7 @@ fn main() -> Result<()> {
|
|||
|
||||
let source = read_to_string(&cli.input)?;
|
||||
|
||||
let (content, mut metadata) = frontmatter::extract(&source)?;
|
||||
metadata.push(("fontsize".to_string(), format!("{}pt", cli.size)));
|
||||
let (content, metadata) = frontmatter::extract(&source)?;
|
||||
|
||||
if cli.debug {
|
||||
println!("{}", content);
|
||||
|
@ -80,19 +79,19 @@ fn main() -> Result<()> {
|
|||
tmp.push(hasher.finish().to_string());
|
||||
tmp.set_extension("pdf");
|
||||
|
||||
let pages_count = generate::linear(&content, tmp.clone(), metadata)?;
|
||||
let pages_count = generate::linear(&content, tmp.clone(), &cli.size, metadata)?;
|
||||
println!("Intermediary step: generated linear output to {:?} ({} pages)", &tmp, pages_count);
|
||||
generate::book(&tmp, &output)?;
|
||||
println!("Generated A5 booklet output to {:?}", output);
|
||||
},
|
||||
(false, true) => {
|
||||
// Only linear output
|
||||
let pages_count = generate::linear(&content, output.clone(), metadata)?;
|
||||
let pages_count = generate::linear(&content, output.clone(), &cli.size, metadata)?;
|
||||
println!("Generated linear A4 output to {:?} ({} pages)", output, pages_count);
|
||||
},
|
||||
(false, false) => {
|
||||
// Both outputs
|
||||
let pages_count = generate::linear(&content, output.clone(), metadata)?;
|
||||
let pages_count = generate::linear(&content, output.clone(), &cli.size, metadata)?;
|
||||
println!("Generated linear A4 output to {:?} ({} pages)", output, pages_count);
|
||||
let mut book_output = output.clone();
|
||||
book_output.set_extension("book.pdf");
|
||||
|
|
Loading…
Reference in New Issue