Skip to content

Deprecate some FT2Image methods. #14906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2019
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
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/2019-07-27-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Deprecations
````````````

The ``as_str``, ``as_rgba_str``, ``as_array``, ``get_width`` and ``get_height``
methods of ``matplotlib.ft2font.FT2Image`` are deprecated. Convert the ``FT2Image``
to a numpy array with ``np.asarray`` before processing it.
6 changes: 2 additions & 4 deletions examples/user_interfaces/mathtext_wx_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@


def mathtext_to_wxbitmap(s):
ftimage, depth = mathtext_parser.parse(s, 150)
return wx.Bitmap.FromBufferRGBA(
ftimage.get_width(), ftimage.get_height(),
ftimage.as_rgba_str())
rgba, depth = mathtext_parser.to_rgba(s, dpi=150, fontsize=10)
return wx.Bitmap.FromBufferRGBA(rgba.shape[1], rgba.shape[0], rgba)
############################################################

functions = [
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def get_label_width(self, lev, fmt, fsize):
self._mathtext_parser = mathtext.MathTextParser('bitmap')
img, _ = self._mathtext_parser.parse(lev, dpi=72,
prop=self.labelFontProps)
lw = img.get_width() # at dpi=72, the units are PostScript points
_, lw = np.shape(img) # at dpi=72, the units are PostScript points
else:
# width is much less than "font size"
lw = len(lev) * fsize * 0.6
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3376,9 +3376,7 @@ def to_mask(self, texstr, dpi=120, fontsize=14):
assert self._output == "bitmap"
prop = FontProperties(size=fontsize)
ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)

x = ftimage.as_array()
return x, depth
return np.asarray(ftimage), depth

def to_rgba(self, texstr, color='black', dpi=120, fontsize=14):
r"""
Expand Down
39 changes: 38 additions & 1 deletion src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,39 @@ static PyObject *PyFT2Image_draw_rect_filled(PyFT2Image *self, PyObject *args, P
const char *PyFT2Image_as_str__doc__ =
"s = image.as_str()\n"
"\n"
"[*Deprecated*]\n"
"Return the image buffer as a string\n"
"\n";

static PyObject *PyFT2Image_as_str(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
// TODO: Use a buffer to avoid the copy
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"FT2Image.as_str is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
return PyBytes_FromStringAndSize((const char *)self->x->get_buffer(),
self->x->get_width() * self->x->get_height());
}

const char *PyFT2Image_as_rgba_str__doc__ =
"s = image.as_rgba_str()\n"
"\n"
"[*Deprecated*]\n"
"Return the image buffer as a RGBA string\n"
"\n";

static PyObject *PyFT2Image_as_rgba_str(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"FT2Image.as_rgba_str is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
npy_intp dims[] = {(npy_intp)self->x->get_height(), (npy_intp)self->x->get_width(), 4 };
numpy::array_view<unsigned char, 3> result(dims);

Expand All @@ -141,22 +156,44 @@ static PyObject *PyFT2Image_as_rgba_str(PyFT2Image *self, PyObject *args, PyObje
const char *PyFT2Image_as_array__doc__ =
"x = image.as_array()\n"
"\n"
"[*Deprecated*]\n"
"Return the image buffer as a width x height numpy array of ubyte \n"
"\n";

static PyObject *PyFT2Image_as_array(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"FT2Image.as_array is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
npy_intp dims[] = {(npy_intp)self->x->get_height(), (npy_intp)self->x->get_width() };
return PyArray_SimpleNewFromData(2, dims, NPY_UBYTE, self->x->get_buffer());
}

static PyObject *PyFT2Image_get_width(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"FT2Image.get_width is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
return PyLong_FromLong(self->x->get_width());
}

static PyObject *PyFT2Image_get_height(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"FT2Image.get_height is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
return PyLong_FromLong(self->x->get_height());
}

Expand Down