Visitar URL original
Remove FT2Font._render_glyph and _PositionedBitmap by iccir · Pull Request #32412 · matplotlib/matplotlib · GitHub
Skip to content
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
25 changes: 0 additions & 25 deletions lib/matplotlib/tests/test_ft2font.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,31 +1077,6 @@ def test__layout():
assert Path(item.ft_object.fname).name == 'cmr10.ttf'


def test_render_glyph_cache():
# Reusing a cached outline must not change what is rendered.
ft = fm.get_font(fm.findfont('DejaVu Sans'))
ft.set_size(12, 100)
index = ft.get_char_index(ord('e'))
identity = [[0x10000, 0], [0, 0x10000]]

def render(delta=(0, 0)):
ft._set_transform(identity, list(delta))
return ft._render_glyph(index, ft2font.LoadFlags.DEFAULT,
ft2font.RenderMode.NORMAL)

first = render()
reference = first.buffer.copy()
# A whole-pixel shift reuses the outline and only moves the glyph.
shifted = render(delta=(0x40 * 3, 0x40 * 5))
assert np.array_equal(shifted.buffer, reference)
assert (shifted.left, shifted.top) == (first.left + 3, first.top + 5)
# A fractional shift must reach the rasterizer rather than be rounded away.
assert not np.array_equal(render(delta=(0x20, 0x20)).buffer, reference)
# The size is part of the key.
ft.set_size(24, 100)
assert render().buffer.shape != reference.shape


def test_layout_cache():
# A cached layout must match a fresh one, and the size is part of the key.
ft = fm.get_font(fm.findfont('DejaVu Sans'))
Expand Down
65 changes: 1 addition & 64 deletions src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,47 +179,6 @@ const char *PyFT2Image_draw_rect_filled__doc__ = R"""(
The bounds of the rectangle from (x0, y0) to (x1, y1).
)""";

/**********************************************************************
* Positioned Bitmap; owns the FT_Bitmap!
* */

struct PyPositionedBitmap {
FT_Library _ft2Library;
FT_Int left, top;
bool owning;
FT_Bitmap bitmap;

PyPositionedBitmap(FT_Library ft2Library, FT_GlyphSlot slot) :
_ft2Library{ft2Library}, left{slot->bitmap_left}, top{slot->bitmap_top}, owning{true}
{
FT_Bitmap_Init(&bitmap);
FT_CHECK(FT_Bitmap_Convert, _ft2Library, &slot->bitmap, &bitmap, 1);
}

PyPositionedBitmap(FT_Library ft2Library, FT_BitmapGlyph bg) :
_ft2Library{ft2Library}, left{bg->left}, top{bg->top}, owning{true}
{
FT_Bitmap_Init(&bitmap);
FT_CHECK(FT_Bitmap_Convert, _ft2Library, &bg->bitmap, &bitmap, 1);
}

PyPositionedBitmap(PyPositionedBitmap& other) = delete; // Non-copyable.

PyPositionedBitmap(PyPositionedBitmap&& other) :
_ft2Library{other._ft2Library}, left{other.left}, top{other.top}, owning{true},
bitmap{other.bitmap}
{
other.owning = false; // Prevent double deletion.
}

~PyPositionedBitmap()
{
if (owning) {
FT_Bitmap_Done(_ft2Library, &bitmap);
}
}
};

/**********************************************************************
* Glyph
* */
Expand Down Expand Up @@ -1658,17 +1617,6 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
return py::buffer_info(self.get_buffer(), shape, strides);
});

py::classh<PyPositionedBitmap>(m, "_PositionedBitmap", py::is_final())
.def_readonly("left", &PyPositionedBitmap::left)
.def_readonly("top", &PyPositionedBitmap::top)
.def_property_readonly(
"buffer", [](PyPositionedBitmap &self) -> py::array {
return {{self.bitmap.rows, self.bitmap.width},
{self.bitmap.pitch, 1},
self.bitmap.buffer};
})
;

py::classh<PyGlyph>(m, "Glyph", py::is_final(), PyGlyph__doc__)
.def(py::init<>([]() -> PyGlyph {
// Glyph is not useful from Python, so mark it as not constructible.
Expand Down Expand Up @@ -1897,18 +1845,7 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())

.def_buffer([](PyFT2Font &self) -> py::buffer_info {
return self.get_image().request();
})

.def("_render_glyph",
[ft2Library](PyFT2Font *self, FT_UInt idx, LoadFlags flags,
FT_Render_Mode render_mode)
{
auto glyph = self->render_glyph(
idx, static_cast<FT_Int32>(flags), render_mode);
return PyPositionedBitmap{
ft2Library, reinterpret_cast<FT_BitmapGlyph>(glyph.get())};
})
;
});

m.def("_render_glyph_run",
[ft2Library](py::sequence glyphs, double dpi, double x, double y, double angle,
Expand Down
Loading