Skip to content

Commit

Permalink
Adding second (flippable) page to weekly.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmadrid committed Jan 6, 2023
1 parent 26931b7 commit 8800ccb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
35 changes: 27 additions & 8 deletions src/bin/weekly.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use argh::FromArgs;
use printpdf::PdfDocumentReference;
use weekly::{
save_double_sided_document, sizes, Attributes, Circle, Colors, GridDescription, HasRenderAttrs,
Instructions, NumericUnit, Result, TGrid, TextContext, Unit, WLine, WRect,
save_double_sided_document, save_one_page_document, sizes, Attributes, Circle, Colors,
GridDescription, HasRenderAttrs, Instructions, NumericUnit, Result, TGrid, TextContext, Unit,
WLine, WRect,
};

const GOLDEN_RATIO: f64 = 1.618033988749894;
Expand Down Expand Up @@ -30,6 +31,14 @@ struct Args {
/// name of the output file
#[argh(positional, default = "String::from(\"weekly.pdf\")")]
output_filename: String,

/// print twice on two pages
#[argh(switch, short = '2')]
twice: bool,

/// flip the second page
#[argh(switch, short = 'f')]
flip: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -393,10 +402,20 @@ pub fn main() -> Result<()> {

let page_rect = sizes::letter();

save_double_sided_document(
"Productivity Tracker",
args.output_filename,
&page_rect,
render_weekly_page,
)
if args.twice {
save_double_sided_document(
"Productivity Tracker",
args.output_filename,
&page_rect,
args.flip,
render_weekly_page,
)
} else {
save_one_page_document(
"Productivity Tracker",
args.output_filename,
&page_rect,
render_weekly_page,
)
}
}
13 changes: 12 additions & 1 deletion src/pdfutils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ pub fn save_double_sided_document<F>(
title: &str,
filename: impl AsRef<Path>,
page_bounds: &WRect,
flip_page_2: bool,
callback: F,
) -> Result<()>
where
Expand All @@ -335,7 +336,17 @@ where
"Layer 1",
);

instructions.draw_to_layer(&doc, &doc.get_page(page2).get_layer(layer1))?;
let layer1_ref = &doc.get_page(page2).get_layer(layer1);
layer1_ref.save_graphics_state();

if flip_page_2 {
let width_mm: Mm = page_bounds.width().into();
let height_mm: Mm = page_bounds.height().into();
let mat = CurTransMat::TranslateRotate(width_mm.into_pt(), height_mm.into_pt(), 180.0);
layer1_ref.set_ctm(mat);
}
instructions.draw_to_layer(&doc, layer1_ref)?;
layer1_ref.restore_graphics_state();

doc.save(&mut BufWriter::new(File::create(filename)?))
.map_err(|e| e.into())
Expand Down

0 comments on commit 8800ccb

Please sign in to comment.