forked from drewish/planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.rb
129 lines (114 loc) · 3.77 KB
/
shared.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'prawn'
require 'prawn/measurement_extensions'
require 'pry'
require 'date'
require_relative './config'
def init_pdf
pdf = Prawn::Document.new(margin: RIGHT_PAGE_MARGINS, print_scaling: :none, page_size: PAGE_SIZE)
pdf.font_families.update(FONTS)
pdf.font(FONTS.keys.first)
pdf.stroke_color MEDIUM_COLOR
pdf.line_width(0.5)
pdf
end
# Returns the date and an explanation of it
def parse_start_of_week
unless ARGV.empty?
date = DateTime.parse(ARGV.first).to_date
return [date.prev_day(date.wday), "Parsed #{date} from arguments"]
end
date = DateTime.now.to_date
if date.wday > 2
[date.next_day(7-date.wday), "No date argument, using next week"]
else
[date.prev_day(date.wday), "No date argument, using this week"]
end
end
def begin_new_page pdf, side
margin = side == :left ? LEFT_PAGE_MARGINS : RIGHT_PAGE_MARGINS
pdf.start_new_page size: PAGE_SIZE, layout: :portrait, margin: margin
if side == :right
#hole_punches pdf
end
end
def hole_punches pdf
pdf.canvas do
x = 25
# Measuring it on the page it should be `[(1.25).in, (5.5).in, (9.75).in]`,
# but depending on the printer driver it might do some scaling. With one
# driver I printed a bunch of test pages and found that `[72, 392, 710]`
# put it in the right place so your milage may vary.
[(1.25).in, (5.5).in, (9.75).in].each do |y|
pdf.horizontal_line x - 5, x + 5, at: y
pdf.vertical_line y - 5, y + 5, at: x
end
end
end
def heading_format(overrides = {})
{ size: 12, color: DARK_COLOR }.merge(overrides)
end
def subheading_format(overrides = {})
{ size: 10, color: MEDIUM_COLOR }.merge(overrides)
end
def draw_checkbox pdf, checkbox_size, checkbox_padding, label = nil
no_label = label.nil? || label.empty?
original_color = pdf.stroke_color
pdf.stroke_color(LIGHT_COLOR)
pdf.dash([1, 2], phase: 0.5) if no_label
pdf.rectangle [pdf.bounds.top_left[0] + checkbox_padding, pdf.bounds.top_left[1] - checkbox_padding], checkbox_size, checkbox_size
pdf.stroke
pdf.undash if no_label
pdf.stroke_color(original_color)
unless no_label
pdf.translate checkbox_size + (2 * checkbox_padding), 0 do
pdf.text label, color: MEDIUM_COLOR, valign: :center
end
end
end
# Caller needs to start the page, so this could be the first page.
def notes_page pdf, heading_left, subheading_left = nil, heading_right = nil, subheading_right = nil
header_row_count = 2
body_row_count = HOUR_COUNT * 2
first_column = 0
last_column = COLUMN_COUNT - 1
first_row = header_row_count
last_row = header_row_count + body_row_count - 1
pdf.define_grid(columns: COLUMN_COUNT, rows: header_row_count + body_row_count, gutter: 0)
# grid.show_all
# Header Left
if heading_left
pdf.grid([0, first_column],[0, last_column]).bounding_box do
pdf.text heading_left, heading_format(align: :left)
end
end
if subheading_left
pdf.grid([1, first_column],[1, last_column]).bounding_box do
pdf.text subheading_left, subheading_format(align: :left)
end
end
# Header Right
if heading_right
pdf.grid([0, 3],[0, last_column]).bounding_box do
pdf.text heading_right, heading_format(align: :right)
end
end
if subheading_right
pdf.grid([1, 3],[1, last_column]).bounding_box do
pdf.text subheading_right, subheading_format(align: :right)
end
end
# Horizontal lines
(first_row..last_row).each do |row|
pdf.grid([row, first_column], [row, last_column]).bounding_box do
pdf.stroke_line pdf.bounds.bottom_left, pdf.bounds.bottom_right
end
end
# Checkboxes
checkbox_padding = 6
checkbox_size = pdf.grid.row_height - (2 * checkbox_padding)
((first_row + 1)..last_row).each do |row|
pdf.grid(row, 0).bounding_box do
draw_checkbox pdf, checkbox_size, checkbox_padding
end
end
end