Add a CBitmap class. The C is for Courage.
This commit is contained in:
parent
e23ac56017
commit
c7463aad11
Notes:
sideshowbarker
2024-07-19 18:50:11 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/c7463aad115
8 changed files with 63 additions and 18 deletions
17
Widgets/CBitmap.cpp
Normal file
17
Widgets/CBitmap.cpp
Normal file
|
@ -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> CBitmap::createFromASCII(const char* asciiData, unsigned width, unsigned height)
|
||||
{
|
||||
return adopt(*new CBitmap(asciiData, width, height));
|
||||
}
|
||||
|
24
Widgets/CBitmap.h
Normal file
24
Widgets/CBitmap.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include "Size.h"
|
||||
#include <AK/Retainable.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
|
||||
class CBitmap : public Retainable<CBitmap> {
|
||||
public:
|
||||
static RetainPtr<CBitmap> 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;
|
||||
};
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include "CBitmap.h"
|
||||
#include <AK/Retainable.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
class Font : public Retainable<Font> {
|
||||
|
@ -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<CBitmap> m_bitmaps[256];
|
||||
|
||||
byte m_glyphWidth { 0 };
|
||||
byte m_glyphHeight { 0 };
|
||||
|
|
|
@ -23,6 +23,7 @@ VFS_OBJS = \
|
|||
Font.o \
|
||||
Window.o \
|
||||
ClockWidget.o \
|
||||
CBitmap.o \
|
||||
test.o
|
||||
|
||||
OBJS = $(AK_OBJS) $(VFS_OBJS)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "Size.h"
|
||||
#include <AK/String.h>
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -63,7 +63,6 @@ void Widget::onPaint(PaintEvent& event)
|
|||
|
||||
void Widget::onShow(ShowEvent&)
|
||||
{
|
||||
update();
|
||||
}
|
||||
|
||||
void Widget::onHide(HideEvent&)
|
||||
|
|
Loading…
Add table
Reference in a new issue