require 'prawn' module Mork class SheetPDF < Prawn::Document def initialize(grid, info) @grid = grid super my_page_params # @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 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 c = @grid.pdf_dark_calibration_area rectangle c[:p], c[:w], c[:h] # draw the bars corresponding to the code # least to most significant bit, left to right @grid.pdf_code_areas_for(code).each do |c| rectangle c[:p], c[:w], c[:h] end end end 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 text v end end else text_box v, at: @grid.pdf_header_xy(k), width: @grid.pdf_header_width(k) end end end end def questions_and_choices(info) stroke do 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: @grid.pdf_qnum_size stroke_color "ff0000" 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], [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 nquestions # @info[:choices].length # end # # def nchoices(i) # @info[:choices][i] # end end end class Fixnum def mm self * 2.83464566929134 end end class Float def mm self * 2.83464566929134 end end