Skip to content

Commit 744495f

Browse files
committed
Expand skin tones support
- Lookup emoji by unicode that contains a skin tone modifier - Generate skin tone variants from an emoji
1 parent ea4d738 commit 744495f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

lib/emoji.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ def find_by_alias(name)
5151

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

5757
private
5858
VARIATION_SELECTOR_16 = "\u{fe0f}".freeze
59+
SKIN_TONE_RE = /[\u{1F3FB}-\u{1F3FF}]/
5960

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

73-
private_constant :VARIATION_SELECTOR_16, :TEXT_GLYPHS
74+
private_constant :VARIATION_SELECTOR_16, :TEXT_GLYPHS, :SKIN_TONE_RE
7475

7576
def parse_data_file
7677
data = File.open(data_file, 'r:UTF-8') do |file|

lib/emoji/character.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ def add_alias(name)
4343
# Raw Unicode string for an emoji. Nil if emoji is non-standard.
4444
def raw() unicode_aliases.first end
4545

46+
# Raw Unicode strings for each skin tone variant of this emoji.
47+
def raw_skin_tone_variants
48+
return [] if custom? || !skin_tones?
49+
raw_normalized = raw.sub("\u{fe0f}", "") # strip VARIATION_SELECTOR_16
50+
idx = raw_normalized.index("\u{200d}") # detect zero-width joiner
51+
SKIN_TONES.map do |modifier|
52+
if idx
53+
# insert modifier before zero-width joiner
54+
raw_normalized[...idx] + modifier + raw_normalized[idx..]
55+
else
56+
raw_normalized + modifier
57+
end
58+
end
59+
end
60+
4661
def add_unicode_alias(str)
4762
unicode_aliases << str
4863
end
@@ -82,6 +97,16 @@ def image_filename
8297
end
8398

8499
private
100+
101+
SKIN_TONES = [
102+
"\u{1F3FB}", # light skin tone
103+
"\u{1F3FC}", # medium-light skin tone
104+
"\u{1F3FD}", # medium skin tone
105+
"\u{1F3FE}", # medium-dark skin tone
106+
"\u{1F3FF}", # dark skin tone
107+
]
108+
109+
private_constant :SKIN_TONES
85110

86111
def default_image_filename
87112
if custom?

test/emoji_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,22 @@ class EmojiTest < TestCase
155155
test "skin tones" do
156156
smiley = Emoji.find_by_alias("smiley")
157157
assert_equal false, smiley.skin_tones?
158+
assert_equal [], smiley.raw_skin_tone_variants
158159

159160
wave = Emoji.find_by_alias("wave")
160161
assert_equal true, wave.skin_tones?
162+
163+
wave = Emoji.find_by_unicode("\u{1f44b}\u{1f3ff}") # wave + dark skin tone
164+
assert_equal "wave", wave.name
165+
166+
woman_with_beard = Emoji.find_by_unicode("\u{1f9d4}\u{200d}\u{2640}\u{fe0f}")
167+
assert_equal [
168+
"1f9d4-1f3fb-200d-2640",
169+
"1f9d4-1f3fc-200d-2640",
170+
"1f9d4-1f3fd-200d-2640",
171+
"1f9d4-1f3fe-200d-2640",
172+
"1f9d4-1f3ff-200d-2640",
173+
], woman_with_beard.raw_skin_tone_variants.map { |u| Emoji::Character.hex_inspect(u) }
161174
end
162175

163176
test "no custom emojis" do

0 commit comments

Comments
 (0)