3

I have a question in regards to finding if a unicode character is present in the font on the system.

>>> chr(9966)
'⛮'
>>> chr(9955)
'⛣'
>>> chr(9955) == chr(9966)
False

Basically, what I am trying to do, is to do a check where if the value is . Is there any way in python to figure out if the value is ?

Edit: I now understand that unicode characters are not inherently defined or undefined. But, is there a way to check if a character exists in a certain font? I am on OS X.

alexis
  • 48,685
  • 16
  • 101
  • 161
Anthony Dito
  • 3,610
  • 3
  • 29
  • 56
  • It's not actually printing an empty string, it is printing the unicode character (which happens to not show up). On my system, I see a question mark in a box. – L3viathan Mar 31 '16 at 22:09
  • 6
    What do you mean with "undefined on the system"? Do you mean "not present in the font"? (For me, both characters show up fine, by the way). – Martin Tournoij Mar 31 '16 at 22:10
  • 7
    This might be relevant http://stackoverflow.com/questions/4458696/finding-out-what-characters-a-font-supports – John Mar 31 '16 at 22:11
  • 1
    To clarify the above, you are *not* seeing 'a character that looks like a box' (which in itself is a valid Unicode character [`□`](http://www.fileformat.info/info/unicode/char/25a1/index.htm)). You are seeing the *default character* if the one asked for is not present in the font that is happened to be used for display. And usually, Python does not know what font your system is using so it cannot somehow 'change' the character to a box (which you seem to think it does), nor can it tell you if the character is available (unless you tell it what font gets used). – Jongware Mar 31 '16 at 22:15
  • @alexis I'm on a mac – Anthony Dito Mar 31 '16 at 22:35
  • Hmm, your question was marked as a duplicate of a question about **Linux**. Do the solutions given there work for you? – alexis Apr 01 '16 at 18:47
  • @alexis not really. No – Anthony Dito Apr 01 '16 at 18:48
  • I have flagged the question for reopening, since the duplicate question is about Linux, and the answers do not work on OS X. – alexis Apr 01 '16 at 20:12
  • The Python fonttools lib does not work on OSX? Why not? – Jongware Apr 01 '16 at 20:19
  • @alexis and Anthony: I just tested and can absolutely **confirm** that the solution in the duplicate **does work**. For `python font.py /Library/Fonts/Arial.ttf 9966` the output is `GEAR WITH HANDLES False`. (You need to enable the commented-out part of that code.) – Jongware Apr 01 '16 at 23:14
  • @RadLexus, that's good to know. This question still needs to be reopened, though, and you or someone else can explain what works from the other answers (I have a simpler solution, I think). – alexis Apr 01 '16 at 23:36
  • @alexis: since the solution in the duplicate works, the problem is at OPs side – it is *not* related to OP's OS being different. If you have another solution, you still can answer the original question, right? – Jongware Apr 01 '16 at 23:38
  • Not if the solution is inapplicable to Linux-- it would be off-topic in the original question. (In any case, I don't have access to a linux system I can test on.) – alexis Apr 01 '16 at 23:54

1 Answers1

0

This is related to the used font, and its support for Unicode, not python itself. I don't have these characters on my system.

Using the library unidecode, any unknown character seems to be returned as [?]:

from unidecode import unidecode
>>> unidecode(chr(9966))
'[?]'
>>> unidecode(chr(9955))
'[?]'
>>> unidecode(chr(9955)) == unidecode(chr(9966))
True

This method rely on appearance for get an ascii equivalent. Some others Unicode characters can be found as close to [?], so probably you will get some false positive.

aluriak
  • 5,559
  • 2
  • 26
  • 39