Module: Prawn::Document::Text

Includes:
Wrapping
Included in:
Prawn::Document
Defined in:
lib/prawn/document/text.rb,
lib/prawn/document/text/box.rb,
lib/prawn/document/text/wrapping.rb

Defined Under Namespace

Modules: Wrapping Classes: Box

Instance Method Summary collapse

Methods included from Wrapping

#height_of, #naive_unwrap, #naive_wrap

Instance Method Details

#text(text, options = {}) ⇒ Object

Draws text on the page. If a point is specified via the :at option the text will begin exactly at that point, and the string is assumed to be pre-formatted to properly fit the page.

pdf.text "Hello World", :at => [100,100]
pdf.text "Goodbye World", :at => [50,50], :size => 16

When :at is not specified, Prawn attempts to wrap the text to fit within your current bounding box (or margin_box if no bounding box is being used ). Text will flow onto the next page when it reaches the bottom of the bounding box. Text wrap in Prawn does not re-flow linebreaks, so if you want fully automated text wrapping, be sure to remove newlines before attempting to draw your string.

 pdf.text "Will be wrapped when it hits the edge of your bounding box"
 pdf.text "This will be centered", :align => :center
 pdf.text "This will be right aligned", :align => :right     

Wrapping is done by splitting words by spaces by default.  If your text
does not contain spaces, you can wrap based on characters instead:

 pdf.text "This will be wrapped by character", :wrap => :character

If your font contains kerning pairs data that Prawn can parse, the text will be kerned by default. You can disable this feature by passing :kerning => false.

Text Positioning Details:

When using the :at parameter, Prawn will position your text by the left-most edge of its baseline, and flow along a single line. (This means that :align will not work)

Otherwise, the text is positioned at font.ascender below the baseline, making it easy to use this method within bounding boxes and spans.

Rotation

Text can be rotated before it is placed on the canvas by specifying the :rotate option with a given angle. Rotation occurs counter-clockwise.

Encoding

Note that strings passed to this function should be encoded as UTF-8. If you get unexpected characters appearing in your rendered document, check this.

If the current font is a built-in one, although the string must be encoded as UTF-8, only characters that are available in WinAnsi are allowed.

If an empty box is rendered to your PDF instead of the character you wanted it usually means the current font doesn't include that character.


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/prawn/document/text.rb', line 72

def text(text,options={})            
  # we'll be messing with the strings encoding, don't change the users
  # original string
  text = text.to_s.dup                      
  
  save_font do
    options = @text_options.merge(options)
    process_text_options(options) 
     
    font.normalize_encoding!(text) unless @skip_encoding        

    if options[:at]                

      if options[:align]
        raise ArgumentError, "The :align option does not work with :at" 
      end

      x,y = translate(options[:at])            
      font_size(options[:size]) { add_text_content(text,x,y,options) }
    else
      if options[:rotate]
        raise ArgumentError, "Rotated text may only be used with :at" 
      end
      wrapped_text(text,options)
    end         
  end
end