5 class WrongColor < Exception
10 "Wrong color at #{@pixel}"
13 class AreaError < Exception
14 def initialize(area,problem)
15 @area,@problem = area,problem
18 "Area at #{@area} #{@problem}"
21 class PixelError < Exception
22 def initialize(p1, relation,p2)
23 @p1,@p2,@relation = p1,p2,relation
26 "Pixel #{@p1} #{@relation} #{@p2}"
29 class ConversionFailed < Exception
30 def initialize(output,file)
33 @exists = File.exists?(file)
36 puts "-"*26+" Conversion failed "+"-"*27
37 (puts @output) if @output
38 puts "file #{@file} doesn't exist" if not @exists
44 def initialize(x1,y1,x2,y2,data)
45 @x1,@y1,@x2,@y2 = x1,y1,x2,y2
46 @rgb = Array.new(data.size/3) do |i| data.slice(i*3,3) end
48 def should_be_plain_colored
49 @rgb.minmax == [@rgb[0],@rgb[0]] or raise AreaError.new(self,"is not plain colored")
51 def should_not_be_plain_colored
52 @rgb.minmax != [@rgb[0],@rgb[0]] or raise AreaError.new(self,"is plain colored")
55 "(#{@x1},#{@y1},#{@x2},#{@y2})"
60 # ImageMagick rgb triples are 16 bit
61 (rgb.reverse+[0]).map {|c| c>>8}.pack("CCCC").unpack("i")[0]
66 def initialize(x,y,rgb)
69 def should_be_of_color(color2)
70 color1 = rgb_to_int(@rgb)
71 color1 == color2 or raise WrongColor.new(self)
73 def should_be_brighter_than(pixel)
74 gray1 = @rgb.inject(0) {|sum,e| sum+e}
75 gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
76 gray1 > gray2 or raise PixelError.new(self,"is not brighter than",pixel)
78 def should_be_darker_than(pixel)
79 gray1 = @rgb.inject(0) {|sum,e| sum+e}
80 gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
81 gray1 < gray2 or raise PixelError.new(self,"is not less bright than",pixel)
83 def should_be_the_same_as(pixel)
84 @rgb == pixel.rgb or raise PixelError.new(self,"is not the same as",pixel)
91 def initialize(filename, page)
96 @swfname = @filename.gsub(/.pdf$/i,"")+".swf"
97 @pngname = @filename.gsub(/.pdf$/i,"")+".png"
99 output = `pdf2swf --flatten -p #{@page} #{@filename} -o #{@swfname} 2>&1`
100 raise ConversionFailed.new(output,@swfname) unless File.exists?(@swfname)
101 output = `swfrender --legacy #{@swfname} -o #{@pngname} 2>&1`
102 raise ConversionFailed.new(output,@pngname) unless File.exists?(@pngname)
103 @img = Magick::Image.read(@pngname).first
109 def area_at(x1,y1,x2,y2)
111 data = @img.export_pixels(x1, y1, x2-x1, y2-y1, "RGB")
112 return Area.new(x1,y1,x2,y2,data)
124 data = @img.export_pixels(x, y, 1, 1, "RGB")
125 return Pixel.new(x,y,data)
131 module ExampleMethods
132 def area_at(x1,y1,x2,y2)
133 @file.area_at(x1,y1,x2,y2)
144 def initialize(proxy,&impl)
146 # overriding the initialize() function saves us from having to
147 # set up the document in the test. The bad news, however, is that
148 # we have to be very careful not to raise exceptions here-
149 # rspec would miss those.
152 @_implementation = impl
154 @file = DocFile.new(proxy.description, (proxy.options[:page] or 1))
157 module ExampleGroupMethods
158 alias :convert_file :example