Skip to content

Commit 4615720

Browse files
committed
Merge pull request #4148 from mdboom/minus-sign
FIX : mathtext image bounding box
2 parents f529297 + 3e60527 commit 4615720

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

lib/matplotlib/mathtext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def _update_bbox(self, x1, y1, x2, y2):
178178
def set_canvas_size(self, w, h, d):
179179
MathtextBackend.set_canvas_size(self, w, h, d)
180180
if self.mode != 'bbox':
181-
self.image = FT2Image(ceil(w), ceil(h + d))
181+
self.image = FT2Image(ceil(w), ceil(h + max(d, 0)))
182182

183183
def render_glyph(self, ox, oy, info):
184184
if self.mode == 'bbox':

lib/matplotlib/tests/test_mathtext.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import six
55

6+
import io
7+
68
import numpy as np
79
import matplotlib
8-
from matplotlib.testing.decorators import image_comparison, knownfailureif
10+
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
911
import matplotlib.pyplot as plt
1012
from matplotlib import mathtext
1113

@@ -210,6 +212,22 @@ def test_mathtext_exceptions():
210212
else:
211213
assert False, "Expected '%s', but didn't get it" % msg
212214

215+
@cleanup
216+
def test_single_minus_sign():
217+
plt.figure(figsize=(0.3, 0.3))
218+
plt.text(0.5, 0.5, '$-$')
219+
for spine in plt.gca().spines.values():
220+
spine.set_visible(False)
221+
plt.gca().set_xticks([])
222+
plt.gca().set_yticks([])
223+
224+
buff = io.BytesIO()
225+
plt.savefig(buff, format="rgba", dpi=1000)
226+
array = np.fromstring(buff.getvalue(), dtype=np.uint8)
227+
228+
# If this fails, it would be all white
229+
assert not np.all(array == 0xff)
230+
213231

214232
if __name__ == '__main__':
215233
import nose

src/ft2font.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ FT2Image::~FT2Image()
5555

5656
void FT2Image::resize(long width, long height)
5757
{
58-
if (width < 0) {
58+
if (width <= 0) {
5959
width = 1;
6060
}
61-
if (height < 0) {
61+
if (height <= 0) {
6262
height = 1;
6363
}
6464
size_t numBytes = width * height;
@@ -146,11 +146,11 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1,
146146
{
147147
x0 = std::min(x0, m_width);
148148
y0 = std::min(y0, m_height);
149-
x1 = std::min(x1, m_width);
150-
y1 = std::min(y1, m_height);
149+
x1 = std::min(x1 + 1, m_width);
150+
y1 = std::min(y1 + 1, m_height);
151151

152-
for (size_t j = y0; j < y1 + 1; j++) {
153-
for (size_t i = x0; i < x1 + 1; i++) {
152+
for (size_t j = y0; j < y1; j++) {
153+
for (size_t i = x0; i < x1; i++) {
154154
m_buffer[i + j * m_width] = 255;
155155
}
156156
}

0 commit comments

Comments
 (0)