class TanukiEmoji::Character

Character represents an Emoji character or sequence which can be formed by one or more Unicode code points respectively which combined form a unique pictographic representation (known as Emoji)

@see www.unicode.org/reports/tr51/

Constants

ALPHA_CODE_REGEXP
EMOJI_VARIATION_SELECTOR

This denotes a “color” or “emoji” version

FLAG_REGEXP
IMAGE_EXTENSION
IMAGE_PREFIX
PLAIN_VARIATION_SELECTOR

This denotes a “plain” (black/white) or “textual” version

ZWJ_TAG

Zero Width Joiner is used in sequences to indicate they should all be evaluated and displayed as a single thing

Attributes

aliases[R]
alpha_code[R]
category[R]
codepoints[R]
codepoints_alternates[R]
description[R]
name[R]
unicode_version[RW]

Public Class Methods

format_alpha_code(alpha_code) click to toggle source

Ensure alpha code is formatted with colons

@param [String] alpha_code @return [String] formatted alpha code

# File lib/tanuki_emoji/character.rb, line 104
def self.format_alpha_code(alpha_code)
  alpha_code.to_s.match?(ALPHA_CODE_REGEXP) ? alpha_code.to_s : ":#{alpha_code}:"
end
format_name(raw_name) click to toggle source
# File lib/tanuki_emoji/character.rb, line 108
def self.format_name(raw_name)
  matched = raw_name.match(ALPHA_CODE_REGEXP)

  matched ? matched['alpha_text'] : raw_name
end
new(name, codepoints:, alpha_code:, description:, category:) click to toggle source

@param [String] name @param [String] codepoints @param [String] alpha_code @param [String] description @param [String] category

# File lib/tanuki_emoji/character.rb, line 32
def initialize(name, codepoints:, alpha_code:, description:, category:)
  @name = self.class.format_name(name)
  @codepoints = codepoints
  @codepoints_alternates = []
  @alpha_code = self.class.format_alpha_code(alpha_code)
  @aliases = []
  @description = description
  @category = category
end

Public Instance Methods

==(other) click to toggle source
# File lib/tanuki_emoji/character.rb, line 91
def ==(other)
  name == other.name &&
    codepoints == other.codepoints &&
    codepoints_alternates == other.codepoints_alternates &&
    alpha_code == other.alpha_code &&
    aliases == other.aliases &&
    description == other.description
end
add_alias(alpha_code) click to toggle source

Add alternative alpha_codes to this character

@param [String] alpha_code

# File lib/tanuki_emoji/character.rb, line 52
def add_alias(alpha_code)
  aliases << self.class.format_alpha_code(alpha_code)
end
add_codepoints(codepoints) click to toggle source

Add alternative codepoints to this character

@param [String] codepoints

# File lib/tanuki_emoji/character.rb, line 45
def add_codepoints(codepoints)
  codepoints_alternates << codepoints
end
flag?() click to toggle source

Return whether current character represents a flag or not

@return [Boolean] whether character represents a flag or not

# File lib/tanuki_emoji/character.rb, line 79
def flag?
  codepoints.match?(FLAG_REGEXP)
end
hex() click to toggle source

Return a Hex formatted version of the Unicode code points

@return [String] Hex formatted version of the unicode

# File lib/tanuki_emoji/character.rb, line 59
def hex
  unicode_to_hex(codepoints).join('-')
end
image_name() click to toggle source

Generate the image name to be used as fallback for this character

@return [String] image name with extension

# File lib/tanuki_emoji/character.rb, line 66
def image_name
  # Noto doesn't ship flags as part of regular hex-named files
  # Flags are stored in a separate third-party folder and follow ISO-3166-1 codes
  # @see http://en.wikipedia.org/wiki/ISO_3166-1
  return alpha_code.tr(':', '').sub('flag_', '').upcase + IMAGE_EXTENSION if flag?

  # Noto omits Emoji Variation Selector on their resources file names
  IMAGE_PREFIX + unicode_to_hex(codepoints).reject { |i| i == EMOJI_VARIATION_SELECTOR.to_s(16) }.join('_') + IMAGE_EXTENSION
end
inspect() click to toggle source
# File lib/tanuki_emoji/character.rb, line 87
def inspect
  "#<#{self.class.name}:#{name} #{codepoints}(#{hex})>"
end
to_s() click to toggle source
# File lib/tanuki_emoji/character.rb, line 83
def to_s
  codepoints
end

Private Instance Methods

unicode_to_hex(value) click to toggle source

Return each codepoint converted to its hex value as string

@param [String] value @return [Array<String>] hex value as string

# File lib/tanuki_emoji/character.rb, line 120
def unicode_to_hex(value)
  value.unpack('U*').map { |i| i.to_s(16).rjust(4, '0') }
end