image processing and start of print handling

This commit is contained in:
John Bintz 2010-01-05 22:07:30 -05:00
parent 247c12ad8e
commit f36a14d9c1
7 changed files with 136 additions and 25 deletions

View File

@ -1,7 +1,7 @@
.PHONY : test test-coverage
test :
testrb -b tests/*.rb
testrb `find tests -name '*.rb'`
test-coverage :
rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' tests/*.rb
rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' `find tests -name '*.rb'`

View File

@ -45,13 +45,6 @@ class Filter
recalc_pixels
end
def convert(command, verbose = false)
if verbose
puts "convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")
end
system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" "))
end
#
# Get the dimensions of a file
#

View File

@ -7,21 +7,6 @@ Dir[File.dirname(__FILE__) + "/classes/*.rb"].each do |file|
require file
end
class OutputFilter < Filter
#
# get the output filename for this filter
#
def filename(info)
target = @config['target']
info.each { |k,v| target = target.gsub("{#{k}}", v.to_s) }
target
end
#
# get the output targets for this filter
#
def targets(info); filename(info); end
end
module PrintHandling
#

View File

@ -32,6 +32,10 @@ module ImageProcessing
call_system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"")
handle_inkscape_rotation(inkscape_target, target)
end
def handle_inkscape_rotation(inkscape_target, target)
if @config['rotate']
command = [
"\"#{inkscape_target}\"",

70
modules/PrintHandling.rb Normal file
View File

@ -0,0 +1,70 @@
module PrintHandling
#
# calculate the page size in PPI
#
def calculate_page_size
ok = false
if @config['dpi']
if @config['page_size']
case @config['page_size'].downcase
when "letter", "letter_portrait"
page_width = 8.5; page_height = 11; ok = true
when "letter_landscape"
page_width = 11; page_height = 8.5; ok = true
when "half_letter_landscape"
page_width = 5.5; page_height = 8.5; ok = true
end
else
if @config['page_width_inches'] && @config['page_height_inches']
page_width = @config['page_width_inches']
page_height = @config['page_height_inches']
ok = true
end
end
if ok
page_width *= @config['dpi'].to_i
page_height *= @config['dpi'].to_i
end
end
if !ok
page_width = @config['page_width']
page_height = @config['page_height']
end
[ page_width, page_height ]
end
#
# align the provided image on a white page
#
def build_for_print(input, output, side = "none")
page_width, page_height = calculate_page_size
command = [
"-density #{config['dpi']}",
"-size #{page_width.to_i}x#{page_height.to_i}",
"xc:white"
]
case side
when "none"
command << "-gravity Center"
when "left"
command << "-gravity East"
when "right"
command << "-gravity West"
end
command << "-draw 'image Over 0,0 0,0 \"#{input}\"'"
if output[0,1] == "|"
command << output[1..-1]
else
command << "\"#{output}\""
end
convert(command)
end
end

View File

@ -1,4 +1,7 @@
require "test/unit"
require 'rubygems'
require 'test/unit'
require 'mocha'
require 'fakefs/safe'
require File.dirname(__FILE__) + '/../modules/ImageProcessing.rb'
class TestImageProcessing < Test::Unit::TestCase
@ -29,4 +32,31 @@ class TestImageProcessing < Test::Unit::TestCase
assert_equal inkscape_target, expected_target
end
end
def test_handle_inkscape_rotation
@instance.config = { 'rotate' => 90 }
@instance.expects(:convert).with(['"target-pre.png"', '-rotate 90', '"target"'])
FakeFS do
FileUtils.touch('target-pre.png')
@instance.handle_inkscape_rotation('target-pre.png', 'target')
assert !(File.exists? 'target-pre.png')
end
end
def test_convert
[ false, true ].each do |verbose|
@instance.expects(:call_system).with('convert ' + (verbose ? '-verbose ' : '') + 'test')
@instance.convert(['test'], verbose)
end
end
def test_inkscape
@instance.expects(:call_system).with('inkscape -e "new-target" -y 1.0 -w 200 "input"')
@instance.expects(:handle_inkscape_rotation).with('new-target', 'target')
@instance.expects(:setup_inkscape).returns([['-w 200'], 'new-target'])
@instance.inkscape('input', 'target')
end
end

View File

@ -0,0 +1,29 @@
require 'rubygems'
require 'test/unit'
require 'mocha'
require 'fakefs/safe'
require File.dirname(__FILE__) + '/../modules/PrintHandling.rb'
class TestPrintHandling < Test::Unit::TestCase
def setup
@instance = Class.new do
include PrintHandling
attr_accessor :config
end.new
end
def test_calculate_page_size
[
[ { 'page_width' => 10, 'page_height' => 20 }, [ 10, 20 ] ],
[ { 'dpi' => 100 , 'page_width' => 10, 'page_height' => 20 }, [ 10, 20 ] ],
[ { 'dpi' => 100 , 'page_width_inches' => 1, 'page_height_inches' => 2 }, [ 100, 200 ] ],
[ { 'dpi' => 1 , 'page_size' => 'letter' }, [ 8.5, 11 ] ],
[ { 'dpi' => 1 , 'page_size' => 'letter_landscape' }, [ 11, 8.5 ] ],
[ { 'dpi' => 1 , 'page_size' => 'half_letter_landscape' }, [ 5.5, 8.5 ] ],
].each do |config, expected_return|
@instance.config = config
assert_equal expected_return, @instance.calculate_page_size
end
end
end