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


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


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


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>


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.


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


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>


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.


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?


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.

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.


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.


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?


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

def variant?
  @variations.length > 0
end