From 4fa3d8358e5404e90da5a85096ab06e2210f550f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 13 Mar 2024 20:08:24 +0800 Subject: [PATCH] =?UTF-8?q?Better=20support=20for=20single=20quotation=20a?= =?UTF-8?q?nd=20double=20quotation=20marks=20('"=E2=80=98=E2=80=99?= =?UTF-8?q?=E2=80=9C=E2=80=9D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pygmt/helpers/utils.py | 11 +++++++++-- pygmt/tests/baseline/test_text_nonascii.png.dvc | 4 ++-- pygmt/tests/test_text.py | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 75514d2077d..6de3fb465e0 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -226,6 +226,8 @@ def non_ascii_to_octal(argstr): '@%34%\\41@%%@%34%\\176@%%@%34%\\241@%%@%34%\\376@%%' >>> non_ascii_to_octal("ABC ±120° DEF α ♥") 'ABC \\261120\\260 DEF @~\\141@~ @%34%\\252@%%' + >>> non_ascii_to_octal("'‘’\"“”") + '\\234\\140\\47\\042\\216\\217' """ # noqa: RUF002 # Dictionary mapping non-ASCII characters to octal codes mapping = {} @@ -299,10 +301,11 @@ def non_ascii_to_octal(argstr): c: "\\" + format(i, "o") for c, i in zip( "•…™—–fiž" # \03x. \030 is undefined + "’‘" # \047 and \140 "š" # \177 "Œ†‡Ł⁄‹Š›œŸŽł‰„“”" # \20x-\21x "ı`´ˆ˜¯˘˙¨‚˚¸'˝˛ˇ", # \22x-\23x - [*range(25, 32), *range(127, 160)], + [*range(25, 32), 39, 96, *range(127, 160)], strict=True, ) } @@ -310,8 +313,12 @@ def non_ascii_to_octal(argstr): # \240-\377 mapping.update({chr(i): "\\" + format(i, "o") for i in range(160, 256)}) - # Remove any printable characters + # Remove any printable characters. mapping = {k: v for k, v in mapping.items() if k not in string.printable} + # Mapping single quote ' and double quote " to octal codes because they often cause + # troubles. + mapping['"'] = "\\042" + mapping["'"] = "\\234" # Not \047 return argstr.translate(str.maketrans(mapping)) diff --git a/pygmt/tests/baseline/test_text_nonascii.png.dvc b/pygmt/tests/baseline/test_text_nonascii.png.dvc index 4e5e7d2187f..d50ffd62da4 100644 --- a/pygmt/tests/baseline/test_text_nonascii.png.dvc +++ b/pygmt/tests/baseline/test_text_nonascii.png.dvc @@ -1,5 +1,5 @@ outs: -- md5: 5442ca4a23e43b9f3e4d5794ccb5448a - size: 17286 +- md5: 41e76a73a9eb2e443fed0acc81010305 + size: 17595 hash: md5 path: test_text_nonascii.png diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 7ef50224c38..ad4a6da2850 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -416,7 +416,7 @@ def test_text_nonascii(): """ fig = Figure() fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True) - fig.text(position="TL", text="position-text:°α") # noqa: RUF001 + fig.text(position="TL", text="position-text:°α'‘’\"“”", offset="j0.2c") # noqa: RUF001 fig.text(x=1, y=1, text="xytext:°α") # noqa: RUF001 fig.text(x=[5, 5], y=[3, 5], text=["xytext1:αζΔ❡", "xytext2:∑π∇✉"]) return fig