From 547f51b8c339353f92db9f1eaa3291c125a42e14 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 21 Aug 2023 23:14:27 +0800 Subject: [PATCH 1/6] Figure.text: Support non-ASCII characters in the 'text' parameter --- pygmt/helpers/__init__.py | 1 + pygmt/src/text.py | 5 ++++- pygmt/tests/baseline/test_text_nonascii.png.dvc | 5 +++++ pygmt/tests/test_text.py | 13 +++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 pygmt/tests/baseline/test_text_nonascii.png.dvc diff --git a/pygmt/helpers/__init__.py b/pygmt/helpers/__init__.py index eabcb87500f..5eb265e8002 100644 --- a/pygmt/helpers/__init__.py +++ b/pygmt/helpers/__init__.py @@ -19,4 +19,5 @@ data_kind, is_nonstr_iter, launch_external_viewer, + non_ascii_to_octal, ) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index eae25894a61..507b8f9841b 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -10,6 +10,7 @@ fmt_docstring, is_nonstr_iter, kwargs_to_strings, + non_ascii_to_octal, use_alias, ) @@ -223,7 +224,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( + list(map(non_ascii_to_octal, np.atleast_1d(text).astype(str))) + ) with Session() as lib: file_context = lib.virtualfile_from_data( diff --git a/pygmt/tests/baseline/test_text_nonascii.png.dvc b/pygmt/tests/baseline/test_text_nonascii.png.dvc new file mode 100644 index 00000000000..0fdd59a3832 --- /dev/null +++ b/pygmt/tests/baseline/test_text_nonascii.png.dvc @@ -0,0 +1,5 @@ +outs: +- md5: 75db22909db0fbdb2a31edab357ed8b9 + size: 16418 + hash: md5 + path: test_text_nonascii.png diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 523da9b8b89..f34232d0568 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -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 From 30ae4c17072782e90e6cc8017ab4ee3f1919b2a9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 23 Aug 2023 07:49:21 +0800 Subject: [PATCH 2/6] Add docstrings to emphasize the support of non-ASCII characters --- pygmt/src/text.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 507b8f9841b..d72b49c5264 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -66,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. + Ses :gmt-docs:`cookbook/octal-codes.html` for the full list of supported + non-ASCII characters. + Full option list at :gmt-docs:`text.html` {aliases} From 73b919f6663d29aeab4ae2f244f6283364aaa7cc Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 24 Aug 2023 08:00:48 +0800 Subject: [PATCH 3/6] Fix a typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/src/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index d72b49c5264..363513f1981 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -69,7 +69,7 @@ def 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. - Ses :gmt-docs:`cookbook/octal-codes.html` for the full list of supported + See :gmt-docs:`cookbook/octal-codes.html` for the full list of supported non-ASCII characters. Full option list at :gmt-docs:`text.html` From 85a0926e457fed9a484dc55845fb87377e258c57 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 25 Aug 2023 08:51:02 +0800 Subject: [PATCH 4/6] Use np.vectorize to make non_ascii_to_octal work with numpy arrays --- pygmt/src/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 363513f1981..cd7dabcc3d5 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -231,7 +231,7 @@ def text_( # Append text at last column. Text must be passed in as str type. if kind == "vectors": extra_arrays.append( - list(map(non_ascii_to_octal, np.atleast_1d(text).astype(str))) + np.vectorize(non_ascii_to_octal)(np.atleast_1d(text).astype(str)) ) with Session() as lib: From adf6eda9d4f88de10ec4169e6f662d0796a48e73 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 25 Aug 2023 08:55:22 +0800 Subject: [PATCH 5/6] Update the test with more characters from Symbol and ZapfDingbats --- pygmt/tests/baseline/test_text_nonascii.png.dvc | 4 ++-- pygmt/tests/test_text.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/baseline/test_text_nonascii.png.dvc b/pygmt/tests/baseline/test_text_nonascii.png.dvc index 0fdd59a3832..57d38c42628 100644 --- a/pygmt/tests/baseline/test_text_nonascii.png.dvc +++ b/pygmt/tests/baseline/test_text_nonascii.png.dvc @@ -1,5 +1,5 @@ outs: -- md5: 75db22909db0fbdb2a31edab357ed8b9 - size: 16418 +- md5: fd8e9b79cd847837464c244563eb5d1a + size: 17341 hash: md5 path: test_text_nonascii.png diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index f34232d0568..9a8b1cf6663 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -382,5 +382,5 @@ def test_text_nonascii(): 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:°α"]) + fig.text(x=[5, 5], y=[3, 5], text=["xytext1:αζΔ❡", "xytext2:∑π∇✉"]) return fig From 05f22991a0647e59c84d2d03cdd5f11882e53d93 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 25 Aug 2023 10:26:15 +0800 Subject: [PATCH 6/6] Regenerate the baseline image using gs 9.54 --- pygmt/tests/baseline/test_text_nonascii.png.dvc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/baseline/test_text_nonascii.png.dvc b/pygmt/tests/baseline/test_text_nonascii.png.dvc index 57d38c42628..a428522975a 100644 --- a/pygmt/tests/baseline/test_text_nonascii.png.dvc +++ b/pygmt/tests/baseline/test_text_nonascii.png.dvc @@ -1,5 +1,5 @@ outs: -- md5: fd8e9b79cd847837464c244563eb5d1a - size: 17341 +- md5: aca7c6732bc8410a6299582a2bf3b997 + size: 17350 hash: md5 path: test_text_nonascii.png