diff --git a/Makefile b/Makefile index f52e12b..aa7f871 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' `find tests -name '*.rb'` \ No newline at end of file diff --git a/classes/Filter.rb b/classes/Filter.rb index fad68e5..aaffc47 100644 --- a/classes/Filter.rb +++ b/classes/Filter.rb @@ -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 # diff --git a/minicomic-backend.rb b/minicomic-backend.rb index 7a9183f..eaa67a2 100755 --- a/minicomic-backend.rb +++ b/minicomic-backend.rb @@ -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 # diff --git a/modules/ImageProcessing.rb b/modules/ImageProcessing.rb index 354b3cb..b3cca47 100644 --- a/modules/ImageProcessing.rb +++ b/modules/ImageProcessing.rb @@ -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}\"", diff --git a/modules/PrintHandling.rb b/modules/PrintHandling.rb new file mode 100644 index 0000000..a5b7d59 --- /dev/null +++ b/modules/PrintHandling.rb @@ -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 diff --git a/tests/test_image_processing.rb b/tests/TestImageProcessing.rb similarity index 50% rename from tests/test_image_processing.rb rename to tests/TestImageProcessing.rb index 63579aa..31cb4cc 100644 --- a/tests/test_image_processing.rb +++ b/tests/TestImageProcessing.rb @@ -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 diff --git a/tests/TestPrintHandling.rb b/tests/TestPrintHandling.rb new file mode 100644 index 0000000..5899340 --- /dev/null +++ b/tests/TestPrintHandling.rb @@ -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