From c7463aad1159488fc6b9f7134dd88d0bf0b87b27 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 12 Oct 2018 12:49:45 +0200 Subject: [PATCH] Add a CBitmap class. The C is for Courage. --- Widgets/CBitmap.cpp | 17 +++++++++++++++++ Widgets/CBitmap.h | 24 ++++++++++++++++++++++++ Widgets/Font.cpp | 13 ++++++++----- Widgets/Font.h | 5 ++++- Widgets/Makefile | 1 + Widgets/Painter.cpp | 17 +++++++---------- Widgets/Painter.h | 3 ++- Widgets/Widget.cpp | 1 - 8 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 Widgets/CBitmap.cpp create mode 100644 Widgets/CBitmap.h diff --git a/Widgets/CBitmap.cpp b/Widgets/CBitmap.cpp new file mode 100644 index 0000000000..ee17e7bf05 --- /dev/null +++ b/Widgets/CBitmap.cpp @@ -0,0 +1,17 @@ +#include "CBitmap.h" + +CBitmap::CBitmap(const char* asciiData, unsigned width, unsigned height) + : m_bits(asciiData) + , m_size(width, height) +{ +} + +CBitmap::~CBitmap() +{ +} + +RetainPtr CBitmap::createFromASCII(const char* asciiData, unsigned width, unsigned height) +{ + return adopt(*new CBitmap(asciiData, width, height)); +} + diff --git a/Widgets/CBitmap.h b/Widgets/CBitmap.h new file mode 100644 index 0000000000..bb378fb2e5 --- /dev/null +++ b/Widgets/CBitmap.h @@ -0,0 +1,24 @@ +#pragma once + +#include "Size.h" +#include +#include + +class CBitmap : public Retainable { +public: + static RetainPtr createFromASCII(const char* asciiData, unsigned width, unsigned height); + ~CBitmap(); + + const char* bits() const { return m_bits; } + + Size size() const { return m_size; } + unsigned width() const { return m_size.width(); } + unsigned height() const { return m_size.height(); } + +private: + CBitmap(const char* b, unsigned w, unsigned h); + + const char* m_bits { nullptr }; + Size m_size; +}; + diff --git a/Widgets/Font.cpp b/Widgets/Font.cpp index 7fda3bff5c..8d69a284be 100644 --- a/Widgets/Font.cpp +++ b/Widgets/Font.cpp @@ -22,10 +22,13 @@ Font::~Font() { } -const char* Font::glyph(char ch) const +const CBitmap* Font::glyphBitmap(byte ch) const { - if (ch < m_firstGlyph || ch > m_lastGlyph) - return nullptr; - return m_glyphs[(unsigned)ch - m_firstGlyph]; + if (!m_bitmaps[ch]) { + if (ch < m_firstGlyph || ch > m_lastGlyph) + return nullptr; + const char* data = m_glyphs[(unsigned)ch - m_firstGlyph]; + m_bitmaps[ch] = CBitmap::createFromASCII(data, m_glyphWidth, m_glyphHeight); + } + return m_bitmaps[ch].ptr(); } - diff --git a/Widgets/Font.h b/Widgets/Font.h index 7429b3f021..6d62ccf979 100644 --- a/Widgets/Font.h +++ b/Widgets/Font.h @@ -1,6 +1,8 @@ #pragma once +#include "CBitmap.h" #include +#include #include class Font : public Retainable { @@ -9,7 +11,7 @@ public: ~Font(); - const char* glyph(char) const; + const CBitmap* glyphBitmap(byte) const; byte glyphWidth() const { return m_glyphWidth; } byte glyphHeight() const { return m_glyphHeight; } @@ -18,6 +20,7 @@ private: Font(const char* const* glyphs, byte glyphWidth, byte glyphHeight, byte firstGlyph, byte lastGlyph); const char* const* m_glyphs { nullptr }; + mutable RetainPtr m_bitmaps[256]; byte m_glyphWidth { 0 }; byte m_glyphHeight { 0 }; diff --git a/Widgets/Makefile b/Widgets/Makefile index 818de90d2d..2754956941 100644 --- a/Widgets/Makefile +++ b/Widgets/Makefile @@ -23,6 +23,7 @@ VFS_OBJS = \ Font.o \ Window.o \ ClockWidget.o \ + CBitmap.o \ test.o OBJS = $(AK_OBJS) $(VFS_OBJS) diff --git a/Widgets/Painter.cpp b/Widgets/Painter.cpp index 08c6389111..03cd713a7f 100644 --- a/Widgets/Painter.cpp +++ b/Widgets/Painter.cpp @@ -80,17 +80,14 @@ void Painter::xorRect(const Rect& rect, Color color) } } -void Painter::drawBitmap(const Point& point, const char* bitmap, const Size& bitmapSize, Color color) +void Painter::drawBitmap(const Point& point, const CBitmap& bitmap, Color color) { - ASSERT(bitmap); - ASSERT(!bitmapSize.isEmpty()); - - for (int row = 0; row < bitmapSize.height(); ++row) { + for (unsigned row = 0; row < bitmap.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]; + for (unsigned j = 0; j < bitmap.width(); ++j) { + char fc = bitmap.bits()[row * bitmap.width() + j]; if (fc == '#') bits[x + j] = color.value(); } @@ -117,14 +114,14 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align byte ch = text[i]; if (ch == ' ') continue; - const char* glyph = m_font.glyph(ch); - if (!glyph) { + auto* bitmap = m_font.glyphBitmap(ch); + if (!bitmap) { printf("Font doesn't have 0x%02x ('%c')\n", ch, ch); ASSERT_NOT_REACHED(); } int x = point.x() + i * m_font.glyphWidth(); int y = point.y(); - drawBitmap({ x, y }, glyph, { m_font.glyphWidth(), m_font.glyphHeight() }, color); + drawBitmap({ x, y }, *bitmap, color); } } diff --git a/Widgets/Painter.h b/Widgets/Painter.h index d7eeedce26..057f78c5a1 100644 --- a/Widgets/Painter.h +++ b/Widgets/Painter.h @@ -6,6 +6,7 @@ #include "Size.h" #include +class CBitmap; class Font; class Widget; @@ -17,7 +18,7 @@ public: void fillRect(const Rect&, Color); void drawRect(const Rect&, 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 drawBitmap(const Point&, const CBitmap&, Color = Color()); void xorRect(const Rect&, Color); diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp index da4fe8914c..8332abd92d 100644 --- a/Widgets/Widget.cpp +++ b/Widgets/Widget.cpp @@ -63,7 +63,6 @@ void Widget::onPaint(PaintEvent& event) void Widget::onShow(ShowEvent&) { - update(); } void Widget::onHide(HideEvent&)