def load_font(self, ttf_filename, charmap_filename): def hook(obj): result = {} for key in obj: result[key] = unichr(int(obj[key], 16)) return result if self.char_map is None: cache_key = ttf_filename + "|" + charmap_filename ttf_fname = os.path.join( os.path.dirname(os.path.realpath(__file__)), ttf_filename) font_id = QFontDatabase.addApplicationFont(ttf_fname) if font_id >= 0: font_families = QFontDatabase.applicationFontFamilies(font_id) else: cache = self.loaded_fonts.get(cache_key, None) if cache is None: raise OSError("Could not load ttf file for icon font.") else: self.char_map = cache['char_map'] self.font_name = cache['font_name'] return self.font_name = font_families[0] with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), charmap_filename), 'r') as codes: self.char_map = json.load(codes, object_hook=hook) self.loaded_fonts[cache_key] = { 'char_map': self.char_map, 'font_name': self.font_name }
def load_font(self, ttf_filename, charmap_filename): def hook(obj): result = {} for key in obj: result[key] = unichr(int(obj[key], 16)) return result if self.char_map is None: cache_key = ttf_filename + "|" + charmap_filename ttf_fname = os.path.join(os.path.dirname(os.path.realpath(__file__)), ttf_filename) font_id = QFontDatabase.addApplicationFont(ttf_fname) if font_id >= 0: font_families = QFontDatabase.applicationFontFamilies(font_id) else: cache = self.loaded_fonts.get(cache_key, None) if cache is None: raise OSError("Could not load ttf file for icon font.") else: self.char_map = cache['char_map'] self.font_name = cache['font_name'] return self.font_name = font_families[0] with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), charmap_filename), 'r') as codes: self.char_map = json.load(codes, object_hook=hook) self.loaded_fonts[cache_key] = {'char_map': self.char_map, 'font_name': self.font_name}
def load_font(font_name, font_file): # NOTE: you need to have created a QApplication() first (see # qtw.Widgets.Application) for this to work correctly, or you will get # a crash! font_id = QFontDatabase.addApplicationFont(font_file) if font_id < 0: raise ValueError("Unspecified Qt problem loading font from '%s'" % (font_file)) font_family = QFontDatabase.applicationFontFamilies(font_id)[0] if font_name != font_family: # If Qt knows this under a different name, add an alias font_asst.add_alias(font_name, font_family) return font_name
def load_font(font_name, font_file): # NOTE: you need to have created a QApplication() first (see # qtw.Widgets.Application) for this to work correctly, or you will get # a crash! font_id = QFontDatabase.addApplicationFont(font_file) if font_id < 0: raise ValueError("Unspecified Qt problem loading font from '%s'" % ( font_file)) font_family = QFontDatabase.applicationFontFamilies(font_id)[0] if font_name != font_family: # If Qt knows this under a different name, add an alias font_asst.add_alias(font_name, font_family) return font_name
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 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 _init_pyqt(self, exp): global app, font_database # Add the Qt plugin folders to the library path, if they exists. Where # these folders are depends on the version of Qt4, but these are two # possible locations. qt_plugin_path = os.path.join(os.path.dirname(sys.executable), 'Library', 'plugins') if os.path.isdir(qt_plugin_path): QCoreApplication.addLibraryPath( safe_decode(qt_plugin_path, enc=misc.filesystem_encoding())) qt_plugin_path = os.path.join(os.path.dirname(sys.executable), 'Library', 'lib', 'qt4', 'plugins') if os.path.isdir(qt_plugin_path): QCoreApplication.addLibraryPath( safe_decode(qt_plugin_path, enc=misc.filesystem_encoding())) # If no instance of QApplication exists, a segmentation fault seems to always # occur. So we create one. if QCoreApplication.instance() is None: app = QApplication([]) # Register the fonts bundled with OpenSesame if font_database is None: font_database = QFontDatabase() for font in FONTS: try: path = exp.resource(font + u'.ttf') except: warnings.warn(u'Font %s not found' % font) continue font_id = font_database.addApplicationFont(path) if font_id < 0: warnings.warn(u'Failed to load font %s' % font) continue font_families = font_database.applicationFontFamilies(font_id) if not font_families: warnings.warn(u'Font %s contains no families' % font) continue font_substitutions.append((font_families[0], font))
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 for in ``./fonts/``. Parameters ---------- 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: try: result[key] = chr(int(obj[key], 16)) except ValueError: if int(obj[key], 16) > 0xffff: # ignoring unsupported code in Python 2.7 32bit Windows # ValueError: chr() arg not in range(0x10000) warnings.warn("Your Python version doesn't support " "character {0}:{1}".format( key, obj[key])) else: raise FontError(u'Failed to load character ' '{0}:{1}'.format(key, obj[key])) return result if directory is None: directory = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'fonts') # Load font if QApplication.instance() is not None: with open(os.path.join(directory, ttf_filename), 'rb') as font_data: id_ = QFontDatabase.addApplicationFontFromData( QByteArray(font_data.read())) font_data.close() loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if loadedFontFamilies: self.fontname[prefix] = loadedFontFamilies[0] else: raise FontError(u"Font at '{0}' appears to be empty. " "If you are on Windows 10, please read " "https://support.microsoft.com/" "en-us/kb/3053676 " "to know how to prevent Windows from blocking " "the fonts that come with QtAwesome.".format( os.path.join(directory, ttf_filename))) with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook) # Verify that vendorized fonts are not corrupt if not SYSTEM_FONTS: ttf_hash = MD5_HASHES.get(ttf_filename, None) if ttf_hash is not None: hasher = hashlib.md5() with open(os.path.join(directory, ttf_filename), 'rb') as f: content = f.read() hasher.update(content) ttf_calculated_hash_code = hasher.hexdigest() if ttf_calculated_hash_code != ttf_hash: raise FontError(u"Font is corrupt at: '{0}'".format( os.path.join(directory, ttf_filename)))
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 for in ``./fonts/``. Parameters ---------- 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') # Load font if QApplication.instance() is not None: id_ = QFontDatabase.addApplicationFont( os.path.join(directory, ttf_filename)) loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if (loadedFontFamilies): self.fontname[prefix] = loadedFontFamilies[0] else: raise FontError(u"Font at '{0}' appears to be empty. " "If you are on Windows 10, please read " "https://support.microsoft.com/" "en-us/kb/3053676 " "to know how to prevent Windows from blocking " "the fonts that come with QtAwesome.".format( os.path.join(directory, ttf_filename))) with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook) # Verify that vendorized fonts are not corrupt if not SYSTEM_FONTS: md5_hashes = { 'fontawesome-webfont.ttf': 'a3de2170e4e9df77161ea5d3f31b2668', 'elusiveicons-webfont.ttf': '207966b04c032d5b873fd595a211582e' } ttf_hash = md5_hashes.get(ttf_filename, None) if ttf_hash is not None: hasher = hashlib.md5() with open(os.path.join(directory, ttf_filename), 'rb') as f: content = f.read() hasher.update(content) ttf_calculated_hash_code = hasher.hexdigest() if ttf_calculated_hash_code != ttf_hash: raise FontError(u"Font is corrupt at: '{0}'".format( os.path.join(directory, ttf_filename)))
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 for in the qtawesome ``fonts`` directory. Parameters ---------- 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 path for font and charmap files """ def hook(obj): result = {} for key in obj: try: result[key] = chr(int(obj[key], 16)) except ValueError: if int(obj[key], 16) > 0xffff: # ignoring unsupported code in Python 2.7 32bit Windows # ValueError: chr() arg not in range(0x10000) pass else: raise FontError(u'Failed to load character ' '{0}:{1}'.format(key, obj[key])) return result if directory is None: directory = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'fonts') # Load font if QApplication.instance() is not None: with open(os.path.join(directory, ttf_filename), 'rb') as font_data: id_ = QFontDatabase.addApplicationFontFromData( QByteArray(font_data.read())) font_data.close() loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if loadedFontFamilies: self.fontids[prefix] = id_ self.fontname[prefix] = loadedFontFamilies[0] else: raise FontError(u"Font at '{0}' appears to be empty. " "If you are on Windows 10, please read " "https://support.microsoft.com/" "en-us/kb/3053676 " "to know how to prevent Windows from blocking " "the fonts that come with QtAwesome.".format( os.path.join(directory, ttf_filename))) with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook)
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 for in ``./fonts/``. Parameters ---------- 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') # Load font if QApplication.instance() is not None: id_ = QFontDatabase.addApplicationFont( os.path.join(directory, ttf_filename)) loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if (loadedFontFamilies): self.fontname[prefix] = loadedFontFamilies[0] else: raise FontError(u"Font at '{0}' appears to be empty. " "If you are on Windows 10, please read " "https://support.microsoft.com/" "en-us/kb/3053676 " "to know how to prevent Windows from blocking " "the fonts that come with QtAwesome.".format( os.path.join(directory, ttf_filename))) with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook) # Verify that vendorized fonts are not corrupt if not SYSTEM_FONTS: md5_hashes = { 'fontawesome4.7-webfont.ttf': 'b06871f281fee6b241d60582ae9369b9', 'fontawesome5-regular-webfont.ttf': '73fe7f1effbf382f499831a0a9f18626', 'fontawesome5-solid-webfont.ttf': '0079a0ab6bec4da7d6e16f2a2e87cd71', 'fontawesome5-brands-webfont.ttf': '947b9537bc0fecc8130d48eb753495a1', 'elusiveicons-webfont.ttf': '207966b04c032d5b873fd595a211582e', 'materialdesignicons-webfont.ttf': '64b96825d49e070ea87c7100f2db3f46' } ttf_hash = md5_hashes.get(ttf_filename, None) if ttf_hash is not None: hasher = hashlib.md5() with open(os.path.join(directory, ttf_filename), 'rb') as f: content = f.read() hasher.update(content) ttf_calculated_hash_code = hasher.hexdigest() if ttf_calculated_hash_code != ttf_hash: raise FontError(u"Font is corrupt at: '{0}'".format( os.path.join(directory, ttf_filename)))
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 for in ``./fonts/``. Parameters ---------- 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') # Load font if QApplication.instance() is not None: id_ = QFontDatabase.addApplicationFont( os.path.join(directory, ttf_filename)) loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_) if (loadedFontFamilies): self.fontname[prefix] = loadedFontFamilies[0] else: raise FontError(u"Font at '{0}' appears to be empty. " "If you are on Windows 10, please read " "https://support.microsoft.com/" "en-us/kb/3053676 " "to know how to prevent Windows from blocking " "the fonts that come with QtAwesome.".format( os.path.join(directory, ttf_filename))) with open(os.path.join(directory, charmap_filename), 'r') as codes: self.charmap[prefix] = json.load(codes, object_hook=hook) # Verify that vendorized fonts are not corrupt if not SYSTEM_FONTS: md5_hashes = { 'fontawesome4.7-webfont.ttf': 'b06871f281fee6b241d60582ae9369b9', 'fontawesome5-regular-webfont.ttf': '0e2e26fb3527ae47f9eb1c217592b706', 'fontawesome5-solid-webfont.ttf': 'e143b57de78138e6d5963908afa7e393', 'fontawesome5-brands-webfont.ttf': 'dec02372212aab5a2e5294f1a11756ed', 'elusiveicons-webfont.ttf': '207966b04c032d5b873fd595a211582e', 'materialdesignicons-webfont.ttf': '023db9122f66b7d693bc52bcdf09e6b3' } ttf_hash = md5_hashes.get(ttf_filename, None) if ttf_hash is not None: hasher = hashlib.md5() with open(os.path.join(directory, ttf_filename), 'rb') as f: content = f.read() hasher.update(content) ttf_calculated_hash_code = hasher.hexdigest() if ttf_calculated_hash_code != ttf_hash: raise FontError(u"Font is corrupt at: '{0}'".format( os.path.join(directory, ttf_filename)))