Detect if a unicode character is supported? - android

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.

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

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.

You can use Character.isDefined to check if a character is defined in the version of Unicode on the device.

Related

Want to show a small italic i (math symbol) in Kotlin

I would like to show a small italic i (math symbol) in Kotlin using Android Studio. I see it has a value in Unicode-32, but that is not the character set that is the base for Kotlin in Android. Based on what I can determine, the base unicode is UTF-16 (hex), since that is what I used to define the other unicode characters I use. See https://unicode-table.com/en/.
For example, I use \u221A for the square root sign. However, there is apparently no small italic i in the same character set. Is there a way to generate the small italic i using Unicode 32 without having to redefine all the other unicode characters I use? Thanks in advance for your help.
Strings in Kotlin (and Java) are specified in 16-bit characters, therefore you can't include a 32-bit Unicode character inside a string literal.
You can, however, break your character into two 16-bit code points called a surrogate pair, like so:
\uD835\uDC56

Apply two custom fonts in a paragraph and display them in android

The project I worked on has some rare characters that are not supported by Unicode
"private-use area" (PUA) code points in the range U+EE80 through U+EFF. One custom open type font range from U+0000 to U+FFFF and the other has characters beyond U+FFFF. Both fonts are in the same language.
I need to display the characters based on their PUA range(characters are retrieved from database in random lines, no fixed location for a particular character). I tried different solutions but none of them works(bidi class with typeface, composite fonts, merging fonts, fallback, etc).
Has somebody used custom fonts that can display different font based on their range of PUA code points(character by character) on Android?
If you are using java to develop your android app, java only support unicode system so you cannot use characters beyond unicode system.

How to get system Android emoji drawable by unicode character?

I want to implement an android emoji keyboard just using emoji unicode array, without any extra res.
So for the emoji unicode character, I need to get the drawable from android system, if the system doesn't support the charactor, I just ignore this character.
After some basic research,
emojicon, https://github.com/rockerhieu/emojicon
emojicon, https://github.com/ankushsachdeva/emojicon
These implements all use self made resources.
use TextView setText, tv.setText("\u263A"); TextView can show the resource, but how can I get the resource?
Here's the problem- the system doesn't determine what characters can be displayed. The font does. The font needs to define how to draw every character it will display. That's why you'll see boxes or ? for missing characters- that's the font's default render. There is no method on Android to test if a character will display in a given font (although it would be really nice). And since there's no way to know what font the EditText is using, there's no way to know if a given character is supported. Your best bet is to limit yourself to characters defined in the default Android System font. But even then you're assuming the OEM didn't use a different default font, the user hasn't overridden it, and the app hasn't overridden it. Basically, you're forced to guess and hope.

how to convert ascii fonts to unicode for translation

I am developing an android and ios app which will be translated in multiple languages including english, hindi and gurmukhi. We have some pdf files in Gurmukhi that use ASCII fonts instead of unicode and we need to translate them to other languages so that when we try to copy the text we end up with some weird text. Is there a way to do it?
Thanks in advance.
Every symbol font has its own repertoire, so you need a custom lookup table from the ‘wrong’ character to the real character corresponding to what the glyph looks like in the particular font you are using. You can work out this table by installing the font in question and opening it in the Character Map (charmap.exe), noting down which symbol appears in each of the boxes normally occupied by A, B, C...
There is a converter tool here which knows about some common symbol fonts. If the font you want is in that list, you could try to extract the lookup table by pasting in all the characters in the range 32–126;128–255 (!"#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_``abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ) and grabbing what comes out when converted to ‘Unicode’.
Do you need to do the conversion in the application itself? If so you will typically need to write a character-by-character lookup loop. If not, best to use a tool like this manually to convert the material you want into standard Unicode, because dealing with text encoded in arbitrary symbol fonts is a pain in the neck.

Android: Display Special Characters

I would like to display special characters such as: ṁ ṭ m ē. In case they don't display here as well, this is how the four characters should look like:
In Android, these will display in squares. For other scripts, I am able to come over this problem with using a different font. But in this case setting the font (TextView.setTypeFace) will not solve this issue. These characters display correctly in for example OpenOffice (using Arial or Courier New), but inside Android it doesn't even when using the same fonts).
I also tried having the string saved as a unicode encoded string (e.g. in strings.xml: \u1E41 \u1E6D) getting the same result (in the logs they appear as they should). Any ideas?
If these characters are representable in Unicode, then you should be able to use Html.fromHtml() to get the glyph into a TextView, e.g.
textView.setText(Html.fromHtml("Ӓ"), TextView.BufferType.SPANNABLE);
It was really only a font issue. It was just hard to find a font that supports all characters I need.
Seeing that Google Translate has no problems with transliteration characters motivated me to make a more thorough search for fonts. Below is a list of useful fonts for this purpose:
http://guindo.pntic.mec.es/jmag0042/alphaeng.html (extensive but non-free)
http://users.teilar.gr/~g1951d/
http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=FontDownloads
http://www.mufi.info/fonts/

Categories

Resources