| 597 | ]) |
| 598 | @pytest.mark.parametrize("include_generic", [True, False]) |
| 599 | def test_svg_font_string(font_str, include_generic): |
| 600 | fp = fm.FontProperties(family=["WenQuanYi Zen Hei"]) |
| 601 | if Path(fm.findfont(fp)).name != "wqy-zenhei.ttc": |
| 602 | pytest.skip("Font may be missing") |
| 603 | |
| 604 | explicit, *rest, generic = map( |
| 605 | lambda x: x.strip("'"), font_str.split(", ") |
| 606 | ) |
| 607 | size = len(generic) |
| 608 | if include_generic: |
| 609 | rest = rest + [generic] |
| 610 | plt.rcParams[f"font.{generic}"] = rest |
| 611 | plt.rcParams["font.size"] = size |
| 612 | plt.rcParams["svg.fonttype"] = "none" |
| 613 | |
| 614 | fig, ax = plt.subplots() |
| 615 | if generic == "sans-serif": |
| 616 | generic_options = ["sans", "sans-serif", "sans serif"] |
| 617 | else: |
| 618 | generic_options = [generic] |
| 619 | |
| 620 | for generic_name in generic_options: |
| 621 | # test that fallback works |
| 622 | ax.text(0.5, 0.5, "There are 几个汉字 in between!", |
| 623 | family=[explicit, generic_name], ha="center") |
| 624 | # test deduplication works |
| 625 | ax.text(0.5, 0.1, "There are 几个汉字 in between!", |
| 626 | family=[explicit, *rest, generic_name], ha="center") |
| 627 | ax.axis("off") |
| 628 | |
| 629 | with BytesIO() as fd: |
| 630 | fig.savefig(fd, format="svg") |
| 631 | buf = fd.getvalue() |
| 632 | |
| 633 | tree = xml.etree.ElementTree.fromstring(buf) |
| 634 | ns = "http://www.w3.org/2000/svg" |
| 635 | text_count = 0 |
| 636 | for text_element in tree.findall(f".//{{{ns}}}text"): |
| 637 | text_count += 1 |
| 638 | font_style = dict( |
| 639 | map(lambda x: x.strip(), _.strip().split(":")) |
| 640 | for _ in dict(text_element.items())["style"].split(";") |
| 641 | ) |
| 642 | |
| 643 | assert font_style["font-size"] == f"{size}px" |
| 644 | assert font_style["font-family"] == font_str |
| 645 | assert text_count == len(ax.texts) |
| 646 | |
| 647 | |
| 648 | def test_annotationbbox_gid(): |