Class: Prawn::Text::LineWrap

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/text/box.rb

Instance Method Summary collapse

Instance Method Details

#wrap_line(line, options) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# 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