def load_fonts(self, lrf, load_substitutions=True): font_map = {} for font in lrf.font_map: fdata = QByteArray(lrf.font_map[font].data) id = QFontDatabase.addApplicationFontFromData(fdata) if id != -1: font_map[font] = [str(i) for i in QFontDatabase.applicationFontFamilies(id)][0] if load_substitutions: base = P("fonts/liberation/*.ttf") for f in glob.glob(base): QFontDatabase.addApplicationFont(f) self.font_loader = FontLoader(font_map, self.dpi)
def addFont(self): ''' @note:: 成功或者失败 ''' font_path = self.font_path fontId = QFontDatabase.addApplicationFont(font_path) if(fontId != -1): fontInfoList = QFontDatabase.applicationFontFamilies(fontId) fontFamily = fontInfoList[0] self.__font.setFamily(fontFamily) log.info("添加字体成功") return True else: log.warning("添加字体失败") return False
def addFont(self): ''' @note:: 成功或者失败 ''' font_path = self.font_path fontId = QFontDatabase.addApplicationFont(font_path) if (fontId != -1): fontInfoList = QFontDatabase.applicationFontFamilies(fontId) fontFamily = fontInfoList[0] self.__font.setFamily(fontFamily) log.info("添加字体成功") return True else: log.warning("添加字体失败") return False
def load_fonts(self, lrf, load_substitutions=True): font_map = {} for font in lrf.font_map: fdata = QByteArray(lrf.font_map[font].data) id = QFontDatabase.addApplicationFontFromData(fdata) if id != -1: font_map[font] = [ str(i) for i in QFontDatabase.applicationFontFamilies(id) ][0] if load_substitutions: base = P('fonts/liberation/*.ttf') for f in glob.glob(base): QFontDatabase.addApplicationFont(f) self.font_loader = FontLoader(font_map, self.dpi)
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': 'b06871f281fee6b241d60582ae9369b9', '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)))