How can I check if a user's computer supports Emoji?
Asked Answered
N

2

25

This question could be generalized to any character, I suppose; but my specific use-case are emoji.

I'm writing a command-line program, and I want to detect if the computer on which it is running has a font installed that can display emoji; and whether the current terminal application will display them in that font.

At the moment, I have a hack in place that simply filters them out on non-OS X; but I don't want to short-change Linux users who have an emoji-enabled setup. I'd prefer to do this the β€˜right way.’

Thanks! πŸ’–

Norword answered 8/5, 2014 at 17:44 Comment(3)
"asked May 8 '14 at 17:44" 😫 – Larkins
Checking for locale support (sic) is trivial: keep in mind that it's in the private-use area. OP's expectations won't be met unless someone points out a method for checking that the glyph displays as expected. – Masto
See: askubuntu.com/a/784753/334094 and bonus: imgur.com/a/WAKF2 – Mercola
A
3

Your script would have to:

  1. know which system fonts are available, and where the font files are (for example C:\Windows\Fonts)
  2. inspect these files for their content. I suppose inspecting them could be done in a technical way, unravelling file formats. But you could also simulate the visual use, by calculating the bounding box or simulate font usage for the Emoji characters.

If you're into PHP, you could calculate the bounding box of the TTF fonts that are known for the Emoji, and see if the value is what is to be expected of a font that supports it.

You could also simulate the font use by drawing in a black in-memory image with a white colored font using GD, and calculate the (average) amount of white pixels to see if it fits an expected threshold.

You also asked for a check if the current terminal application will display them. For that you need to be able to check the config, which is non trivial since you'd need to be able to differentiate on all terminal applications. A practical solution would be to display some Emoji with the current font and just ask the user if it is visable. After all, you need to rely on the user anyway if the terminal application needs to support Emoji?

Amalee answered 8/6, 2017 at 15:14 Comment(2)
I really like the idea of allowing the user to flip the switch on emojis πŸ‘ – Larkins
A toggle is almost a given β€” personally, I hate the damn things. The original question (which is still relevant in my work) applies only because they're mainstream, expected, and lend a certain playfulness / fit in a certain community. (Any software which uses them without a toggle is broken, for me, honestly. :P) – Norword
N
0

Bash

I haven't tried these across environments, so I'm not positive they'll work -- but I think you can make this approach work. The basic approach is to try a hidden render and check if the render was successful. For instance:

echo -e '\xE0\xA5\xA5' | wc -m

wc -m counts the number of characters that are rendered. If you find that failure to render causes your input string to print, then this should be a way to perform an expected length check. If you find that failure to render causes a default symbol to be rendered, then I would suggest piping the output of echo into an equality check:

echo -e '\xE0\xA5\xA5' | grep -q 'some-default-failure-symbol'

A try-fail approach might seem hacky, but I don't think there's a standard for this so it's probably the most robust approach for now.

Node.js

Note: only works in browser, possibly a mock-browser. Not sure how or if a mock-browser would locate a local emoji package, but it's probably an easy thing to check.

There is a node.js module that uses very simple code to check if a system/browser supports an emoji. The code attempts to render the emoji and can tell whether the render was successful.

The npm package is called: if-emoji

The source code is only a few lines long, and should be easy to understand if you're familiar with javascript (I won't copy and paste the full code here to leave Dafrok with the credit): https://github.com/Dafrok/if-emoji

Napkin answered 8/6, 2017 at 21:4 Comment(4)
Interesting, I will have to investigate further. FYI, the if-emoji package looks specific to browers - it uses canvas. – Larkins
Mmmm, looks like if-emoji needs to be extended with command-line support using exactly the methods from this question. ;) – Norword
In a Linux terminal, wc -m reports an emoji as a single character but the terminal prints junk. I think wc only looks at $LC_*. I've asked for a POSIX solution here. – Hydantoin
The best approaches seem to look at cursor position before and after printing a multi-byte char. Can a script determine which characters a terminal can display? – Hydantoin

© 2022 - 2024 β€” McMap. All rights reserved.