lib/tanuki_emoji/character.rb in tanuki_emoji-0.1.0 vs lib/tanuki_emoji/character.rb in tanuki_emoji-0.2.0

- old
+ new

@@ -4,10 +4,23 @@ # 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) # # @see https://www.unicode.org/reports/tr51/ class Character + IMAGE_PREFIX = 'emoji_u' + IMAGE_EXTENSION = '.png' + FLAG_REGEXP = /[🇦-🇿]{2}/u.freeze + + # This denotes a "color" or "emoji" version + EMOJI_VARIATION_SELECTOR = 0xFE0F + + # This denotes a "plain" (black/white) or "textual" version + PLAIN_VARIATION_SELECTOR = 0xFE0E + + # 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, :description # @param [String] name # @param [String] codepoints # @param [String] alpha_code @@ -37,9 +50,29 @@ # 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('-') + 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? + + # 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 + # + # @return [Boolean] whether character represents a flag or not + def flag? + codepoints.match?(FLAG_REGEXP) end def to_s codepoints end