Skip to content

Commit

Permalink
Making Unit more unit-preserving.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmadrid committed Jan 5, 2023
1 parent d01ef3d commit 427af94
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/bin/cornell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn compute_bottom_line_y(device_rect: &WRect) -> Unit {
let lines = cornell_height / rule_height;
device_rect.height()
- if rule_height * lines != cornell_height {
rule_height * (lines + 1)
rule_height * (lines + 1.0)
} else {
rule_height
}
Expand Down
83 changes: 43 additions & 40 deletions src/bin/weekly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<F: Fn(&WRect, usize, &mut Instructions)> GridDescription for SimpleDescript
instructions.set_fill_color(Colors::white());
instructions.push_text(
&self.text,
((cell_rect.height() - 1.0.mm()) * 1.9).into(),
((cell_rect.height() - 1.0.mm()) * 1.9).to_mm(),
cell_rect.left() + self.offset,
cell_rect.bottom_q1() + 1.5.mm(),
FontProxy::Helvetica(true, false),
Expand Down Expand Up @@ -117,8 +117,8 @@ fn render_lines<T: AsRef<str>, F: Fn(&WRect, usize, &mut Instructions)>(
let table_rect = rect.resize(rect.width(), rect.height() - line_space);
instructions.push_shape(table_rect.as_pdf_line().fill(false).stroke(true));

let description =
SimpleDescription::new(&table_rect, num_rows, text.as_ref(), render_func).set_offset(offset);
let description = SimpleDescription::new(&table_rect, num_rows, text.as_ref(), render_func)
.set_offset(offset);
let tgrid = TGrid::with_description(description);

tgrid.append_to_instructions(&mut instructions);
Expand All @@ -130,7 +130,7 @@ fn render_left_circle(rect: &WRect, instructions: &mut Instructions) {
let radius = rect.height() / 2.0;

let points = printpdf::utils::calculate_points_for_circle(
radius - 1.15,
radius - 1.15.mm(),
rect.left() + radius,
rect.bottom_q1() + radius,
);
Expand All @@ -153,37 +153,40 @@ fn render_days(rect: &WRect) -> Instructions {

let day_rect = rect.resize(day_width, rect.height());
for (i, abbrev) in DAY_ABBREVS.iter().enumerate() {
instructions.append(render_lines(
&day_rect.move_by(day_width * i as f64, 0.0.mm()),
abbrev,
14,
12.0.mm(),
|rect, idx, instructions| {
if idx == 0 {
let radius = rect.height() / 2.0 + 1.5.mm();
instructions.push_state();
instructions.set_fill_color(Colors::white());
instructions.set_stroke_color(Colors::gray(0.6));
instructions.set_stroke_width(1.0);

let points = printpdf::utils::calculate_points_for_circle(
radius,
rect.left() + radius + 2.0.mm(),
rect.bottom_q1() + radius / 2.0 + 0.8.mm(),
);
let circle = Line {
points,
is_closed: true,
has_fill: true,
has_stroke: true,
is_clipping_path: false,
};
instructions.push_shape(circle);

instructions.pop_state();
}
},
).unwrap());
instructions.append(
render_lines(
&day_rect.move_by(day_width * i as f64, 0.0.mm()),
abbrev,
14,
12.0.mm(),
|rect, idx, instructions| {
if idx == 0 {
let radius = rect.height() / 2.0 + 1.5.mm();
instructions.push_state();
instructions.set_fill_color(Colors::white());
instructions.set_stroke_color(Colors::gray(0.6));
instructions.set_stroke_width(1.0);

let points = printpdf::utils::calculate_points_for_circle(
radius,
rect.left() + radius + 2.0.mm(),
rect.bottom_q1() + radius / 2.0 + 0.8.mm(),
);
let circle = Line {
points,
is_closed: true,
has_fill: true,
has_stroke: true,
is_clipping_path: false,
};
instructions.push_shape(circle);

instructions.pop_state();
}
},
)
.unwrap(),
);
}

instructions
Expand Down Expand Up @@ -222,7 +225,7 @@ fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instruct
},
)?);

let tracker_rect = priorities_rect.move_by(grid_x * 2.0, 0.0.into());
let tracker_rect = priorities_rect.move_by(grid_x * 2.0, Unit::zero());
instructions.append(render_lines(
&tracker_rect,
"Habit Tracker",
Expand All @@ -244,10 +247,10 @@ fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instruct
instructions.push_state();
instructions.set_fill_color(Colors::white());
for (i, letter) in DAY_LETTERS.iter().enumerate() {
let l = small_grid_left + rect.height() * i;
let l = small_grid_left + rect.height() * i as f64;
instructions.push_text(
letter,
((rect.height() - 1.0.mm()) * 1.9).into(),
((rect.height() - 1.0.mm()) * 1.9).to_mm(),
l + 1.4.mm(),
rect.bottom_q1() + 1.5.mm(),
FontProxy::Helvetica(true, false),
Expand All @@ -260,7 +263,7 @@ fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instruct
)?);

let weekend_rect = tracker_rect
.move_by(grid_x * 2.0, 0.0.into())
.move_by(grid_x * 2.0, Unit::zero())
.resize(grid_x, priorities_rect.height());
instructions.append(render_lines(
&weekend_rect,
Expand All @@ -272,7 +275,7 @@ fn render_weekly(_: &PdfDocumentReference, page_rect: &WRect) -> Result<Instruct

let calendar_rect = print_rect
.resize(print_rect.width(), bottom_height)
.move_by(0.0.into(), -top_height);
.move_by(Unit::zero(), -top_height);
instructions.append(render_days(&calendar_rect));

Ok(instructions)
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl WRect {
}

pub fn as_rounded_rect_shape(&self, radius: Unit) -> Line {
let pv = Unit::from(1.0 - 0.55228);
let pv = 1.0_f64 - 0.55228;
Line {
points: vec![
point_pair(self.right() - radius, self.top, true),
Expand Down
32 changes: 19 additions & 13 deletions src/tgrid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ where

fn render_horizontal_lines(&self, instructions: &mut Instructions) {
let left = self.params.grid_bounds.left();
let right =
left + self.params.row_label_width + self.params.col_width * self.params.num_cols;
let right = left
+ self.params.row_label_width
+ self.params.col_width * self.params.num_cols as f64;
let top = self.params.grid_bounds.top() - self.params.col_label_height;
let num_rows = self.params.num_rows;
for row in 0..=num_rows {
if let Some(attrs) = self.params.horiz_line_style(row, num_rows) {
attrs.render(instructions, |instructions| {
let y = top - self.params.row_height * row;
let y = top - self.params.row_height * row as f64;
instructions.push_shape(WLine::line(left, y, right, y).as_pdf_line());
});
}
Expand All @@ -40,26 +41,31 @@ where

fn render_vertical_lines(&self, instructions: &mut Instructions) {
let top = self.params.grid_bounds.top();
let bottom =
top - self.params.col_label_height - self.params.row_height * self.params.num_rows;
let bottom = top
- self.params.col_label_height
- self.params.row_height * self.params.num_rows as f64;
let left = self.params.grid_bounds.left() + self.params.row_label_width;
let num_cols = self.params.num_cols;
for col in 0..=num_cols {
if let Some(attrs) = self.params.vert_line_style(col, num_cols) {
attrs.render(instructions, |instructions| {
let x = left + self.params.col_width * col;
let x = left + self.params.col_width * col as f64;
instructions.push_shape(WLine::line(x, top, x, bottom).as_pdf_line())
});
}
}
}

fn row_y(&self, row: usize) -> Unit {
self.params.grid_bounds.top() - self.params.col_label_height - self.params.row_height * row
self.params.grid_bounds.top()
- self.params.col_label_height
- self.params.row_height * row as f64
}

fn col_x(&self, col: usize) -> Unit {
self.params.grid_bounds.left() + self.params.row_label_width + self.params.col_width * col
self.params.grid_bounds.left()
+ self.params.row_label_width
+ self.params.col_width * col as f64
}

fn render_row_labels(&self, instructions: &mut Instructions) {
Expand All @@ -70,12 +76,12 @@ where
let row_height = self.params.row_height;

let x = self.params.grid_bounds.left() + 2.0.mm();
let text_height = f64::from(row_height) * 1.9;
let text_height = row_height * 1.9;
for row in 0..self.params.num_rows {
let y = self.row_y(row + 1) + 1.5.mm();
instructions.push_text(
self.params.row_label(row).as_ref(),
text_height,
text_height.to_mm(),
x,
y,
self.params.font,
Expand All @@ -91,7 +97,7 @@ where
let row_height = self.params.row_height;

// (159, -21) after rotation.
let text_height = f64::from(row_height) * 1.9;
let text_height = row_height * 1.9;
let y = self.params.grid_bounds.top() - self.params.col_label_height + 1.0.mm();
for col in 0..self.params.num_cols {
let x = self.col_x(col + 1) - 1.0.mm();
Expand All @@ -103,7 +109,7 @@ where
// Text position is (0.0), so that we can rotate the text before translating it.
instructions.push_text(
self.params.col_label(col).as_ref(),
text_height,
text_height.to_mm(),
Unit::zero(),
Unit::zero(),
self.params.font,
Expand All @@ -115,7 +121,7 @@ where
fn render_column_backgrounds(&self, instructions: &mut Instructions) {
let base_col_rect = WRect::with_dimensions(
self.params.col_width,
self.params.col_label_height + self.params.row_height * self.params.num_rows,
self.params.col_label_height + self.params.row_height * self.params.num_rows as f64,
);

for col in 0..self.params.num_cols {
Expand Down
10 changes: 6 additions & 4 deletions src/tgrid/renderparams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,25 @@ where
let num_rows = description.num_rows().unwrap_or_else(|| {
// unwrap: we check that both num_rows and row_height cannot be none.
// If num_rows isn't set, we compute it from the label size, bounds, and cell size.
(grid_bounds.height() - col_label_height) / description.row_height().unwrap()
((grid_bounds.height() - col_label_height) / description.row_height().unwrap()).trunc()
as usize
});
let num_cols = description.num_cols().unwrap_or_else(|| {
// unwrap: we check that both num_cols and col_width cannot be none.
// If num_cols isn't set, we compute it from the label size, bounds, and cell size.
(grid_bounds.width() - row_label_width) / description.col_width().unwrap()
((grid_bounds.width() - row_label_width) / description.col_width().unwrap()).trunc()
as usize
});

let row_height = description.row_height().unwrap_or_else(|| {
// unwrap: we check that both num_rows and row_height cannot be none.
// If row_height isn't set, we compute is from the label size, bounds, and num rows.
(grid_bounds.height() - col_label_height) / description.num_rows().unwrap()
(grid_bounds.height() - col_label_height) / description.num_rows().unwrap() as f64
});
let col_width = description.col_width().unwrap_or_else(|| {
// unwrap: we check that both num_cols and col_width cannot be none.
// If col_width isn't set, we compute is from the label size, bounds, and num cols.
(grid_bounds.width() - row_label_width) / description.num_cols().unwrap()
(grid_bounds.width() - row_label_width) / description.num_cols().unwrap() as f64
});

let font = description.font();
Expand Down
Loading

0 comments on commit 427af94

Please sign in to comment.