1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:07:36 +00:00

Add a Painter::drawBitmap() and make Painter::drawText() use it.

This commit is contained in:
Andreas Kling 2018-10-12 12:29:58 +02:00
parent 73895ce043
commit e23ac56017
4 changed files with 51 additions and 20 deletions

Binary file not shown.

View file

@ -80,8 +80,24 @@ void Painter::xorRect(const Rect& rect, Color color)
} }
} }
void Painter::drawBitmap(const Point& point, const char* bitmap, const Size& bitmapSize, Color color)
{
ASSERT(bitmap);
ASSERT(!bitmapSize.isEmpty());
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, const Color& color) for (int row = 0; row < bitmapSize.height(); ++row) {
int y = point.y() + row;
int x = point.x();
dword* bits = scanline(y);
for (int j = 0; j < bitmapSize.width(); ++j) {
char fc = bitmap[row * bitmapSize.width() + j];
if (fc == '#')
bits[x + j] = color.value();
}
}
}
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, Color color)
{ {
Point point; Point point;
@ -97,9 +113,6 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
for (int row = 0; row < m_font.glyphHeight(); ++row) {
int y = point.y() + row;
dword* bits = scanline(y);
for (unsigned i = 0; i < text.length(); ++i) { for (unsigned i = 0; i < text.length(); ++i) {
byte ch = text[i]; byte ch = text[i];
if (ch == ' ') if (ch == ' ')
@ -110,12 +123,8 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
int x = point.x() + i * m_font.glyphWidth(); int x = point.x() + i * m_font.glyphWidth();
for (int j = 0; j < m_font.glyphWidth(); ++j) { int y = point.y();
char fc = glyph[row * m_font.glyphWidth() + j]; drawBitmap({ x, y }, glyph, { m_font.glyphWidth(), m_font.glyphHeight() }, color);
if (fc == '#')
bits[x + j] = color.value();
}
}
} }
} }

View file

@ -3,6 +3,7 @@
#include "Color.h" #include "Color.h"
#include "Point.h" #include "Point.h"
#include "Rect.h" #include "Rect.h"
#include "Size.h"
#include <AK/String.h> #include <AK/String.h>
class Font; class Font;
@ -15,7 +16,8 @@ public:
~Painter(); ~Painter();
void fillRect(const Rect&, Color); void fillRect(const Rect&, Color);
void drawRect(const Rect&, Color); void drawRect(const Rect&, Color);
void drawText(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, const Color& = Color()); void drawText(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
void drawBitmap(const Point&, const char* bitmap, const Size& bitmapSize, Color = Color());
void xorRect(const Rect&, Color); void xorRect(const Rect&, Color);

20
Widgets/Size.h Normal file
View file

@ -0,0 +1,20 @@
#pragma once
class Size {
public:
Size() { }
Size(int w, int h) : m_width(w), m_height(h) { }
bool isEmpty() const { return !m_width || !m_height; }
int width() const { return m_width; }
int height() const { return m_height; }
void setWidth(int w) { m_width = w; }
void setHeight(int h) { m_height = h; }
private:
int m_width { 0 };
int m_height { 0 };
};