File: emoji.rb

package info (click to toggle)
ruby-unicode-emoji 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: ruby: 1,344; makefile: 4
file content (111 lines) | stat: -rw-r--r-- 2,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require_relative "emoji/constants"

module Unicode
  module Emoji
    autoload :INDEX, File.expand_path('emoji/index', __dir__)

    %w[
      EMOJI_CHAR
      EMOJI_PRESENTATION
      TEXT_PRESENTATION
      EMOJI_COMPONENT
      EMOJI_MODIFIER_BASES
      EMOJI_MODIFIERS
      EXTENDED_PICTOGRAPHIC
      EXTENDED_PICTOGRAPHIC_NO_EMOJI
      EMOJI_KEYCAPS
      VALID_REGION_FLAGS
      VALID_SUBDIVISIONS
      RECOMMENDED_SUBDIVISION_FLAGS
      RECOMMENDED_ZWJ_SEQUENCES
    ].each do |const_name|
      autoload const_name, File.expand_path('emoji/lazy_constants', __dir__)
    end

    %w[
      LIST
      LIST_REMOVED_KEYS
    ].each do |const_name|
      autoload const_name, File.expand_path('emoji/list', __dir__)
    end

    generated_constants_dirpath = File.expand_path(
      EMOJI_VERSION == RbConfig::CONFIG["UNICODE_EMOJI_VERSION"] ? "emoji/generated_native/" : "emoji/generated/",
      __dir__
    )

    %w[
      REGEX
      REGEX_INCLUDE_TEXT
      REGEX_INCLUDE_MQE
      REGEX_INCLUDE_MQE_UQE
      REGEX_VALID
      REGEX_VALID_INCLUDE_TEXT
      REGEX_WELL_FORMED
      REGEX_WELL_FORMED_INCLUDE_TEXT
      REGEX_POSSIBLE
      REGEX_BASIC
      REGEX_TEXT
      REGEX_TEXT_PRESENTATION
      REGEX_PROP_EMOJI
      REGEX_PROP_MODIFIER
      REGEX_PROP_MODIFIER_BASE
      REGEX_PROP_COMPONENT
      REGEX_PROP_PRESENTATION
      REGEX_PICTO
      REGEX_PICTO_NO_EMOJI
      REGEX_EMOJI_KEYCAP
    ].each do |const_name|
      autoload const_name, File.join(generated_constants_dirpath, const_name.downcase)
    end

    # Return Emoji properties of character as an Array or nil
    # See PROPERTY_NAMES constant for possible properties
    # 
    # Source: see https://www.unicode.org/Public/16.0.0/ucd/emoji/emoji-data.txt
    def self.properties(char)
      ord = get_codepoint_value(char)
      props = INDEX[:PROPERTIES][ord]

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

    # Returns ordered list of Emoji, categorized in a three-level deep Hash structure
    def self.list(key = nil, sub_key = nil)
      return LIST unless key || sub_key
      if LIST_REMOVED_KEYS.include?(key)
        $stderr.puts "Warning(unicode-emoji): The category of #{key} does not exist anymore"
      end
      LIST.dig(*[key, sub_key].compact)
    end

    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

    class << self
      private :get_codepoint_value
    end
  end
end