diff --git a/bin/svggvs b/bin/svggvs index 1ba9fc1..326de1f 100755 --- a/bin/svggvs +++ b/bin/svggvs @@ -46,6 +46,8 @@ module SVGGVS end end + CARDS_PER_PAGE = 9 + desc "pdf", "Create PDF of card images" def pdf pngs @@ -58,19 +60,23 @@ module SVGGVS tmp_target end - png_slices = trimmed_pngs.each_slice(9) + png_slices = trimmed_pngs.each_slice(CARDS_PER_PAGE) - page_count = trimmed_pngs.length / 9 + page_count = trimmed_pngs.length / CARDS_PER_PAGE placeholder = tmp_target_for("placeholder.png") system %{convert -size #{context.session.pdf_card_size} xc:white #{placeholder}} + pdf_obj = SVGGVS::PDF.new(card_size: context.session.pdf_card_size) + pages = Parallel.map(png_slices.each_with_index) do |files, page_index| + tmp_pdf_png_target = tmp_path.join("page%05d.pdf" % page_index) tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index) files += Array.new(9 - files.length, placeholder) - system %{montage -density #{context.session.pdf_dpi} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}} + system %{montage -density #{context.session.pdf_dpi} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_png_target}} + system %{convert -density #{context.session.pdf_dpi} #{tmp_pdf_png_target} -bordercolor white -border #{SVGGVS::PDF.border_size} #{pdf_obj.generate_crop_mark_draws.join(' ')} #{tmp_pdf_target}}.tap { |o| p o } tmp_pdf_target end diff --git a/lib/svggvs.rb b/lib/svggvs.rb index 4984359..f66c5b6 100644 --- a/lib/svggvs.rb +++ b/lib/svggvs.rb @@ -3,6 +3,7 @@ require_relative './svggvs/target' require_relative './svggvs/context' require_relative './svggvs/session' require_relative './svggvs/data_source' +require_relative './svggvs/pdf' module SVGGVS end diff --git a/lib/svggvs/pdf.rb b/lib/svggvs/pdf.rb new file mode 100644 index 0000000..6fbbff7 --- /dev/null +++ b/lib/svggvs/pdf.rb @@ -0,0 +1,57 @@ +module SVGGVS + CROP_MARK_SIZE = 20.freeze + + class PDF + def initialize(options) + @options = options + end + + def self.border_size + ([ CROP_MARK_SIZE ] * 2).join('x') + end + + def page_size_with_crop_marks + [ card_width * 3, card_height * 3 ].collect { |size| size + CROP_MARK_SIZE * 2 }.join('x') + end + + def generate_crop_mark_directives + (0..3).collect { |index| + pos_x = CROP_MARK_SIZE + index * card_width + pos_y = CROP_MARK_SIZE + index * card_height + + [ [ 0 ], [ CROP_MARK_SIZE + page_height ] ].collect { |size| + [ pos_x ] + size + [ pos_x, size.first + CROP_MARK_SIZE ] + } + + [ [ 0 ], [ CROP_MARK_SIZE + page_width ] ].collect { |size| + size + [ pos_y ] + [ size.first + CROP_MARK_SIZE, pos_y ] + } + }.flatten(1).collect { |sx, sy, ex, ey| "#{sx},#{sy} #{ex},#{ey}" } + end + + def generate_crop_mark_draws + generate_crop_mark_directives.collect { |coords| %{-stroke black -strokewidth 3 -draw "line #{coords}"} } + end + + private + def card_width + card_size.first + end + + def card_height + card_size.last + end + + def page_height + card_height * 3 + end + + def page_width + card_width * 3 + end + + def card_size + @card_size ||= @options[:card_size].split('x').collect(&:to_i) + end + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..b1c4556 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,2 @@ +$: << File.expand_path('../lib', __FILE__) + diff --git a/spec/svggvs/pdf_spec.rb b/spec/svggvs/pdf_spec.rb new file mode 100644 index 0000000..976dc98 --- /dev/null +++ b/spec/svggvs/pdf_spec.rb @@ -0,0 +1,46 @@ +require_relative '../spec_helper' +require 'svggvs/pdf' +require 'digest/md5' + +describe SVGGVS::PDF do + subject { SVGGVS::PDF.new(options) } + + let(:options) { + { card_size: '100x100' } + } + + describe '#page_size_with_crop_marks' do + it "should have the right size" do + subject.page_size_with_crop_marks.should be == "340x340" + end + end + + describe '#generate_crop_mark_directives' do + let(:result) { + [ + "20,0 20,20", + "20,320 20,340", + "120,0 120,20", + "120,320 120,340", + "220,0 220,20", + "220,320 220,340", + "320,0 320,20", + "320,20 340,20", + "320,120 340,120", + "320,220 340,220", + "320,320 320,340", + "320,320 340,320", + "0,20 20,20", + "0,120 20,120", + "0,220 20,220", + "0,320 20,320", + ] + } + + it 'should create correct definitions' do + subject.generate_crop_mark_directives.each { |coords| + result.should include(coords) + } + end + end +end diff --git a/svggvs.gemspec b/svggvs.gemspec index 54ac407..f53c6b5 100644 --- a/svggvs.gemspec +++ b/svggvs.gemspec @@ -22,4 +22,6 @@ Gem::Specification.new do |gem| gem.add_dependency 'parallel' gem.add_dependency 'roo' gem.add_dependency 'activesupport' + + gem.add_development_dependency 'rspec' end