Add crop marks to PDF output

This commit is contained in:
John Bintz 2014-03-01 13:15:01 -05:00
parent 4160a3fcdd
commit e672388460
6 changed files with 117 additions and 3 deletions

View File

@ -46,6 +46,8 @@ module SVGGVS
end end
end end
CARDS_PER_PAGE = 9
desc "pdf", "Create PDF of card images" desc "pdf", "Create PDF of card images"
def pdf def pdf
pngs pngs
@ -58,19 +60,23 @@ module SVGGVS
tmp_target tmp_target
end 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") placeholder = tmp_target_for("placeholder.png")
system %{convert -size #{context.session.pdf_card_size} xc:white #{placeholder}} 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| 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) tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index)
files += Array.new(9 - files.length, placeholder) 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 tmp_pdf_target
end end

View File

@ -3,6 +3,7 @@ require_relative './svggvs/target'
require_relative './svggvs/context' require_relative './svggvs/context'
require_relative './svggvs/session' require_relative './svggvs/session'
require_relative './svggvs/data_source' require_relative './svggvs/data_source'
require_relative './svggvs/pdf'
module SVGGVS module SVGGVS
end end

57
lib/svggvs/pdf.rb Normal file
View File

@ -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

2
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,2 @@
$: << File.expand_path('../lib', __FILE__)

46
spec/svggvs/pdf_spec.rb Normal file
View File

@ -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

View File

@ -22,4 +22,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'parallel' gem.add_dependency 'parallel'
gem.add_dependency 'roo' gem.add_dependency 'roo'
gem.add_dependency 'activesupport' gem.add_dependency 'activesupport'
gem.add_development_dependency 'rspec'
end end