|
8 | 8 | from matplotlib import ft2font
|
9 | 9 | from matplotlib.testing.decorators import check_figures_equal
|
10 | 10 | import matplotlib.font_manager as fm
|
| 11 | +import matplotlib.path as mpath |
11 | 12 | import matplotlib.pyplot as plt
|
12 | 13 |
|
13 | 14 |
|
@@ -764,6 +765,65 @@ def test_ft2font_loading():
|
764 | 765 | assert font.get_bitmap_offset() == (0, 0)
|
765 | 766 |
|
766 | 767 |
|
| 768 | +def test_ft2font_drawing(): |
| 769 | + expected_str = ( |
| 770 | + ' ', |
| 771 | + '11 11 ', |
| 772 | + '11 11 ', |
| 773 | + '1 1 1 1 ', |
| 774 | + '1 1 1 1 ', |
| 775 | + '1 1 1 1 ', |
| 776 | + '1 11 1 ', |
| 777 | + '1 11 1 ', |
| 778 | + '1 1 ', |
| 779 | + '1 1 ', |
| 780 | + ' ', |
| 781 | + ) |
| 782 | + expected = np.array([ |
| 783 | + [int(c) for c in line.replace(' ', '0')] for line in expected_str |
| 784 | + ]) |
| 785 | + expected *= 255 |
| 786 | + file = fm.findfont('DejaVu Sans') |
| 787 | + font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0) |
| 788 | + font.set_text('M') |
| 789 | + font.draw_glyphs_to_bitmap(antialiased=False) |
| 790 | + image = font.get_image() |
| 791 | + np.testing.assert_array_equal(image, expected) |
| 792 | + font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0) |
| 793 | + glyph = font.load_char(ord('M')) |
| 794 | + image = ft2font.FT2Image(expected.shape[1], expected.shape[0]) |
| 795 | + font.draw_glyph_to_bitmap(image, -1, 1, glyph, antialiased=False) |
| 796 | + np.testing.assert_array_equal(image, expected) |
| 797 | + |
| 798 | + |
| 799 | +def test_ft2font_get_path(): |
| 800 | + file = fm.findfont('DejaVu Sans') |
| 801 | + font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0) |
| 802 | + vertices, codes = font.get_path() |
| 803 | + assert vertices.shape == (0, 2) |
| 804 | + assert codes.shape == (0, ) |
| 805 | + font.load_char(ord('M')) |
| 806 | + vertices, codes = font.get_path() |
| 807 | + expected_vertices = np.array([ |
| 808 | + (0.843750, 9.000000), (2.609375, 9.000000), # Top left. |
| 809 | + (4.906250, 2.875000), # Top of midpoint. |
| 810 | + (7.218750, 9.000000), (8.968750, 9.000000), # Top right. |
| 811 | + (8.968750, 0.000000), (7.843750, 0.000000), # Bottom right. |
| 812 | + (7.843750, 7.906250), # Point under top right. |
| 813 | + (5.531250, 1.734375), (4.296875, 1.734375), # Bar under midpoint. |
| 814 | + (1.984375, 7.906250), # Point under top left. |
| 815 | + (1.984375, 0.000000), (0.843750, 0.000000), # Bottom left. |
| 816 | + (0.843750, 9.000000), # Back to top left corner. |
| 817 | + (0.000000, 0.000000), |
| 818 | + ]) |
| 819 | + np.testing.assert_array_equal(vertices, expected_vertices) |
| 820 | + expected_codes = np.full(expected_vertices.shape[0], mpath.Path.LINETO, |
| 821 | + dtype=mpath.Path.code_type) |
| 822 | + expected_codes[0] = mpath.Path.MOVETO |
| 823 | + expected_codes[-1] = mpath.Path.CLOSEPOLY |
| 824 | + np.testing.assert_array_equal(codes, expected_codes) |
| 825 | + |
| 826 | + |
767 | 827 | @pytest.mark.parametrize('family_name, file_name',
|
768 | 828 | [("WenQuanYi Zen Hei", "wqy-zenhei.ttc"),
|
769 | 829 | ("Noto Sans CJK JP", "NotoSansCJK.ttc"),
|
|
0 commit comments