Method: HexaPDF::Content::Canvas#text

Defined in:
lib/hexapdf/content/canvas.rb

#text(text, at: nil) ⇒ Object

:call-seq:

canvas.text(text)                  -> canvas
canvas.text(text, at: [x, y])      -> canvas

Shows the given text string, either at the current or the provided position. Returns self.

If no position is provided, the text is positioned at the current position of the text cursor (see #text_cursor).

The text string may contain any valid Unicode newline separator and if so, multiple lines are shown, using #leading for offsetting the lines. If no leading has been set, a leading equal to the font size will be set..

Note that there are no provisions to make sure that all text is visible! So if the text string is too long, it may be outside the cropped page and be cut off.

Examples:

#>pdf
canvas.font('Times', size: 12)
# Sets leading=12 because mulitple lines are drawn
canvas.text("This is a \n    multiline text", at: [15, 150])
# Starts right after the last text
canvas.text(". Some more text\nafter the newline.")

See: #leading, #font, #font_size, #show_glyphs, www.unicode.org/reports/tr18/#Line_Boundaries



2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
# File 'lib/hexapdf/content/canvas.rb', line 2336

def text(text, at: nil)
  raise_unless_font_set
  move_text_cursor(offset: at) if at
  lines = text.split(/\u{D A}|(?!\u{D A})[\u{A}-\u{D}\u{85}\u{2028}\u{2029}]/, -1)
  leading(font_size) if leading == 0 && lines.length > 1
  lines.each_with_index do |str, index|
    show_glyphs(@font.decode_utf8(str))
    move_text_cursor unless index == lines.length - 1
  end
  self
end