Skip to content

Commit 05c9fb5

Browse files
authored
Merge pull request #14906 from anntzer/ft2image
Deprecate some FT2Image methods.
2 parents 39c2264 + 4b85a7b commit 05c9fb5

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Deprecations
2+
````````````
3+
4+
The ``as_str``, ``as_rgba_str``, ``as_array``, ``get_width`` and ``get_height``
5+
methods of ``matplotlib.ft2font.FT2Image`` are deprecated. Convert the ``FT2Image``
6+
to a numpy array with ``np.asarray`` before processing it.

examples/user_interfaces/mathtext_wx_sgskip.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626

2727

2828
def mathtext_to_wxbitmap(s):
29-
ftimage, depth = mathtext_parser.parse(s, 150)
30-
return wx.Bitmap.FromBufferRGBA(
31-
ftimage.get_width(), ftimage.get_height(),
32-
ftimage.as_rgba_str())
29+
rgba, depth = mathtext_parser.to_rgba(s, dpi=150, fontsize=10)
30+
return wx.Bitmap.FromBufferRGBA(rgba.shape[1], rgba.shape[0], rgba)
3331
############################################################
3432

3533
functions = [

lib/matplotlib/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def get_label_width(self, lev, fmt, fsize):
249249
self._mathtext_parser = mathtext.MathTextParser('bitmap')
250250
img, _ = self._mathtext_parser.parse(lev, dpi=72,
251251
prop=self.labelFontProps)
252-
lw = img.get_width() # at dpi=72, the units are PostScript points
252+
_, lw = np.shape(img) # at dpi=72, the units are PostScript points
253253
else:
254254
# width is much less than "font size"
255255
lw = len(lev) * fsize * 0.6

lib/matplotlib/mathtext.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,9 +3376,7 @@ def to_mask(self, texstr, dpi=120, fontsize=14):
33763376
assert self._output == "bitmap"
33773377
prop = FontProperties(size=fontsize)
33783378
ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
3379-
3380-
x = ftimage.as_array()
3381-
return x, depth
3379+
return np.asarray(ftimage), depth
33823380

33833381
def to_rgba(self, texstr, color='black', dpi=120, fontsize=14):
33843382
r"""

src/ft2font_wrapper.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,39 @@ static PyObject *PyFT2Image_draw_rect_filled(PyFT2Image *self, PyObject *args, P
103103
const char *PyFT2Image_as_str__doc__ =
104104
"s = image.as_str()\n"
105105
"\n"
106+
"[*Deprecated*]\n"
106107
"Return the image buffer as a string\n"
107108
"\n";
108109

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

116123
const char *PyFT2Image_as_rgba_str__doc__ =
117124
"s = image.as_rgba_str()\n"
118125
"\n"
126+
"[*Deprecated*]\n"
119127
"Return the image buffer as a RGBA string\n"
120128
"\n";
121129

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

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

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

153176
static PyObject *PyFT2Image_get_width(PyFT2Image *self, PyObject *args, PyObject *kwds)
154177
{
178+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
179+
"FT2Image.get_width is deprecated since Matplotlib 3.2 and "
180+
"will be removed in Matplotlib 3.4; convert the FT2Image "
181+
"to a NumPy array with np.asarray instead.",
182+
1)) {
183+
return NULL;
184+
}
155185
return PyLong_FromLong(self->x->get_width());
156186
}
157187

158188
static PyObject *PyFT2Image_get_height(PyFT2Image *self, PyObject *args, PyObject *kwds)
159189
{
190+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
191+
"FT2Image.get_height is deprecated since Matplotlib 3.2 and "
192+
"will be removed in Matplotlib 3.4; convert the FT2Image "
193+
"to a NumPy array with np.asarray instead.",
194+
1)) {
195+
return NULL;
196+
}
160197
return PyLong_FromLong(self->x->get_height());
161198
}
162199

0 commit comments

Comments
 (0)