Skip to content

Commit

Permalink
Adding TextContext to try to simplify text rendering. (Moved pdfutils…
Browse files Browse the repository at this point in the history
… to subdir.)
  • Loading branch information
gmadrid committed Jan 5, 2023
1 parent 8816724 commit 80832cd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
19 changes: 10 additions & 9 deletions src/bin/weekly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use argh::FromArgs;
use printpdf::PdfDocumentReference;
use weekly::{
save_one_page_document, sizes, Attributes, Circle, Colors, FontProxy, GridDescription,
Instructions, LineModifiers, NumericUnit, Result, TGrid, ToPdfLine, Unit, WLine, WRect,
Instructions, LineModifiers, NumericUnit, Result, TGrid, TextContext, ToPdfLine, Unit, WLine,
WRect,
};

const GOLDEN_RATIO: f64 = 1.618033988749894;
Expand Down Expand Up @@ -253,8 +254,10 @@ fn render_tracker(
8,
top_text_offset,
|rect, row, instructions| {
let text_context =
TextContext::helvetica().with_text_height((rect.height() - 1.0.mm()) * 1.9);

let small_grid_left = rect.right() - rect.height() * 7.0;
let text_height = ((rect.height() - 1.0.mm()) * 1.9).to_mm();
if row > 0 {
instructions.push_state();
instructions.set_stroke_color(Colors::gray(0.75));
Expand All @@ -269,14 +272,14 @@ fn render_tracker(
// Top row labels
instructions.push_state();
instructions.set_fill_color(Colors::white());
let bold_context = text_context.bold(true);
for (i, letter) in DAY_LETTERS.iter().enumerate() {
let l = small_grid_left + rect.height() * i as f64;
instructions.push_text(
bold_context.render(
letter,
text_height,
l + 1.4.mm(),
rect.bottom_q1() + 1.5.mm(),
FontProxy::Helvetica(true, false),
instructions,
);
}

Expand All @@ -286,12 +289,11 @@ fn render_tracker(
if row > 0 && row < HABITS.len() + 1 {
instructions.push_state();
instructions.set_fill_color(Colors::black());
instructions.push_text(
text_context.render(
HABITS[row - 1],
text_height,
rect.left() + 1.5.mm(),
rect.bottom_q1() + 1.5.mm(),
FontProxy::Helvetica(false, false),
instructions,
);
instructions.pop_state();
}
Expand Down Expand Up @@ -337,7 +339,6 @@ fn render_dotted(_: &PdfDocumentReference, dotted_rect: &WRect) -> Result<Instru
instructions.set_fill_color(Colors::gray(0.7));
let grid_spacing = 0.25.inches();

//let radius = 0.25.mm();
let base_circle = Circle::at_zero(0.25.mm());
let mut x = dotted_rect.left() + grid_spacing;
while x <= dotted_rect.right() - grid_spacing {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use thiserror::Error;
pub use datetools::{today, Datetools};
pub use pdfutils::sizes;
pub use pdfutils::{
save_one_page_document, Attributes, Colors, FontProxy, Instructions, LineModifiers,
save_one_page_document, Attributes, Colors, FontProxy, Instructions, LineModifiers, TextContext,
};
pub use shapes::circle::Circle;
pub use shapes::line::WLine;
Expand Down
22 changes: 20 additions & 2 deletions src/pdfutils.rs → src/pdfutils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod text_context;

use crate::units::Unit;
use crate::{Result, ToPdfLine, WRect};
use printpdf::*;
Expand All @@ -7,6 +9,8 @@ use std::hash::Hash;
use std::io::BufWriter;
use std::path::Path;

pub use text_context::TextContext;

pub fn point_pair(x: Unit, y: Unit, next: bool) -> (Point, bool) {
(Point::new(x.into(), y.into()), next)
}
Expand Down Expand Up @@ -145,11 +149,25 @@ pub enum FontProxy {
}

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(true, false)
FontProxy::times().bold(true)
}
pub fn helvetica_bold() -> FontProxy {
FontProxy::Helvetica(true, false)
//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),
}
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/pdfutils/text_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{FontProxy, Instructions, NumericUnit, Unit};

#[derive(Debug, Clone)]
pub struct TextContext {
proxy: FontProxy,
text_height: Unit,
}

impl TextContext {
pub fn times() -> TextContext {
TextContext {
proxy: FontProxy::Times(false, false),
..Default::default()
}
}

pub fn helvetica() -> TextContext {
TextContext {
proxy: FontProxy::Helvetica(false, false),
..Default::default()
}
}

pub fn with_text_height(&self, text_height: Unit) -> TextContext {
TextContext {
text_height,
..*self
}
}

pub fn bold(&self, bold: bool) -> TextContext {
TextContext {
proxy: self.proxy.bold(bold),
..*self
}
}

pub fn render(&self, txt: impl AsRef<str>, x: Unit, y: Unit, instructions: &mut Instructions) {
instructions.push_text(txt.as_ref(), self.text_height.to_mm(), x, y, self.proxy);
}
}

impl Default for TextContext {
fn default() -> Self {
TextContext {
proxy: Default::default(),
text_height: 12.0.mm(),
}
}
}

0 comments on commit 80832cd

Please sign in to comment.