diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index f70a7264191..89459a787c1 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -484,17 +484,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 ad98711824b..ef6599b3333 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}" diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 593c07a7b4d..22b446302b8 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 from pygmt.helpers.testing import skip_if_no @@ -426,12 +426,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