def char_support(scr, character=chr(0x28a7)): font_names = fontconfig.query() row = 4 for name in font_names: font = fontconfig.FcFont(name) if font.has_char(character) and font.fullname: # info = font.file # info = font.fullname[0][1] + ' [' + str(font.spacing) + ']' info = str(font.fullname) scr.addstr(row, 0, info) row += 1
def does_support(enc, font, chars): import fontconfig fonts = fontconfig.query() font_re = re.compile(sys.argv[2]) # print(fonts) fonts = [path for path in fonts if re.search(font_re, path) ] if len(fonts) == 0: return {'_error': 'No font found for '+font} # print(fonts) res = {} for c in chars: if c.startswith('U+'): if (sys.version_info > (3, 0)): # python 3 c_dec = chr(int(c[2:], 16)) else: # python 2 c_dec = ('\\U%08x' % int(c[2:], 16)).decode('unicode-escape') elif isinstance(c, bytes): c_dec = c.decode(enc) else: c_dec = c # print(c) # c_dec = c.decode(enc) if isinstance(c, bytes) else c cp = code_points(c_dec) # print(cp) if sys.maxunicode < cp[0]: # With some python versions, we cannot decode "high" unicode code points # even if the CP has a glyph in the current fontset :( res.update({c: 0}) continue for path in fonts: font = fontconfig.FcFont(path) # print('%s ' % (c_dec, )) if font.has_char(c_dec): # print('%s -> OK in %s' % (c_dec, font)) res.update({c_dec: 1}) break else: res.update({c_dec: 0}) return res
class FcFontTestCase(unittest.TestCase): fonts = fontconfig.query(family='dejavu serif', lang='en') font = fontconfig.FcFont(fonts[0]) def test_get_object_from_path(self): """Get FcFont instance""" fc = fontconfig.FcFont(self.font.file) self.assertIsInstance(fc, fontconfig.FcFont) def test_char_in_font(self): """Test the given character in font charset""" fc = fontconfig.FcFont(self.font.file) char = 'A' if pyver == 3 else 'A'.decode('utf8') self.assertTrue(fc.has_char(char)) @unittest.expectedFailure def test_char_not_in_font(self): """Test the given character not in font charset""" fc = fontconfig.FcFont(self.font.file) char = '永' if pyver == 3 else '永'.decode('utf8') self.assertTrue(fc.has_char(char))
#!/usr/bin/env python2 import re, sys import fontconfig if len(sys.argv) < 1: print('''Usage: ''' + sys.argv[0] + '''CHARS [REGEX] Print the names of available fonts containing the code point(s) CHARS. If CHARS contains multiple characters, they must all be present. Alternatively you can use U+xxxx to search for a single character with code point xxxx (hexadecimal digits). If REGEX is specified, the font name must match this regular expression.''') sys.exit(0) characters = sys.argv[1] if characters.startswith('U+'): characters = unichr(int(characters[2:], 16)) else: characters = characters.decode(sys.stdout.encoding) regexp = re.compile(sys.argv[2] if len(sys.argv) > 2 else '') font_names = fontconfig.query() found = False for name in font_names: if not re.search(regexp, name): continue font = fontconfig.FcFont(name) if all(font.has_char(c) for c in characters): print(name) found = True sys.exit(0 if found else 1)
def get_fonts_with_chars(chars): for font_file in fontconfig.query(): font = fontconfig.FcFont(font_file) if all([font.has_char(char) for char in chars]): yield font
def test_char_not_in_font(self): """Test the given character not in font charset""" fc = fontconfig.FcFont(self.font.file) char = '永' if pyver == 3 else '永'.decode('utf8') self.assertTrue(fc.has_char(char))
def test_get_object_from_path(self): """Get FcFont instance""" fc = fontconfig.FcFont(self.font.file) self.assertIsInstance(fc, fontconfig.FcFont)