Module: Prawn::Font::Wrapping

Included in:
Metrics
Defined in:
lib/prawn/font/wrapping.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#naive_wrap(string, line_width, font_size, options = {}) ⇒ Object

TODO: Replace with TeX optimal algorithm



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
# File 'lib/prawn/font/wrapping.rb', line 13

def naive_wrap(string, line_width, font_size, options = {})
  output = ""
  string.lines.each do |line|
    accumulated_width = 0
    segments = line.scan(/\S+|\s+/)
                                  
    segments.each do |segment|    
      segment_width = string_width(segment, font_size, 
        :kerning => options[:kerning]) 

      if (accumulated_width + segment_width).round > line_width.round
        output << "\n"
        
        if segment =~ /\s/
          accumulated_width = 0
        else
          output << segment
          accumulated_width = segment_width
        end
      else
        output << segment
        accumulated_width += segment_width
      end
    end
  end
  
  output
end