svg to temp bitmap work

This commit is contained in:
John Bintz 2010-01-06 19:57:32 -05:00
parent 93a540f5c1
commit e355a34a17
5 changed files with 134 additions and 98 deletions

View File

@ -4,4 +4,6 @@ test :
testrb `find tests -name '*.rb'` testrb `find tests -name '*.rb'`
test-coverage : test-coverage :
rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' `find tests -name '*.rb'` rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' `find tests -name '*.rb'`
sed 's#<td class="left_align"><a href="<script>.html"><script></a></td>#<td></td>#' coverage/index.html > coverage/index.html.new
mv coverage/index.html.new coverage/index.html

View File

@ -1,4 +1,4 @@
require 'filter' require File.dirname(__FILE__) + '/Filter.rb'
class InputFilter < Filter class InputFilter < Filter
OutputFilename = "tmp.png" OutputFilename = "tmp.png"

102
classes/SVGToTempBitmap.rb Normal file
View File

@ -0,0 +1,102 @@
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)
filename = Dir.pwd + '/' + OutputFilename
inkscape(input, filename)
@cleanup << filename
if @config['spread']
targets = build_spread(filename)
@cleanup.concat targets
return targets
end
return filename
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
return targets
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 }
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
command = [
"-size #{page_width}x#{page_height}",
"xc:white"
]
joined_files.each do |file, x, y|
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
command << OutputFilename
convert(command)
@cleanup << OutputFilename
OutputFilename
end
end

View File

@ -7,102 +7,6 @@ Dir[File.dirname(__FILE__) + "/classes/*.rb"].each do |file|
require file require file
end end
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)
filename = Dir.pwd + '/' + OutputFilename
inkscape(input, filename)
@cleanup << filename
if @config['spread']
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
@cleanup << target
end
return targets
end
return filename
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 }
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
command = [
"-size #{page_width}x#{page_height}",
"xc:white"
]
joined_files.each do |file, x, y|
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
command << OutputFilename
convert(command)
@cleanup << OutputFilename
OutputFilename
end
end
# #
# Process an input file for the Web # Process an input file for the Web
# #

View File

@ -0,0 +1,28 @@
require 'rubygems'
require 'test/unit'
require 'fakefs/safe'
require File.dirname(__FILE__) + '/../classes/SVGToTempBitmap.rb'
class TestSVGToTempBitmap < Test::Unit::TestCase
def setup
@filter = SVGToTempBitmap.new
end
def test_build
[
[ "", :single ],
[ [], :multiple ]
].each do |input, expected_method|
@filter.expects(expected_method).with(input)
@filter.build(input)
end
end
def test_build_spread
@filter.stubs(:get_dimensions).with('filename').returns([50, 75])
@filter.expects(:convert).with(['"filename"', '-gravity Northwest', '-crop 25x75+0+0', '+repage', '"filename-left.png"'])
@filter.expects(:convert).with(['"filename"', '-gravity Northwest', '-crop 25x75+25+0', '+repage', '"filename-right.png"'])
assert_equal [ 'filename-left.png', 'filename-right.png' ], @filter.build_spread('filename')
end
end