From ea4435f8ca32639ad2c516cd89123b8e88b666bc Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 5 Sep 2016 11:45:29 +0200 Subject: Added painter unit test and fixed image and text rendering boundaries. --- plugingui/painter.cc | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'plugingui') diff --git a/plugingui/painter.cc b/plugingui/painter.cc index db67a0e..3c290f3 100644 --- a/plugingui/painter.cc +++ b/plugingui/painter.cc @@ -27,6 +27,7 @@ #include "painter.h" #include +#include namespace GUI { @@ -203,24 +204,48 @@ void Painter::drawText(int x0, int y0, const Font& font, if(nocolour) { - for(int y = 0; y < renderHeight; ++y) + for(int y = -1 * std::min(0, y0); y < renderHeight; ++y) { - for(int x = 0; x < renderWidth; ++x) + for(int x = -1 * std::min(0, x0); x < renderWidth; ++x) { unsigned char r, g, b, a; + + assert(x >= 0); + assert(y >= 0); + assert(x < (int)textbuf->width); + assert(y < (int)textbuf->height); + textbuf->pixel(x, y, &r, &g, &b, &a); + + assert(x + x0 >= 0); + assert(y + y0 >= 0); + assert(x + x0 < (int)pixbuf.width); + assert(y + y0 < (int)pixbuf.height); + pixbuf.addPixel(x + x0, y + y0, r, g, b, a); } } } else { - for(int y = 0; y < renderHeight; ++y) + for(int y = -1 * std::min(0, y0); y < renderHeight; ++y) { - for(int x = 0; x < renderWidth; ++x) + for(int x = -1 * std::min(0, x0); x < renderWidth; ++x) { unsigned char r,g,b,a; + + assert(x >= 0); + assert(y >= 0); + assert(x < (int)textbuf->width); + assert(y < (int)textbuf->height); + textbuf->pixel(x, y, &r, &g, &b, &a); + + assert(x + x0 >= 0); + assert(y + y0 >= 0); + assert(x + x0 < (int)pixbuf.width); + assert(y + y0 < (int)pixbuf.height); + pixbuf.addPixel(x + x0, y + y0, colour.red() * 255, colour.green() * 255, @@ -361,7 +386,16 @@ void Painter::drawImage(int x0, int y0, const Drawable& image) { for(std::size_t x = -1 * std::min(0, x0); x < (std::size_t)fw; ++x) { + assert(x >= 0); + assert(y >= 0); + assert(x < image.width()); + assert(y < image.height()); auto& c = image.getPixel(x, y); + + assert(x0 + x >= 0); + assert(y0 + y >= 0); + assert(x0 + x < pixbuf.width); + assert(y0 + y < pixbuf.height); pixbuf.addPixel(x0 + x, y0 + y, c); } } -- cgit v1.2.3