Skip to content

Commit f76d25e

Browse files
authored
Ensure non-ASCII characters are typeset correctly even if PS_CHAR_ENCODING is not 'ISOLatin1+' (#3611)
1 parent d0bad08 commit f76d25e

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

pygmt/helpers/utils.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -484,17 +484,14 @@ def build_arg_list( # noqa: PLR0912
484484
else:
485485
gmt_args.append(f"-{key}{value}")
486486

487-
# Convert non-ASCII characters (if any) in the arguments to octal codes
488-
encoding = _check_encoding("".join(gmt_args))
489-
if encoding != "ascii":
490-
gmt_args = [non_ascii_to_octal(arg, encoding=encoding) for arg in gmt_args]
491487
gmt_args = sorted(gmt_args)
492488

493-
# Set --PS_CHAR_ENCODING=encoding if necessary
494-
if encoding not in {"ascii", "ISOLatin1+"} and not (
495-
confdict and "PS_CHAR_ENCODING" in confdict
496-
):
497-
gmt_args.append(f"--PS_CHAR_ENCODING={encoding}")
489+
# Convert non-ASCII characters (if any) in the arguments to octal codes and set
490+
# --PS_CHAR_ENCODING=encoding if necessary
491+
if (encoding := _check_encoding("".join(gmt_args))) != "ascii":
492+
gmt_args = [non_ascii_to_octal(arg, encoding=encoding) for arg in gmt_args]
493+
if not (confdict and "PS_CHAR_ENCODING" in confdict):
494+
gmt_args.append(f"--PS_CHAR_ENCODING={encoding}")
498495

499496
if confdict:
500497
gmt_args.extend(f"--{key}={value}" for key, value in confdict.items())

pygmt/src/text.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,12 @@ def text_( # noqa: PLR0912
238238

239239
# Append text to the last column. Text must be passed in as str type.
240240
text = np.asarray(text, dtype=np.str_)
241-
encoding = _check_encoding("".join(text.flatten()))
242-
if encoding != "ascii":
241+
if (encoding := _check_encoding("".join(text.flatten()))) != "ascii":
243242
text = np.vectorize(non_ascii_to_octal, excluded="encoding")(
244243
text, encoding=encoding
245244
)
245+
confdict["PS_CHAR_ENCODING"] = encoding
246246
extra_arrays.append(text)
247-
248-
if encoding not in {"ascii", "ISOLatin1+"}:
249-
confdict = {"PS_CHAR_ENCODING": encoding}
250247
else:
251248
if isinstance(position, str):
252249
kwargs["F"] += f"+c{position}+t{text}"

pygmt/tests/test_text.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88
import pytest
9-
from pygmt import Figure
9+
from pygmt import Figure, config
1010
from pygmt.exceptions import GMTCLibError, GMTInvalidInput
1111
from pygmt.helpers import GMTTempFile
1212
from pygmt.helpers.testing import skip_if_no
@@ -426,12 +426,17 @@ def test_text_nonstr_text():
426426
return fig
427427

428428

429-
@pytest.mark.mpl_image_compare
430-
def test_text_nonascii():
429+
@pytest.mark.mpl_image_compare(filename="test_text_nonascii.png")
430+
@pytest.mark.parametrize("encoding", ["ISOLatin1+", "Standard+"])
431+
def test_text_nonascii(encoding):
431432
"""
432433
Test passing text strings with non-ascii characters.
434+
435+
Default PS_CHAR_ENCODING setting should not affect the result.
433436
"""
434437
fig = Figure()
438+
if encoding == "Standard+": # Temporarily set the PS_CHAR_ENCODING to "Standard+".
439+
config(PS_CHAR_ENCODING="Standard+")
435440
fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True)
436441
fig.text(position="TL", text="position-text:°α") # noqa: RUF001
437442
fig.text(x=1, y=1, text="xytext:°α") # noqa: RUF001

0 commit comments

Comments
 (0)