2010-01-07 00:57:32 +00:00
|
|
|
require File.dirname(__FILE__) + '/InputFilter.rb'
|
|
|
|
|
|
|
|
class SVGToTempBitmap < InputFilter
|
|
|
|
include PrintHandling
|
|
|
|
|
|
|
|
#
|
|
|
|
# select which build method to use based on the number of provided images.
|
|
|
|
#
|
|
|
|
def build(input)
|
|
|
|
(input.instance_of? Array) ? multiple(input) : single(input)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# process a single input file, possibly splitting it into two output files if it's a spread
|
|
|
|
#
|
|
|
|
def single(input)
|
2010-01-07 01:35:13 +00:00
|
|
|
filename = Dir.pwd + '/' + @output_filename
|
2010-01-07 00:57:32 +00:00
|
|
|
inkscape(input, filename)
|
|
|
|
@cleanup << filename
|
|
|
|
|
|
|
|
if @config['spread']
|
|
|
|
targets = build_spread(filename)
|
|
|
|
@cleanup.concat targets
|
|
|
|
return targets
|
|
|
|
end
|
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
filename
|
2010-01-07 00:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_spread(filename)
|
|
|
|
width, height = get_dimensions(filename)
|
|
|
|
targets = []
|
|
|
|
|
|
|
|
[ [ "left", 0 ], [ "right", width / 2 ] ].each do |side, offset|
|
|
|
|
target = filename + "-#{side}.png"
|
|
|
|
command = [
|
|
|
|
"\"#{filename}\"",
|
|
|
|
"-gravity Northwest",
|
|
|
|
"-crop #{width/2}x#{height}+#{offset}+0",
|
|
|
|
"+repage",
|
|
|
|
"\"#{target}\""
|
|
|
|
]
|
|
|
|
|
|
|
|
convert(command)
|
|
|
|
|
|
|
|
targets << target
|
|
|
|
end
|
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
targets
|
2010-01-07 00:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# combine multiple input files onto a single page-sized canvas.
|
|
|
|
# images are added left-to-right and top-to-bottom starting from the top-left of the page.
|
|
|
|
# leave a grid square blank by passing the filename "/blank".
|
|
|
|
#
|
|
|
|
def multiple(files)
|
|
|
|
if @config['spread']; raise "Spreads and grids combined do not make sense"; end
|
|
|
|
|
|
|
|
width, height = @config['grid'].split("x").collect { |f| f.to_f }
|
|
|
|
|
|
|
|
page_width, page_height = calculate_page_size
|
|
|
|
|
|
|
|
grid_width = page_width / width
|
|
|
|
grid_height = page_height / height
|
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
joined_files = join_files(files, width)
|
|
|
|
|
|
|
|
command = [
|
|
|
|
"-size #{page_width}x#{page_height}",
|
|
|
|
"xc:white"
|
|
|
|
]
|
|
|
|
|
|
|
|
command.concat generate_joined_files_command(joined_files, grid_width, grid_height)
|
|
|
|
|
|
|
|
command << "\"#{@output_filename}\""
|
|
|
|
|
|
|
|
convert(command)
|
|
|
|
|
|
|
|
@cleanup.concat joined_files.collect { |file, x, y| file }
|
|
|
|
@cleanup << @output_filename
|
|
|
|
|
|
|
|
@output_filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def join_files(files, width)
|
|
|
|
joined_files = []
|
|
|
|
|
|
|
|
files.each_index do |i|
|
2010-01-07 00:57:32 +00:00
|
|
|
x = i % width
|
|
|
|
y = (i / width).floor
|
|
|
|
|
|
|
|
if files[i].split('/').last != "blank"
|
2010-01-07 01:35:13 +00:00
|
|
|
tmp_file = @output_filename + "-#{i}.png"
|
|
|
|
inkscape(Dir.pwd + '/' + files[i], tmp_file)
|
2010-01-07 00:57:32 +00:00
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
joined_files << [ tmp_file, x, y ]
|
2010-01-07 00:57:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
joined_files
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_joined_files_command(files, grid_width, grid_height)
|
|
|
|
command = []
|
2010-01-07 00:57:32 +00:00
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
files.each do |file, x, y|
|
2010-01-07 00:57:32 +00:00
|
|
|
image_width, image_height = get_dimensions(file)
|
|
|
|
|
|
|
|
x_offset = (grid_width - image_width) / 2
|
|
|
|
y_offset = (grid_height - image_height) / 2
|
|
|
|
|
|
|
|
command << "\"#{file}\" -geometry +#{x * grid_width + x_offset}+#{y * grid_height + y_offset} -composite"
|
|
|
|
end
|
|
|
|
|
2010-01-07 01:35:13 +00:00
|
|
|
command
|
2010-01-07 00:57:32 +00:00
|
|
|
end
|
|
|
|
end
|