def set_font(self, family_name): font_db = QFontDatabase() font_db.removeAllApplicationFonts() font_archive = ZipFile(f'static/fonts/{family_name}.zip') for font_name in font_archive.namelist(): with font_archive.open(font_name) as font_file: font_data = font_file.read() font_data_as_bytearray = QByteArray(font_data) font_db.addApplicationFontFromData(font_data_as_bytearray) family_name = family_name.replace('_', ' ') self.setFont(QFont(family_name, 12))
def __init__(self, parent): super(CColorStraw, self).__init__(parent) QFontDatabase.addApplicationFontFromData(QByteArray.fromBase64(FONT)) font = self.font() or QFont() font.setFamily('iconfont') self.setFont(font) self.setToolTip('Нарисуйте цвет экрана') self._scaleWindow = ScaleWindow() # Не забудьте сначала показать его, а затем спрятать. self._scaleWindow.show() self._scaleWindow.hide()
def __init__(self, parent): super(CColorStraw, self).__init__(parent) QFontDatabase.addApplicationFontFromData(QByteArray.fromBase64(FONT)) font = self.font() or QFont() font.setFamily('iconfont') self.setFont(font) self.setText('') self.setToolTip('吸取屏幕颜色') self._scaleWindow = ScaleWindow() # 一定要先显示再隐藏,否则QDialog情况下第一次会卡死 self._scaleWindow.show() self._scaleWindow.hide()
def load_font(self): if QApplication.instance() is None: logger.warning('No QApplication instance found') return font_file = QFile(':font/awesome') if font_file.open(QFile.ReadOnly): idf = QFontDatabase.addApplicationFontFromData( QByteArray(font_file.readAll())) font_file.close() self.font_name = QFontDatabase.applicationFontFamilies(idf)[0]
def setTextFont(self, fileName, size=92): print(size) bin = self.readFontFileAsBinary(fileName) QFontDatabase.removeAllApplicationFonts() #id = QFontDatabase.addApplicationFont(fileName) id = QFontDatabase.addApplicationFontFromData(bin) try: family = QFontDatabase.applicationFontFamilies(id)[0] except: return "Not found" font = QFontDatabase.font(self.fontsDB, family, 'bold', size) font.setStrikeOut(False) self.ui.textEdit.setFont(font) self.ui.textEdit.setCurrentFont(font) self.ui.chkObserve.setEnabled(True) self.ui.chkHB.setEnabled(True) return family
class GFonts(QtWidgets.QMainWindow): def __init__(self): super(GFonts, self).__init__() uic.loadUi('gfonts.ui', self) self.fTool = gFontsTool() self.threadPool = QtCore.QThreadPool() self.DDFamily.currentIndexChanged.connect(self.familySelected) self.DDWeight.currentIndexChanged.connect(self.weightSelected) self.show() worker = bgProc(self.fTool.getMetadata) worker.signals.finished.connect(self.mdLoaded) self.threadPool.start(worker) self.statusBar().showMessage('Loading metadata...') def mdLoaded(self): # metadata loaded handler self.DDFamily.addItems(self.fTool.getFamilies()) self.statusBar().showMessage('Loading metadata... Done') def familySelected(self, index): selName = self.DDFamily.currentText() self.DDWeight.clear() self.DDWeight.addItems(self.fTool.getWeights(selName)) def weightSelected(self, index): selName = self.DDFamily.currentText() selWeight = self.DDWeight.currentText() worker = bgProc(self.fTool.getCSS, selName, selWeight) worker.signals.result.connect(self.cssLoaded) self.threadPool.start(worker) self.statusBar().showMessage('Retrieving CSS') def cssLoaded(self, css): print('CSS', css) pcss = self.fTool.getParsedCSS(css) au = self.fTool.getFontAllURIs(pcss) fancyName = self.fTool.getFontFullNames(pcss.rules) sName = self.fTool.selectSimplestName(fancyName) worker = bgProc(self.fTool.getFontBitStreams, au) worker.signals.result.connect(self.bsLoaded) self.threadPool.start(worker) self.statusBar().showMessage('Retrieving font') def bsLoaded(self, bs): self.statusBar().showMessage('Font loaded') key = list(bs.keys())[0] self.ttf = QtCore.QByteArray() bio = QtCore.QBuffer(self.ttf) bio.open(QtCore.QIODevice.WriteOnly) self.fTool.mergeBitStreams(bs[key], bio) self.statusBar().showMessage('Font merged') self.fdb = QFontDatabase() insertion = self.fdb.addApplicationFontFromData(self.ttf) print('Insertion:', insertion) if insertion == -1: print('Failed to insert font into database') fName = key[0] fWeight = key[1] print('Converted bitstream to ttf for:', fName, fWeight) print('Bitstream keys:', bs.keys()) if type(fWeight) == str and fWeight[-1] == 'i': fWeight = fWeight[:-1] italic = True else: italic = False styles = self.fdb.styles(fName) print('styles', styles) print(fWeight, italic) for s in styles: print('s:', s) print('s weight:', self.fdb.weight(fName, s)) print('s italic:', self.fdb.italic(fName, s)) if self.fdb.weight(fName, s) == self.fTool.CSS2QtFontWeight( fWeight) and self.fdb.italic(fName, s) == italic: thisStyle = s break else: thisStyle = None print(thisStyle) font = self.fdb.font(fName, thisStyle, 40) self.SampleText.setFont(font)