Class: EmojiData::EmojiChar

Inherits:
Object
  • Object
show all
Defined in:
lib/emoji_data/emoji_char.rb

Overview

EmojiChar represents a single Emoji character and its associated metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(emoji_hash) ⇒ EmojiChar

Returns a new instance of EmojiChar.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/emoji_data/emoji_char.rb', line 32

def initialize(emoji_hash)
  # work around inconsistency in emoji.json for now by just setting a blank
  # array for instance value, and let it get overriden in main
  # deserialization loop if variable is present.
  @variations = []

  # trick for declaring instance variables while iterating over a hash
  # http://stackoverflow.com/questions/1615190/
  emoji_hash.each do |k,v|
    instance_variable_set("@#{k}",v)
    eigenclass = class<<self; self; end
    eigenclass.class_eval { attr_reader k }
  end
end

Instance Attribute Details

#nameString

Returns The standardized name used in the Unicode specification to represent this emoji character.

Returns:

  • (String)

    The standardized name used in the Unicode specification to represent this emoji character.



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
112
113
114
115
116
117
118
119
120
# File 'lib/emoji_data/emoji_char.rb', line 30

class EmojiChar

  def initialize(emoji_hash)
    # work around inconsistency in emoji.json for now by just setting a blank
    # array for instance value, and let it get overriden in main
    # deserialization loop if variable is present.
    @variations = []

    # trick for declaring instance variables while iterating over a hash
    # http://stackoverflow.com/questions/1615190/
    emoji_hash.each do |k,v|
      instance_variable_set("@#{k}",v)
      eigenclass = class<<self; self; end
      eigenclass.class_eval { attr_reader k }
    end
  end

  # Renders an `EmojiChar` to its string glyph representation, suitable for
  # printing to screen.
  #
  # @option opts [Boolean] :variant_encoding specify whether the variant
  #   encoding selector should be used to hint to rendering devices that
  #   "graphic" representation should be used. By default, we use this for all
  #   Emoji characters that contain a possible variant.
  #
  # @return [String] the emoji character rendered to a UTF-8 string
  def render(opts = {})
    options = {variant_encoding: true}.merge(opts)
    #decide whether to use the normal unified ID or the variant for encoding to str
    target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
    EmojiChar::unified_to_char(target)
  end

  alias_method :to_s, :render
  alias_method :char, :render

  # Returns a list of all possible UTF-8 string renderings of an `EmojiChar`.
  #
  # E.g., normal, with variant selectors, etc. This is useful if you want to
  # have all possible values to match against when searching for the emoji in
  # a string representation.
  #
  # @return [Array<String>] all possible UTF-8 string renderings
  def chars
    results = [self.render({variant_encoding: false})]
    @variations.each do |variation|
      results << EmojiChar::unified_to_char(variation)
    end
    @chars ||= results
  end

  # Is the `EmojiChar` represented by a doublebyte codepoint in Unicode?
  #
  # @return [Boolean]
  def doublebyte?
    @unified.include? "-"
  end

  # Does the `EmojiChar` have an alternate Unicode variant encoding?
  #
  # @return [Boolean]
  def variant?
    @variations.length > 0
  end

  # Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
  #
  # For now we only know of one possible variant encoding for certain
  # characters, but there could be others in the future.
  #
  # This is typically used to force Emoji rendering for characters that could
  # be represented in standard font glyphs on certain operating systems.
  #
  # The resulting encoded string will be two codepoints, or three codepoints
  # for doublebyte Emoji characters.
  #
  # @return [String, nil]
  #   The most likely variant-encoding codepoint ID.
  #   If there is no variant-encoding for a character, returns nil.
  def variant
    @variations.first
  end


  protected

  def self.unified_to_char(cps)
    cps.split('-').map { |i| i.hex }.pack("U*")
  end

end

#short_nameString

Returns The canonical "short name" or keyword used in many systems to refer to this emoji. Often surrounded by :colons: in systems like GitHub & Campfire.

Returns:

  • (String)

    The canonical "short name" or keyword used in many systems to refer to this emoji. Often surrounded by :colons: in systems like GitHub & Campfire.



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
112
113
114
115
116
117
118
119
120
# File 'lib/emoji_data/emoji_char.rb', line 30

class EmojiChar

  def initialize(emoji_hash)
    # work around inconsistency in emoji.json for now by just setting a blank
    # array for instance value, and let it get overriden in main
    # deserialization loop if variable is present.
    @variations = []

    # trick for declaring instance variables while iterating over a hash
    # http://stackoverflow.com/questions/1615190/
    emoji_hash.each do |k,v|
      instance_variable_set("@#{k}",v)
      eigenclass = class<<self; self; end
      eigenclass.class_eval { attr_reader k }
    end
  end

  # Renders an `EmojiChar` to its string glyph representation, suitable for
  # printing to screen.
  #
  # @option opts [Boolean] :variant_encoding specify whether the variant
  #   encoding selector should be used to hint to rendering devices that
  #   "graphic" representation should be used. By default, we use this for all
  #   Emoji characters that contain a possible variant.
  #
  # @return [String] the emoji character rendered to a UTF-8 string
  def render(opts = {})
    options = {variant_encoding: true}.merge(opts)
    #decide whether to use the normal unified ID or the variant for encoding to str
    target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
    EmojiChar::unified_to_char(target)
  end

  alias_method :to_s, :render
  alias_method :char, :render

  # Returns a list of all possible UTF-8 string renderings of an `EmojiChar`.
  #
  # E.g., normal, with variant selectors, etc. This is useful if you want to
  # have all possible values to match against when searching for the emoji in
  # a string representation.
  #
  # @return [Array<String>] all possible UTF-8 string renderings
  def chars
    results = [self.render({variant_encoding: false})]
    @variations.each do |variation|
      results << EmojiChar::unified_to_char(variation)
    end
    @chars ||= results
  end

  # Is the `EmojiChar` represented by a doublebyte codepoint in Unicode?
  #
  # @return [Boolean]
  def doublebyte?
    @unified.include? "-"
  end

  # Does the `EmojiChar` have an alternate Unicode variant encoding?
  #
  # @return [Boolean]
  def variant?
    @variations.length > 0
  end

  # Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
  #
  # For now we only know of one possible variant encoding for certain
  # characters, but there could be others in the future.
  #
  # This is typically used to force Emoji rendering for characters that could
  # be represented in standard font glyphs on certain operating systems.
  #
  # The resulting encoded string will be two codepoints, or three codepoints
  # for doublebyte Emoji characters.
  #
  # @return [String, nil]
  #   The most likely variant-encoding codepoint ID.
  #   If there is no variant-encoding for a character, returns nil.
  def variant
    @variations.first
  end


  protected

  def self.unified_to_char(cps)
    cps.split('-').map { |i| i.hex }.pack("U*")
  end

end

#short_namesArray<String>

Returns A full list of possible keywords for the emoji.

Returns:

  • (Array<String>)

    A full list of possible keywords for the emoji.



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
112
113
114
115
116
117
118
119
120
# File 'lib/emoji_data/emoji_char.rb', line 30

class EmojiChar

  def initialize(emoji_hash)
    # work around inconsistency in emoji.json for now by just setting a blank
    # array for instance value, and let it get overriden in main
    # deserialization loop if variable is present.
    @variations = []

    # trick for declaring instance variables while iterating over a hash
    # http://stackoverflow.com/questions/1615190/
    emoji_hash.each do |k,v|
      instance_variable_set("@#{k}",v)
      eigenclass = class<<self; self; end
      eigenclass.class_eval { attr_reader k }
    end
  end

  # Renders an `EmojiChar` to its string glyph representation, suitable for
  # printing to screen.
  #
  # @option opts [Boolean] :variant_encoding specify whether the variant
  #   encoding selector should be used to hint to rendering devices that
  #   "graphic" representation should be used. By default, we use this for all
  #   Emoji characters that contain a possible variant.
  #
  # @return [String] the emoji character rendered to a UTF-8 string
  def render(opts = {})
    options = {variant_encoding: true}.merge(opts)
    #decide whether to use the normal unified ID or the variant for encoding to str
    target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
    EmojiChar::unified_to_char(target)
  end

  alias_method :to_s, :render
  alias_method :char, :render

  # Returns a list of all possible UTF-8 string renderings of an `EmojiChar`.
  #
  # E.g., normal, with variant selectors, etc. This is useful if you want to
  # have all possible values to match against when searching for the emoji in
  # a string representation.
  #
  # @return [Array<String>] all possible UTF-8 string renderings
  def chars
    results = [self.render({variant_encoding: false})]
    @variations.each do |variation|
      results << EmojiChar::unified_to_char(variation)
    end
    @chars ||= results
  end

  # Is the `EmojiChar` represented by a doublebyte codepoint in Unicode?
  #
  # @return [Boolean]
  def doublebyte?
    @unified.include? "-"
  end

  # Does the `EmojiChar` have an alternate Unicode variant encoding?
  #
  # @return [Boolean]
  def variant?
    @variations.length > 0
  end

  # Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
  #
  # For now we only know of one possible variant encoding for certain
  # characters, but there could be others in the future.
  #
  # This is typically used to force Emoji rendering for characters that could
  # be represented in standard font glyphs on certain operating systems.
  #
  # The resulting encoded string will be two codepoints, or three codepoints
  # for doublebyte Emoji characters.
  #
  # @return [String, nil]
  #   The most likely variant-encoding codepoint ID.
  #   If there is no variant-encoding for a character, returns nil.
  def variant
    @variations.first
  end


  protected

  def self.unified_to_char(cps)
    cps.split('-').map { |i| i.hex }.pack("U*")
  end

end

#textString

example a smiley face emoji may be represented with an ASCII alternative. Most emoji do not have a text alternative. This is typically used when building an automatic translation from typed emoticons.

Returns:

  • (String)

    An alternate textual representation of the emoji, for



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
112
113
114
115
116
117
118
119
120
# File 'lib/emoji_data/emoji_char.rb', line 30

class EmojiChar

  def initialize(emoji_hash)
    # work around inconsistency in emoji.json for now by just setting a blank
    # array for instance value, and let it get overriden in main
    # deserialization loop if variable is present.
    @variations = []

    # trick for declaring instance variables while iterating over a hash
    # http://stackoverflow.com/questions/1615190/
    emoji_hash.each do |k,v|
      instance_variable_set("@#{k}",v)
      eigenclass = class<<self; self; end
      eigenclass.class_eval { attr_reader k }
    end
  end

  # Renders an `EmojiChar` to its string glyph representation, suitable for
  # printing to screen.
  #
  # @option opts [Boolean] :variant_encoding specify whether the variant
  #   encoding selector should be used to hint to rendering devices that
  #   "graphic" representation should be used. By default, we use this for all
  #   Emoji characters that contain a possible variant.
  #
  # @return [String] the emoji character rendered to a UTF-8 string
  def render(opts = {})
    options = {variant_encoding: true}.merge(opts)
    #decide whether to use the normal unified ID or the variant for encoding to str
    target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
    EmojiChar::unified_to_char(target)
  end

  alias_method :to_s, :render
  alias_method :char, :render

  # Returns a list of all possible UTF-8 string renderings of an `EmojiChar`.
  #
  # E.g., normal, with variant selectors, etc. This is useful if you want to
  # have all possible values to match against when searching for the emoji in
  # a string representation.
  #
  # @return [Array<String>] all possible UTF-8 string renderings
  def chars
    results = [self.render({variant_encoding: false})]
    @variations.each do |variation|
      results << EmojiChar::unified_to_char(variation)
    end
    @chars ||= results
  end

  # Is the `EmojiChar` represented by a doublebyte codepoint in Unicode?
  #
  # @return [Boolean]
  def doublebyte?
    @unified.include? "-"
  end

  # Does the `EmojiChar` have an alternate Unicode variant encoding?
  #
  # @return [Boolean]
  def variant?
    @variations.length > 0
  end

  # Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
  #
  # For now we only know of one possible variant encoding for certain
  # characters, but there could be others in the future.
  #
  # This is typically used to force Emoji rendering for characters that could
  # be represented in standard font glyphs on certain operating systems.
  #
  # The resulting encoded string will be two codepoints, or three codepoints
  # for doublebyte Emoji characters.
  #
  # @return [String, nil]
  #   The most likely variant-encoding codepoint ID.
  #   If there is no variant-encoding for a character, returns nil.
  def variant
    @variations.first
  end


  protected

  def self.unified_to_char(cps)
    cps.split('-').map { |i| i.hex }.pack("U*")
  end

end

#unifiedString

Returns The primary unified codepoint ID for the emoji character.

Returns:

  • (String)

    The primary unified codepoint ID for the emoji character.



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
112
113
114
115
116
117
118
119
120
# File 'lib/emoji_data/emoji_char.rb', line 30

class EmojiChar

  def initialize(emoji_hash)
    # work around inconsistency in emoji.json for now by just setting a blank
    # array for instance value, and let it get overriden in main
    # deserialization loop if variable is present.
    @variations = []

    # trick for declaring instance variables while iterating over a hash
    # http://stackoverflow.com/questions/1615190/
    emoji_hash.each do |k,v|
      instance_variable_set("@#{k}",v)
      eigenclass = class<<self; self; end
      eigenclass.class_eval { attr_reader k }
    end
  end

  # Renders an `EmojiChar` to its string glyph representation, suitable for
  # printing to screen.
  #
  # @option opts [Boolean] :variant_encoding specify whether the variant
  #   encoding selector should be used to hint to rendering devices that
  #   "graphic" representation should be used. By default, we use this for all
  #   Emoji characters that contain a possible variant.
  #
  # @return [String] the emoji character rendered to a UTF-8 string
  def render(opts = {})
    options = {variant_encoding: true}.merge(opts)
    #decide whether to use the normal unified ID or the variant for encoding to str
    target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
    EmojiChar::unified_to_char(target)
  end

  alias_method :to_s, :render
  alias_method :char, :render

  # Returns a list of all possible UTF-8 string renderings of an `EmojiChar`.
  #
  # E.g., normal, with variant selectors, etc. This is useful if you want to
  # have all possible values to match against when searching for the emoji in
  # a string representation.
  #
  # @return [Array<String>] all possible UTF-8 string renderings
  def chars
    results = [self.render({variant_encoding: false})]
    @variations.each do |variation|
      results << EmojiChar::unified_to_char(variation)
    end
    @chars ||= results
  end

  # Is the `EmojiChar` represented by a doublebyte codepoint in Unicode?
  #
  # @return [Boolean]
  def doublebyte?
    @unified.include? "-"
  end

  # Does the `EmojiChar` have an alternate Unicode variant encoding?
  #
  # @return [Boolean]
  def variant?
    @variations.length > 0
  end

  # Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
  #
  # For now we only know of one possible variant encoding for certain
  # characters, but there could be others in the future.
  #
  # This is typically used to force Emoji rendering for characters that could
  # be represented in standard font glyphs on certain operating systems.
  #
  # The resulting encoded string will be two codepoints, or three codepoints
  # for doublebyte Emoji characters.
  #
  # @return [String, nil]
  #   The most likely variant-encoding codepoint ID.
  #   If there is no variant-encoding for a character, returns nil.
  def variant
    @variations.first
  end


  protected

  def self.unified_to_char(cps)
    cps.split('-').map { |i| i.hex }.pack("U*")
  end

end

#variationsArray<String>

Returns A list of all variant codepoints that may also represent this emoji.

Returns:

  • (Array<String>)

    A list of all variant codepoints that may also represent this emoji.



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
112
113
114
115
116
117
118
119
120
# File 'lib/emoji_data/emoji_char.rb', line 30

class EmojiChar

  def initialize(emoji_hash)
    # work around inconsistency in emoji.json for now by just setting a blank
    # array for instance value, and let it get overriden in main
    # deserialization loop if variable is present.
    @variations = []

    # trick for declaring instance variables while iterating over a hash
    # http://stackoverflow.com/questions/1615190/
    emoji_hash.each do |k,v|
      instance_variable_set("@#{k}",v)
      eigenclass = class<<self; self; end
      eigenclass.class_eval { attr_reader k }
    end
  end

  # Renders an `EmojiChar` to its string glyph representation, suitable for
  # printing to screen.
  #
  # @option opts [Boolean] :variant_encoding specify whether the variant
  #   encoding selector should be used to hint to rendering devices that
  #   "graphic" representation should be used. By default, we use this for all
  #   Emoji characters that contain a possible variant.
  #
  # @return [String] the emoji character rendered to a UTF-8 string
  def render(opts = {})
    options = {variant_encoding: true}.merge(opts)
    #decide whether to use the normal unified ID or the variant for encoding to str
    target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
    EmojiChar::unified_to_char(target)
  end

  alias_method :to_s, :render
  alias_method :char, :render

  # Returns a list of all possible UTF-8 string renderings of an `EmojiChar`.
  #
  # E.g., normal, with variant selectors, etc. This is useful if you want to
  # have all possible values to match against when searching for the emoji in
  # a string representation.
  #
  # @return [Array<String>] all possible UTF-8 string renderings
  def chars
    results = [self.render({variant_encoding: false})]
    @variations.each do |variation|
      results << EmojiChar::unified_to_char(variation)
    end
    @chars ||= results
  end

  # Is the `EmojiChar` represented by a doublebyte codepoint in Unicode?
  #
  # @return [Boolean]
  def doublebyte?
    @unified.include? "-"
  end

  # Does the `EmojiChar` have an alternate Unicode variant encoding?
  #
  # @return [Boolean]
  def variant?
    @variations.length > 0
  end

  # Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
  #
  # For now we only know of one possible variant encoding for certain
  # characters, but there could be others in the future.
  #
  # This is typically used to force Emoji rendering for characters that could
  # be represented in standard font glyphs on certain operating systems.
  #
  # The resulting encoded string will be two codepoints, or three codepoints
  # for doublebyte Emoji characters.
  #
  # @return [String, nil]
  #   The most likely variant-encoding codepoint ID.
  #   If there is no variant-encoding for a character, returns nil.
  def variant
    @variations.first
  end


  protected

  def self.unified_to_char(cps)
    cps.split('-').map { |i| i.hex }.pack("U*")
  end

end

Instance Method Details

#charsArray<String>

Returns a list of all possible UTF-8 string renderings of an EmojiChar.

E.g., normal, with variant selectors, etc. This is useful if you want to have all possible values to match against when searching for the emoji in a string representation.

Returns:

  • (Array<String>)

    all possible UTF-8 string renderings



73
74
75
76
77
78
79
# File 'lib/emoji_data/emoji_char.rb', line 73

def chars
  results = [self.render({variant_encoding: false})]
  @variations.each do |variation|
    results << EmojiChar::unified_to_char(variation)
  end
  @chars ||= results
end

#doublebyte?Boolean

Is the EmojiChar represented by a doublebyte codepoint in Unicode?

Returns:

  • (Boolean)


84
85
86
# File 'lib/emoji_data/emoji_char.rb', line 84

def doublebyte?
  @unified.include? "-"
end

#render(opts = {}) ⇒ String Also known as: to_s, char

Renders an EmojiChar to its string glyph representation, suitable for printing to screen.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :variant_encoding (Boolean)

    specify whether the variant encoding selector should be used to hint to rendering devices that "graphic" representation should be used. By default, we use this for all Emoji characters that contain a possible variant.

Returns:

  • (String)

    the emoji character rendered to a UTF-8 string



56
57
58
59
60
61
# File 'lib/emoji_data/emoji_char.rb', line 56

def render(opts = {})
  options = {variant_encoding: true}.merge(opts)
  #decide whether to use the normal unified ID or the variant for encoding to str
  target = (self.variant? && options[:variant_encoding]) ? self.variant : @unified
  EmojiChar::unified_to_char(target)
end

#variantString?

Returns the most likely variant-encoding codepoint ID for an EmojiChar.

For now we only know of one possible variant encoding for certain characters, but there could be others in the future.

This is typically used to force Emoji rendering for characters that could be represented in standard font glyphs on certain operating systems.

The resulting encoded string will be two codepoints, or three codepoints for doublebyte Emoji characters.

Returns:

  • (String, nil)

    The most likely variant-encoding codepoint ID. If there is no variant-encoding for a character, returns nil.



109
110
111
# File 'lib/emoji_data/emoji_char.rb', line 109

def variant
  @variations.first
end

#variant?Boolean

Does the EmojiChar have an alternate Unicode variant encoding?

Returns:

  • (Boolean)


91
92
93
# File 'lib/emoji_data/emoji_char.rb', line 91

def variant?
  @variations.length > 0
end