class Prawn::Emoji::Drawer

Attributes

document[R]
emoji_index[R]

Public Class Methods

drawable?(text) click to toggle source
# File lib/prawn/emoji/drawer.rb, line 10
def self.drawable?(text)
  text.encoding == ::Encoding::UTF_8 && Emoji.regex.match?(text)
end
new(document) click to toggle source
# File lib/prawn/emoji/drawer.rb, line 14
def initialize(document)
  @document = document
  @emoji_index = Emoji::Index.new
end

Public Instance Methods

draw(text, text_options) click to toggle source
# 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

Private Instance Methods

draw_emoji(emoji_char, at:) click to toggle source
# File lib/prawn/emoji/drawer.rb, line 55
def draw_emoji(emoji_char, at:)
  emoji_image = Emoji::Image.new(emoji_char)
  emoji_image.render(document, at: at)

  emoji_image.width + document.character_spacing
end
draw_text(text, at:, text_options:) click to toggle source
# File lib/prawn/emoji/drawer.rb, line 46
def draw_text(text, at:, text_options:)
  draw_text!(text, at: at, text_options: text_options)

  width = document.width_of(text, text_options)
  # In prawn v2.3.0, the character spacing at the end of the text is not included in the calculated text width.
  # https://github.com/prawnpdf/prawn/pull/1117
  width > 0 ? width + document.character_spacing : width
end
draw_text!(text, at:, text_options:) click to toggle source
# File lib/prawn/emoji/drawer.rb, line 42
def draw_text!(text, at:, text_options:)
  document.draw_text!(text, text_options.merge(emoji: false, at: at))
end