Class: ReportBuilder::Image
- Inherits:
-
Object
- Object
- ReportBuilder::Image
- Defined in:
- lib/reportbuilder/image.rb
Overview
Abstract class for an image
Direct Known Subclasses
Constant Summary collapse
- @@n =
1
Instance Attribute Summary collapse
-
#alt ⇒ Object
Returns the value of attribute alt.
-
#chars ⇒ Object
Returns the value of attribute chars.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#font_cols ⇒ Object
Returns the value of attribute font_cols.
-
#font_rows ⇒ Object
Returns the value of attribute font_rows.
-
#height ⇒ Object
Returns the value of attribute height.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #create_file(directory) ⇒ Object
-
#generate_raster_from_svg(dir) ⇒ Object
return filename.
-
#generate_tag_html(builder) ⇒ Object
Generate the code for images on html.
-
#image_magick ⇒ Object
Get image_magick version of the image.
-
#initialize(options = Hash.new) ⇒ Image
constructor
A new instance of Image.
- #report_building_html(builder) ⇒ Object
- #report_building_rtf(builder) ⇒ Object
-
#report_building_text(builder) ⇒ Object
Based on rubyquiz.com/quiz50.html.
Constructor Details
#initialize(options = Hash.new) ⇒ Image
Returns a new instance of Image.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/reportbuilder/image.rb', line 18 def initialize(=Hash.new) @id=Digest::MD5.hexdigest(Time.new.to_f.to_s) @type=nil if !.has_key? :name @name="Image #{@@n}" @@n+=1 else @name=[:name] end ={ :alt=>@name, :chars => [ 'W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+', ':', '~', '.', ' ' ], :font_rows => 8, :font_cols => 4, :width=>400, :height=>300 } @options=.merge() @options.each {|k,v| self.send("#{k}=",v) if self.respond_to? k } end |
Instance Attribute Details
#alt ⇒ Object
Returns the value of attribute alt.
7 8 9 |
# File 'lib/reportbuilder/image.rb', line 7 def alt @alt end |
#chars ⇒ Object
Returns the value of attribute chars.
8 9 10 |
# File 'lib/reportbuilder/image.rb', line 8 def chars @chars end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
14 15 16 |
# File 'lib/reportbuilder/image.rb', line 14 def filename @filename end |
#font_cols ⇒ Object
Returns the value of attribute font_cols.
10 11 12 |
# File 'lib/reportbuilder/image.rb', line 10 def font_cols @font_cols end |
#font_rows ⇒ Object
Returns the value of attribute font_rows.
9 10 11 |
# File 'lib/reportbuilder/image.rb', line 9 def font_rows @font_rows end |
#height ⇒ Object
Returns the value of attribute height.
12 13 14 |
# File 'lib/reportbuilder/image.rb', line 12 def height @height end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
16 17 18 |
# File 'lib/reportbuilder/image.rb', line 16 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/reportbuilder/image.rb', line 6 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
13 14 15 |
# File 'lib/reportbuilder/image.rb', line 13 def type @type end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
15 16 17 |
# File 'lib/reportbuilder/image.rb', line 15 def url @url end |
#width ⇒ Object
Returns the value of attribute width.
11 12 13 |
# File 'lib/reportbuilder/image.rb', line 11 def width @width end |
Instance Method Details
#create_file(directory) ⇒ Object
112 113 114 |
# File 'lib/reportbuilder/image.rb', line 112 def create_file(directory) raise "Must be implemented" end |
#generate_raster_from_svg(dir) ⇒ Object
return filename
120 121 122 123 124 125 |
# File 'lib/reportbuilder/image.rb', line 120 def generate_raster_from_svg(dir) out_file="#{dir}/#{@id}.png" `rsvg '#{filename}' #{out_file}` out_file end |
#generate_tag_html(builder) ⇒ Object
Generate the code for images on html
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/reportbuilder/image.rb', line 99 def generate_tag_html(builder) if @type=='svg' builder.html(" <div class='image'> <!--[if IE]> <embed class='svg' height='#{@options[:height]}' src='#{@url}' width='#{@options[:width]}'></embed> <![endif]--> <object class='svg' data='#{@url}' height='#{@options[:height]}' type='image/svg+xml' width='#{@options[:width]}'></object> </div>") else builder.html("<img src='#{@url}' alt='#{@options[:alt]}' />") end end |
#image_magick ⇒ Object
Get image_magick version of the image
43 44 45 |
# File 'lib/reportbuilder/image.rb', line 43 def image_magick raise "Must be implemented" end |
#report_building_html(builder) ⇒ Object
115 116 117 118 |
# File 'lib/reportbuilder/image.rb', line 115 def report_building_html(builder) create_file(builder.directory) generate_tag_html(builder) end |
#report_building_rtf(builder) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/reportbuilder/image.rb', line 126 def report_building_rtf(builder) require 'tmpdir' directory=Dir::mktmpdir create_file(directory) raise "Not implemented on RTF::Document. Use gem install clbustos-rtf for support" unless builder.rtf.respond_to? :image if @type=='svg' builder.rtf.image(generate_raster_from_svg(directory)) else builder.rtf.image(filename) end end |
#report_building_text(builder) ⇒ Object
Based on rubyquiz.com/quiz50.html
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/reportbuilder/image.rb', line 47 def report_building_text(builder) if ReportBuilder.has_rmagick? # get image_magick version of image img = image_magick # Resize too-large images. The resulting image is going to be # about twice the size of the input, so if the original image is too # large we need to make it smaller so the ASCII version won't be too # big. The `change_geometry' method computes new dimensions for an # image based on the geometry argument. The '320x320>' argument says # "If the image is too big to fit in a 320x320 square, compute the # dimensions of an image that will fit, but retain the original aspect # ratio. If the image is already smaller than 320x320, keep the same # dimensions." img.change_geometry('320x320>') do |cols, rows| img.resize!(cols, rows) if cols != img.columns || rows != img.rows end # Compute the image size in ASCII "pixels" and resize the image to have # those dimensions. The resulting image does not have the same aspect # ratio as the original, but since our "pixels" are twice as tall as # they are wide we'll get our proportions back (roughly) when we render. pr = img.rows / font_rows pc = img.columns / font_cols img.resize!(pc, pr) img = img.quantize(chars.size, Magick::GRAYColorspace) img = img.normalize out="" # Draw the image surrounded by a border. The `view' method is slow but # it makes it easy to address individual pixels. In grayscale images, # all three RGB channels have the same value so the red channel is as # good as any for choosing which character to represent the intensity of # this particular pixel. border = '+' + ('-' * pc) + '+' out += border+"\n" img.view(0, 0, pc, pr) do |view| pr.times do |i| out+= '|' pc.times do |j| out+= chars[view[i][j].red / (2**16 / chars.size)] end out+= '|'+"\n" end end out+= border builder.preformatted(out) else raise "Requires RMagick" end end |