Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/emoji.json
Original file line number Diff line number Diff line change
Expand Up @@ -6416,7 +6416,7 @@
]
, "unicode_version": "12.0"
, "ios_version": "13.0"
, "skin_tones": true
, "skin_tones": false
}
, {
"emoji": "👭"
Expand Down
6 changes: 4 additions & 2 deletions lib/emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ def find_by_alias(name)

# Public: Find an emoji by its unicode character. Return nil if missing.
def find_by_unicode(unicode)
unicodes_index[unicode]
unicodes_index[unicode] || unicodes_index[unicode.sub(SKIN_TONE_RE, "")]
end

private
VARIATION_SELECTOR_16 = "\u{fe0f}".freeze
SKIN_TONE_RE = /[\u{1F3FB}-\u{1F3FF}]/

# Characters which must have VARIATION_SELECTOR_16 to render as color emoji:
TEXT_GLYPHS = [
Expand All @@ -70,7 +71,7 @@ def find_by_unicode(unicode)
"\u{3030}", # wavy dash
].freeze

private_constant :VARIATION_SELECTOR_16, :TEXT_GLYPHS
private_constant :VARIATION_SELECTOR_16, :TEXT_GLYPHS, :SKIN_TONE_RE

def parse_data_file
data = File.open(data_file, 'r:UTF-8') do |file|
Expand Down Expand Up @@ -118,6 +119,7 @@ def parse_data_file
emoji.description = dedup.call(raw_emoji[:description])
emoji.unicode_version = dedup.call(raw_emoji[:unicode_version])
emoji.ios_version = dedup.call(raw_emoji[:ios_version])
emoji.skin_tones = raw_emoji.fetch(:skin_tones, false)
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions lib/emoji/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def self.hex_inspect(str)
# True if the emoji is not a standard Emoji character.
def custom?() !raw end

# True if the emoji supports Fitzpatrick scale skin tone modifiers
def skin_tones?() @skin_tones end

attr_writer :skin_tones

# A list of names uniquely referring to this emoji.
attr_reader :aliases

Expand Down Expand Up @@ -38,6 +43,21 @@ def add_alias(name)
# Raw Unicode string for an emoji. Nil if emoji is non-standard.
def raw() unicode_aliases.first end

# Raw Unicode strings for each skin tone variant of this emoji.
def raw_skin_tone_variants
return [] if custom? || !skin_tones?
raw_normalized = raw.sub("\u{fe0f}", "") # strip VARIATION_SELECTOR_16
idx = raw_normalized.index("\u{200d}") # detect zero-width joiner
SKIN_TONES.map do |modifier|
if idx
# insert modifier before zero-width joiner
raw_normalized[...idx] + modifier + raw_normalized[idx..]
else
raw_normalized + modifier
end
end
end

def add_unicode_alias(str)
unicode_aliases << str
end
Expand All @@ -54,6 +74,7 @@ def initialize(name)
@aliases = Array(name)
@unicode_aliases = []
@tags = []
@skin_tones = false
end

def inspect
Expand All @@ -76,6 +97,16 @@ def image_filename
end

private

SKIN_TONES = [
"\u{1F3FB}", # light skin tone
"\u{1F3FC}", # medium-light skin tone
"\u{1F3FD}", # medium skin tone
"\u{1F3FE}", # medium-dark skin tone
"\u{1F3FF}", # dark skin tone
]

private_constant :SKIN_TONES

def default_image_filename
if custom?
Expand Down
21 changes: 21 additions & 0 deletions test/emoji_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ class EmojiTest < TestCase
assert_equal '8.3', emoji.ios_version
end

test "skin tones" do
smiley = Emoji.find_by_alias("smiley")
assert_equal false, smiley.skin_tones?
assert_equal [], smiley.raw_skin_tone_variants

wave = Emoji.find_by_alias("wave")
assert_equal true, wave.skin_tones?

wave = Emoji.find_by_unicode("\u{1f44b}\u{1f3ff}") # wave + dark skin tone
assert_equal "wave", wave.name

woman_with_beard = Emoji.find_by_unicode("\u{1f9d4}\u{200d}\u{2640}\u{fe0f}")
assert_equal [
"1f9d4-1f3fb-200d-2640",
"1f9d4-1f3fc-200d-2640",
"1f9d4-1f3fd-200d-2640",
"1f9d4-1f3fe-200d-2640",
"1f9d4-1f3ff-200d-2640",
], woman_with_beard.raw_skin_tone_variants.map { |u| Emoji::Character.hex_inspect(u) }
end

test "no custom emojis" do
custom = Emoji.all.select(&:custom?)
assert 0, custom.size
Expand Down