Skip to content

Commit

Permalink
Refactored Shape/Line/push_shape to make it a little more ergonomic.
Browse files Browse the repository at this point in the history
The Line conversion is now (mostly) invisible to the callers.
  • Loading branch information
gmadrid committed Jan 6, 2023
1 parent 679ac76 commit a18ddb1
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/bin/active.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use printpdf::*;
use weekly::{
save_one_page_document, Attributes, GridDescription, Instructions, NumericUnit, TGrid,
ToPdfLine, Unit, WRect,
save_one_page_document, Attributes, GridDescription, HasRenderAttrs, Instructions, NumericUnit,
TGrid, Unit, WRect,
};

struct ActiveDescription {
Expand Down Expand Up @@ -51,7 +51,7 @@ impl GridDescription for ActiveDescription {
cell_rect.left() + self.task_height / 4,
cell_rect.top() - self.task_height / 4,
);
instructions.push_shape(check_rect.to_stroked_line());
instructions.push_shape(check_rect.stroke());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/bin/cornell.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, Attributes, Colors, GridDescription, Instructions, TGrid, ToPdfLine,
Unit, WLine, WRect,
save_one_page_document, Attributes, Colors, GridDescription, HasRenderAttrs, Instructions,
TGrid, Unit, WLine, WRect,
};

const NOTE_HORIZ_PCT: f64 = 70.0;
Expand Down 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_stroked_line());
instructions.push_shape(notes_bottom_line.stroke());

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_stroked_line());
instructions.push_shape(notes_left_line.stroke());

let grid_rect = WRect::with_dimensions(
device_rect.right() - left_line_x,
Expand Down
9 changes: 5 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, NumericUnit, Result, TGrid,
ToPdfLine, Unit, WRect,
save_one_page_document, sizes, Attributes, Colors, Datetools, HasRenderAttrs, NumericUnit,
Result, TGrid, Unit, WRect,
};
use weekly::{GridDescription, Instructions};

Expand Down Expand Up @@ -252,7 +252,8 @@ 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_filled_line());
// TODO: can we get rid of this clone()?
instructions.push_shape(cell_rect.clone().fill());
should_draw_checkbox = false;
}
}
Expand All @@ -277,7 +278,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_stroked_line());
instructions.push_shape(checkbox_rect.stroke());
}

fn render_dailies(
Expand Down
6 changes: 3 additions & 3 deletions src/bin/projects.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, Colors, Instructions, LineModifiers, NumericUnit, ToPdfLine, Unit,
save_one_page_document, Colors, HasRenderAttrs, Instructions, LineModifiers, NumericUnit, Unit,
WLine, WRect,
};

Expand Down Expand Up @@ -63,7 +63,7 @@ fn fill_project_into_rect(rect: WRect, instructions: &mut Instructions) {
rect.right(),
rect.top() - 0.25.inches(),
)
.to_stroked_line(),
.stroke(),
);

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_stroked_line());
instructions.push_shape(line.stroke());
curr_y = curr_y - gap;
}
}
15 changes: 8 additions & 7 deletions src/bin/weekly.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use argh::FromArgs;
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, sizes, Attributes, Circle, Colors, GridDescription, Instructions,
LineModifiers, NumericUnit, Result, TGrid, TextContext, ToPdfLine, Unit, WLine, WRect,
save_one_page_document, sizes, Attributes, Circle, Colors, GridDescription, HasRenderAttrs,
Instructions, LineModifiers, NumericUnit, Result, TGrid, TextContext, Unit, WLine, WRect,
};

const GOLDEN_RATIO: f64 = 1.618033988749894;
Expand Down Expand Up @@ -108,7 +108,8 @@ impl<F: Fn(&WRect, usize, &mut Instructions)> GridDescription for SimpleDescript

if row == 0 {
instructions.set_fill_color(Colors::black());
instructions.push_shape(cell_rect.to_filled_line());
// TODO: can we get rid of this clone?
instructions.push_shape(cell_rect.clone().fill());

instructions.set_fill_color(Colors::white());
self.text_context
Expand Down Expand Up @@ -151,7 +152,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_stroked_line());
instructions.push_shape(table_rect.stroke());

tgrid.append_to_instructions(instructions);
}
Expand Down Expand Up @@ -190,8 +191,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_filled_line()
.stroke(true),
.fill()
.stroke(),
);
instructions.pop_state();
}
Expand Down Expand Up @@ -366,7 +367,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_filled_line());
instructions.push_shape(base_circle.move_to(x, y).fill());

y = y - grid_spacing;
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use pdfutils::{
pub use shapes::circle::Circle;
pub use shapes::line::WLine;
pub use shapes::rect::WRect;
pub use shapes::HasRenderAttrs;
pub use shapes::ToPdfLine;
pub use tgrid::description::GridDescription;
pub use tgrid::TGrid;
Expand Down
2 changes: 1 addition & 1 deletion src/pdfutils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Instructions {

pub fn push_shape(&mut self, shape: impl ToPdfLine) {
self.instructions
.push(Instruction::Shape(shape.to_pdf_line_basic()))
.push(Instruction::Shape(shape.to_pdf_line()))
}

pub fn push_text(&mut self, s: &str, text_height: f64, x: Unit, y: Unit, font: FontProxy) {
Expand Down
26 changes: 21 additions & 5 deletions src/shapes/circle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{NumericUnit, ToPdfLine, Unit};
use crate::shapes::{RenderAttrsImpl, ToPlainPdfLine};
use crate::{NumericUnit, Unit};
use printpdf::Line;

#[derive(Debug)]
pub struct Circle {
render_attrs: RenderAttrsImpl,

radius: Unit,
x: Unit,
y: Unit,
Expand All @@ -11,6 +14,7 @@ pub struct Circle {
impl Circle {
pub fn at_zero(radius: Unit) -> Circle {
Circle {
render_attrs: RenderAttrsImpl::default(),
radius,
x: Unit::zero(),
y: Unit::zero(),
Expand All @@ -19,6 +23,7 @@ impl Circle {

pub fn unit_at(x: Unit, y: Unit) -> Circle {
Circle {
render_attrs: RenderAttrsImpl::default(),
radius: 1.0.mm(),
x,
y,
Expand All @@ -34,11 +39,22 @@ impl Circle {
}
}

impl ToPdfLine for Circle {
fn to_pdf_line_basic(self) -> Line {
let points = printpdf::calculate_points_for_circle(self.radius, self.x, self.y);
impl AsRef<RenderAttrsImpl> for Circle {
fn as_ref(&self) -> &RenderAttrsImpl {
&self.render_attrs
}
}

impl AsMut<RenderAttrsImpl> for Circle {
fn as_mut(&mut self) -> &mut RenderAttrsImpl {
&mut self.render_attrs
}
}

impl ToPlainPdfLine for Circle {
fn to_plain_pdf_line(self) -> Line {
Line {
points,
points: printpdf::calculate_points_for_circle(self.radius, self.x, self.y),
is_closed: true,
..Line::default()
}
Expand Down
28 changes: 24 additions & 4 deletions src/shapes/line.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::pdfutils::point_pair;
use crate::shapes::ToPdfLine;
use crate::shapes::{RenderAttrsImpl, ToPlainPdfLine};
use crate::units::Unit;
use printpdf::Line;

#[derive(Debug)]
pub struct WLine {
render_attrs: RenderAttrsImpl,

x1: Unit,
y1: Unit,
x2: Unit,
Expand All @@ -13,12 +15,30 @@ pub struct WLine {

impl WLine {
pub fn line(x1: Unit, y1: Unit, x2: Unit, y2: Unit) -> WLine {
WLine { x1, y1, x2, y2 }
WLine {
render_attrs: RenderAttrsImpl::default(),
x1,
y1,
x2,
y2,
}
}
}

impl AsRef<RenderAttrsImpl> for WLine {
fn as_ref(&self) -> &RenderAttrsImpl {
&self.render_attrs
}
}

impl AsMut<RenderAttrsImpl> for WLine {
fn as_mut(&mut self) -> &mut RenderAttrsImpl {
&mut self.render_attrs
}
}

impl ToPdfLine for WLine {
fn to_pdf_line_basic(self) -> Line {
impl ToPlainPdfLine for WLine {
fn to_plain_pdf_line(self) -> Line {
Line {
points: vec![
point_pair(self.x1, self.y1, false),
Expand Down
Loading

0 comments on commit a18ddb1

Please sign in to comment.