Parent

ReportBuilder::Image

Creates an Image

Attributes

name[R]

Public Class Methods

new(filename, options={}) click to toggle source
    # File lib/reportbuilder/image.rb, line 6
 6:   def initialize(filename, options={})
 7:     if !options.has_key? :name
 8:       @name="Image #{@@n}"
 9:       @@n+=1
10:     else
11:       @name=options[:name]
12:     end
13:     default_options={
14:       :alt=>@name,
15:       :chars => [ 'W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+',
16:       ':', '~', '.', ' ' ],
17:       :font_rows => 8,
18:       :font_cols => 4
19:     }
20:     @options=default_options.merge(options)
21:     @filename=filename
22:   end

Public Instance Methods

report_building_html(builder) click to toggle source
    # File lib/reportbuilder/image.rb, line 78
78:   def report_building_html(builder)
79:     basedir=builder.directory+"/images"
80:     out=basedir+"/"+File.basename(@filename)
81:     if(File.exists? @filename)
82:       if !File.exists? out
83:         FileUtils.mkdir_p basedir
84:         FileUtils.cp @filename, out
85:       end
86:     end
87:     builder.html("<img src='images/#{File.basename(@filename)}' alt='#{@options[:alt]}' />")
88:   end
report_building_rtf(builder) click to toggle source
    # File lib/reportbuilder/image.rb, line 74
74:   def report_building_rtf(builder)
75:     raise "Not implemented on RTF::Document. Use gem install thecrisoshow-ruby-rtf for support" unless builder.rtf.respond_to? :image
76:     builder.rtf.image(@filename)
77:   end
report_building_text(builder) click to toggle source

Based on rubyquiz.com/quiz50.html

    # File lib/reportbuilder/image.rb, line 24
24:   def report_building_text(builder)
25:     require 'RMagick'
26: 
27: 
28:     img = Magick::Image.read(@filename).first
29: 
30:     # Resize too-large images. The resulting image is going to be
31:     # about twice the size of the input, so if the original image is too
32:     # large we need to make it smaller so the ASCII version won't be too
33:     # big. The `change_geometry' method computes new dimensions for an
34:     # image based on the geometry argument. The '320x320>' argument says
35:     # "If the image is too big to fit in a 320x320 square, compute the
36:     # dimensions of an image that will fit, but retain the original aspect
37:     # ratio. If the image is already smaller than 320x320, keep the same
38:     # dimensions."
39:     img.change_geometry('320x320>') do |cols, rows|
40:       img.resize!(cols, rows) if cols != img.columns || rows != img.rows
41:     end
42: 
43:     # Compute the image size in ASCII "pixels" and resize the image to have
44:     # those dimensions. The resulting image does not have the same aspect
45:     # ratio as the original, but since our "pixels" are twice as tall as
46:     # they are wide we'll get our proportions back (roughly) when we render.
47:     pr = img.rows / @options[:font_rows]
48:     pc = img.columns / @options[:font_cols]
49:     img.resize!(pc, pr)
50: 
51:     img = img.quantize(@options[:chars].size, Magick::GRAYColorspace)
52:     img = img.normalize
53: 
54:     out=""
55:     # Draw the image surrounded by a border. The `view' method is slow but
56:     # it makes it easy to address individual pixels. In grayscale images,
57:     # all three RGB channels have the same value so the red channel is as
58:     # good as any for choosing which character to represent the intensity of
59:     # this particular pixel.
60:     border = '+' + ('-' * pc) + '+'
61:     out += border+"\n"
62:     img.view(0, 0, pc, pr) do |view|
63:       pr.times do |i|
64:         out+= '|'
65:         pc.times do |j|
66:           out+= @options[:chars][view[i][j].red / (2**16/@options[:chars].size)]
67:         end
68:         out+= '|'+"\n"
69:       end
70:     end
71:     out+= border
72:     builder.preformatted(out)
73:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.