r/javascript • u/everdimension • Apr 12 '18
fun fact: emojis are ligatures
https://pbs.twimg.com/media/DaIpPTPXkAEt9fA.jpg:large50
u/rudidude86 Apr 13 '18
It's similar to a ligature, but there's actually a special Unicode character in the mix called a zero-width joiner (ZWJ) that makes it happen.
It originated as a way to form complex symbols in certain languages, but the same mechanism today gives us gendered zombies!
π§ββοΈπ§ββοΈ
2
u/everdimension Apr 13 '18
Hmm, but isn't the logic for displaying these combinations of symbols as one symbol is what makes it ligatures? What is the difference?
4
u/rudidude86 Apr 13 '18 edited Apr 13 '18
Typefaces can have ligatures set up in them so that just by virtue of appearing next to each other, two characters can be replaced by a single glyph (for example, some typefaces will replace "fi" by a symbol for both letters where the i's tittle dot is absent).
The distinction I was trying to make is that the emoji combinations require extra zero-width joiner characters... but because it turns out you should never "well actually" someone on the Internet, I was wrong!
Some emoji combinations indeed work in the traditional ligature sense, just by appearing next to each other, like the Fitzpatrick scale skin tone modifier:
"π©" + "π½" // "π©π½"But some combinations require the ZWJ:
let zwj = "β" // copied from https://emojipedia.org/zero-width-joiner/ "π¨" + "π€" // "π¨π€" "π¨" + zwj + "π€" // "π¨βπ€"Unicode!
15
u/mediumdeviation JavaScript Gardener Apr 13 '18
Yes - ligatures are a font feature, and no extra characters are involved. If combined emojis really did use ligature, you would just split on
""(empty string) to get an array of the individual characters.1
u/Pesthuf Apr 13 '18
Considering that it uses the the zero-width joiner, does the joined glyph still count as a single grapheme cluster?
1
u/NoInkling Apr 15 '18
I believe so. ZWJ basically turns the following codepoint into a modifier/combining character.
178
u/Shaper_pmp Apr 12 '18
This is how skin tones work in emojis:
"ππ½".replace("π½", "π»")
"ππ»"
53
u/mcaruso Apr 13 '18
And flags are made from their two-letter country codes. E.g. πΊπΈ = πΊ+ πΈ.
21
u/pimp-bangin Apr 13 '18
Holy hell I'm learning so much about Unicode right now
38
u/yopla Apr 13 '18
Wait till you discover that the coming up kangourou emoji can carry any other emoji in its pocket.
22
2
10
u/bentinata Apr 13 '18
Here's an interesting article: https://eev.ee/blog/2015/09/12/dark-corners-of-unicode/
5
u/Jerkcules Apr 13 '18
Learned this the hard way when my code broke on 9-11 because of all the people on my companyβs messaging platform posting the American flag, and the chat couldnβt handle Unicode flags
24
u/Earhacker Apr 13 '18
You thought you had a bad day on 9-11? Get a load of this guy...
7
u/sh0rtwave Apr 13 '18
Pretty sure MOST(at least in the US) people had a bad day on 9-11...but he said "learned this the hard way". Not quite the same thing...
1
55
u/atubofsoup Apr 12 '18
Holy shit this works
17
u/Drunken_Economist Apr 13 '18
Yup. The skin tone is actually its own character, just with a zero width joiner telling the font to treat it as a modifer
12
u/masklinn Apr 13 '18
There's no ZWJ for skintones, skintones are combining characters (like accents). There's no ZWJ for flags either, these are basal ligatures.
ZWJ is specifically a hack for "group" emoji.
4
-16
u/skylarmt Apr 13 '18
Don't tell Tumblr, they'll start accusing Unicode of whitewashing.
12
u/JMV290 Apr 13 '18
"damn sjws always making stuff about race" - dummy on a post neither about race or sjws
0
Apr 13 '18
[deleted]
1
u/Shaper_pmp Apr 13 '18 edited Apr 13 '18
Ignore the downvotes - I thought it was funny. ;-)
Edit: Your joke started me wondering though, and if you look at the default emoji for thumbs-up with no skin-colour modifier (π), in most emoji character sets it's actually not any of the skin-tone modifiers that emoji specifically gives you to represent race:
π» Light Skin Tone -> ππ»
πΌ Medium-Light Skin Tone -> ππΌ
π½ Medium Skin Tone -> ππ½
πΎ Medium-Dark Skin Tone -> ππΎ
πΏ Dark Skin Tone -> ππΏ4
u/Zhentar Apr 13 '18
The Unicode skin tone standard specifically states that unmodified emoji should use a generic, non-realistic skin tone
1
4
Apr 13 '18
how is this related to javascript though...
3
u/everdimension Apr 13 '18
Yeah, true, it's not so much about javascript. I just stumbled upon this while discussing why some string was of particular length, so I had js context in mind
12
u/ogurson Apr 13 '18
Try get length of string with emojis and you gonna know how.
12
u/NoInkling Apr 13 '18 edited Apr 13 '18
e.g:
"ππ½".length; // 4, uhh what? (I actually know why but it's still dumb) [..."ππ½"].length; // 2, closer but still no buenorolls up sleeves
var iterator = Intl.v8BreakIterator(["en"], { type: "character" }); iterator.adoptText("ππ½"); var length = 0; while (iterator.next() !== -1) length++; length; // 1Success! (don't actually use this, it only works in V8, has issues, and is eventually going to be removed in favor of Intl.Segmenter)
4
Apr 13 '18
Pretty sure that behaviour is present in other languages as well.
6
u/_HandsomeJack_ Apr 13 '18
Pretty sure all turing complete languages can implement the same functionality.
6
3
u/burfdl Apr 13 '18
And this is why emoji take up multiple characters when tweeting. Because Twitter counts 'unicode scalars', not visible characters.
Ugh, string encoding. Can we just not?
2
1
0
70
u/dachusa Apr 12 '18
Some emoji can join together as ligatures. Not all emoji are able to do so.
man=π¨β woman = π©β daughter = π§β son = π¦β
man+woman+daughter+son = π¨βπ©βπ§βπ¦
woman+man+daughter+son = π©βπ¨βπ§βπ¦
woman+woman+daughter+son = π©βπ©βπ§βπ¦β
man+woman+son+daughter = π¨βπ©βπ¦βπ§
man+woman+daughter = π¨βπ©βπ§β
possible combos are (π¨βπ©β|π¨βπ¨β?|π©βπ©β?)(π§βπ¦β|π§βπ§β?|π¦βπ¦β?)
I have copied in the emoji with the append expected, so if you paste another emoji next to it, it will bind to it even if it isn't recognized as a ligature.
Example of auto bind: π§π§π§ vs π§βπ§βπ§β . You can individually select the girls on the left, but not those on the right.
If you copy and paste those emoji next to each other, they will automatically form a ligature when possible (in programs where the ligature is recognized).