Skip to content
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

seisman
Copy link
Member

@seisman seisman commented Nov 12, 2024

Description of proposed changes

Non-ASCII characters may not be typeset correctly if the default PS_CHAR_ENCODING setting is not ISOLatin1+. This can happen when (1) GMT is compiled with US units as default; (2) users have a global gmt.conf file with PS_CHAR_ENCODING set to Standard+; (3) users set PS_CHAR_ENCODING to Standard+ in the middle of a script.

Taking the gallery example at https://www.pygmt.org/dev/tutorials/advanced/non_ascii_text.html as an example.

If you have a gmt.conf file with PS_CHAR_ENCODING set to Standard+ (e.g., you can create a gmt.conf file by running GMT command gmt set PS_CHAR_ENCODING=Standard+ in terminal), running the gallery example will result in the incorrect figure shown in the left column below:

PS_CHAR_ENCODING=Standard+ PS_CHAR_ENCODING=ISOLatin1+
Standard+ image

Previously, we assumed the default PS_CHAR_ENCODING is ISOLatin1+ and didn't add --PS_CHAR_ENCODING=ISOLatin1+ if the text string contains non-ASCII characters in the ISOLatin1+ charset. This PR fixes the issue by always adding --PS_CHAR_ENCODING=<encoding> even if encoding is ISOLatin1+.

In 8c34683, the existing test test_text_nonascii is modified to show that setting PS_CHAR_ENCODING to Standard+ breaks the test (https://github.com/GenericMappingTools/pygmt/actions/runs/11800822308/job/32872646796?pr=3611). The fix is added in d9e5203.

Closes #2204.

@seisman seisman added the bug Something isn't working label Nov 12, 2024
@seisman seisman added enhancement Improving an existing feature and removed bug Something isn't working labels Nov 12, 2024
@seisman seisman added this to the 0.14.0 milestone Nov 12, 2024
@seisman seisman added the needs review This PR has higher priority and needs review. label Nov 12, 2024
@seisman seisman marked this pull request as ready for review November 12, 2024 15:54
"""
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature needs review This PR has higher priority and needs review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support non-ASCII characters in PyGMT arguments and text in Figure.text
1 participant