8000 TST: Add tests for FT2Font drawing and path generation · matplotlib/matplotlib@06df253 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06df253

Browse files
committed
TST: Add tests for FT2Font drawing and path generation
1 parent 7c390de commit 06df253
< 8000 div class="prc-PageLayout-Pane-Vl5LI" data-resizable="true" style="--spacing:var(--spacing-normal);--pane-min-width:256px;--pane-max-width:calc(100vw - var(--pane-max-width-diff));--pane-width-size:var(--pane-width-medium);--pane-width:296px">

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

lib/matplotlib/tests/test_ft2font.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from matplotlib import ft2font
99
from matplotlib.testing.decorators import check_figures_equal
1010
import matplotlib.font_manager as fm
11+
import matplotlib.path as mpath
1112
import matplotlib.pyplot as plt
1213

1314

@@ -764,6 +765,65 @@ def test_ft2font_loading():
764765
assert font.get_bitmap_offset() == (0, 0)
765766

766767

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+
767827
@pytest.mark.parametrize('family_name, file_name',
768828
[("WenQuanYi Zen Hei", "wqy-zenhei.ttc"),
769829
("Noto Sans CJK JP", "NotoSansCJK.ttc"),

src/ft2font_wrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,9 @@ static PyObject *PyFT2Font_draw_glyphs_to_bitmap(PyFT2Font *self, PyObject *args
832832
const char *PyFT2Font_draw_glyph_to_bitmap__doc__ =
833833
"draw_glyph_to_bitmap(self, image, x, y, glyph, antialiased=True)\n"
834834
"--\n\n"
835-
"Draw a single glyph to the bitmap at pixel locations x, y\n"
836-
"Note it is your responsibility to set up the bitmap manually\n"
837-
"with ``set_bitmap_size(w, h)`` before this call is made.\n"
835+
"Draw a single glyph to *image* at pixel locations *x*, *y*\n"
836+
"Note it is your responsibility to create the image manually\n"
837+
"with the correct size before this call is made.\n"
838838
"\n"
839839
"If you want automatic layout, use `.set_text` in combinations with\n"
840840
"`.draw_glyphs_to_bitmap`. This function is instead intended for people\n"

0 commit comments

Comments
 (0)
0