mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-22 02:26:11 +00:00
Add a CBitmap class. The C is for Courage.
This commit is contained in:
parent
e23ac56017
commit
c7463aad11
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)
|
if (!m_bitmaps[ch]) {
|
||||||
return nullptr;
|
if (ch < m_firstGlyph || ch > m_lastGlyph)
|
||||||
return m_glyphs[(unsigned)ch - m_firstGlyph];
|
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
|
#pragma once
|
||||||
|
|
||||||
|
#include "CBitmap.h"
|
||||||
#include <AK/Retainable.h>
|
#include <AK/Retainable.h>
|
||||||
|
#include <AK/RetainPtr.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
|
||||||
class Font : public Retainable<Font> {
|
class Font : public Retainable<Font> {
|
||||||
|
@ -9,7 +11,7 @@ public:
|
||||||
|
|
||||||
~Font();
|
~Font();
|
||||||
|
|
||||||
const char* glyph(char) const;
|
const CBitmap* glyphBitmap(byte) const;
|
||||||
|
|
||||||
byte glyphWidth() const { return m_glyphWidth; }
|
byte glyphWidth() const { return m_glyphWidth; }
|
||||||
byte glyphHeight() const { return m_glyphHeight; }
|
byte glyphHeight() const { return m_glyphHeight; }
|
||||||
|
@ -18,6 +20,7 @@ private:
|
||||||
Font(const char* const* glyphs, byte glyphWidth, byte glyphHeight, byte firstGlyph, byte lastGlyph);
|
Font(const char* const* glyphs, byte glyphWidth, byte glyphHeight, byte firstGlyph, byte lastGlyph);
|
||||||
|
|
||||||
const char* const* m_glyphs { nullptr };
|
const char* const* m_glyphs { nullptr };
|
||||||
|
mutable RetainPtr<CBitmap> m_bitmaps[256];
|
||||||
|
|
||||||
byte m_glyphWidth { 0 };
|
byte m_glyphWidth { 0 };
|
||||||
byte m_glyphHeight { 0 };
|
byte m_glyphHeight { 0 };
|
||||||
|
|
|
@ -23,6 +23,7 @@ VFS_OBJS = \
|
||||||
Font.o \
|
Font.o \
|
||||||
Window.o \
|
Window.o \
|
||||||
ClockWidget.o \
|
ClockWidget.o \
|
||||||
|
CBitmap.o \
|
||||||
test.o
|
test.o
|
||||||
|
|
||||||
OBJS = $(AK_OBJS) $(VFS_OBJS)
|
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);
|
for (unsigned row = 0; row < bitmap.height(); ++row) {
|
||||||
ASSERT(!bitmapSize.isEmpty());
|
|
||||||
|
|
||||||
for (int row = 0; row < bitmapSize.height(); ++row) {
|
|
||||||
int y = point.y() + row;
|
int y = point.y() + row;
|
||||||
int x = point.x();
|
int x = point.x();
|
||||||
dword* bits = scanline(y);
|
dword* bits = scanline(y);
|
||||||
for (int j = 0; j < bitmapSize.width(); ++j) {
|
for (unsigned j = 0; j < bitmap.width(); ++j) {
|
||||||
char fc = bitmap[row * bitmapSize.width() + j];
|
char fc = bitmap.bits()[row * bitmap.width() + j];
|
||||||
if (fc == '#')
|
if (fc == '#')
|
||||||
bits[x + j] = color.value();
|
bits[x + j] = color.value();
|
||||||
}
|
}
|
||||||
|
@ -117,14 +114,14 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
|
||||||
byte ch = text[i];
|
byte ch = text[i];
|
||||||
if (ch == ' ')
|
if (ch == ' ')
|
||||||
continue;
|
continue;
|
||||||
const char* glyph = m_font.glyph(ch);
|
auto* bitmap = m_font.glyphBitmap(ch);
|
||||||
if (!glyph) {
|
if (!bitmap) {
|
||||||
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
int x = point.x() + i * m_font.glyphWidth();
|
int x = point.x() + i * m_font.glyphWidth();
|
||||||
int y = point.y();
|
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 "Size.h"
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
|
||||||
|
class CBitmap;
|
||||||
class Font;
|
class Font;
|
||||||
class Widget;
|
class Widget;
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ public:
|
||||||
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, 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 drawBitmap(const Point&, const CBitmap&, Color = Color());
|
||||||
|
|
||||||
void xorRect(const Rect&, Color);
|
void xorRect(const Rect&, Color);
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,6 @@ void Widget::onPaint(PaintEvent& event)
|
||||||
|
|
||||||
void Widget::onShow(ShowEvent&)
|
void Widget::onShow(ShowEvent&)
|
||||||
{
|
{
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::onHide(HideEvent&)
|
void Widget::onHide(HideEvent&)
|
||||||
|
|
Loading…
Reference in a new issue