diff --git a/Makefile b/Makefile index f7c3612..f52e12b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY : test test-coverage test : - testrb -b tests/* + testrb -b tests/*.rb test-coverage : rcov -x '\/jruby\/lib\/' -x '\/tests\/data\/' tests/*.rb \ No newline at end of file diff --git a/classes/Filter.rb b/classes/Filter.rb index 36c6e78..fad68e5 100644 --- a/classes/Filter.rb +++ b/classes/Filter.rb @@ -45,41 +45,6 @@ class Filter recalc_pixels end - # - # Build a temporary PNG from an SVG file - # - def inkscape(input, target) - params = [] - - width = @config['width'] - height = @config['height'] - inkscape_target = target - - if @config['rotate'] - case @config['rotate'] - when 90, -90 - t = width; width = height; height = t - inkscape_target = target + "-pre.png" - end - end - - if width && (width.to_i != 0); params << "-w #{width} "; end - if height && (height.to_i != 0); params << "-h #{height} "; end - - system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"") - - if @config['rotate'] - command = [ - "\"#{inkscape_target}\"", - "-rotate #{@config['rotate']}", - "\"#{target}\"" - ] - - convert(command) - File.unlink(inkscape_target) - end - end - def convert(command, verbose = false) if verbose puts "convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ") diff --git a/modules/ImageProcessing.rb b/modules/ImageProcessing.rb new file mode 100644 index 0000000..354b3cb --- /dev/null +++ b/modules/ImageProcessing.rb @@ -0,0 +1,57 @@ +module ImageProcessing + def setup_inkscape(target) + params = [] + + width = @config['width'] + height = @config['height'] + inkscape_target = target + + if @config['rotate'] + case @config['rotate'] + when 90, -90 + t = width; width = height; height = t + inkscape_target = target + "-pre.png" + end + end + + if width && (width.to_i != 0) + params << "-w #{width}" + end + if height && (height.to_i != 0) + params << "-h #{height}" + end + + [ params, inkscape_target ] + end + + # + # Build a PNG from an SVG file + # + def inkscape(input, target) + params, inkscape_target = setup_inkscape(target) + + call_system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"") + + if @config['rotate'] + command = [ + "\"#{inkscape_target}\"", + "-rotate #{@config['rotate']}", + "\"#{target}\"" + ] + + convert(command) + File.unlink(inkscape_target) + end + end + + def convert(command, verbose = false) + if verbose + puts "convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ") + end + call_system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")) + end + + def call_system(command) + Kernel.system(command) + end +end diff --git a/tests/test_image_processing.rb b/tests/test_image_processing.rb new file mode 100644 index 0000000..63579aa --- /dev/null +++ b/tests/test_image_processing.rb @@ -0,0 +1,32 @@ +require "test/unit" +require File.dirname(__FILE__) + '/../modules/ImageProcessing.rb' + +class TestImageProcessing < Test::Unit::TestCase + def setup + @instance = Class.new do + include ImageProcessing + + attr_accessor :config + end.new + end + + def test_setup_inkscape + [ + [ {}, [], 'target' ], + [ { 'width' => 'test' }, [], 'target' ], + [ { 'width' => 200 }, ['-w 200'], 'target' ], + [ { 'height' => 'test' }, [], 'target' ], + [ { 'height' => 200 }, ['-h 200'], 'target' ], + [ { 'rotate' => 0 }, [], 'target' ], + [ { 'rotate' => 90 }, [], 'target-pre.png' ], + [ { 'rotate' => 90, 'width' => 50, 'height' => 75 }, ['-w 75', '-h 50'], 'target-pre.png' ], + ].each do |config, expected_params, expected_target| + @instance.config = config + + params, inkscape_target = @instance.setup_inkscape('target') + + assert_equal expected_params, params + assert_equal inkscape_target, expected_target + end + end +end