Class: ReportBuilder::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/reportbuilder/image.rb

Overview

Abstract class for an image

Direct Known Subclasses

ImageBlob, ImageFilename

Constant Summary collapse

@@n =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options=Hash.new)
  @id=Digest::MD5.hexdigest(Time.new.to_f.to_s)      
  @type=nil
  if !options.has_key? :name
    @name="Image #{@@n}"
    @@n+=1
  else
    @name=options[:name]
  end
  
  default_options={
    :alt=>@name,
    :chars => [ 'W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+',
    ':', '~', '.', ' ' ],
    :font_rows => 8,
    :font_cols => 4,
    :width=>400,
    :height=>300
  }
  @options=default_options.merge(options)
  @options.each {|k,v|
    self.send("#{k}=",v) if self.respond_to? k
  }
end

Instance Attribute Details

#altObject

Returns the value of attribute alt.



7
8
9
# File 'lib/reportbuilder/image.rb', line 7

def alt
  @alt
end

#charsObject

Returns the value of attribute chars.



8
9
10
# File 'lib/reportbuilder/image.rb', line 8

def chars
  @chars
end

#filenameObject (readonly)

Returns the value of attribute filename.



14
15
16
# File 'lib/reportbuilder/image.rb', line 14

def filename
  @filename
end

#font_colsObject

Returns the value of attribute font_cols.



10
11
12
# File 'lib/reportbuilder/image.rb', line 10

def font_cols
  @font_cols
end

#font_rowsObject

Returns the value of attribute font_rows.



9
10
11
# File 'lib/reportbuilder/image.rb', line 9

def font_rows
  @font_rows
end

#heightObject

Returns the value of attribute height.



12
13
14
# File 'lib/reportbuilder/image.rb', line 12

def height
  @height
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/reportbuilder/image.rb', line 16

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/reportbuilder/image.rb', line 6

def name
  @name
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/reportbuilder/image.rb', line 13

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



15
16
17
# File 'lib/reportbuilder/image.rb', line 15

def url
  @url
end

#widthObject

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_magickObject

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



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