more filter separation

This commit is contained in:
John Bintz 2010-01-04 22:55:50 -05:00
parent 2758239c1a
commit 8e16d7cc00
8 changed files with 110 additions and 76 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
.PHONY : test
test :
testrb -b tests/*

View File

@ -1,19 +1,16 @@
require 'singleton'
class Filter class Filter
include Singleton
@config = {} @config = {}
@cleanup = [] @cleanup = []
attr_accessor :config, :cleanup attr_accessor :config, :cleanup
def initialize def initialize
@config = {} @config = {}
@cleanup = [] @cleanup = []
end end
def cleanup def cleanup
@cleanup.each do |f| @cleanup.each do |f|
if File.exists? f; File.unlink(f); end if File.exists? f; File.unlink(f); end
end end
end end
@ -31,8 +28,8 @@ class Filter
@config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i @config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i
else else
@config.delete('height') @config.delete('height')
end end
if (!@config['width'] && !@config['height']) if (!@config['width'] && !@config['height'])
raise "No dimensions defined!" raise "No dimensions defined!"
end end
@ -44,7 +41,7 @@ class Filter
# #
def config=(c) def config=(c)
@config = c @config = c
recalc_pixels recalc_pixels
end end
@ -57,7 +54,7 @@ class Filter
width = @config['width'] width = @config['width']
height = @config['height'] height = @config['height']
inkscape_target = target inkscape_target = target
if @config['rotate'] if @config['rotate']
case @config['rotate'] case @config['rotate']
when 90, -90 when 90, -90
@ -68,16 +65,16 @@ class Filter
if width && (width.to_i != 0); params << "-w #{width} "; end if width && (width.to_i != 0); params << "-w #{width} "; end
if height && (height.to_i != 0); params << "-h #{height} "; end if height && (height.to_i != 0); params << "-h #{height} "; end
system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"") system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"")
if @config['rotate'] if @config['rotate']
command = [ command = [
"\"#{inkscape_target}\"", "\"#{inkscape_target}\"",
"-rotate #{@config['rotate']}", "-rotate #{@config['rotate']}",
"\"#{target}\"" "\"#{target}\""
] ]
convert(command) convert(command)
File.unlink(inkscape_target) File.unlink(inkscape_target)
end end

5
classes/InputFilter.rb Normal file
View File

@ -0,0 +1,5 @@
require 'filter'
class InputFilter < Filter
OutputFilename = "tmp.png"
end

17
classes/OutputFilter.rb Normal file
View File

@ -0,0 +1,17 @@
require File.dirname(__FILE__) + '/Filter.rb'
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

View File

@ -3,10 +3,8 @@
require 'yaml' require 'yaml'
require 'time' require 'time'
require File.dirname(__FILE__) + '/classes/Filter.rb' Dir[File.dirname(__FILE__) + "/classes/*.rb"].each do |file|
require file
class InputFilter < Filter
OutputFilename = "tmp.png"
end end
class OutputFilter < Filter class OutputFilter < Filter
@ -15,9 +13,9 @@ class OutputFilter < Filter
# #
def filename(info) def filename(info)
target = @config['target'] target = @config['target']
info.each { |k,v| target = target.gsub("{#{k}}", v.to_s) } info.each { |k,v| target = target.gsub("{#{k}}", v.to_s) }
target target
end end
# #
# get the output targets for this filter # get the output targets for this filter
@ -44,7 +42,7 @@ module PrintHandling
page_width = @config['page_width_inches'] page_width = @config['page_width_inches']
page_height = @config['page_height_inches'] page_height = @config['page_height_inches']
end end
page_width *= @config['dpi'] page_width *= @config['dpi']
page_height *= @config['dpi'] page_height *= @config['dpi']
else else
@ -65,7 +63,7 @@ module PrintHandling
"-size #{page_width.to_i}x#{page_height.to_i}", "-size #{page_width.to_i}x#{page_height.to_i}",
"xc:white" "xc:white"
] ]
case side case side
when "none" when "none"
command << "-gravity Center" command << "-gravity Center"
@ -82,21 +80,21 @@ module PrintHandling
else else
command << "\"#{output}\"" command << "\"#{output}\""
end end
convert(command) convert(command)
end end
end end
class SVGToTempBitmap < InputFilter class SVGToTempBitmap < InputFilter
include PrintHandling include PrintHandling
# #
# select which build method to use based on the number of provided images. # select which build method to use based on the number of provided images.
# #
def build(input) def build(input)
(input.instance_of? Array) ? multiple(input) : single(input) (input.instance_of? Array) ? multiple(input) : single(input)
end end
# #
# process a single input file, possibly splitting it into two output files if it's a spread # process a single input file, possibly splitting it into two output files if it's a spread
# #
@ -108,7 +106,7 @@ class SVGToTempBitmap < InputFilter
if @config['spread'] if @config['spread']
width, height = get_dimensions(filename) width, height = get_dimensions(filename)
targets = [] targets = []
[ [ "left", 0 ], [ "right", width / 2 ] ].each do |side, offset| [ [ "left", 0 ], [ "right", width / 2 ] ].each do |side, offset|
target = filename + "-#{side}.png" target = filename + "-#{side}.png"
command = [ command = [
@ -118,16 +116,16 @@ class SVGToTempBitmap < InputFilter
"+repage", "+repage",
"\"#{target}\"" "\"#{target}\""
] ]
convert(command) convert(command)
targets << target targets << target
@cleanup << target @cleanup << target
end end
return targets return targets
end end
return filename return filename
end end
@ -138,23 +136,23 @@ class SVGToTempBitmap < InputFilter
# #
def multiple(files) def multiple(files)
if @config['spread']; raise "Spreads and grids combined do not make sense"; end if @config['spread']; raise "Spreads and grids combined do not make sense"; end
width, height = @config['grid'].split("x").collect { |f| f.to_f } width, height = @config['grid'].split("x").collect { |f| f.to_f }
joined_files = [] joined_files = []
page_width, page_height = calculate_page_size page_width, page_height = calculate_page_size
grid_width = page_width / width grid_width = page_width / width
grid_height = page_height / height grid_height = page_height / height
0.upto(files.length - 1) do |i| 0.upto(files.length - 1) do |i|
x = i % width x = i % width
y = (i / width).floor y = (i / width).floor
if files[i].split('/').last != "blank" if files[i].split('/').last != "blank"
tmp_svg_output = OutputFilename + "-#{i}.png" tmp_svg_output = OutputFilename + "-#{i}.png"
inkscape(Dir.pwd + '/' + files[i], tmp_svg_output) inkscape(Dir.pwd + '/' + files[i], tmp_svg_output)
joined_files << [ tmp_svg_output, x, y ] joined_files << [ tmp_svg_output, x, y ]
@cleanup << tmp_svg_output @cleanup << tmp_svg_output
end end
@ -164,20 +162,20 @@ class SVGToTempBitmap < InputFilter
"-size #{page_width}x#{page_height}", "-size #{page_width}x#{page_height}",
"xc:white" "xc:white"
] ]
joined_files.each do |file, x, y| joined_files.each do |file, x, y|
image_width, image_height = get_dimensions(file) image_width, image_height = get_dimensions(file)
x_offset = (grid_width - image_width) / 2 x_offset = (grid_width - image_width) / 2
y_offset = (grid_height - image_height) / 2 y_offset = (grid_height - image_height) / 2
command << "\"#{file}\" -geometry +#{x * grid_width + x_offset}+#{y * grid_height + y_offset} -composite" command << "\"#{file}\" -geometry +#{x * grid_width + x_offset}+#{y * grid_height + y_offset} -composite"
end end
command << OutputFilename command << OutputFilename
convert(command) convert(command)
@cleanup << OutputFilename @cleanup << OutputFilename
OutputFilename OutputFilename
end end
@ -191,11 +189,11 @@ class TempBitmapToWeb < OutputFilter
quality = @config['quality'] ? @config['quality'] : 80 quality = @config['quality'] ? @config['quality'] : 80
convert("\"#{input}\" -quality #{quality} \"#{output}\"") convert("\"#{input}\" -quality #{quality} \"#{output}\"")
end end
def filename(info) def filename(info)
if !@config['start_date']; raise "Must define a start date!"; end if !@config['start_date']; raise "Must define a start date!"; end
if !@config['period']; raise "Must define a period!"; end if !@config['period']; raise "Must define a period!"; end
index = info['index'].to_i index = info['index'].to_i
if index == 0 && @config['announce_date'] if index == 0 && @config['announce_date']
comic_date = @config['announce_date'] comic_date = @config['announce_date']
@ -210,7 +208,7 @@ class TempBitmapToWeb < OutputFilter
when "weekly" when "weekly"
weeks_from_start = index weeks_from_start = index
end end
if @config['delay_at_index'] && @config['delay_length_weeks'] if @config['delay_at_index'] && @config['delay_length_weeks']
if index >= @config['delay_at_index'] if index >= @config['delay_at_index']
weeks_from_start += @config['delay_length_weeks'] weeks_from_start += @config['delay_length_weeks']
@ -219,7 +217,7 @@ class TempBitmapToWeb < OutputFilter
comic_date = Time.parse(@config['start_date']) + ((((day_of_week + weeks_from_start * 7) * 24) + 6) * 60 * 60) comic_date = Time.parse(@config['start_date']) + ((((day_of_week + weeks_from_start * 7) * 24) + 6) * 60 * 60)
end end
info['date'] = comic_date.strftime("%Y-%m-%d") info['date'] = comic_date.strftime("%Y-%m-%d")
super(info) super(info)
end end
@ -247,9 +245,9 @@ module Pagination
sheet_faces[sheet_face_index][is_right] = file sheet_faces[sheet_face_index][is_right] = file
is_right = 1 - is_right is_right = 1 - is_right
sheet_face_index += is_descending sheet_face_index += is_descending
if sheet_face_index == number_of_sheet_faces if sheet_face_index == number_of_sheet_faces
sheet_face_index -= 1 sheet_face_index -= 1
is_descending = -1 is_descending = -1
@ -262,7 +260,7 @@ module Pagination
process_pagination(f, i, sheet_faces.length, *sheet_faces[i]) process_pagination(f, i, sheet_faces.length, *sheet_faces[i])
tmp_pdf_files << f tmp_pdf_files << f
end end
system("pdfjoin #{tmp_pdf_files.collect { |f| "\"#{f}\"" }.join(" ")} --outfile \"#{@config['target']}\"") system("pdfjoin #{tmp_pdf_files.collect { |f| "\"#{f}\"" }.join(" ")} --outfile \"#{@config['target']}\"")
end end
end end
@ -292,12 +290,12 @@ class TempBitmapToPaginatedPrint < OutputFilter
def process_pagination(output, face, total_faces, left, right) def process_pagination(output, face, total_faces, left, right)
page_width, page_height = calculate_page_size page_width, page_height = calculate_page_size
commands = [ commands = [
"-size #{page_width * 2}x#{page_height}", "-size #{page_width * 2}x#{page_height}",
"xc:white" "xc:white"
] ]
left_creep = 0 left_creep = 0
right_creep = 0 right_creep = 0
if @config['page_thickness'] if @config['page_thickness']
@ -306,7 +304,7 @@ class TempBitmapToPaginatedPrint < OutputFilter
left_creep = ((max_creep - (face - max_creep).abs) * @config['page_thickness'].to_f * @config['dpi'].to_i).floor left_creep = ((max_creep - (face - max_creep).abs) * @config['page_thickness'].to_f * @config['dpi'].to_i).floor
right_creep = ((max_creep - (total_faces - face - max_creep).abs) * @config['page_thickness'].to_f * @config['dpi'].to_i).floor right_creep = ((max_creep - (total_faces - face - max_creep).abs) * @config['page_thickness'].to_f * @config['dpi'].to_i).floor
end end
if left if left
commands << "\"#{left}\" -geometry +#{left_creep.to_i}+0 -composite" commands << "\"#{left}\" -geometry +#{left_creep.to_i}+0 -composite"
end end
@ -371,7 +369,7 @@ if global['pages']
end end
else else
re = Regexp.new(global['match']) re = Regexp.new(global['match'])
files = Dir[global['path']].sort.collect do |filename| files = Dir[global['path']].sort.collect do |filename|
if matches = re.match(filename) if matches = re.match(filename)
filename filename
@ -405,20 +403,20 @@ files.each do |filename|
if ok if ok
filename_display = (filename.instance_of? Array) ? filename.join(", ") : filename filename_display = (filename.instance_of? Array) ? filename.join(", ") : filename
puts "Examining #{filename_display}..." puts "Examining #{filename_display}..."
filename_parts = { filename_parts = {
'page_index' => sprintf(page_index_format, page_index) 'page_index' => sprintf(page_index_format, page_index)
} }
if matches if matches
all, index, title = matches.to_a all, index, title = matches.to_a
else else
index = page_index - 1 index = page_index - 1
title = "" title = ""
end end
if global['title']; title = global['title'].gsub("{index}", index).gsub("{title}", title); end if global['title']; title = global['title'].gsub("{index}", index).gsub("{title}", title); end
filename_parts['index'] = index filename_parts['index'] = index
filename_parts['title'] = title filename_parts['title'] = title
@ -428,7 +426,7 @@ files.each do |filename|
input = nil; output = nil input = nil; output = nil
fileinfo_key = (filename.instance_of? Array) ? filename.join(",") : filename fileinfo_key = (filename.instance_of? Array) ? filename.join(",") : filename
file_fileinfo = (fileinfo_by_file[fileinfo_key]) ? fileinfo_by_file[fileinfo_key] : {} file_fileinfo = (fileinfo_by_file[fileinfo_key]) ? fileinfo_by_file[fileinfo_key] : {}
extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase
@ -450,7 +448,7 @@ files.each do |filename|
input_obj = input.instance input_obj = input.instance
input_obj.config = info.dup.merge(fileinfo).merge(file_fileinfo) input_obj.config = info.dup.merge(fileinfo).merge(file_fileinfo)
output_obj = output.instance output_obj = output.instance
output_obj.config = info.dup.merge(fileinfo).merge(file_fileinfo) output_obj.config = info.dup.merge(fileinfo).merge(file_fileinfo)
@ -459,9 +457,9 @@ files.each do |filename|
end end
targets = output_obj.targets(filename_parts) targets = output_obj.targets(filename_parts)
rebuild = false rebuild = false
[ targets ].flatten.each do |t| [ targets ].flatten.each do |t|
if !File.exists?(t) if !File.exists?(t)
rebuild = true rebuild = true
@ -470,12 +468,12 @@ files.each do |filename|
if File.basename(f) != "blank" if File.basename(f) != "blank"
if File.mtime(f) > File.mtime(t) if File.mtime(f) > File.mtime(t)
rebuild = true rebuild = true
end end
end end
end end
end end
end end
if rebuild if rebuild
any_rebuilt = true any_rebuilt = true
@ -496,7 +494,7 @@ files.each do |filename|
output_files << targets[i] output_files << targets[i]
end end
end end
input_obj.cleanup input_obj.cleanup
end end
if info['is_paginated'] if info['is_paginated']
@ -505,11 +503,11 @@ files.each do |filename|
end end
if info['rsync'] if info['rsync']
if !rsync_files_by_target[info['rsync']]; rsync_files_by_target[info['rsync']] = []; end if !rsync_files_by_target[info['rsync']]; rsync_files_by_target[info['rsync']] = []; end
rsync_files_by_target[info['rsync']] << targets rsync_files_by_target[info['rsync']] << targets
end end
end end
end end
page_index += 1 page_index += 1
end end
end end
@ -517,13 +515,13 @@ end
config.each do |type, info| config.each do |type, info|
if info['is_paginated'] if info['is_paginated']
output = TempBitmapToPaginatedPrint output = TempBitmapToPaginatedPrint
output_obj = output.instance output_obj = output.instance
output_obj.config = info.dup output_obj.config = info.dup
output_obj.paginate(paginated_source_files[type].flatten) output_obj.paginate(paginated_source_files[type].flatten)
end end
if info['rsync'] if info['rsync']
system("echo '#{rsync_files_by_target[info['rsync']].join("\n")}' | rsync -vru --files-from=- . #{info['rsync']}") system("echo '#{rsync_files_by_target[info['rsync']].join("\n")}' | rsync -vru --files-from=- . #{info['rsync']}")
end end

View File

@ -1,5 +1,3 @@
#!/usr/bin/ruby
require 'rubygems' require 'rubygems'
require 'test/unit' require 'test/unit'
require 'mockfs/override' require 'mockfs/override'
@ -28,7 +26,7 @@ class TestFilter < Test::Unit::TestCase
'dpi' => 10 'dpi' => 10
} }
end end
[ 'width', 'height' ].each do |dim| [ 'width', 'height' ].each do |dim|
assert_raise RuntimeError do assert_raise RuntimeError do
@filter.config = { @filter.config = {
@ -44,4 +42,4 @@ class TestFilter < Test::Unit::TestCase
assert_equal(10, @filter.config['width']) assert_equal(10, @filter.config['width'])
end end
end end
end end

17
tests/TestOutputFilter.rb Normal file
View File

@ -0,0 +1,17 @@
require 'rubygems'
require 'test/unit'
require 'mocha'
require File.dirname(__FILE__) + '/../classes/OutputFilter.rb'
class TestOutputFilter < Test::Unit::TestCase
def setup
@of = OutputFilter.new
@of.stubs(:recalc_pixels)
end
def test_filename
@of.config = { 'target' => 'test{test}{test2}test3' }
assert_equal 'testtest4test5test3', @of.filename({ 'test' => 'test4', 'test2' => 'test5' })
end
end

View File

@ -1,5 +1,3 @@
#!/usr/bin/ruby
require 'rubygems' require 'rubygems'
require 'test/unit' require 'test/unit'
require File.dirname(__FILE__) + '/../classes/Scheduler.rb' require File.dirname(__FILE__) + '/../classes/Scheduler.rb'