From 222021b0de0ab6c7db10dcf827c86bbbd0b318bd Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 23 Feb 2015 16:40:00 -0500 Subject: [PATCH 1/4] Keep depth positive when calculating mathtext bounding box --- lib/matplotlib/mathtext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/mathtext.py b/lib/matplotlib/mathtext.py index e58713a2f8d9..9f975bda9312 100644 --- a/lib/matplotlib/mathtext.py +++ b/lib/matplotlib/mathtext.py @@ -178,7 +178,7 @@ def _update_bbox(self, x1, y1, x2, y2): def set_canvas_size(self, w, h, d): MathtextBackend.set_canvas_size(self, w, h, d) if self.mode != 'bbox': - self.image = FT2Image(ceil(w), ceil(h + d)) + self.image = FT2Image(ceil(w), ceil(h + max(d, 0))) def render_glyph(self, ox, oy, info): if self.mode == 'bbox': From 08e63fe02b03dc456b29365f2ddcc6ca10735f0d Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 23 Feb 2015 16:40:27 -0500 Subject: [PATCH 2/4] Don't create 0-length text buffers --- src/ft2font.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ft2font.cpp b/src/ft2font.cpp index 4bb494c0bc1e..a2d572cb03ac 100644 --- a/src/ft2font.cpp +++ b/src/ft2font.cpp @@ -55,10 +55,10 @@ FT2Image::~FT2Image() void FT2Image::resize(long width, long height) { - if (width < 0) { + if (width <= 0) { width = 1; } - if (height < 0) { + if (height <= 0) { height = 1; } size_t numBytes = width * height; From 4871e1f3c7f1f02a18b3799f0818cc199dfaf3a6 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 23 Feb 2015 16:40:39 -0500 Subject: [PATCH 3/4] Don't draw filled rectangles outside of text image --- src/ft2font.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ft2font.cpp b/src/ft2font.cpp index a2d572cb03ac..a02b105777e5 100644 --- a/src/ft2font.cpp +++ b/src/ft2font.cpp @@ -146,11 +146,11 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1, { x0 = std::min(x0, m_width); y0 = std::min(y0, m_height); - x1 = std::min(x1, m_width); - y1 = std::min(y1, m_height); + x1 = std::min(x1 + 1, m_width); + y1 = std::min(y1 + 1, m_height); - for (size_t j = y0; j < y1 + 1; j++) { - for (size_t i = x0; i < x1 + 1; i++) { + for (size_t j = y0; j < y1; j++) { + for (size_t i = x0; i < x1; i++) { m_buffer[i + j * m_width] = 255; } } From 3e60527a4cc64c5d906564dd54489a701da919e1 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 23 Feb 2015 16:40:51 -0500 Subject: [PATCH 4/4] Test based on #4147 --- lib/matplotlib/tests/test_mathtext.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index da3135c61786..365f1a3d456c 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -3,9 +3,11 @@ import six +import io + import numpy as np import matplotlib -from matplotlib.testing.decorators import image_comparison, knownfailureif +from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup import matplotlib.pyplot as plt from matplotlib import mathtext @@ -210,6 +212,22 @@ def test_mathtext_exceptions(): else: assert False, "Expected '%s', but didn't get it" % msg +@cleanup +def test_single_minus_sign(): + plt.figure(figsize=(0.3, 0.3)) + plt.text(0.5, 0.5, '$-$') + for spine in plt.gca().spines.values(): + spine.set_visible(False) + plt.gca().set_xticks([]) + plt.gca().set_yticks([]) + + buff = io.BytesIO() + plt.savefig(buff, format="rgba", dpi=1000) + array = np.fromstring(buff.getvalue(), dtype=np.uint8) + + # If this fails, it would be all white + assert not np.all(array == 0xff) + if __name__ == '__main__': import nose