Module: Prawn::Document::Text::Wrapping

Included in:
Prawn::Document::Text
Defined in:
lib/prawn/document/text/wrapping.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#height_of(string, line_width, size = font_size) ⇒ Object

Gets height of text in PDF points at current font size. Text :line_width must be specified in PDF points.

If using an AFM, string must be encoded as WinAnsi (Use normalize_encoding to convert)



20
21
22
23
# File 'lib/prawn/document/text/wrapping.rb', line 20

def height_of(string, line_width, size=font_size)
  string = naive_wrap(string, line_width, size)
  string.lines.to_a.length * font.height_at(size)
end

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

TODO: Replace with TeX optimal algorithm



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
# File 'lib/prawn/document/text/wrapping.rb', line 26

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

      if (accumulated_width + segment_width).round > line_width.round
        output = "#{output.sub(/[ \t]*\n?(\n*)\z/, "\n\\1")}"
        
        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