Skip to content

Commit b3cfa4d

Browse files
committed
Add language parameter to Text objects
1 parent b9e94e4 commit b3cfa4d

16 files changed

+123
-33
lines changed

lib/matplotlib/_text_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def warn_on_missing_glyph(codepoint, fontnames):
4343
f"Matplotlib currently does not support {block} natively.")
4444

4545

46-
def layout(string, font, *, kern_mode=Kerning.DEFAULT):
46+
def layout(string, font, language, *, kern_mode=Kerning.DEFAULT):
4747
"""
4848
Render *string* with *font*.
4949
@@ -65,7 +65,7 @@ def layout(string, font, *, kern_mode=Kerning.DEFAULT):
6565
"""
6666
x = 0
6767
prev_glyph_idx = None
68-
char_to_font = font._get_fontmap(string)
68+
char_to_font = font._get_fontmap(string) # TODO: Pass in language.
6969
base_font = font
7070
for char in string:
7171
# This has done the fallback logic

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
494494
"""
495495
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath="TeX")
496496

497-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
497+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
498+
language=None):
498499
"""
499500
Draw a text instance.
500501
@@ -516,6 +517,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
516517
If True, use mathtext parser.
517518
mtext : `~matplotlib.text.Text`
518519
The original text object to be rendered.
520+
language : str or list[tuple[str, int, int]]
521+
The language of the text.
519522
520523
Notes
521524
-----

lib/matplotlib/backend_bases.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class RendererBase:
116116
angle: float,
117117
ismath: bool | Literal["TeX"] = ...,
118118
mtext: Text | None = ...,
119+
language: str | list[tuple[str, int, int]] | None = ...,
119120
) -> None: ...
120121
def get_text_width_height_descent(
121122
self, s: str, prop: FontProperties, ismath: bool | Literal["TeX"]

lib/matplotlib/backends/backend_agg.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,15 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
182182
y = round(y - oy + yd)
183183
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)
184184

185-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
185+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
186+
language=None):
186187
# docstring inherited
187188
if ismath:
188189
return self.draw_mathtext(gc, x, y, s, prop, angle)
189190
font = self._prepare_font(prop)
190191
# We pass '0' for angle here, since it will be rotated (in raster
191192
# space) in the following call to draw_text_image).
192-
font.set_text(s, 0, flags=get_hinting_flag())
193+
font.set_text(s, 0, flags=get_hinting_flag(), language=language)
193194
font.draw_glyphs_to_bitmap(
194195
antialiased=gc.get_antialiased())
195196
d = font.get_descent() / 64.0
@@ -203,7 +204,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
203204
y = round(y + yo + yd)
204205
self._renderer.draw_text_image(font, x, y + 1, angle, gc)
205206

206-
def get_text_width_height_descent(self, s, prop, ismath):
207+
def get_text_width_height_descent(self, s, prop, ismath, language=None):
207208
# docstring inherited
208209

209210
_api.check_in_list(["TeX", True, False], ismath=ismath)
@@ -216,7 +217,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
216217
return width, height, descent
217218

218219
font = self._prepare_font(prop)
219-
font.set_text(s, 0.0, flags=get_hinting_flag())
220+
font.set_text(s, 0.0, flags=get_hinting_flag(), language=language)
220221
w, h = font.get_width_height() # width and height of unrotated string
221222
d = font.get_descent()
222223
w /= 64.0 # convert from subpixels

lib/matplotlib/backends/backend_cairo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ def draw_image(self, gc, x, y, im):
214214
ctx.paint()
215215
ctx.restore()
216216

217-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
217+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
218+
language=None): # TODO: Apply language tag, if possible.
218219
# docstring inherited
219220

220221
# Note: (x, y) are device/display coords, not user-coords, unlike other

lib/matplotlib/backends/backend_pdf.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,8 @@ def encode_string(self, s, fonttype):
23272327
return s.encode('cp1252', 'replace')
23282328
return s.encode('utf-16be', 'replace')
23292329

2330-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
2330+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
2331+
language=None):
23312332
# docstring inherited
23322333

23332334
# TODO: combine consecutive texts into one BT/ET delimited section
@@ -2347,7 +2348,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23472348
fonttype = mpl.rcParams['pdf.fonttype']
23482349

23492350
if gc.get_url() is not None:
2350-
font.set_text(s)
2351+
font.set_text(s, language=language)
23512352
width, height = font.get_width_height()
23522353
self.file._annotations[-1][1].append(_get_link_annotation(
23532354
gc, x, y, width / 64, height / 64, angle))
@@ -2381,7 +2382,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23812382
multibyte_glyphs = []
23822383
prev_was_multibyte = True
23832384
prev_font = font
2384-
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
2385+
for item in _text_helpers.layout(s, font, language,
2386+
kern_mode=Kerning.UNFITTED):
23852387
if _font_supports_glyph(fonttype, ord(item.char)):
23862388
if prev_was_multibyte or item.ft_object != prev_font:
23872389
singlebyte_chunks.append((item.ft_object, item.x, []))

lib/matplotlib/backends/backend_pgf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
673673
# docstring inherited
674674
self.draw_text(gc, x, y, s, prop, angle, ismath="TeX", mtext=mtext)
675675

676-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
676+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
677+
language=None): # TODO: Implement language if possible.
677678
# docstring inherited
678679

679680
# prepare string for tex

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
758758
self.textcnt += 1
759759

760760
@_log_if_debug_on
761-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
761+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
762+
language=None):
762763
# docstring inherited
763764

764765
if self._is_transparent(gc.get_rgb()):
@@ -795,7 +796,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
795796
else:
796797
font = self._get_font_ttf(prop)
797798
self._character_tracker.track(font, s)
798-
for item in _text_helpers.layout(s, font):
799+
for item in _text_helpers.layout(s, font, language):
799800
ps_name = (item.ft_object.postscript_name
800801
.encode("ascii", "replace").decode("ascii"))
801802
glyph_name = item.ft_object.get_glyph_name(item.glyph_idx)

lib/matplotlib/backends/backend_svg.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,8 @@ def _update_glyph_map_defs(self, glyph_map_new):
10351035
def _adjust_char_id(self, char_id):
10361036
return char_id.replace("%20", "_")
10371037

1038-
def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1038+
def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None,
1039+
language=None):
10391040
# docstring inherited
10401041
writer = self.writer
10411042

@@ -1106,7 +1107,8 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
11061107

11071108
writer.end('g')
11081109

1109-
def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1110+
def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None,
1111+
language=None):
11101112
# NOTE: If you change the font styling CSS, then be sure the check for
11111113
# svg.fonttype = none in `lib/matplotlib/testing/compare.py::convert` remains in
11121114
# sync. Also be sure to re-generate any SVG using this mode, or else such tests
@@ -1263,7 +1265,8 @@ def _get_all_quoted_names(prop):
12631265

12641266
writer.end('g')
12651267

1266-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
1268+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
1269+
language=None):
12671270
# docstring inherited
12681271

12691272
clip_attrs = self._get_clip_attrs(gc)
@@ -1276,9 +1279,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
12761279
self.writer.start('a', {'xlink:href': gc.get_url()})
12771280

12781281
if mpl.rcParams['svg.fonttype'] == 'path':
1279-
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath, mtext)
1282+
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath, mtext, language)
12801283
else:
1281-
self._draw_text_as_text(gc, x, y, s, prop, angle, ismath, mtext)
1284+
self._draw_text_as_text(gc, x, y, s, prop, angle, ismath, mtext, language)
12821285

12831286
if gc.get_url() is not None:
12841287
self.writer.end('a')

lib/matplotlib/backends/backend_template.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def draw_path(self, gc, path, transform, rgbFace=None):
7979
def draw_image(self, gc, x, y, im):
8080
pass
8181

82-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
82+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None,
83+
language=None):
8384
pass
8485

8586
def flipy(self):

0 commit comments

Comments
 (0)