Module: Unicode::Emoji

Defined in:
lib/unicode/emoji.rb,
lib/unicode/emoji/index.rb,
lib/unicode/emoji/constants.rb

Constant Summary collapse

PROPERTY_NAMES =
{
  B: "Emoji_Modifier_Base",
  M: "Emoji_Modifier",
  C: "Emoji_Component",
  P: "Emoji_Presentation",
}
EMOJI_VARIATION_SELECTOR =
0xFE0F
TEXT_VARIATION_SELECTOR =
0xFE0E
EMOJI_TAG_BASE_FLAG =
0x1F3F4
CANCEL_TAG =
0xE007F
EMOJI_KEYCAP_SUFFIX =
0x20E3
ZWJ =
0x200D
EMOJI_CHAR =
INDEX[:PROPERTIES].keys.freeze
EMOJI_PRESENTATION =
INDEX[:PROPERTIES].select{ |ord, props| props.include?(:P) }.keys.freeze
TEXT_PRESENTATION =
INDEX[:PROPERTIES].select{ |ord, props| !props.include?(:P) }.keys.freeze
EMOJI_COMPONENT =
INDEX[:PROPERTIES].select{ |ord, props| props.include?(:C) }.keys.freeze
EMOJI_MODIFIER_BASES =
INDEX[:PROPERTIES].select{ |ord, props| props.include?(:B) }.keys.freeze
EMOJI_MODIFIERS =
INDEX[:PROPERTIES].select{ |ord, props| props.include?(:M) }.keys.freeze
EMOJI_KEYCAPS =
INDEX[:KEYCAPS].freeze
VALID_REGION_FLAGS =
INDEX[:FLAGS].freeze
VALID_SUBDIVISIONS =
INDEX[:SD].freeze
INDEX[:TAGS].freeze
INDEX[:ZWJ].freeze
LIST =
INDEX[:LIST].freeze.each_value(&:freeze)
REGEX =

Matches basic singleton emoji and all kind of sequences, but restrict zwj and tag sequences to known sequences

Regexp.compile(
  pack_and_join[RECOMMENDED_ZWJ_SEQUENCES] +
  ?| + pack_and_join[RECOMMENDED_SUBDIVISION_FLAGS] +
  ?| + emoji_modifier_sequence +
  ?| + "(?!" + emoji_component + ")" + emoji_presentation_sequence +
  ?| + emoji_keycap_sequence +
  ?| + emoji_valid_region_sequence  +
  ""
)
REGEX_VALID =

Matches basic singleton emoji and all kind of valid sequences

Regexp.compile(
  # EMOJI_TAGS.map{ |base, spec| "(?:" + pack[base] + "[" + pack[spec] + "]+" + pack[CANCEL_TAG] + ")" }.join("|") +
  emoji_valid_tag_sequence +
  ?| + "(?:" + "(?:" + emoji_zwj_element + pack[ZWJ] + "){1,3}" + emoji_zwj_element + ")" +
  ?| + emoji_modifier_sequence +
  ?| + "(?!" + emoji_component + ")" + emoji_presentation_sequence +
  ?| + emoji_keycap_sequence +
  ?| + emoji_valid_region_sequence +
  ""
)
REGEX_BASIC =

Matches only basic single, non-textual emoji Ignores “components” like modifiers or simple digits

Regexp.compile(
  "(?!" + emoji_component + ")" + emoji_presentation_sequence
)
REGEX_TEXT =

Matches only basic single, textual emoji Ignores “components” like modifiers or simple digits

Regexp.compile(
  "(?!" + emoji_component + ")" + text_presentation_sequence
)
REGEX_ANY =

Matches any emoji-related codepoint

Regexp.compile(
  emoji_character
)
INDEX =
Marshal.load(Gem.gunzip(File.binread(INDEX_FILENAME)))
VERSION =
"1.0.1".freeze
EMOJI_VERSION =
"5.0".freeze
DATA_DIRECTORY =
File.expand_path(File.dirname(__FILE__) + '/../../../data/').freeze
INDEX_FILENAME =
(DATA_DIRECTORY + '/emoji.marshal.gz').freeze
ENABLE_NATIVE_EMOJI_UNICODE_PROPERTIES =
false

Class Method Summary collapse

Class Method Details

.get_codepoint_value(char) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/unicode/emoji.rb', line 145

def self.get_codepoint_value(char)
  ord = nil

  if char.valid_encoding?
    ord = char.ord
  elsif char.encoding.name == "UTF-8"
    begin
      ord = char.unpack("U*")[0]
    rescue ArgumentError
    end
  end

  if ord
    ord
  else
    raise(ArgumentError, "Unicode::Emoji must be given a valid string")
  end
end

.list(key = nil, sub_key = nil) ⇒ Object



140
141
142
143
# File 'lib/unicode/emoji.rb', line 140

def self.list(key = nil, sub_key = nil)
  return LIST unless key || sub_key
  LIST.dig(*[key, sub_key].compact)
end

.properties(char) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/unicode/emoji.rb', line 129

def self.properties(char)
  ord = get_codepoint_value(char)
  props = INDEX[:PROPERTIES][ord]

  if props
    ["Emoji"] + props.map{ |prop| PROPERTY_NAMES[prop] }
  else
    # nothing
  end
end