Class: TanukiEmoji::Character
- Inherits:
-
Object
- Object
- TanukiEmoji::Character
- Defined in:
- lib/tanuki_emoji/character.rb
Overview
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)
Constant Summary collapse
- IMAGE_PREFIX =
'emoji_u'
- IMAGE_EXTENSION =
'.png'
- FLAG_REGEXP =
/[🇦-🇿]{2}|(\u{1F3F4}.+\u{E007F})/u
- ALPHA_CODE_REGEXP =
/:(?<alpha_text>[_+\-a-z0-9]+):/
- EMOJI_VARIATION_SELECTOR =
This denotes a “color” or “emoji” version
0xFE0F
- PLAIN_VARIATION_SELECTOR =
This denotes a “plain” (black/white) or “textual” version
0xFE0E
- PLAIN_VARIATION_SELECTOR_STRING =
PLAIN_VARIATION_SELECTOR.chr(Encoding::UTF_8)
- ZWJ_TAG =
Zero Width Joiner is used in sequences to indicate they should all be evaluated and displayed as a single thing
0x200D
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#alpha_code ⇒ Object
readonly
Returns the value of attribute alpha_code.
-
#ascii_aliases ⇒ Object
readonly
Returns the value of attribute ascii_aliases.
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#codepoints ⇒ Object
readonly
Returns the value of attribute codepoints.
-
#codepoints_alternates ⇒ Object
readonly
Returns the value of attribute codepoints_alternates.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#noto_image ⇒ Object
Returns the value of attribute noto_image.
-
#sort_key ⇒ Object
Returns the value of attribute sort_key.
-
#unicode_version ⇒ Object
Returns the value of attribute unicode_version.
Class Method Summary collapse
-
.format_alpha_code(alpha_code) ⇒ String
Ensure alpha code is formatted with colons.
- .format_name(raw_name) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add_alias(alpha_code) ⇒ Object
Add alternative alpha_codes to this character.
-
#add_ascii_alias(ascii_string) ⇒ Object
Add alternative ASCII aliases to this character.
-
#add_codepoints(codepoints) ⇒ Object
Add alternative codepoints to this character.
-
#flag? ⇒ Boolean
Return whether current character represents a flag or not.
-
#hex(codepoint = nil) ⇒ String
Return a Hex formatted version of the Unicode code points.
-
#image_name ⇒ String
Generate the image name to be used as fallback for this character.
-
#initialize(name, codepoints:, alpha_code:, description:, category:) ⇒ Character
constructor
A new instance of Character.
- #inspect ⇒ Object
-
#replace_alpha_code(alpha_code) ⇒ Object
Replace the current alpha_code.
- #to_s ⇒ Object
Constructor Details
#initialize(name, codepoints:, alpha_code:, description:, category:) ⇒ Character
Returns a new instance of Character.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tanuki_emoji/character.rb', line 47 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 = [] @ascii_aliases = [] @description = description @category = category end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def aliases @aliases end |
#alpha_code ⇒ Object (readonly)
Returns the value of attribute alpha_code.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def alpha_code @alpha_code end |
#ascii_aliases ⇒ Object (readonly)
Returns the value of attribute ascii_aliases.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def ascii_aliases @ascii_aliases end |
#category ⇒ Object (readonly)
Returns the value of attribute category.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def category @category end |
#codepoints ⇒ Object (readonly)
Returns the value of attribute codepoints.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def codepoints @codepoints end |
#codepoints_alternates ⇒ Object (readonly)
Returns the value of attribute codepoints_alternates.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def codepoints_alternates @codepoints_alternates end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/tanuki_emoji/character.rb', line 24 def name @name end |
#noto_image ⇒ Object
Returns the value of attribute noto_image.
26 27 28 |
# File 'lib/tanuki_emoji/character.rb', line 26 def noto_image @noto_image end |
#sort_key ⇒ Object
Returns the value of attribute sort_key.
26 27 28 |
# File 'lib/tanuki_emoji/character.rb', line 26 def sort_key @sort_key end |
#unicode_version ⇒ Object
Returns the value of attribute unicode_version.
26 27 28 |
# File 'lib/tanuki_emoji/character.rb', line 26 def unicode_version @unicode_version end |
Class Method Details
.format_alpha_code(alpha_code) ⇒ String
Ensure alpha code is formatted with colons
32 33 34 |
# File 'lib/tanuki_emoji/character.rb', line 32 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) ⇒ Object
36 37 38 39 40 |
# File 'lib/tanuki_emoji/character.rb', line 36 def self.format_name(raw_name) matched = raw_name.match(ALPHA_CODE_REGEXP) matched ? matched['alpha_text'] : raw_name end |
Instance Method Details
#==(other) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/tanuki_emoji/character.rb', line 151 def ==(other) name == other.name && codepoints == other.codepoints && codepoints_alternates == other.codepoints_alternates && alpha_code == other.alpha_code && aliases == other.aliases && ascii_aliases == other.ascii_aliases && description == other.description end |
#add_alias(alpha_code) ⇒ Object
Add alternative alpha_codes to this character
71 72 73 74 75 76 77 78 |
# File 'lib/tanuki_emoji/character.rb', line 71 def add_alias(alpha_code) formatted_code = self.class.format_alpha_code(alpha_code) return if @alpha_code == alpha_code return if aliases.include?(formatted_code) aliases << formatted_code end |
#add_ascii_alias(ascii_string) ⇒ Object
Add alternative ASCII aliases to this character
83 84 85 86 87 |
# File 'lib/tanuki_emoji/character.rb', line 83 def add_ascii_alias(ascii_string) return if ascii_aliases.include?(ascii_string) ascii_aliases << ascii_string end |
#add_codepoints(codepoints) ⇒ Object
Add alternative codepoints to this character
61 62 63 64 65 66 |
# File 'lib/tanuki_emoji/character.rb', line 61 def add_codepoints(codepoints) return if @codepoints == codepoints return if codepoints_alternates.include?(codepoints) codepoints_alternates << codepoints end |
#flag? ⇒ Boolean
Return whether current character represents a flag or not
137 138 139 |
# File 'lib/tanuki_emoji/character.rb', line 137 def flag? codepoints.match?(FLAG_REGEXP) end |
#hex(codepoint = nil) ⇒ String
Return a Hex formatted version of the Unicode code points
104 105 106 |
# File 'lib/tanuki_emoji/character.rb', line 104 def hex(codepoint = nil) codepoint ? unicode_to_hex(codepoint).join('-') : unicode_to_hex(codepoints).join('-') end |
#image_name ⇒ String
Generate the image name to be used as fallback for this character
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tanuki_emoji/character.rb', line 111 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 # also see https://www.unicode.org/reports/tr51/#flag-emoji-tag-sequences for # regional flags. if flag? name = noto_image unless name ([alpha_code] + aliases).each do |item| name = item.tr(':', '').sub('flag_', '') break if name.length == 2 end end return name.upcase + IMAGE_EXTENSION end # 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 ⇒ Object
145 146 147 148 149 |
# File 'lib/tanuki_emoji/character.rb', line 145 def inspect # rubocop:disable Layout/LineLength %(#<#{self.class.name}: #{codepoints} (#{hex}), alpha_code: "#{alpha_code}", aliases: #{aliases}, name: "#{name}", description: "#{description}">) # rubocop:enable Layout/LineLength end |
#replace_alpha_code(alpha_code) ⇒ Object
Replace the current alpha_code
92 93 94 95 96 97 98 99 |
# File 'lib/tanuki_emoji/character.rb', line 92 def replace_alpha_code(alpha_code) formatted_code = self.class.format_alpha_code(alpha_code) aliases.delete(formatted_code) @name = self.class.format_name(alpha_code) @alpha_code = formatted_code end |
#to_s ⇒ Object
141 142 143 |
# File 'lib/tanuki_emoji/character.rb', line 141 def to_s codepoints end |