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,file)
45 @x1,@y1,@x2,@y2,@file = x1,y1,x2,y2,file
47 def should_be_plain_colored
48 @rgb = @file.get_area(@x1,@y1,@x2,@y2) unless @rgb
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 = @file.get_area(@x1,@y1,@x2,@y2) unless @rgb
53 @rgb.minmax != [@rgb[0],@rgb[0]] or raise AreaError.new(self,"is plain colored")
55 def should_contain_text(text)
56 text2 = @file.get_text(@x1,@y1,@x2,@y2)
57 text2 == text or raise AreaError.new(self, "doesn't contain text \"#{text}\" (found: \"#{text2}\")")
60 "(#{@x1},#{@y1},#{@x2},#{@y2})"
65 # ImageMagick rgb triples are 16 bit
66 (rgb.reverse+[0]).map {|c| c>>8}.pack("CCCC").unpack("i")[0]
71 def initialize(x,y,rgb)
74 def should_be_of_color(color2)
75 color1 = rgb_to_int(@rgb)
76 color1 == color2 or raise WrongColor.new(self)
78 def should_be_brighter_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 brighter than",pixel)
83 def should_be_darker_than(pixel)
84 gray1 = @rgb.inject(0) {|sum,e| sum+e}
85 gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
86 gray1 < gray2 or raise PixelError.new(self,"is not less bright than",pixel)
88 def should_be_the_same_as(pixel)
89 @rgb == pixel.rgb or raise PixelError.new(self,"is not the same as",pixel)
98 $tempfiles.each do |file|
104 def initialize(filename, page)
110 @swfname = @filename.gsub(/.pdf$/i,"")+".swf"
111 $tempfiles += [@swfname]
112 `pdfinfo #{@filename}` =~ /Page size:\s*([0-9]+) x ([0-9]+) pts/
114 dpi = (72.0 * 612 / width.to_i).to_i
115 output = `pdf2swf -f --flatten -s zoom=#{dpi} -p #{@page} #{@filename} -o #{@swfname} 2>&1`
116 #output = `pdf2swf -s zoom=#{dpi} --flatten -p #{@page} #{@filename} -o #{@swfname} 2>&1`
117 raise ConversionFailed.new(output,@swfname) unless File.exists?(@swfname)
122 @pngname = @filename.gsub(/.pdf$/i,"")+".png"
124 output = `swfrender --legacy #{@swfname} -o #{@pngname} 2>&1`
125 raise ConversionFailed.new(output,@pngname) unless File.exists?(@pngname)
126 @img = Magick::Image.read(@pngname).first
131 def get_text(x1,y1,x2,y2)
133 #puts "swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}"
134 #puts `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
135 `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`.chomp
137 def get_area(x1,y1,x2,y2)
139 data = @img.export_pixels(x1, y1, x2-x1, y2-y1, "RGB")
140 Array.new(data.size/3) do |i| data.slice(i*3,3) end
142 def area_at(x1,y1,x2,y2)
143 return Area.new(x1,y1,x2,y2,self)
155 data = @img.export_pixels(x, y, 1, 1, "RGB")
156 return Pixel.new(x,y,data)
162 module ExampleMethods
163 def area_at(x1,y1,x2,y2)
164 @file.area_at(x1,y1,x2,y2)
175 def initialize(proxy,&impl)
177 # overriding the initialize() function saves us from having to
178 # set up the document in the test. The bad news, however, is that
179 # we have to be very careful not to raise exceptions here-
180 # rspec would miss those.
183 @_implementation = impl
185 @file = DocFile.new(proxy.description, (proxy.options[:page] or 1))
188 module ExampleGroupMethods
189 alias :convert_file :example