Skip to content

Commit

Permalink
Figure.text: Support non-ASCII characters in the 'text' parameter (#2638
Browse files Browse the repository at this point in the history
)

Co-authored-by: Yvonne Fröhlich <[email protected]>
  • Loading branch information
seisman and yvonnefroehlich authored Aug 25, 2023
1 parent f620903 commit 604d077
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions pygmt/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
data_kind,
is_nonstr_iter,
launch_external_viewer,
non_ascii_to_octal,
)
11 changes: 10 additions & 1 deletion pygmt/src/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
fmt_docstring,
is_nonstr_iter,
kwargs_to_strings,
non_ascii_to_octal,
use_alias,
)

Expand Down Expand Up @@ -65,6 +66,12 @@ def text_(
- ``x``/``y``, and ``text``
- ``position`` and ``text``
The text strings passed via the ``text`` parameter can contain ASCII
characters and non-ASCII characters defined in the ISOLatin1+ encoding
(i.e., IEC_8859-1), and the Symbol and ZapfDingbats character sets.
See :gmt-docs:`cookbook/octal-codes.html` for the full list of supported
non-ASCII characters.
Full option list at :gmt-docs:`text.html`
{aliases}
Expand Down Expand Up @@ -223,7 +230,9 @@ def text_(

# Append text at last column. Text must be passed in as str type.
if kind == "vectors":
extra_arrays.append(np.atleast_1d(text).astype(str))
extra_arrays.append(
np.vectorize(non_ascii_to_octal)(np.atleast_1d(text).astype(str))
)

with Session() as lib:
file_context = lib.virtualfile_from_data(
Expand Down
5 changes: 5 additions & 0 deletions pygmt/tests/baseline/test_text_nonascii.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
outs:
- md5: aca7c6732bc8410a6299582a2bf3b997
size: 17350
hash: md5
path: test_text_nonascii.png
13 changes: 13 additions & 0 deletions pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,16 @@ def test_text_nonstr_text():
text=[1, 2, 3.0, 4.0],
)
return fig


@pytest.mark.mpl_image_compare
def test_text_nonascii():
"""
Test passing text strings with non-ascii characters.
"""
fig = Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True)
fig.text(position="TL", text="position-text:°α")
fig.text(x=1, y=1, text="xytext:°α")
fig.text(x=[5, 5], y=[3, 5], text=["xytext1:αζΔ❡", "xytext2:∑π∇✉"])
return fig

0 comments on commit 604d077

Please sign in to comment.