View Single Post
Old 03-23-2025, 11:44 PM   #3
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,732
Karma: 2197770
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
The font selection combos:
Code:
class FontFamilyModel(QAbstractListModel):

    def __init__(self, *args):
        QAbstractListModel.__init__(self, *args)
        from calibre.utils.fonts.scanner import font_scanner
        try:
            self.families = font_scanner.find_font_families()
        except:
            self.families = []
            print('WARNING: Could not load fonts')
            traceback.print_exc()
        # Restrict to Qt families as Qt tends to crash
        self.font = QFont('Arial' if iswindows else 'sansserif')

    def rowCount(self, *args):
        return len(self.families)

    def data(self, index, role):
        try:
            family = self.families[index.row()]
        except:
            traceback.print_exc()
            return None
        if role == Qt.DisplayRole:
            return family
        if role == Qt.FontRole:
            # If a user chooses some non standard font as the interface font,
            # rendering some font names causes Qt to crash, so return what is
            # hopefully a "safe" font
            return self.font
        return None

    def index_of(self, family):
        return self.families.index(family.strip())


class FontComboBox(QComboBox):
    def __init__(self, parent):
        QComboBox.__init__(self, parent)
        self.font_family_model = FontFamilyModel()
        self.setModel(self.font_family_model)

    def select_value(self, value):
        idx = self.findText(value) if value else -1
        self.setCurrentIndex(idx)

    def get_value(self):
        if self.currentIndex() < 0:
            return None
        return unicode(self.currentText()).strip()
Then the code for drawing making use of the font...
Code:
class TextLine(object):

    def __init__(self, text, font_name, font_size,
                 bottom_margin=30, align='center'):
        self.text = force_unicode(text)
        self.bottom_margin = bottom_margin
        try:
            from qt.core import QFont, Qt
        except ImportError:
            from PyQt5.Qt import QFont, Qt
        self.font = QFont(font_name) if font_name else QFont()
        self.font.setPixelSize(font_size)
        self._align = {'center': Qt.AlignHCenter,
                       'left': Qt.AlignLeft, 'right': Qt.AlignRight}[align]
kiwidude is offline   Reply With Quote