Module: Prawn::Core::Text::Wrap

Included in:
Text::Box
Defined in:
lib/prawn/core/text/wrap.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#initialize(text, options) ⇒ Object



8
9
10
# File 'lib/prawn/core/text/wrap.rb', line 8

def initialize(text, options)
  @line_wrap = Prawn::Core::Text::LineWrap.new
end

#wrap(text) ⇒ Object

#wrap is part of the developer API. Override it in extensions to Prawn that inherit Text::Box but need a different placement algorithm. #wrap is where the actual placement of text happens. If @inked is false, then all the placement computations should be performed, and unprinted text returned, but no text should actually be drawn to the PDF. This enables look-ahead computations that need to know whether all the text was printed under a set of conditions or how tall the text was under certain conditions.

#wrap is called from several places within box.rb and relies on certain conditions established by render. Do not call #wrap from outside of Text::Box or its descendants.

#wrap should set the following instance variables:

<tt>@text</tt>:: the text that was printed
<tt>@line_height</tt>:: the height of the last printed line
<tt>@descender</tt>:: the descender height of the last printed line
<tt>@ascender</tt>:: the ascender heigth of the last printed line
<tt>@baseline_y</tt>:: the base line of the last printed line

Returns any unprinted text



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
61
62
63
64
65
# File 'lib/prawn/core/text/wrap.rb', line 34

def wrap(text) #:nodoc:
  @text = nil
  remaining_text = text
  @line_height = @document.font.height
  @descender   = @document.font.descender
  @ascender    = @document.font.ascender
  @baseline_y  = -@ascender

  printed_lines = []

  while remaining_text &&
      remaining_text.length > 0 &&
      @baseline_y.abs + @descender <= @height
    line_to_print = @line_wrap.wrap_line(remaining_text.first_line,
                                         :document => @document,
                                         :kerning => @kerning,
                                         :width => available_width)

    remaining_text = remaining_text.slice(@line_wrap.consumed_char_count..
                                          remaining_text.length)
    include_ellipses = (@overflow == :ellipses && last_line? &&
                        remaining_text.length > 0)
    printed_lines << draw_line(line_to_print, @line_wrap.width,
                               word_spacing_for_this_line, include_ellipses)
    @baseline_y -= (@line_height + @leading)
    break if @single_line
  end

  @text = printed_lines.join("\n")

  remaining_text
end