diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2020-05-02 13:16:18 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2020-05-02 13:16:18 +0200 |
commit | 8e5b1e0b171d0e4ff326a3ff1af162805d82a3d6 (patch) | |
tree | e7e48cbd9b3baabc7465ebb72f05f9a54fa595a4 | |
parent | 32878759a55f8b80d808a3647a5802984438d8c0 (diff) |
Add support for 90 dgr. rotated text rendering.
-rw-r--r-- | plugingui/painter.cc | 48 | ||||
-rw-r--r-- | plugingui/painter.h | 2 |
2 files changed, 46 insertions, 4 deletions
diff --git a/plugingui/painter.cc b/plugingui/painter.cc index bad5318..f2fc66a 100644 --- a/plugingui/painter.cc +++ b/plugingui/painter.cc @@ -175,11 +175,14 @@ void Painter::clear() } void Painter::drawText(int x0, int y0, const Font& font, - const std::string& text, bool nocolour) + const std::string& text, bool nocolour, bool rotate) { PixelBufferAlpha* textbuf = font.render(text); - y0 -= textbuf->height; // The y0 offset (baseline) is the bottom of the text. + if(!rotate) + { + y0 -= textbuf->height; // The y0 offset (baseline) is the bottom of the text. + } // If the text offset is outside the buffer; skip it. if((x0 > (int)pixbuf.width) || (y0 > (int)pixbuf.height)) @@ -222,6 +225,42 @@ void Painter::drawText(int x0, int y0, const Font& font, pixbuf.blendLine(x + x0, y + y0, c, renderWidth - x); } } + else if(rotate) + { + int renderWidth = textbuf->height; + if(renderWidth > (int)(pixbuf.width - x0)) + { + renderWidth = pixbuf.width - x0; + } + + int renderHeight = textbuf->width; + if(renderHeight > ((int)pixbuf.height - y0)) + { + renderHeight = ((int)pixbuf.height - y0); + } + + for(int y = -1 * std::min(0, y0); y < renderHeight; ++y) + { + for(int x = -1 * std::min(0, x0); x < renderWidth; ++x) + { + assert(x >= 0); + assert(y >= 0); + assert(x < (int)textbuf->height); + assert(y < (int)textbuf->width); + + auto c = textbuf->pixel(textbuf->width - y - 1, x); + + assert(x + x0 >= 0); + assert(y + y0 >= 0); + assert(x + x0 < (int)pixbuf.width); + assert(y + y0 < (int)pixbuf.height); + + Colour col(colour.red(), colour.green(), + colour.blue(), (int)(colour.alpha() * c.alpha()) / 255); + pixbuf.addPixel(x + x0, y + y0, col); + } + } + } else { for(int y = -1 * std::min(0, y0); y < renderHeight; ++y) @@ -252,7 +291,10 @@ void Painter::drawText(int x0, int y0, const Font& font, void Painter::drawPoint(int x, int y) { - pixbuf.setPixel(x, y, colour); + if(x >= 0 && y >= 0 && (std::size_t)x < pixbuf.width && (std::size_t)y < pixbuf.height) + { + pixbuf.setPixel(x, y, colour); + } } static void plot4points(Painter *p, int cx, int cy, int x, int y) diff --git a/plugingui/painter.h b/plugingui/painter.h index b38bf88..9bf7fbf 100644 --- a/plugingui/painter.h +++ b/plugingui/painter.h @@ -49,7 +49,7 @@ public: void drawLine(int x1, int y1, int x2, int y2); void drawText(int x, int y, const Font& font, const std::string& text, - bool nocolour = false); + bool nocolour = false, bool rotate = false); void drawRectangle(int x1, int y1, int x2, int y2); void drawFilledRectangle(int x1, int y1, int x2, int y2); void drawPoint(int x, int y); |