From 3d7606232c094440b92fe7fb50f6c8c9c2b1ac43 Mon Sep 17 00:00:00 2001 From: Valentin Valls Date: Mon, 18 Dec 2023 13:49:33 +0100 Subject: [PATCH] Use the default font from mpl --- src/silx/gui/plot/backends/BackendOpenGL.py | 19 ++++++++++++++++++- .../gui/plot/backends/glutils/GLPlotFrame.py | 12 +++++++----- src/silx/gui/utils/matplotlib.py | 3 +++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/silx/gui/plot/backends/BackendOpenGL.py b/src/silx/gui/plot/backends/BackendOpenGL.py index 10eaca7bd4..9aacbc3818 100755 --- a/src/silx/gui/plot/backends/BackendOpenGL.py +++ b/src/silx/gui/plot/backends/BackendOpenGL.py @@ -226,6 +226,7 @@ def __init__(self, plot, parent=None, f=qt.Qt.Widget): ) BackendBase.BackendBase.__init__(self, plot, parent) + self._defaultFont: qt.QFont = None self.__isOpenGLValid = False self._backgroundColor = 1.0, 1.0, 1.0, 1.0 @@ -248,6 +249,7 @@ def __init__(self, plot, parent=None, f=qt.Qt.Widget): foregroundColor=(0.0, 0.0, 0.0, 1.0), gridColor=(0.7, 0.7, 0.7, 1.0), marginRatios=(0.15, 0.1, 0.1, 0.15), + font=self.getDefaultFont(), ) self._plotFrame.size = ( # Init size with size int int(self.getDevicePixelRatio() * 640), @@ -1113,6 +1115,19 @@ def addShape( x, y, shape, color, fill, overlay, linewidth, dashpattern, gapcolor ) + def getDefaultFont(self): + """Returns the default font, used by raw markers and axes labels""" + if self._defaultFont is None: + from matplotlib.font_manager import findfont, FontProperties + font_filename = findfont(FontProperties(family=["sans-serif"])) + _logger.debug("Load font from mpl: %s", font_filename) + id = qt.QFontDatabase.addApplicationFont(font_filename) + family = qt.QFontDatabase.applicationFontFamilies(id)[0] + font = qt.QFont(family, 10, qt.QFont.Normal, False) + font.setStyleStrategy(qt.QFont.PreferAntialias) + self._defaultFont = font + return self._defaultFont + def addMarker( self, x, @@ -1127,7 +1142,9 @@ def addMarker( font, bgcolor: RGBAColorType | None, ): - font = qt.QApplication.instance().font() if font is None else font + if font is None: + font = self.getDefaultFont() + dashpattern = self._lineStyleToDashPattern(linestyle) return _MarkerItem( x, diff --git a/src/silx/gui/plot/backends/glutils/GLPlotFrame.py b/src/silx/gui/plot/backends/glutils/GLPlotFrame.py index df333a4ec6..530f079442 100644 --- a/src/silx/gui/plot/backends/glutils/GLPlotFrame.py +++ b/src/silx/gui/plot/backends/glutils/GLPlotFrame.py @@ -494,7 +494,7 @@ class GLPlotFrame(object): # Margins used when plot frame is not displayed _NoDisplayMargins = _Margins(0, 0, 0, 0) - def __init__(self, marginRatios, foregroundColor, gridColor): + def __init__(self, marginRatios, foregroundColor, gridColor, font: qt.QFont): """ :param List[float] marginRatios: The ratios of margins around plot area for axis and labels. @@ -503,6 +503,7 @@ def __init__(self, marginRatios, foregroundColor, gridColor): :type foregroundColor: tuple with RGBA values ranging from 0.0 to 1.0 :param gridColor: color used for grid lines. :type gridColor: tuple RGBA with RGBA values ranging from 0.0 to 1.0 + :param font: Font used by the axes label """ self._renderResources = None @@ -517,6 +518,7 @@ def __init__(self, marginRatios, foregroundColor, gridColor): self._grid = False self._size = 0.0, 0.0 self._title = "" + self._font: qt.QFont = font self._devicePixelRatio = 1.0 self._dpi = 92 @@ -730,7 +732,7 @@ def _buildVerticesAndLabels(self): labels.append( Text2D( text=self.title, - font=qt.QApplication.instance().font(), + font=self._font, color=self._foregroundColor, x=xTitle, y=yTitle, @@ -816,7 +818,7 @@ def renderGrid(self): class GLPlotFrame2D(GLPlotFrame): - def __init__(self, marginRatios, foregroundColor, gridColor): + def __init__(self, marginRatios, foregroundColor, gridColor, font: qt.QFont): """ :param List[float] marginRatios: The ratios of margins around plot area for axis and labels. @@ -825,9 +827,9 @@ def __init__(self, marginRatios, foregroundColor, gridColor): :type foregroundColor: tuple with RGBA values ranging from 0.0 to 1.0 :param gridColor: color used for grid lines. :type gridColor: tuple RGBA with RGBA values ranging from 0.0 to 1.0 - + :param font: Font used by the axes label """ - super(GLPlotFrame2D, self).__init__(marginRatios, foregroundColor, gridColor) + super(GLPlotFrame2D, self).__init__(marginRatios, foregroundColor, gridColor, font) self.axes.append( PlotAxis( self, diff --git a/src/silx/gui/utils/matplotlib.py b/src/silx/gui/utils/matplotlib.py index 3cab910bc2..76ee5e7eec 100644 --- a/src/silx/gui/utils/matplotlib.py +++ b/src/silx/gui/utils/matplotlib.py @@ -93,6 +93,9 @@ def qFontToFontProperties(font: qt.QFont): families = [f for f in families if f in availableNames] families.append(font_manager.fontManager.defaultFamily["ttf"]) + if "Sans" in font.family(): + families.insert(0, "sans-serif") + return FontProperties( family=families, style=_FONT_STYLES[font.style()],