Skip to content

Commit

Permalink
Refactoring push_shape to make it less awkward. Still not happy.
Browse files Browse the repository at this point in the history
(Also refactoring a bunch of stuff under pdfutils into its own files.)
  • Loading branch information
gmadrid committed Jan 6, 2023
1 parent efa9840 commit 679ac76
Show file tree
Hide file tree
Showing 21 changed files with 207 additions and 225 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@ name = "magic"

[[bin]]
name = "weekly"

[[bin]]
name = "remtest"
5 changes: 1 addition & 4 deletions src/bin/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ impl GridDescription for ActiveDescription {
cell_rect.left() + self.task_height / 4,
cell_rect.top() - self.task_height / 4,
);
let mut shape = check_rect.to_pdf_line();
shape.has_fill = false;
shape.has_stroke = true;
instructions.push_shape(shape);
instructions.push_shape(check_rect.to_stroked_line());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bin/cornell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ fn render_cornell(_: &PdfDocumentReference, device_rect: &WRect) -> weekly::Resu
device_rect.right(),
bottom_line_y,
);
instructions.push_shape(notes_bottom_line.to_pdf_line());
instructions.push_shape(notes_bottom_line.to_stroked_line());

let left_line_x = device_rect.width().pct(100.0 - NOTE_HORIZ_PCT);

let notes_left_line = WLine::line(left_line_x, bottom_line_y, left_line_x, device_rect.top());
instructions.push_shape(notes_left_line.to_pdf_line());
instructions.push_shape(notes_left_line.to_stroked_line());

let grid_rect = WRect::with_dimensions(
device_rect.right() - left_line_x,
Expand Down
8 changes: 4 additions & 4 deletions src/bin/daily.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use printpdf::{Color, PdfDocumentReference};
use std::borrow::Cow;
use std::path::PathBuf;
use weekly::{
save_one_page_document, sizes, Attributes, Colors, Datetools, LineModifiers, NumericUnit,
Result, TGrid, ToPdfLine, Unit, WRect,
save_one_page_document, sizes, Attributes, Colors, Datetools, NumericUnit, Result, TGrid,
ToPdfLine, Unit, WRect,
};
use weekly::{GridDescription, Instructions};

Expand Down Expand Up @@ -252,7 +252,7 @@ impl GridDescription for DailyDescription {
let date = &self.dates_in_month[row];
if !day_set.contains(&date.weekday()) {
instructions.set_fill_color(Colors::gray(0.7));
instructions.push_shape(cell_rect.to_pdf_line());
instructions.push_shape(cell_rect.to_filled_line());
should_draw_checkbox = false;
}
}
Expand All @@ -277,7 +277,7 @@ fn render_checkbox(cell_rect: &WRect, instructions: &mut Instructions) {
instructions.set_stroke_color(Colors::gray(0.25));
instructions.set_stroke_width(0.0);

instructions.push_shape(checkbox_rect.to_pdf_line().fill(false).stroke(true));
instructions.push_shape(checkbox_rect.to_stroked_line());
}

fn render_dailies(
Expand Down
5 changes: 3 additions & 2 deletions src/bin/monthlies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use chrono::{Datelike, NaiveDate};
use printpdf::*;
use std::borrow::Cow;
use std::path::PathBuf;
use weekly::FontProxy;
use weekly::{
save_one_page_document, Colors, Datetools, FontProxy, GridDescription, Instructions,
NumericUnit, TGrid, Unit, WRect,
save_one_page_document, Colors, Datetools, GridDescription, Instructions, NumericUnit, TGrid,
Unit, WRect,
};

#[derive(FromArgs)]
Expand Down
4 changes: 2 additions & 2 deletions src/bin/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn fill_project_into_rect(rect: WRect, instructions: &mut Instructions) {
rect.right(),
rect.top() - 0.25.inches(),
)
.to_pdf_line(),
.to_stroked_line(),
);

instructions.set_stroke_color(Colors::gray(0.75));
Expand All @@ -80,7 +80,7 @@ fn fill_box_with_lines(boxx: &WRect, offset: Unit, gap: Unit, instructions: &mut

while curr_y > boxx.bottom_q1() {
let line = WLine::line(boxx.left(), curr_y, boxx.right(), curr_y);
instructions.push_shape(line.to_pdf_line());
instructions.push_shape(line.to_stroked_line());
curr_y = curr_y - gap;
}
}
30 changes: 0 additions & 30 deletions src/bin/remtest.rs

This file was deleted.

16 changes: 5 additions & 11 deletions src/bin/weekly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<F: Fn(&WRect, usize, &mut Instructions)> GridDescription for SimpleDescript

if row == 0 {
instructions.set_fill_color(Colors::black());
instructions.push_shape(cell_rect);
instructions.push_shape(cell_rect.to_filled_line());

instructions.set_fill_color(Colors::white());
self.text_context
Expand Down Expand Up @@ -151,7 +151,7 @@ fn render_lines<T: AsRef<str>, F: Fn(&WRect, usize, &mut Instructions)>(
.set_offset(offset);
let tgrid = TGrid::with_description(description);

instructions.push_shape(table_rect.to_pdf_line().fill(false).stroke(true));
instructions.push_shape(table_rect.to_stroked_line());

tgrid.append_to_instructions(instructions);
}
Expand Down Expand Up @@ -190,8 +190,8 @@ fn render_days(rect: &WRect, text_context: &TextContext, instructions: &mut Inst
rect.left() + radius + 2.0.mm(),
rect.bottom_q1() + radius / 2.0 + 0.8.mm(),
)
.to_pdf_line()
.fill(true),
.to_filled_line()
.stroke(true),
);
instructions.pop_state();
}
Expand Down Expand Up @@ -366,13 +366,7 @@ fn render_dotted(_: &PdfDocumentReference, dotted_rect: &WRect, instructions: &m
let mut y = dotted_rect.top() - grid_spacing;

while y >= dotted_rect.bottom_q1() + grid_spacing {
instructions.push_shape(
base_circle
.move_to(x, y)
.to_pdf_line()
.fill(true)
.stroke(false),
);
instructions.push_shape(base_circle.move_to(x, y).to_filled_line());

y = y - grid_spacing;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use thiserror::Error;

pub use datetools::{today, Datetools};
pub use pdfutils::sizes;
pub use pdfutils::FontProxy;
pub use pdfutils::{
save_one_page_document, Attributes, Colors, FontProxy, Instructions, LineModifiers, TextContext,
save_one_page_document, Attributes, Colors, Instructions, LineModifiers, TextContext,
};
pub use shapes::circle::Circle;
pub use shapes::line::WLine;
Expand Down
42 changes: 42 additions & 0 deletions src/pdfutils/font_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::pdfutils::font_proxy::FontProxy;
use crate::pdfutils::Instruction;
use crate::Instructions;
use printpdf::{IndirectFontRef, PdfDocumentReference};
use std::collections::HashMap;

#[derive(Debug, Default)]
pub struct FontMap(HashMap<FontProxy, IndirectFontRef>);

impl FontMap {
pub fn resolve_fonts(
mut self,
doc: &PdfDocumentReference,
instructions: &Instructions,
) -> crate::Result<FontMap> {
// Look for all of the fonts referenced in the Instructions,
// add them to the PdfDocument, adding the fonts to map.
instructions
.instructions
.iter()
.filter_map(|i| match i {
Instruction::Text(tv) => Some(tv.font),
_ => None,
})
.try_for_each::<_, crate::Result<()>>(|font| {
let entry = self.0.entry(font);

// Basically doing or_insert_with(), but I need to propagate an error.
if let std::collections::hash_map::Entry::Vacant(ve) = entry {
let indirect_font = doc.add_builtin_font(font.into())?;
ve.insert(indirect_font);
}
Ok(())
})?;
Ok(self)
}

pub fn lookup(&self, font_proxy: FontProxy) -> &IndirectFontRef {
// unwrap: can we get rid of this?
self.0.get(&font_proxy).unwrap()
}
}
66 changes: 66 additions & 0 deletions src/pdfutils/font_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use printpdf::BuiltinFont;

#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub enum FontProxy {
// first bool is Bold, second bool is Italics
Helvetica(bool, bool),
Times(bool, bool),
}

impl FontProxy {
pub fn times() -> FontProxy {
FontProxy::Times(false, false)
}
pub fn helvetica() -> FontProxy {
FontProxy::Helvetica(false, false)
}
pub fn times_bold() -> FontProxy {
//FontProxy::Times(true, false)
FontProxy::times().bold(true)
}
pub fn helvetica_bold() -> FontProxy {
//FontProxy::Helvetica(true, false)
FontProxy::helvetica().bold(true)
}
pub fn bold(&self, bold: bool) -> FontProxy {
match self {
FontProxy::Helvetica(_, it) => FontProxy::Helvetica(bold, *it),
FontProxy::Times(_, it) => FontProxy::Times(bold, *it),
}
}
}

impl Default for FontProxy {
fn default() -> Self {
FontProxy::Times(false, false)
}
}

impl From<FontProxy> for BuiltinFont {
fn from(font_proxy: FontProxy) -> Self {
match font_proxy {
FontProxy::Helvetica(bold, italic) => {
if bold && italic {
BuiltinFont::HelveticaBoldOblique
} else if bold {
BuiltinFont::HelveticaBold
} else if italic {
BuiltinFont::HelveticaOblique
} else {
BuiltinFont::Helvetica
}
}
FontProxy::Times(bold, italic) => {
if bold && italic {
BuiltinFont::TimesBoldItalic
} else if bold {
BuiltinFont::TimesBold
} else if italic {
BuiltinFont::TimesItalic
} else {
BuiltinFont::TimesRoman
}
}
}
}
}
Loading

0 comments on commit 679ac76

Please sign in to comment.