Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use the default font from mpl #4025

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/silx/gui/plot/backends/BackendOpenGL.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,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
Expand All @@ -261,6 +262,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),
Expand Down Expand Up @@ -1156,6 +1158,19 @@ def addShape(
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,
Expand All @@ -1170,7 +1185,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()

dashoffset, dashpattern = self._lineStyleToDashOffsetPattern(linestyle)
return _MarkerItem(
x,
Expand Down
12 changes: 7 additions & 5 deletions src/silx/gui/plot/backends/glutils/GLPlotFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/silx/gui/utils/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
Loading