require 'prawn' 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 end def my_page_params { page_size: @grid.pdf_page_size, margin: @grid.pdf_margins } end def registration_marks fill { @grid.pdf_reg_marks.each do |r| circle r[:p], r[:r] 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 header @info[:header].each do |k,v| text_box v, at: @grid.pdf_header_xy(k), width: @grid.pdf_header_width(k), size: @grid.pdf_header_size(k) || 12 end end def questions_and_choices stroke do line_width 0.3 nquestions.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 stroke_color "ff0000" font_size CH_LETTER_SZ nchoices(q).times do |c| a = @grid.pdf_choice_cell_area q, c rounded_rectangle a[:p], a[:w], a[:h], 2.mm 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 end end class Fixnum def mm self * 2.83464566929134 end end class Float def mm self * 2.83464566929134 end end