-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: text-overhaul
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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*. | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, smoke testing only; full tests come with libraqm. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Uh oh!
There was an error while loading. Please reload this page.