Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 833a259

Browse files
committed
Fix return value of FT2Font.set_text and add tests for it
`PyArray_SimpleNewFromData` does not copy its input data, and the `std::vector` is a local variable that disappears after the C++ method returns.
1 parent 3748c99 commit 833a259

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/matplotlib/tests/test_ft2font.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,27 @@ def test_ft2font_get_kerning(left, right, unscaled, unfitted, default):
706706
ft2font.KERNING_DEFAULT) == default
707707

708708

709+
def test_ft2font_set_text():
710+
file = fm.findfont('DejaVu Sans')
711+
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
712+
xys = font.set_text('')
713+
np.testing.assert_array_equal(xys, np.empty((0, 2)))
714+
assert font.get_width_height() == (0, 0)
715+
assert font.get_num_glyphs() == 0
716+
assert font.get_descent() == 0
717+
assert font.get_bitmap_offset() == (0, 0)
718+
# This string uses all the kerning pairs defined for test_ft2font_get_kerning.
719+
xys = font.set_text('AADAT.XC-J')
720+
np.testing.assert_array_equal(
721+
xys,
722+
[(0, 0), (512, 0), (1024, 0), (1600, 0), (2112, 0), (2496, 0), (2688, 0),
723+
(3200, 0), (3712, 0), (4032, 0)])
724+
assert font.get_width_height() == (4288, 768)
725+
assert font.get_num_glyphs() == 10
726+
assert font.get_descent() == 192
727+
assert font.get_bitmap_offset() == (6, 0)
728+
729+
709730
@pytest.mark.parametrize('family_name, file_name',
710731
[("WenQuanYi Zen Hei", "wqy-zenhei.ttc"),
711732
("Noto Sans CJK JP", "NotoSansCJK.ttc"),

src/ft2font_wrapper.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ static PyObject *convert_xys_to_array(std::vector<double> &xys)
1414
{
1515
npy_intp dims[] = {(npy_intp)xys.size() / 2, 2 };
1616
if (dims[0] > 0) {
17-
return PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, &xys[0]);
17+
auto obj = PyArray_SimpleNew(2, dims, NPY_DOUBLE);
18+
auto array = reinterpret_cast<PyArrayObject *>(obj);
19+
memcpy(PyArray_DATA(array), xys.data(), PyArray_NBYTES(array));
20+
return obj;
1821
} else {
1922
return PyArray_SimpleNew(2, dims, NPY_DOUBLE);
2023
}
@@ -631,7 +634,7 @@ const char *PyFT2Font_set_text__doc__ =
631634
"*flags* can be a bitwise-or of the LOAD_XXX constants;\n"
632635
"the default value is LOAD_FORCE_AUTOHINT.\n"
633636
"You must call this before `.draw_glyphs_to_bitmap`.\n"
634-
"A sequence of x,y positions is returned.\n";
637+
"A sequence of x,y positions in 26.6 subpixels is returned; divide by 64 for pixels.\n";
635638

636639
static PyObject *PyFT2Font_set_text(PyFT2Font *self, PyObject *args, PyObject *kwds)
637640
{

0 commit comments

Comments
 (0)