From 8c346835314514c9aad21e9c78851f6a48f54590 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 12 Nov 2024 23:49:05 +0800 Subject: [PATCH 1/2] Add a test for PS_CHAR_ENCODING=Standard+ --- pygmt/tests/test_text.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 64781c514bc..427030c05ff 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -6,7 +6,7 @@ import numpy as np import pytest -from pygmt import Figure +from pygmt import Figure, config from pygmt.exceptions import GMTCLibError, GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -410,12 +410,17 @@ def test_text_nonstr_text(): return fig -@pytest.mark.mpl_image_compare -def test_text_nonascii(): +@pytest.mark.mpl_image_compare(filename="test_text_nonascii.png") +@pytest.mark.parametrize("encoding", ["ISOLatin1+", "Standard+"]) +def test_text_nonascii(encoding): """ Test passing text strings with non-ascii characters. + + Default PS_CHAR_ENCODING setting should not affect the result. """ fig = Figure() + if encoding == "Standard+": # Temporarily set the PS_CHAR_ENCODING to "Standard+". + config(PS_CHAR_ENCODING="Standard+") fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True) fig.text(position="TL", text="position-text:°α") # noqa: RUF001 fig.text(x=1, y=1, text="xytext:°α") # noqa: RUF001 From d9e5203fbe879481922b2a222c8383df2e2df552 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 12 Nov 2024 23:52:02 +0800 Subject: [PATCH 2/2] Ensure non-ASCII characters are typeset correctly even if PS_CHAR_ENCODING is not 'ISOLatin1+' --- pygmt/helpers/utils.py | 15 ++++++--------- pygmt/src/text.py | 7 ++----- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index beb3da630d0..9f1b3f8b162 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -506,17 +506,14 @@ def build_arg_list( # noqa: PLR0912 else: gmt_args.append(f"-{key}{value}") - # Convert non-ASCII characters (if any) in the arguments to octal codes - encoding = _check_encoding("".join(gmt_args)) - if encoding != "ascii": - gmt_args = [non_ascii_to_octal(arg, encoding=encoding) for arg in gmt_args] gmt_args = sorted(gmt_args) - # Set --PS_CHAR_ENCODING=encoding if necessary - if encoding not in {"ascii", "ISOLatin1+"} and not ( - confdict and "PS_CHAR_ENCODING" in confdict - ): - gmt_args.append(f"--PS_CHAR_ENCODING={encoding}") + # Convert non-ASCII characters (if any) in the arguments to octal codes and set + # --PS_CHAR_ENCODING=encoding if necessary + if (encoding := _check_encoding("".join(gmt_args))) != "ascii": + gmt_args = [non_ascii_to_octal(arg, encoding=encoding) for arg in gmt_args] + if not (confdict and "PS_CHAR_ENCODING" in confdict): + gmt_args.append(f"--PS_CHAR_ENCODING={encoding}") if confdict: gmt_args.extend(f"--{key}={value}" for key, value in confdict.items()) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 2ed475c9ac2..7a495babd9c 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -238,15 +238,12 @@ def text_( # noqa: PLR0912 # Append text to the last column. Text must be passed in as str type. text = np.asarray(text, dtype=np.str_) - encoding = _check_encoding("".join(text.flatten())) - if encoding != "ascii": + if (encoding := _check_encoding("".join(text.flatten()))) != "ascii": text = np.vectorize(non_ascii_to_octal, excluded="encoding")( text, encoding=encoding ) + confdict["PS_CHAR_ENCODING"] = encoding extra_arrays.append(text) - - if encoding not in {"ascii", "ISOLatin1+"}: - confdict = {"PS_CHAR_ENCODING": encoding} else: if isinstance(position, str): kwargs["F"] += f"+c{position}+t{text}"