def load_font(self, prefix, ttf_filename, charmap_filename, directory=None): """Loads a font file and the associated charmap If `directory` is None, the files will be looked up in ./fonts/ Arguments --------- prefix: str prefix string to be used when accessing a given font set ttf_filename: str ttf font filename charmap_filename: str charmap filename directory: str or None, optional directory for font and charmap files """ def hook(obj): result = {} for key in obj: result[key] = unichr(int(obj[key], 16)) return result if directory is None: directory = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'fonts') with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook) id_ = QFontDatabase.addApplicationFont( os.path.join(directory, ttf_filename)) loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if (loadedFontFamilies): self.fontname[prefix] = loadedFontFamilies[0] else: print('Font is empty')
def load_font(self, prefix, ttf_filename, charmap_filename, directory=None): """Loads a font file and the associated charmap If `directory` is None, the files will be looked up in ./fonts/ Arguments --------- prefix: str prefix string to be used when accessing a given font set ttf_filename: str ttf font filename charmap_filename: str charmap filename directory: str or None, optional directory for font and charmap files """ def hook(obj): result = {} for key in obj: result[key] = unichr(int(obj[key], 16)) return result if directory is None: directory = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'fonts') with open(os.path.join(directory, ttf_filename), 'rb') as f: font_data = QByteArray(f.read()) with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook) id_ = QFontDatabase.addApplicationFontFromData(font_data) loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if(loadedFontFamilies): self.fontname[prefix] = loadedFontFamilies[0] else: print('Font is empty')
def font_is_installed(font): """Check if font is installed""" return [ fam for fam in QFontDatabase().families() if to_text_string(fam) == font ]