Skip to content

Improve fallback font export tests #28784

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 3 commits into from
Jan 31, 2025
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
47 changes: 45 additions & 2 deletions lib/matplotlib/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""
Helper functions for testing.
"""
from pathlib import Path
from tempfile import TemporaryDirectory
import itertools
import locale
import logging
import os
from pathlib import Path
import string
import subprocess
import sys
from tempfile import TemporaryDirectory

import matplotlib as mpl
from matplotlib import _api
Expand Down Expand Up @@ -232,3 +234,44 @@ def is_ci_environment():
return True

return False


def _gen_multi_font_text():
"""
Generate text intended for use with multiple fonts to exercise font fallbacks.

Returns
-------
fonts : list of str
The names of the fonts used to render the test string, sorted by intended
priority. This should be set as the font family for the Figure or Text artist.
text : str
The test string.
"""
# These fonts are serif and sans-serif, and would not normally be combined, but that
# should make it easier to see which glyph is from which font.
fonts = ['cmr10', 'DejaVu Sans']
# cmr10 does not contain accented characters, so they should fall back to DejaVu
# Sans. However, some accented capital A versions *are* in cmr10 with non-standard
# glyph shapes, so don't test those (otherwise this Latin1 supplement group would
# start at 0xA0.)
start = 0xC5
latin1_supplement = [chr(x) for x in range(start, 0xFF+1)]
latin_extended_A = [chr(x) for x in range(0x100, 0x17F+1)]
latin_extended_B = [chr(x) for x in range(0x180, 0x24F+1)]
count = itertools.count(start - 0xA0)
non_basic_characters = '\n'.join(
''.join(line)
for _, line in itertools.groupby( # Replace with itertools.batched for Py3.12+.
[*latin1_supplement, *latin_extended_A, *latin_extended_B],
key=lambda x: next(count) // 32) # 32 characters per line.
)
test_str = f"""There are basic characters
{string.ascii_uppercase} {string.ascii_lowercase}
{string.digits} {string.punctuation}
and accented characters
{non_basic_characters}
in between!"""
# The resulting string contains 491 unique characters. Some file formats use 8-bit
# tables, which the large number of characters exercises twice over.
return fonts, test_str
1 change: 1 addition & 0 deletions lib/matplotlib/testing/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ def ipython_in_subprocess(
all_expected_backends: dict[tuple[int, int], str],
) -> None: ...
def is_ci_environment() -> bool: ...
def _gen_multi_font_text() -> tuple[list[str], str]: ...
2 changes: 1 addition & 1 deletion lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def convert(filename, cache):
_log.debug("For %s: converting to png.", filename)
convert = converter[path.suffix[1:]]
if path.suffix == ".svg":
contents = path.read_text()
contents = path.read_text(encoding="utf-8")
# NOTE: This check should be kept in sync with font styling in
# `lib/matplotlib/backends/backend_svg.py`. If it changes, then be sure to
# re-generate any SVG test files using this mode, or else such tests will
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Loading