Skip to content

Commit dd7612c

Browse files
PragnyaKhandelwaldrammockwmvanvliet
authored
ENH: expose font_file in Brain.add_text (#13778)
Co-authored-by: Daniel McCloy <[email protected]> Co-authored-by: Marijn van Vliet <[email protected]>
1 parent f9e14c7 commit dd7612c

5 files changed

Lines changed: 47 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a ``font_file`` parameter to :meth:`mne.viz.Brain.add_text` to support rendering glyphs not available in the default font, by `Pragnya Khandelwal`_.

mne/viz/_brain/_brain.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,7 @@ def add_text(
27962796
col=0,
27972797
font_size=None,
27982798
justification=None,
2799+
font_file=None,
27992800
):
28002801
"""Add a text to the visualization.
28012802
@@ -2824,6 +2825,10 @@ def add_text(
28242825
The font size to use.
28252826
justification : str | None
28262827
The text justification.
2828+
font_file : str | None
2829+
Path to an absolute path of a font file to use for rendering
2830+
the text. See https://freetype.org/freetype2/docs/index.html for a list of
2831+
supported font file formats.
28272832
"""
28282833
_validate_type(name, (str, None), "name")
28292834
name = text if name is None else name
@@ -2840,6 +2845,7 @@ def add_text(
28402845
color=color,
28412846
size=font_size,
28422847
justification=justification,
2848+
font_file=font_file,
28432849
)
28442850
if "text" not in self._actors:
28452851
self._actors["text"] = dict()

mne/viz/backends/_abstract.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,16 @@ def quiver3d(
487487

488488
@classmethod
489489
@abstractmethod
490-
def text2d(self, x_window, y_window, text, size=14, color="white"):
490+
def text2d(
491+
self,
492+
x_window,
493+
y_window,
494+
text,
495+
size=14,
496+
color="white",
497+
justification=None,
498+
font_file=None,
499+
):
491500
"""Add 2d text in the scene.
492501
493502
Parameters
@@ -506,6 +515,13 @@ def text2d(self, x_window, y_window, text, size=14, color="white"):
506515
The color of the text as a tuple (red, green, blue) of float
507516
values between 0 and 1 or a valid color name (i.e. 'white'
508517
or 'w').
518+
justification : str | None
519+
The text justification. Can be 'left', 'center', or 'right'.
520+
font_file : str | None
521+
Path to an absolute path of a font file to use for rendering
522+
the text. FreeType is used for loading, supporting many font
523+
formats beyond ``.ttf`` and ``.ttc``. This can be helpful for
524+
non-ASCII glyph coverage.
509525
"""
510526
pass
511527

mne/viz/backends/_pyvista.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,24 @@ def quiver3d(
720720
return actor, mesh
721721

722722
def text2d(
723-
self, x_window, y_window, text, size=14, color="white", justification=None
723+
self,
724+
x_window,
725+
y_window,
726+
text,
727+
size=14,
728+
color="white",
729+
justification=None,
730+
font_file=None,
724731
):
725732
size = 14 if size is None else size
726733
position = (x_window, y_window)
727734
actor = self.plotter.add_text(
728-
text, position=position, font_size=size, color=color, viewport=True
735+
text=text,
736+
position=position,
737+
font_size=size,
738+
color=color,
739+
viewport=True,
740+
font_file=font_file,
729741
)
730742
if isinstance(justification, str):
731743
if justification == "left":

mne/viz/backends/tests/test_renderer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import numpy as np
1010
import pytest
11+
from matplotlib.font_manager import findfont
1112

1213
from mne.utils import run_subprocess
1314
from mne.viz import Figure3D, get_3d_backend, set_3d_backend
@@ -168,6 +169,14 @@ def test_3d_backend(renderer):
168169
size=txt_size,
169170
justification="right",
170171
)
172+
# test font_file passthrough with a real font from matplotlib
173+
font_path = findfont("serif")
174+
rend.text2d(
175+
x_window=txt_x + 0.1,
176+
y_window=txt_y + 0.1,
177+
text="font test",
178+
font_file=font_path,
179+
)
171180
rend.text3d(x=0, y=0, z=0, text=txt_text, scale=1.0)
172181
rend.set_camera(
173182
azimuth=180.0, elevation=90.0, distance=cam_distance, focalpoint=center

0 commit comments

Comments
 (0)