lib/mork/sheet_pdf.rb in mork-0.0.3 vs lib/mork/sheet_pdf.rb in mork-0.0.5
- old
+ new
@@ -2,31 +2,55 @@
module Mork
class SheetPDF < Prawn::Document
def initialize(grid, info)
@grid = grid
- @info = info
super my_page_params
- registration_marks
- calibration_and_code @info[:code]
- header
- questions_and_choices
+ # @info should be an array of hashes, one per page;
+ # convert to array if a single hash was passed
+ @info = info.class == Hash ? [info] : info
+ process
end
+ def save(fn)
+ render_file fn
+ end
+
+ def to_pdf
+ render
+ end
+
+ private
+
+ def process
+ # for each response sheet
+ @info.each_with_index do |info, i|
+ start_new_page if i>0
+ fill_color "000000"
+ stroke_color "000000"
+ line_width 0.3
+ registration_marks
+ calibration_and_code info[:code]
+ header info[:header]
+ control info[:control] if info[:control]
+ questions_and_choices info[:choices]
+ end
+ end
+
def my_page_params
{
page_size: @grid.pdf_page_size,
margin: @grid.pdf_margins
}
end
def registration_marks
- fill {
+ fill do
@grid.pdf_reg_marks.each do |r|
circle r[:p], r[:r]
end
- }
+ end
end
def calibration_and_code(code)
fill do
# draw the dark calibration bar
@@ -38,12 +62,35 @@
rectangle c[:p], c[:w], c[:h]
end
end
end
- def header
- @info[:header].each do |k,v|
+ def control(info)
+ font_size @grid.pdf_control_size do
+ text_box info[:string], at: @grid.pdf_control_xy,
+ width: @grid.pdf_control_width,
+ align: :right
+ stroke do
+ font_size
+ stroke_color "ff0000"
+ # dark
+ a = @grid.pdf_dark_control_cell_area
+ rounded_rectangle a[:p], a[:w], a[:h], [a[:h], a[:w]].min / 2
+ fill_color "ff0000"
+ draw_text info[:labels][0], at: @grid.pdf_dark_control_letter_xy
+ # light
+ a = @grid.pdf_light_control_cell_area
+ rounded_rectangle a[:p], a[:w], a[:h], [a[:h], a[:w]].min / 2
+ fill_color "ff0000"
+ draw_text info[:labels][1], at: @grid.pdf_light_control_letter_xy
+
+ end
+ end
+ end
+
+ def header(info)
+ info.each do |k,v|
font_size @grid.pdf_header_size(k) do
if @grid.pdf_header_boxed?(k)
bounding_box @grid.pdf_header_xy(k), width: @grid.pdf_header_width(k), height: @grid.pdf_header_height(k) do
stroke_bounds
bounding_box @grid.pdf_header_padding(k), width: @grid.pdf_header_width(k) do
@@ -55,42 +102,36 @@
end
end
end
end
- def questions_and_choices
+ def questions_and_choices(info)
stroke do
- line_width 0.3
- nquestions.times do |q|
+ info.length.times do |q|
fill_color "000000"
text_box "#{q+1}", at: @grid.pdf_qnum_xy(q),
- width: @grid.pdf_qnum_width,
- align: :right,
- size: Q_NUM_SIZE
+ width: @grid.pdf_qnum_width,
+ align: :right,
+ size: @grid.pdf_qnum_size
stroke_color "ff0000"
- font_size CH_LETTER_SZ
- nchoices(q).times do |c|
+ font_size @grid.pdf_chlett_size
+ info[q].times do |c|
a = @grid.pdf_choice_cell_area q, c
- rounded_rectangle a[:p], a[:w], a[:h], 2.mm
+ rounded_rectangle a[:p], a[:w], a[:h], [a[:h], a[:w]].min / 2
fill_color "ff0000"
draw_text (65+c).chr, at: @grid.pdf_choice_letter_xy(q, c)
end
end
end
end
- def save(fn)
- render_file fn
- end
-
- private
- def nquestions
- @info[:choices].length
- end
-
- def nchoices(i)
- @info[:choices][i]
- end
+ # def nquestions
+ # @info[:choices].length
+ # end
+ #
+ # def nchoices(i)
+ # @info[:choices][i]
+ # end
end
end
class Fixnum
def mm