Class: Prawn::Emoji::Drawer

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/emoji/drawer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Drawer

Returns a new instance of Drawer.



14
15
16
17
# File 'lib/prawn/emoji/drawer.rb', line 14

def initialize(document)
  @document = document
  @emoji_index = Emoji::Index.new
end

Class Method Details

.drawable?(text) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/prawn/emoji/drawer.rb', line 10

def self.drawable?(text)
  text.encoding == ::Encoding::UTF_8 && Emoji.regex.match?(text)
end

Instance Method Details

#draw(text, text_options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/prawn/emoji/drawer.rb', line 19

def draw(text, text_options)
  cursor_x, cursor_y = text_options[:at]

  emoji_text = Emoji::Text.new(text, document.font_size)

  while emoji_text.contains_emoji? do
    if emoji_index.include?(emoji_text.emoji_char.codepoint)
      cursor_x += draw_text(emoji_text.left, at: [cursor_x, cursor_y], text_options: text_options)
      cursor_x += draw_emoji(emoji_text.emoji_char, at: [cursor_x, cursor_y])
    else
      cursor_x += draw_text(emoji_text.left_with_emoji, at: [cursor_x, cursor_y], text_options: text_options)
    end
    emoji_text = Emoji::Text.new(emoji_text.remaining, document.font_size)
  end

  draw_text!(emoji_text.to_s, at: [cursor_x, cursor_y], text_options: text_options)
end