Skip to content

Ensure non-ASCII characters are typeset correctly even if PS_CHAR_ENCODING is not 'ISOLatin1+' #3611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 2 additions & 5 deletions pygmt/src/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
11 changes: 8 additions & 3 deletions pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+".
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should test the case that PS_CHAR_ENCODING is set to "Standard+" in a global "gmt.conf" file. However, it's technically difficult. Similar to the test test_gmt_compat_6_is_applied at

def test_gmt_compat_6_is_applied(capsys):

Generally we need to:

  1. Generate a gmt.conf file in the current directory
  2. Kill the global session
  3. Start a new session
  4. Create a new figure and adding non-ASCII characters to it
  5. End the session so the "gmt.conf" file won't affect other tests
  6. Start a new, clean global session

But the issue is that, after starting a new global session, we can't return the Figure instance for comparing images, because mpl_image_compare needs to call Figure.savefig which no longer works after the session is destroied.

So here we only test the case that PS_CHAR_ENCODING is changed in the middle of a script.

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
Expand Down
Loading