svg to temp bitmap
This commit is contained in:
parent
e355a34a17
commit
e6f352ecb4
|
@ -1,5 +1,10 @@
|
|||
require File.dirname(__FILE__) + '/Filter.rb'
|
||||
|
||||
class InputFilter < Filter
|
||||
OutputFilename = "tmp.png"
|
||||
attr_accessor :output_filename
|
||||
|
||||
def initialize
|
||||
super
|
||||
@output_filename = "tmp.png"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,7 +14,7 @@ class SVGToTempBitmap < InputFilter
|
|||
# process a single input file, possibly splitting it into two output files if it's a spread
|
||||
#
|
||||
def single(input)
|
||||
filename = Dir.pwd + '/' + OutputFilename
|
||||
filename = Dir.pwd + '/' + @output_filename
|
||||
inkscape(input, filename)
|
||||
@cleanup << filename
|
||||
|
||||
|
@ -24,7 +24,7 @@ class SVGToTempBitmap < InputFilter
|
|||
return targets
|
||||
end
|
||||
|
||||
return filename
|
||||
filename
|
||||
end
|
||||
|
||||
def build_spread(filename)
|
||||
|
@ -46,7 +46,7 @@ class SVGToTempBitmap < InputFilter
|
|||
targets << target
|
||||
end
|
||||
|
||||
return targets
|
||||
targets
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -58,32 +58,53 @@ class SVGToTempBitmap < InputFilter
|
|||
if @config['spread']; raise "Spreads and grids combined do not make sense"; end
|
||||
|
||||
width, height = @config['grid'].split("x").collect { |f| f.to_f }
|
||||
joined_files = []
|
||||
|
||||
page_width, page_height = calculate_page_size
|
||||
|
||||
grid_width = page_width / width
|
||||
grid_height = page_height / height
|
||||
|
||||
0.upto(files.length - 1) do |i|
|
||||
x = i % width
|
||||
y = (i / width).floor
|
||||
|
||||
if files[i].split('/').last != "blank"
|
||||
tmp_svg_output = OutputFilename + "-#{i}.png"
|
||||
inkscape(Dir.pwd + '/' + files[i], tmp_svg_output)
|
||||
|
||||
joined_files << [ tmp_svg_output, x, y ]
|
||||
@cleanup << tmp_svg_output
|
||||
end
|
||||
end
|
||||
joined_files = join_files(files, width)
|
||||
|
||||
command = [
|
||||
"-size #{page_width}x#{page_height}",
|
||||
"xc:white"
|
||||
]
|
||||
|
||||
joined_files.each do |file, x, y|
|
||||
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|
|
||||
x = i % width
|
||||
y = (i / width).floor
|
||||
|
||||
if files[i].split('/').last != "blank"
|
||||
tmp_file = @output_filename + "-#{i}.png"
|
||||
inkscape(Dir.pwd + '/' + files[i], tmp_file)
|
||||
|
||||
joined_files << [ tmp_file, x, y ]
|
||||
end
|
||||
end
|
||||
|
||||
joined_files
|
||||
end
|
||||
|
||||
def generate_joined_files_command(files, grid_width, grid_height)
|
||||
command = []
|
||||
|
||||
files.each do |file, x, y|
|
||||
image_width, image_height = get_dimensions(file)
|
||||
|
||||
x_offset = (grid_width - image_width) / 2
|
||||
|
@ -92,11 +113,6 @@ class SVGToTempBitmap < InputFilter
|
|||
command << "\"#{file}\" -geometry +#{x * grid_width + x_offset}+#{y * grid_height + y_offset} -composite"
|
||||
end
|
||||
|
||||
command << OutputFilename
|
||||
|
||||
convert(command)
|
||||
|
||||
@cleanup << OutputFilename
|
||||
OutputFilename
|
||||
command
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,4 +25,98 @@ class TestSVGToTempBitmap < Test::Unit::TestCase
|
|||
|
||||
assert_equal [ 'filename-left.png', 'filename-right.png' ], @filter.build_spread('filename')
|
||||
end
|
||||
|
||||
def test_join_files
|
||||
@filter.output_filename = 'test'
|
||||
[
|
||||
[ [], 1, [] ],
|
||||
[ ['file'], 1, [
|
||||
[ 'test-0.png', 0, 0 ]
|
||||
] ],
|
||||
[ ['file', 'file2'], 1, [
|
||||
[ 'test-0.png', 0, 0 ],
|
||||
[ 'test-1.png', 0, 1 ],
|
||||
] ],
|
||||
[ ['file', 'file2'], 2, [
|
||||
[ 'test-0.png', 0, 0 ],
|
||||
[ 'test-1.png', 1, 0 ],
|
||||
] ],
|
||||
[ ['blank', 'file2'], 2, [
|
||||
[ 'test-1.png', 1, 0 ],
|
||||
] ],
|
||||
].each do |files, width, expected_output|
|
||||
@filter.stubs(:inkscape)
|
||||
assert_equal expected_output, @filter.join_files(files, width)
|
||||
end
|
||||
end
|
||||
|
||||
def test_generate_joined_files_command
|
||||
[
|
||||
[ [], 100, 50, [], [] ],
|
||||
[
|
||||
[
|
||||
[ 'file', 0, 0 ]
|
||||
], 100, 50, [
|
||||
[ 50, 25 ]
|
||||
], [
|
||||
'"file" -geometry +25+12 -composite'
|
||||
]
|
||||
],
|
||||
].each do |files, grid_width, grid_height, image_size_returns, expected_command|
|
||||
@filter.stubs(:get_dimensions).returns(*image_size_returns)
|
||||
|
||||
assert_equal expected_command, @filter.generate_joined_files_command(files, grid_width, grid_height)
|
||||
end
|
||||
end
|
||||
|
||||
def test_single_no_spread
|
||||
@filter.output_filename = 'test'
|
||||
@filter.expects(:inkscape).with('file', Dir.pwd + '/test')
|
||||
assert_equal Dir.pwd + '/test', @filter.single('file')
|
||||
assert_equal [ Dir.pwd + '/test' ], @filter.cleanup
|
||||
end
|
||||
|
||||
def test_single_spread
|
||||
@filter.stubs(:recalc_pixels)
|
||||
@filter.output_filename = 'test'
|
||||
@filter.config = {
|
||||
'spread' => true
|
||||
}
|
||||
|
||||
@filter.expects(:inkscape).with('file', Dir.pwd + '/test')
|
||||
@filter.expects(:build_spread).with(Dir.pwd + '/test').returns(['target1', 'target2'])
|
||||
|
||||
@filter.single('file')
|
||||
end
|
||||
|
||||
def test_multiple
|
||||
@filter.stubs(:recalc_pixels)
|
||||
@filter.output_filename = 'test'
|
||||
@filter.config = {
|
||||
'grid' => '2x1'
|
||||
}
|
||||
@filter.stubs(:calculate_page_size).returns([100,50])
|
||||
|
||||
joined_files = [
|
||||
[ 'test-0.png', 0, 0 ],
|
||||
[ 'test-1.png', 1, 0 ]
|
||||
]
|
||||
|
||||
@filter.expects(:join_files).with(['file1', 'file2'], 2).returns(joined_files)
|
||||
@filter.expects(:generate_joined_files_command).with(joined_files, 50, 50).returns([
|
||||
'command-1', 'command-2'
|
||||
])
|
||||
|
||||
@filter.expects(:convert).with([
|
||||
'-size 100x50',
|
||||
'xc:white',
|
||||
'command-1',
|
||||
'command-2',
|
||||
'"test"'
|
||||
])
|
||||
|
||||
assert_equal 'test', @filter.multiple(['file1', 'file2'])
|
||||
assert_equal [ 'test-0.png', 'test-1.png', 'test' ], @filter.cleanup
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue