-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
QuLogic
wants to merge
2
commits into
matplotlib:text-overhaul
Choose a base branch
from
QuLogic:text-language
base: text-overhaul
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, Matplotlib's 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
Classes for including text in a figure. | ||||||
""" | ||||||
|
||||||
from collections.abc import Sequence | ||||||
import functools | ||||||
import logging | ||||||
import math | ||||||
|
@@ -136,6 +137,7 @@ def __init__(self, | |||||
super().__init__() | ||||||
self._x, self._y = x, y | ||||||
self._text = '' | ||||||
self.set_language(None) | ||||||
self._reset_visual_defaults( | ||||||
text=text, | ||||||
color=color, | ||||||
|
@@ -1422,6 +1424,42 @@ def _va_for_angle(self, angle): | |||||
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 None | ||||||
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.
Suggested change
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. I believe we decided not to document the split version yet until we figure out the multiline work. |
||||||
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((Sequence, str, None), language=language) | ||||||
language = mpl._val_or_rc(language, 'text.language') | ||||||
|
||||||
if not cbook.is_scalar_or_string(language): | ||||||
language = tuple(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( | ||||||
'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`.""" | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.