Skip to content

Commit 00e276e

Browse files
committed
Try to use the default font from mpl
1 parent 521e533 commit 00e276e

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/silx/gui/plot/backends/BackendOpenGL.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def __init__(self, plot, parent=None, f=qt.Qt.Widget):
248248
foregroundColor=(0.0, 0.0, 0.0, 1.0),
249249
gridColor=(0.7, 0.7, 0.7, 1.0),
250250
marginRatios=(0.15, 0.1, 0.1, 0.15),
251+
font=self.getDefaultFont(),
251252
)
252253
self._plotFrame.size = ( # Init size with size int
253254
int(self.getDevicePixelRatio() * 640),
@@ -1113,6 +1114,20 @@ def addShape(
11131114
x, y, shape, color, fill, overlay, linewidth, dashpattern, gapcolor
11141115
)
11151116

1117+
def getDefaultFont(self):
1118+
"""Returns the default font, used by raw markers and axes labels"""
1119+
from matplotlib.font_manager import findfont, FontProperties
1120+
font_filename = findfont(FontProperties(family=["sans-serif"]))
1121+
print("font_filename", font_filename)
1122+
_logger.debug("Load font from mpl: %s", font_filename)
1123+
id = qt.QFontDatabase.addApplicationFont(font_filename)
1124+
family = qt.QFontDatabase.applicationFontFamilies(id)[0]
1125+
font = qt.QFont(family, 10, qt.QFont.Normal, False)
1126+
font.setStretch(110) # Local tuning
1127+
font.setPointSizeF(8.5) # Local tuning
1128+
font.setStyleStrategy(qt.QFont.PreferAntialias)
1129+
return font
1130+
11161131
def addMarker(
11171132
self,
11181133
x,
@@ -1127,7 +1142,9 @@ def addMarker(
11271142
font,
11281143
bgcolor: RGBAColorType | None,
11291144
):
1130-
font = qt.QApplication.instance().font() if font is None else font
1145+
if font is None:
1146+
font = self.getDefaultFont()
1147+
11311148
dashpattern = self._lineStyleToDashPattern(linestyle)
11321149
return _MarkerItem(
11331150
x,

src/silx/gui/plot/backends/glutils/GLPlotFrame.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class GLPlotFrame(object):
494494
# Margins used when plot frame is not displayed
495495
_NoDisplayMargins = _Margins(0, 0, 0, 0)
496496

497-
def __init__(self, marginRatios, foregroundColor, gridColor):
497+
def __init__(self, marginRatios, foregroundColor, gridColor, font: qt.QFont):
498498
"""
499499
:param List[float] marginRatios:
500500
The ratios of margins around plot area for axis and labels.
@@ -503,6 +503,7 @@ def __init__(self, marginRatios, foregroundColor, gridColor):
503503
:type foregroundColor: tuple with RGBA values ranging from 0.0 to 1.0
504504
:param gridColor: color used for grid lines.
505505
:type gridColor: tuple RGBA with RGBA values ranging from 0.0 to 1.0
506+
:param font: Font used by the axes label
506507
"""
507508
self._renderResources = None
508509

@@ -517,6 +518,7 @@ def __init__(self, marginRatios, foregroundColor, gridColor):
517518
self._grid = False
518519
self._size = 0.0, 0.0
519520
self._title = ""
521+
self._font: qt.QFont = font
520522

521523
self._devicePixelRatio = 1.0
522524
self._dpi = 92
@@ -730,7 +732,7 @@ def _buildVerticesAndLabels(self):
730732
labels.append(
731733
Text2D(
732734
text=self.title,
733-
font=qt.QApplication.instance().font(),
735+
font=self._font,
734736
color=self._foregroundColor,
735737
x=xTitle,
736738
y=yTitle,
@@ -816,7 +818,7 @@ def renderGrid(self):
816818

817819

818820
class GLPlotFrame2D(GLPlotFrame):
819-
def __init__(self, marginRatios, foregroundColor, gridColor):
821+
def __init__(self, marginRatios, foregroundColor, gridColor, font: qt.QFont):
820822
"""
821823
:param List[float] marginRatios:
822824
The ratios of margins around plot area for axis and labels.
@@ -825,9 +827,9 @@ def __init__(self, marginRatios, foregroundColor, gridColor):
825827
:type foregroundColor: tuple with RGBA values ranging from 0.0 to 1.0
826828
:param gridColor: color used for grid lines.
827829
:type gridColor: tuple RGBA with RGBA values ranging from 0.0 to 1.0
828-
830+
:param font: Font used by the axes label
829831
"""
830-
super(GLPlotFrame2D, self).__init__(marginRatios, foregroundColor, gridColor)
832+
super(GLPlotFrame2D, self).__init__(marginRatios, foregroundColor, gridColor, font)
831833
self.axes.append(
832834
PlotAxis(
833835
self,

0 commit comments

Comments
 (0)