From dc5b186c7b88fc7598614880605149dfd270d2ba Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 22 May 2009 09:36:08 -0400 Subject: [PATCH] start breaking out classes for testing --- classes/Filter.rb | 100 ++++++++++++++++++++++++++++++++++++++++++ minicomic-backend.rb | 101 +++---------------------------------------- tests/TestFilter.rb | 47 ++++++++++++++++++++ 3 files changed, 152 insertions(+), 96 deletions(-) create mode 100644 classes/Filter.rb create mode 100644 tests/TestFilter.rb diff --git a/classes/Filter.rb b/classes/Filter.rb new file mode 100644 index 0000000..8b6673a --- /dev/null +++ b/classes/Filter.rb @@ -0,0 +1,100 @@ +require 'singleton' + +class Filter + include Singleton + @config = {} + @cleanup = [] + + attr_accessor :config, :cleanup + + def initialize + @config = {} + @cleanup = [] + end + + def cleanup + @cleanup.each do |f| + if File.exists? f; File.unlink(f); end + end + end + + def recalc_pixels + if @config['print'] + if !@config['dpi']; raise "DPI not defined"; end + if (@config['dpi'].to_i.to_s) != @config['dpi'].to_s; raise "DPI not integer"; end + if @config['width_inches'] && (@config['width_inches'].to_f != 0) + @config['width'] = (@config['width_inches'].to_f * @config['dpi'].to_f).to_i + else + @config.delete('width') + end + if @config['height_inches'] && (@config['height_inches'].to_f != 0) + @config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i + else + @config.delete('height') + end + + if (!@config['width'] && !@config['height']) + raise "No dimensions defined!" + end + end + end + + # + # Set the config + # + def config=(c) + @config = c + + 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) + system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")) + end + + # + # Get the dimensions of a file + # + def get_dimensions(input) + dimensions = nil + IO.popen("identify -format '%w,%h' \"#{input}\"") do |fh| + dimensions = fh.readlines.first.split(",").collect { |d| d.to_i } + end + dimensions + end +end diff --git a/minicomic-backend.rb b/minicomic-backend.rb index df95356..af28fb9 100755 --- a/minicomic-backend.rb +++ b/minicomic-backend.rb @@ -2,101 +2,8 @@ require 'yaml' require 'time' -require 'singleton' -class Filter - include Singleton - @config = {} - @cleanup = [] - - attr_accessor :config, :cleanup - - def initialize - @config = {} - @cleanup = [] - end - - def cleanup - @cleanup.each do |f| - if File.exists? f; File.unlink(f); end - end - end - - def recalc_pixels - if @config['print'] - if !@config['dpi']; raise "DPI not defined"; end - if @config['width_inches'] && (@config['width_inches'].to_f != 0) - @config['width'] = (@config['width_inches'].to_f * @config['dpi'].to_f).to_i - else - @config.delete('width') - end - if @config['height_inches'] && (@config['height_inches'].to_f != 0) - @config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i - else - @config.delete('height') - end - end - end - - # - # Set the config - # - def config=(c) - @config = c - - 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) - system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")) - end - - # - # Get the dimensions of a file - # - def get_dimensions(input) - dimensions = nil - IO.popen("identify -format '%w,%h' \"#{input}\"") do |fh| - dimensions = fh.readlines.first.split(",").collect { |d| d.to_i } - end - dimensions - end -end +require File.dirname(__FILE__) + '/classes/Filter.rb' class InputFilter < Filter OutputFilename = "tmp.png" @@ -378,7 +285,7 @@ class TempBitmapToPaginatedPrint < OutputFilter end def targets(info) - (@config['spread'] == true) ? [ filename(info) + "-left.png", "tmp-right.png" ] : filename(info) + (@config['spread'] == true) ? [ filename(info) + "-left.png", filename(info) + "-right.png" ] : filename(info) end def process_pagination(output, face, total_faces, left, right) @@ -574,6 +481,8 @@ files.each do |filename| any_rebuilt = true puts "Rebuilding #{filename_display} (#{type})..." + puts " Using #{filename} as a source" + puts " and writing to #{targets.inspect}" tmp_files = input_obj.build(filename) @@ -588,7 +497,7 @@ files.each do |filename| output_files << targets[i] end end - + input_obj.cleanup end if info['is_paginated'] diff --git a/tests/TestFilter.rb b/tests/TestFilter.rb new file mode 100644 index 0000000..7b756a4 --- /dev/null +++ b/tests/TestFilter.rb @@ -0,0 +1,47 @@ +#!/usr/bin/ruby + +require 'rubygems' +require 'test/unit' +require 'mockfs/override' +require File.dirname(__FILE__) + '/../classes/Filter.rb' + +class TestFilter < Test::Unit::TestCase + def setup + @filter = Filter.instance + end + + def test_recalc_pixels + @filter.config = {} + + assert_raise RuntimeError do + @filter.config = {'print' => true} + end + + assert_raise RuntimeError do + @filter.config = {'print' => true, + 'dpi' => 'test'} + end + + assert_raise RuntimeError do + @filter.config = { + 'print' => true, + 'dpi' => 10 + } + end + + [ 'width', 'height' ].each do |dim| + assert_raise RuntimeError do + @filter.config = { + 'print' => true, + 'dpi' => 10, + 'width' => 10 + } + end + + @filter.config = { + 'width' => 10 + } + assert_equal(10, @filter.config['width']) + end + end +end \ No newline at end of file