9

I have written a Python3 appindicator which calls fortune and captures the output for display in the on-screen notification.

Some fortunes contain squares with a hexadecimal number when the corresponding glyph does not exist in the current font. Each square is the representation of the hexadecimal Unicode code point for the missing glyph.

I want to remove the hexadecimal text before I display to the user. I was hoping to find some Python API which would let me inspect text, character by character, to determine something like char.isValidCodePoint() or similar but I cannot find as such.

I found a possible solution that I wanted to investigate here but after installing fonttools via the terminal, my Python program could not import fonttools/fontTools.

Any ideas - either using the Python API or calling out to a terminal?

Update #1: I have since realised the fonttools sample code from the link above will not work for me as it is Python2. I suppose if fonttools could somehow be used, I could invoke a Python2 interpreter from my Python3 script.

Update #2: After lots of reading (see references below), I have since found fc-match but it cannot always uniquely identify the font in use. I obtain the current font in Python:

from gi.repository import Gio
fontName = Gio.Settings( "org.gnome.desktop.interface" ).get_string( "font-name" )

resulting in Ubuntu 11. Passing this result to pango-view along with the hexadecimal character, I get a list of fonts including Ubuntu. To my thinking if the glyph was NOT rendered by the font, the font should not appear in the result from pango-view!

References:

0

This is a different approach from where you were going with this, but perhaps you could just use python's str.replace() or re.sub() methods to parse out the hexidecimal strings from your text body. i.e.:

If the hex is predictable:

originalText = "\xc3\xa5Test"
filteredText = originalText.replace("\xc3\xa5", "")

Or if you need to match any hex chars with a regular expression:

import re

originalText = "\xc3\xa5Test"
filteredText = re.sub(r'[^\x00-\x7f]', r'', originalText)

More good discussion of this strategy

  • It's OK to give alternate options but I think you could improve your answer a lot by 1) adding short sample code 2) describing possible PROs and CONs from the original post proposed solution and yours. – lpanebr Nov 10 '17 at 18:51
  • 1
    I'm not attempting to critique the original solution so I don't know if a PRO/CON comparison will be helpful here. I have, however added code examples for my suggestions to the answer. – Christopher Hunter Nov 10 '17 at 20:57
  • @ChristopherHunter: The text which comes from fortune is plain text and it is only when that text is rendered does the hexadecimal appear (and that's too late for me to catch and process as you suggest). – Bernmeister Nov 11 '17 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.