Class: AnalyticsCharts::CustomModule
- Inherits:
-
Object
- Object
- AnalyticsCharts::CustomModule
- Includes:
- Magick
- Defined in:
- lib/analytics_charts/custom_module.rb
Instance Method Summary collapse
- #fits_in_a_line?(text) ⇒ Boolean
-
#initialize(image_path, text, output_path) ⇒ CustomModule
constructor
A new instance of CustomModule.
- #line_wrap(text) ⇒ Object
- #tokenize_text_by_lines(text) ⇒ Object
Constructor Details
#initialize(image_path, text, output_path) ⇒ CustomModule
Returns a new instance of CustomModule.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/analytics_charts/custom_module.rb', line 5 def initialize(image_path, text, output_path) @d = Draw.new @d.fill = 'white' @d.font = 'Helvetica' @pointsize = 18 @d.pointsize = @pointsize @d.font_weight = 500 @composite_image = Image.read(image_path)[0] @dummy_image = Image.new(1,1) @composite_columns = @composite_image.columns @composite_rows = @composite_image.rows text = text.gsub(/['%]/, '%' => '%%', "'" => "\'") begin @rows_of_text = tokenize_text_by_lines(text) rescue puts "Had some error in CustomModule, probably a nil calling each" return end num_separators = @rows_of_text.count {|row| row.empty? } num_special_sep = @rows_of_text.count {|row| row.include? "@$$" } @line_height = @pointsize + 2 @sep_offset = @line_height / 2 starting_offset = @line_height / 2 ending_offset = @line_height / 2 @height = @composite_rows + (@rows_of_text.size - num_separators) * @line_height + (num_separators-num_special_sep) * @sep_offset + starting_offset + ending_offset @base_image = Image.new(@composite_columns, @height) { self.background_color = "black" } @base_image.composite!(@composite_image,0,0,OverCompositeOp) #@d = @d.stroke_color 'white' #@d.stroke_width(1) #@d = @d.line(0, @composite_rows, @composite_columns, @composite_rows) #@d.draw(@base_image) y_offset = @composite_rows + starting_offset @rows_of_text.each do |text| if text.include? "@$$" # No paragraph break if we insert this uncommonly used word text.sub!("@$$", "") y_offset -= @sep_offset y_offset += @line_height @d.annotate(@base_image, 0 ,0, @pointsize, y_offset, text) next else if text.empty? y_offset += @sep_offset else y_offset += @line_height @d.annotate(@base_image, 0 ,0, @pointsize, y_offset, text) end end end @base_image.write(output_path) end |
Instance Method Details
#fits_in_a_line?(text) ⇒ Boolean
88 89 90 |
# File 'lib/analytics_charts/custom_module.rb', line 88 def fits_in_a_line?(text) return @d.get_type_metrics(@dummy_image,text).width < @composite_image.columns - 36 end |
#line_wrap(text) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/analytics_charts/custom_module.rb', line 72 def line_wrap(text) tokens = text.split.reverse # Pop stuff off # Safety check, do not allow super long words. tokens.each {|token| return "" if not fits_in_a_line?(token) } line_wrap_tokens = Array.new line_builder = "" while tokens.size != 0 line_builder = tokens.pop # Pop the first word in a line while tokens.size != 0 and fits_in_a_line?(line_builder + " " + tokens.last) line_builder += " " + tokens.pop end line_wrap_tokens.push(line_builder) # Add to list of lines end line_wrap_tokens end |
#tokenize_text_by_lines(text) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/analytics_charts/custom_module.rb', line 61 def tokenize_text_by_lines(text) # First split the text by the line carriage element carriage_split_lines = text.split("\r\n") line_tokens = Array.new carriage_split_lines.each do |carriage_split_line| line_wrap_lines = line_wrap(carriage_split_line) line_wrap_lines.each { |line| line_tokens.push(line) } line_tokens.push("") end line_tokens end |