Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a unicode character is supported?

I'm trying to put together a small android app that can randomly return an emoji to the user. My intention is to just use actual unicode emoji characters, and return them as unicode string characters.

I built a full array of unicode strings that could be randomly chosen from, and many will display correctly. However some are showing up as unsupported characters (a rectangle with an x through it).

Obviously not every platform will support every unicode emoji character, but if possible I'd like a way to determine what is and isn't a supported character. The ideal would be to query for a list of supported characters, but being able to test individual characters would also do the job just fine.

like image 826
SuperBiasedMan Avatar asked Jul 02 '16 17:07

SuperBiasedMan


People also ask

How do I check if a char is Unicode?

Check the length of the string and size in bytes. If both are equal then it ASCII. If size in bytes is larger than length of the string, then it contains UNICODE characters.

Does Unicode support all languages?

The simplest answer is that Unicode covers all of the languages that can be written in the following widely-used scripts: Latin, Greek, Cyrillic, Armenian, Hebrew, Arabic, Syriac, Thaana, Devanagari, Bengali, Gurmukhi, Oriya, Tamil, Telugu, Kannada, Malayalam, Sinhala, Thai, Lao, Tibetan, Myanmar, Georgian, Hangul, ...

Do all browsers support Unicode?

All modern browsers handle characters as Unicode internally, so for HTML (or XML) you should simply set UTF-8 as the charset. (And if your internal data aren't in Unicode yet, make sure to convert them as you build the page).

How do I find a non Unicode character?

To identify the Non Unicode characters we can use either Google Chrome or Mozilla firefox browser by just dragging and dropping the file to the browser. Chrome will show us only the row and column number of the .


2 Answers

Also check out Paint.hasGlyph(String), which was added in API level 23. You can use this to test if a character like an emoji has a glyph available.

This is what the documentation says:

boolean hasGlyph (String string)

Determine whether the typeface set on the paint has a glyph supporting the string. The simplest case is when the string contains a single character, in which this method determines whether the font has the character. In the case of multiple characters, the method returns true if there is a single glyph representing the ligature. For example, if the input is a pair of regional indicator symbols, determine whether there is an emoji flag for the pair.

Finally, if the string contains a variation selector, the method only returns true if the fonts contains a glyph specific to that variation.

Checking is done on the entire fallback chain, not just the immediate font referenced.

See also

  • How to detect emoji support on Android by code
like image 185
Suragch Avatar answered Nov 13 '22 07:11

Suragch


So, when you talk about a character being "unsupported", it sounds like what you mean is that the current font doesn't have a glyph for the character (and either the application doesn't have fallback logic to find a different font that does, or the system doesn't have any font that does).

In regular Java, this is pretty easy: given an instance of java.awt.Font, you can see if it has a glyph for a given Unicode character by using the canDisplay method.

The Android APIs, for whatever reason, don't seem to expose a way to figure out what font you're actually working with. (android.graphics.Typeface keeps that information private: see "Check the family of a Typeface object in Android".) However, you might at least try something like new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 12) to get a basic 12-point sans-serif font. You'll want to test, of course, to see if that gives a usable approximation for the emoji that the real font will be able to display.

like image 27
ruakh Avatar answered Nov 13 '22 06:11

ruakh