lib/tanuki_emoji/character.rb in tanuki_emoji-0.10.0 vs lib/tanuki_emoji/character.rb in tanuki_emoji-0.11.0
- old
+ new
@@ -6,11 +6,11 @@
#
# @see https://www.unicode.org/reports/tr51/
class Character
IMAGE_PREFIX = 'emoji_u'
IMAGE_EXTENSION = '.png'
- FLAG_REGEXP = /[🇦-🇿]{2}/u
+ FLAG_REGEXP = /[🇦-🇿]{2}|(\u{1F3F4}.+\u{E007F})/u
ALPHA_CODE_REGEXP = /:(?<alpha_text>[_+\-a-z0-9]+):/
# This denotes a "color" or "emoji" version
EMOJI_VARIATION_SELECTOR = 0xFE0F
@@ -21,11 +21,11 @@
# Zero Width Joiner is used in sequences to indicate they should all be evaluated and displayed as a single thing
ZWJ_TAG = 0x200D
attr_reader :name, :codepoints, :codepoints_alternates, :alpha_code, :aliases, :ascii_aliases, :description, :category
- attr_accessor :unicode_version, :sort_key
+ attr_accessor :unicode_version, :sort_key, :noto_image
# @param [String] name
# @param [String] codepoints
# @param [String] alpha_code
# @param [String] description
@@ -43,43 +43,67 @@
# Add alternative codepoints to this character
#
# @param [String] codepoints
def add_codepoints(codepoints)
+ return if @codepoints == codepoints
+ return if codepoints_alternates.include?(codepoints)
+
codepoints_alternates << codepoints
end
# Add alternative alpha_codes to this character
#
# @param [String] alpha_code
def add_alias(alpha_code)
- aliases << self.class.format_alpha_code(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 alternative ASCII aliases to this character
#
# @param [String] ascii_string
def add_ascii_alias(ascii_string)
+ return if ascii_aliases.include?(ascii_string)
+
ascii_aliases << ascii_string
end
# Return a Hex formatted version of the Unicode code points
#
# @return [String] Hex formatted version of the unicode
- def hex
- unicode_to_hex(codepoints).join('-')
+ def hex(codepoint = nil)
+ codepoint ? unicode_to_hex(codepoint).join('-') : unicode_to_hex(codepoints).join('-')
end
# Generate the image name to be used as fallback for this character
#
# @return [String] image name with extension
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
- return alpha_code.tr(':', '').sub('flag_', '').upcase + IMAGE_EXTENSION if flag?
+ # also see https://www.unicode.org/reports/tr51/#flag-emoji-tag-sequences for
+ # regional flags.
+ if flag?
+ name = noto_image
+ unless name
+ # The two character code is only found in the aliases
+ 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
# Return whether current character represents a flag or not
@@ -92,10 +116,10 @@
def to_s
codepoints
end
def inspect
- "#<#{self.class.name}:#{name} #{codepoints}(#{hex})>"
+ "#<#{self.class.name}: #{codepoints} (#{hex}) #{alpha_code} aliases: #{aliases}>"
end
def ==(other)
name == other.name &&
codepoints == other.codepoints &&