Skip to content

Commit da33f74

Browse files
committed
Support skin tones for 🧑‍🤝‍🧑 emoji
1 parent ed57eb8 commit da33f74

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

db/emoji.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6416,7 +6416,7 @@
64166416
]
64176417
, "unicode_version": "12.0"
64186418
, "ios_version": "13.0"
6419-
, "skin_tones": false
6419+
, "skin_tones": true
64206420
}
64216421
, {
64226422
"emoji": "👭"

lib/emoji/character.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ 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.
46+
# Raw Unicode strings for each skin tone variant of this emoji. The result is an empty array
47+
# unless the emoji supports skin tones.
48+
#
49+
# Note: for emojis that depict multiple people (e.g. couples or families), this will not produce
50+
# every possible permutation of skin tone per person.
4751
def raw_skin_tone_variants
4852
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
53+
raw_normalized = raw.sub(VARIATION_SELECTOR_16, "")
54+
idx = raw_normalized.index(ZERO_WIDTH_JOINER)
5155
SKIN_TONES.map do |modifier|
52-
if idx
56+
if raw_normalized == PEOPLE_HOLDING_HANDS
57+
# special case to apply the modifier to both persons
58+
raw_normalized[0...idx] + modifier + raw_normalized[idx..nil] + modifier
59+
elsif idx
5360
# insert modifier before zero-width joiner
5461
raw_normalized[0...idx] + modifier + raw_normalized[idx..nil]
5562
else
@@ -97,7 +104,11 @@ def image_filename
97104
end
98105

99106
private
100-
107+
108+
VARIATION_SELECTOR_16 = "\u{fe0f}".freeze
109+
ZERO_WIDTH_JOINER = "\u{200d}".freeze
110+
PEOPLE_HOLDING_HANDS = "\u{1f9d1}\u{200d}\u{1f91d}\u{200d}\u{1f9d1}".freeze
111+
101112
SKIN_TONES = [
102113
"\u{1F3FB}", # light skin tone
103114
"\u{1F3FC}", # medium-light skin tone
@@ -106,7 +117,7 @@ def image_filename
106117
"\u{1F3FF}", # dark skin tone
107118
]
108119

109-
private_constant :SKIN_TONES
120+
private_constant :VARIATION_SELECTOR_16, :ZERO_WIDTH_JOINER, :PEOPLE_HOLDING_HANDS, :SKIN_TONES
110121

111122
def default_image_filename
112123
if custom?

test/emoji_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ class EmojiTest < TestCase
176176
"1f9d4-1f3fe-200d-2640",
177177
"1f9d4-1f3ff-200d-2640",
178178
], woman_with_beard.raw_skin_tone_variants.map { |u| Emoji::Character.hex_inspect(u) }
179+
180+
people_holding_hands = Emoji.find_by_alias("people_holding_hands")
181+
assert_equal [
182+
"1f9d1-1f3fb-200d-1f91d-200d-1f9d1-1f3fb",
183+
"1f9d1-1f3fc-200d-1f91d-200d-1f9d1-1f3fc",
184+
"1f9d1-1f3fd-200d-1f91d-200d-1f9d1-1f3fd",
185+
"1f9d1-1f3fe-200d-1f91d-200d-1f9d1-1f3fe",
186+
"1f9d1-1f3ff-200d-1f91d-200d-1f9d1-1f3ff",
187+
], people_holding_hands.raw_skin_tone_variants.map { |u| Emoji::Character.hex_inspect(u) }
179188
end
180189

181190
test "no custom emojis" do

0 commit comments

Comments
 (0)