class Prawn::Text::LineWrap

Public Instance Methods

wrap_line(line, options) click to toggle source
# File lib/prawn/text/box.rb, line 349
def wrap_line(line, options)
  @document = options[:document]
  @size = options[:size]
  @kerning = options[:kerning]
  @width = options[:width]
  @accumulated_width = 0
  @output = ""

  scan_pattern = @document.font.unicode? ? /\S+|\s+/ : /\S+|\s+/n
  space_scan_pattern = @document.font.unicode? ? /\s/ : /\s/n

  line.scan(scan_pattern).each do |segment|
    # yes, this block could be split out into another method, but it is
    # called on every word printed, so I'm keeping it here for speed

    segment_width = @document.width_of(segment,
                                       :size => @size,
                                       :kerning => @kerning)

    if @accumulated_width + segment_width <= @width
      @accumulated_width += segment_width
      @output << segment
    else
      # if the line contains white space, don't split the
      # final word that doesn't fit, just return what fits nicely
      break if @output =~ space_scan_pattern
      wrap_by_char(segment)
      break
    end
  end
  @output
end

Private Instance Methods

append_char(char) click to toggle source
# File lib/prawn/text/box.rb, line 396
def append_char(char)
  @accumulated_width += @document.width_of(char,
                                           :size => @size,
                                           :kerning => @kerning)
  if @accumulated_width >= @width
    false
  else
    @output << char
    true
  end
end
wrap_by_char(segment) click to toggle source
# File lib/prawn/text/box.rb, line 384
def wrap_by_char(segment)
  if @document.font.unicode?
    segment.unpack("U*").each do |char_int|
      return unless append_char([char_int].pack("U"))
    end
  else
    segment.each_char do |char|
      return unless append_char(char)
    end
  end
end