Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add language parameter to Text objects #29794

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

Open
wants to merge 1 commit into
base: text-overhaul
Choose a base branch
Loading
from
Open
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
37 changes: 37 additions & 0 deletions 37 doc/users/next_whats_new/text_language.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Specifying text language
------------------------

OpenType fonts may support language systems which can be used to select different
typographic conventions, e.g., localized variants of letters that share a single Unicode
code point, or different default font features. The text API now supports setting a
language to be used and may be set/get with:

- `matplotlib.text.Text.set_language` / `matplotlib.text.Text.get_language`
- Any API that creates a `.Text` object by passing the *language* argument (e.g.,
``plt.xlabel(..., language=...)``)

The language of the text must be in a format accepted by libraqm, namely `a BCP47
language code <https://www.w3.org/International/articles/language-tags/>`_. If None or
unset, then no particular language will be implied, and default font settings will be
used.

For example, the default font ``DejaVu Sans`` supports language-specific glyphs in the
Serbian and Macedonian languages in the Cyrillic alphabet, or the Sámi family of
languages in the Latin alphabet.

.. plot::
:include-source:

fig = plt.figure(figsize=(7, 3))

char = '\U00000431'
fig.text(0.5, 0.8, f'\\U{ord(char):08x}', fontsize=40, horizontalalignment='center')
fig.text(0, 0.6, f'Serbian: {char}', fontsize=40, language='sr')
fig.text(1, 0.6, f'Russian: {char}', fontsize=40, language='ru',
horizontalalignment='right')

char = '\U0000014a'
fig.text(0.5, 0.3, f'\\U{ord(char):08x}', fontsize=40, horizontalalignment='center')
fig.text(0, 0.1, f'English: {char}', fontsize=40, language='en')
fig.text(1, 0.1, f'Inari Sámi: {char}', fontsize=40, language='smn',
horizontalalignment='right')
story645 marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 5 additions & 2 deletions 7 lib/matplotlib/_text_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def warn_on_missing_glyph(codepoint, fontnames):
f"Matplotlib currently does not support {block} natively.")


def layout(string, font, *, kern_mode=Kerning.DEFAULT):
def layout(string, font, *, language=None, kern_mode=Kerning.DEFAULT):
"""
Render *string* with *font*.

Expand All @@ -56,6 +56,9 @@ def layout(string, font, *, kern_mode=Kerning.DEFAULT):
The string to be rendered.
font : FT2Font
The font.
language : str or list of tuples of (str, int, int), optional
The language of the text in a format accepted by libraqm, namely `a BCP47
language code <https://www.w3.org/International/articles/language-tags/>`_.
kern_mode : Kerning
A FreeType kerning mode.

Expand All @@ -65,7 +68,7 @@ def layout(string, font, *, kern_mode=Kerning.DEFAULT):
"""
x = 0
prev_glyph_idx = None
char_to_font = font._get_fontmap(string)
char_to_font = font._get_fontmap(string) # TODO: Pass in language.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, this function is getting rewritten with libraqm, so this is just a note for later.

base_font = font
for char in string:
# This has done the fallback logic
Expand Down
3 changes: 2 additions & 1 deletion 3 lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
font = self._prepare_font(prop)
# We pass '0' for angle here, since it will be rotated (in raster
# space) in the following call to draw_text_image).
font.set_text(s, 0, flags=get_hinting_flag())
font.set_text(s, 0, flags=get_hinting_flag(),
language=mtext.get_language() if mtext is not None else None)
font.draw_glyphs_to_bitmap(
antialiased=gc.get_antialiased())
d = font.get_descent() / 64.0
Expand Down
6 changes: 4 additions & 2 deletions 6 lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,6 +2342,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
return self.draw_mathtext(gc, x, y, s, prop, angle)

fontsize = prop.get_size_in_points()
language = mtext.get_language() if mtext is not None else None

if mpl.rcParams['pdf.use14corefonts']:
font = self._get_font_afm(prop)
Expand All @@ -2352,7 +2353,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
fonttype = mpl.rcParams['pdf.fonttype']

if gc.get_url() is not None:
font.set_text(s)
font.set_text(s, language=language)
width, height = font.get_width_height()
self.file._annotations[-1][1].append(_get_link_annotation(
gc, x, y, width / 64, height / 64, angle))
Expand Down Expand Up @@ -2386,7 +2387,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
multibyte_glyphs = []
prev_was_multibyte = True
prev_font = font
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
for item in _text_helpers.layout(s, font, language=language,
kern_mode=Kerning.UNFITTED):
if _font_supports_glyph(fonttype, ord(item.char)):
if prev_was_multibyte or item.ft_object != prev_font:
singlebyte_chunks.append((item.ft_object, item.x, []))
Expand Down
3 changes: 2 additions & 1 deletion 3 lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,10 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
thisx += width * scale

else:
language = mtext.get_language() if mtext is not None else None
font = self._get_font_ttf(prop)
self._character_tracker.track(font, s)
for item in _text_helpers.layout(s, font):
for item in _text_helpers.layout(s, font, language=language):
ps_name = (item.ft_object.postscript_name
.encode("ascii", "replace").decode("ascii"))
glyph_name = item.ft_object.get_glyph_name(item.glyph_idx)
Expand Down
7 changes: 6 additions & 1 deletion 7 lib/matplotlib/ft2font.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ class FT2Font(Buffer):
def set_charmap(self, i: int) -> None: ...
def set_size(self, ptsize: float, dpi: float) -> None: ...
def set_text(
self, string: str, angle: float = ..., flags: LoadFlags = ...
self,
string: str,
angle: float = ...,
flags: LoadFlags = ...,
*,
language: str | list[tuple[str, int, int]] | None = ...,
) -> NDArray[np.float64]: ...
@property
def ascender(self) -> int: ...
Expand Down
5 changes: 5 additions & 0 deletions 5 lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
## for more information on text properties
#text.color: black

## The language of the text in a format accepted by libraqm, namely `a BCP47 language
## code <https://www.w3.org/International/articles/language-tags/>`_. If None, then no
## particular language will be implied, and default font settings will be used.
#text.language: None

## FreeType hinting flag ("foo" corresponds to FT_LOAD_FOO); may be one of the
## following (Proprietary Matplotlib-specific synonyms are given in parentheses,
## but their use is discouraged):
Expand Down
1 change: 1 addition & 0 deletions 1 lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ def _convert_validator_spec(key, conv):
"text.kerning_factor": validate_int,
"text.antialiased": validate_bool,
"text.parse_math": validate_bool,
"text.language": validate_string_or_None,

"mathtext.cal": validate_font_properties,
"mathtext.rm": validate_font_properties,
Expand Down
31 changes: 31 additions & 0 deletions 31 lib/matplotlib/tests/test_ft2font.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,37 @@ def test_ft2font_set_text():
assert font.get_bitmap_offset() == (6, 0)


@pytest.mark.parametrize(
'input',
[
[1, 2, 3],
[(1, 2)],
[('en', 'foo', 2)],
[('en', 1, 'foo')],
],
ids=[
'nontuple',
'wrong length',
'wrong start type',
'wrong end type',
],
)
def test_ft2font_language_invalid(input):
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
with pytest.raises(TypeError):
font.set_text('foo', language=input)


def test_ft2font_language():
# TODO: This is just a smoke test.
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
font.set_text('foo')
font.set_text('foo', language='en')
font.set_text('foo', language=[('en', 1, 2)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just testing it doesn't blow up? is there a .get_language to test that the setting didn't do anything strange?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, smoke testing only; full tests come with libraqm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add comments about the various todos?



def test_ft2font_loading():
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
Expand Down
21 changes: 21 additions & 0 deletions 21 lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,24 @@ def test_ytick_rotation_mode():
tick.set_rotation(angle)

plt.subplots_adjust(left=0.4, right=0.6, top=.99, bottom=.01)


@pytest.mark.parametrize(
'input, match',
[
([1, 2, 3], 'must be list of tuple'),
([(1, 2)], 'must be list of tuple'),
([('en', 'foo', 2)], 'start location must be int'),
([('en', 1, 'foo')], 'end location must be int'),
],
)
def test_text_language_invalid(input, match):
with pytest.raises(TypeError, match=match):
Text(0, 0, 'foo', language=input)


def test_text_language():
# TODO: This is just a smoke test.
Text(0, 0, 'foo', language='en')
Text(0, 0, 'foo', language=[('en', 1, 2)])
Text(0, 0, 'foo', language=(('en', 1, 2), )) # Not documented, but we'll allow it.
36 changes: 36 additions & 0 deletions 36 lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
super().__init__()
self._x, self._y = x, y
self._text = ''
self._language = None
self._reset_visual_defaults(
text=text,
color=color,
Expand Down Expand Up @@ -1422,6 +1423,41 @@
return 'baseline' if anchor_at_left else 'top'
return 'top' if anchor_at_left else 'baseline'

def get_language(self):
"""Return the language this Text is in."""
return self._language

def set_language(self, language):
"""
Set the language of the text.

Parameters
----------
language : str or list[tuple[str, int, int]] or None
The language of the text in a format accepted by libraqm, namely `a BCP47
language code <https://www.w3.org/International/articles/language-tags/>`_.

If None, then defaults to :rc:`text.language`.
"""
_api.check_isinstance((list, tuple, str, None), language=language)
language = mpl._val_or_rc(language, 'text.language')

if not cbook.is_scalar_or_string(language):
for val in language:
if not isinstance(val, tuple) or len(val) != 3:
raise TypeError('language must be list of tuple, not {language!r}')
sublang, start, end = val
if not isinstance(sublang, str):
raise TypeError(

Check warning on line 1451 in lib/matplotlib/text.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/text.py#L1451

Added line #L1451 was not covered by tests
'sub-language specification must be str, not {sublang!r}')
if not isinstance(start, int):
raise TypeError('start location must be int, not {start!r}')
if not isinstance(end, int):
raise TypeError('end location must be int, not {end!r}')

self._language = language
self.stale = True


class OffsetFrom:
"""Callable helper class for working with `Annotation`."""
Expand Down
2 changes: 2 additions & 0 deletions 2 lib/matplotlib/text.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Text(Artist):
def set_antialiased(self, antialiased: bool) -> None: ...
def _ha_for_angle(self, angle: Any) -> Literal['center', 'right', 'left'] | None: ...
def _va_for_angle(self, angle: Any) -> Literal['center', 'top', 'baseline'] | None: ...
def get_language(self) -> str | list[tuple[str, int, int]] | None: ...
def set_language(self, language: str | list[tuple[str, int, int]] | None) -> None: ...

class OffsetFrom:
def __init__(
Expand Down
12 changes: 8 additions & 4 deletions 12 lib/matplotlib/textpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
d /= 64.0
return w * scale, h * scale, d * scale

def get_text_path(self, prop, s, ismath=False):
def get_text_path(self, prop, s, ismath=False, *, language=None):
"""
Convert text *s* to path (a tuple of vertices and codes for
matplotlib.path.Path).
Expand All @@ -82,6 +82,9 @@ def get_text_path(self, prop, s, ismath=False):
The text to be converted.
ismath : {False, True, "TeX"}
If True, use mathtext parser. If "TeX", use tex for rendering.
language : str or list of tuples of (str, int, int), optional
The language of the text in a format accepted by libraqm, namely `a BCP47
language code <https://www.w3.org/International/articles/language-tags/>`_.

Returns
-------
Expand Down Expand Up @@ -109,7 +112,8 @@ def get_text_path(self, prop, s, ismath=False):
glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s)
elif not ismath:
font = self._get_font(prop)
glyph_info, glyph_map, rects = self.get_glyphs_with_font(font, s)
glyph_info, glyph_map, rects = self.get_glyphs_with_font(font, s,
language=language)
else:
glyph_info, glyph_map, rects = self.get_glyphs_mathtext(prop, s)

Expand All @@ -130,7 +134,7 @@ def get_text_path(self, prop, s, ismath=False):
return verts, codes

def get_glyphs_with_font(self, font, s, glyph_map=None,
return_new_glyphs_only=False):
return_new_glyphs_only=False, *, language=None):
"""
Convert string *s* to vertices and codes using the provided ttf font.
"""
Expand All @@ -145,7 +149,7 @@ def get_glyphs_with_font(self, font, s, glyph_map=None,

xpositions = []
glyph_ids = []
for item in _text_helpers.layout(s, font):
for item in _text_helpers.layout(s, font, language=language):
char_id = self._get_char_id(item.ft_object, ord(item.char))
glyph_ids.append(char_id)
xpositions.append(item.x)
Expand Down
5 changes: 4 additions & 1 deletion 5 lib/matplotlib/textpath.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ class TextToPath:
self, s: str, prop: FontProperties, ismath: bool | Literal["TeX"]
) -> tuple[float, float, float]: ...
def get_text_path(
self, prop: FontProperties, s: str, ismath: bool | Literal["TeX"] = ...
self, prop: FontProperties, s: str, ismath: bool | Literal["TeX"] = ..., *,
language: str | list[tuple[str, int, int]] | None = ...,
) -> list[np.ndarray]: ...
def get_glyphs_with_font(
self,
font: FT2Font,
s: str,
glyph_map: dict[str, tuple[np.ndarray, np.ndarray]] | None = ...,
return_new_glyphs_only: bool = ...,
*,
language: str | list[tuple[str, int, int]] | None = ...,
) -> tuple[
list[tuple[str, float, float, float]],
dict[str, tuple[np.ndarray, np.ndarray]],
Expand Down
4 changes: 3 additions & 1 deletion 4 src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ void FT2Font::set_kerning_factor(int factor)
}

void FT2Font::set_text(
std::u32string_view text, double angle, FT_Int32 flags, std::vector<double> &xys)
// TODO: languages is unused with FreeType, but will with libraqm.
std::u32string_view text, double angle, FT_Int32 flags, LanguageType languages,
std::vector<double> &xys)
{
FT_Matrix matrix; /* transformation matrix */

Expand Down
6 changes: 5 additions & 1 deletion 6 src/ft2font.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef MPL_FT2FONT_H
#define MPL_FT2FONT_H

#include <optional>
#include <set>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -98,6 +99,9 @@ class FT2Font
typedef void (*WarnFunc)(FT_ULong charcode, std::set<FT_String*> family_names);

public:
using LanguageRange = std::tuple<std::string, int, int>;
using LanguageType = std::optional<std::vector<LanguageRange>>;

FT2Font(FT_Open_Args &open_args, long hinting_factor,
std::vector<FT2Font *> &fallback_list,
WarnFunc warn, bool warn_if_used);
Expand All @@ -107,7 +111,7 @@ class FT2Font
void set_charmap(int i);
void select_charmap(unsigned long i);
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
std::vector<double> &xys);
LanguageType languages, std::vector<double> &xys);
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode);
void set_kerning_factor(int factor);
void load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool fallback);
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.