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.
-
#svg_raster ⇒ Object
Returns the value of attribute svg_raster.
-
#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_pdf(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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/reportbuilder/image.rb', line 19 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=.delete :name end ={ :alt=>@name, :chars => [ 'W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+', ':', '~', '.', ' ' ], :font_rows => 8, :font_cols => 4, :width=>nil, :height=>nil, :svg_raster=>false } @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.
15 16 17 |
# File 'lib/reportbuilder/image.rb', line 15 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.
17 18 19 |
# File 'lib/reportbuilder/image.rb', line 17 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 |
#svg_raster ⇒ Object
Returns the value of attribute svg_raster.
13 14 15 |
# File 'lib/reportbuilder/image.rb', line 13 def svg_raster @svg_raster end |
#type ⇒ Object
Returns the value of attribute type.
14 15 16 |
# File 'lib/reportbuilder/image.rb', line 14 def type @type end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
16 17 18 |
# File 'lib/reportbuilder/image.rb', line 16 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
123 124 125 |
# File 'lib/reportbuilder/image.rb', line 123 def create_file(directory) raise "Must be implemented" end |
#generate_raster_from_svg(dir) ⇒ Object
return filename
147 148 149 150 151 |
# File 'lib/reportbuilder/image.rb', line 147 def generate_raster_from_svg(dir) out_file="#{dir}/#{@id}.png" image_magick.write(out_file) out_file end |
#generate_tag_html(builder) ⇒ Object
Generate the code for images on html
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/reportbuilder/image.rb', line 106 def generate_tag_html(builder) attrs="" attrs+=" height='#{:height}' " if :height attrs+=" width='#{:width}' " if :width if @type=='svg' builder.html(" <div class='image'> <!--[if IE]> <embed class='svg' src='#{@url}' #{attrs}'></embed> <![endif]--> <object class='svg' data='#{@url}' type='image/svg+xml' #{attrs} ></object> </div>") else builder.html "<img src='#{@url}' alt='#{alt}' #{attrs} />" end end |
#image_magick ⇒ Object
Get image_magick version of the image
45 46 47 48 49 50 51 52 |
# File 'lib/reportbuilder/image.rb', line 45 def image_magick if ReportBuilder.has_rmagick? _image_magick if respond_to? :_image_magick else raise "Requires RMagick" end end |
#report_building_html(builder) ⇒ Object
126 127 128 129 |
# File 'lib/reportbuilder/image.rb', line 126 def report_building_html(builder) create_file(builder.directory) generate_tag_html(builder) end |
#report_building_pdf(builder) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/reportbuilder/image.rb', line 130 def report_building_pdf(builder) require 'tmpdir' dir=Dir::mktmpdir create_file(dir) if @type=='svg' if svg_raster builder.pdf.image(generate_raster_from_svg(dir)) else # Prawn-svg is not ready for production. y=builder.pdf.y builder.pdf.svg File.read(@filename), :at=>[0, y-60] end else builder.pdf.image(filename, @options) end end |
#report_building_rtf(builder) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/reportbuilder/image.rb', line 153 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
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 98 99 100 101 102 103 104 |
# File 'lib/reportbuilder/image.rb', line 54 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 |