From 498a3c9813a91bf6a5e03497b481fa8c487b590e Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Thu, 4 Aug 2022 15:48:03 +0100 Subject: [PATCH 1/2] Fall back to kiva.fonttools if Qt can't come up with a font name. --- kiva/qpainter.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/kiva/qpainter.py b/kiva/qpainter.py index 8f46958d0..bc3859419 100644 --- a/kiva/qpainter.py +++ b/kiva/qpainter.py @@ -42,6 +42,16 @@ draw_modes[constants.FILL_STROKE] = QtCore.Qt.OddEvenFill draw_modes[constants.EOF_FILL_STROKE] = QtCore.Qt.WindingFill +font_family_to_style_hint = { + constants.DEFAULT: QtGui.QFont.StyleHint.AnyStyle, + constants.SWISS: QtGui.QFont.StyleHint.SansSerif, + constants.ROMAN: QtGui.QFont.StyleHint.Serif, + constants.MODERN: QtGui.QFont.StyleHint.Monospace, + constants.TELETYPE: QtGui.QFont.StyleHint.TypeWriter, + constants.DECORATIVE: QtGui.QFont.StyleHint.Decorative, + constants.SCRIPT: QtGui.QFont.StyleHint.Cursive, +} + font_styles = {} font_styles["regular"] = constants.NORMAL font_styles["bold"] = constants.NORMAL @@ -649,13 +659,27 @@ def select_font(self, name, size, style="regular", encoding=None): def set_font(self, font): """ Set the font for the current graphics context. """ - qfont = QtGui.QFont(font.face_name, font.size) - + qfont = QtGui.QFont() + qfont.setStyleHint(font_family_to_style_hint[font.family]) + qfont.setPointSizeF(font.size) weight = font._get_weight() qfont.setWeight(weight_to_qt_weight[weight]) - qfont.setItalic(font.style in constants.italic_styles) + if font.face_name: + name = font.face_name + else: + # if we don't have a face name, see if Qt can suggest one + name = qfont.defaultFamily() + if not name: + # otherwise use kiva.fonttools to suggest one + name = font.findfontname() + + if hasattr(qfont, 'setFamilies'): + qfont.setFamilies([name]) + else: + qfont.setFamily(name) + self.gc.setFont(qfont) def set_font_size(self, size): From dc824f73aa53734f8ba7643da85cb18cda4128ee Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Mon, 8 Aug 2022 14:09:10 +0100 Subject: [PATCH 2/2] Add a comment about setFamilies() --- kiva/qpainter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/kiva/qpainter.py b/kiva/qpainter.py index bc3859419..a0e356474 100644 --- a/kiva/qpainter.py +++ b/kiva/qpainter.py @@ -675,6 +675,7 @@ def set_font(self, font): # otherwise use kiva.fonttools to suggest one name = font.findfontname() + # setFamilies() was introduced in Qt 5.13 if hasattr(qfont, 'setFamilies'): qfont.setFamilies([name]) else: