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

Make a SharedGraphics directory for classes shared between Kernel and LibGUI.

This commit is contained in:
Andreas Kling 2019-01-19 23:22:46 +01:00
parent b75ee4aacb
commit 7e5b81fe48
31 changed files with 49 additions and 41 deletions

1
SharedGraphics/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.o

View file

@ -0,0 +1,17 @@
#include "CharacterBitmap.h"
CharacterBitmap::CharacterBitmap(const char* ascii_data, unsigned width, unsigned height)
: m_bits(ascii_data)
, m_size(width, height)
{
}
CharacterBitmap::~CharacterBitmap()
{
}
RetainPtr<CharacterBitmap> CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height)
{
return adopt(*new CharacterBitmap(asciiData, width, height));
}

View file

@ -0,0 +1,24 @@
#pragma once
#include "Size.h"
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
class CharacterBitmap : public Retainable<CharacterBitmap> {
public:
static RetainPtr<CharacterBitmap> create_from_ascii(const char* asciiData, unsigned width, unsigned height);
~CharacterBitmap();
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:
CharacterBitmap(const char* b, unsigned w, unsigned h);
const char* m_bits { nullptr };
Size m_size;
};

27
SharedGraphics/Color.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "Color.h"
#include <AK/Assertions.h>
Color::Color(NamedColor named)
{
struct {
byte r;
byte g;
byte b;
} rgb;
switch (named) {
case Black: rgb = { 0, 0, 0 }; break;
case White: rgb = { 255, 255, 255 }; break;
case Red: rgb = { 255, 0, 0}; break;
case Green: rgb = { 0, 255, 0}; break;
case Blue: rgb = { 0, 0, 255}; break;
case Yellow: rgb = { 255, 255, 0 }; break;
case Magenta: rgb = { 255, 0, 255 }; break;
case DarkGray: rgb = { 64, 64, 64 }; break;
case MidGray: rgb = { 127, 127, 127 }; break;
case LightGray: rgb = { 192, 192, 192 }; break;
default: ASSERT_NOT_REACHED(); break;
}
m_value = (rgb.r << 16) | (rgb.g << 8) | rgb.b;
}

31
SharedGraphics/Color.h Normal file
View file

@ -0,0 +1,31 @@
#pragma once
#include <AK/Types.h>
typedef dword RGBA32;
class Color {
public:
enum NamedColor {
Black,
White,
Red,
Green,
Blue,
Yellow,
Magenta,
DarkGray,
MidGray,
LightGray,
};
Color() { }
Color(NamedColor);
Color(byte r, byte g, byte b) : m_value((r << 16) | (g << 8) | b) { }
Color(RGBA32 rgba) : m_value(rgba) { }
RGBA32 value() const { return m_value; }
private:
RGBA32 m_value { 0 };
};

41
SharedGraphics/Font.cpp Normal file
View file

@ -0,0 +1,41 @@
#include "Font.h"
#include "Peanut8x10.h"
static Font* s_default_font;
void Font::initialize()
{
s_default_font = nullptr;
}
Font& Font::default_font()
{
if (!s_default_font)
s_default_font = adopt(*new Font(Peanut8x10::glyphs, Peanut8x10::glyph_width, Peanut8x10::glyph_height, Peanut8x10::first_glyph, Peanut8x10::last_glyph)).leakRef();
return *s_default_font;
}
Font::Font(const char* const* glyphs, byte glyph_width, byte glyph_height, byte first_glyph, byte last_glyph)
: m_glyphs(glyphs)
, m_glyph_width(glyph_width)
, m_glyph_height(glyph_height)
, m_first_glyph(first_glyph)
, m_last_glyph(last_glyph)
{
}
Font::~Font()
{
}
const CharacterBitmap* Font::glyph_bitmap(byte ch) const
{
if (!m_bitmaps[ch]) {
if (ch < m_first_glyph || ch > m_last_glyph)
return nullptr;
const char* data = m_glyphs[(unsigned)ch - m_first_glyph];
m_bitmaps[ch] = CharacterBitmap::create_from_ascii(data, m_glyph_width, m_glyph_height);
}
ASSERT(ch >= m_first_glyph && ch <= m_last_glyph);
return m_bitmaps[ch].ptr();
}

32
SharedGraphics/Font.h Normal file
View file

@ -0,0 +1,32 @@
#pragma once
#include "CharacterBitmap.h"
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#include <AK/Types.h>
class Font : public Retainable<Font> {
public:
static Font& default_font();
~Font();
const CharacterBitmap* glyph_bitmap(byte) const;
byte glyph_width() const { return m_glyph_width; }
byte glyph_height() const { return m_glyph_height; }
static void initialize();
private:
Font(const char* const* glyphs, byte glyph_width, byte glyph_height, byte firstGlyph, byte lastGlyph);
const char* const* m_glyphs { nullptr };
mutable RetainPtr<CharacterBitmap> m_bitmaps[256];
byte m_glyph_width { 0 };
byte m_glyph_height { 0 };
byte m_first_glyph { 0 };
byte m_last_glyph { 0 };
};

View file

@ -0,0 +1,57 @@
#include "GraphicsBitmap.h"
#include <AK/kmalloc.h>
#ifdef KERNEL
#include <Kernel/Process.h>
#include <Kernel/MemoryManager.h>
#include <WindowServer/WSEventLoop.h>
#endif
#ifdef KERNEL
RetainPtr<GraphicsBitmap> GraphicsBitmap::create(Process& process, const Size& size)
{
return adopt(*new GraphicsBitmap(process, size));
}
GraphicsBitmap::GraphicsBitmap(Process& process, const Size& size)
: m_size(size)
, m_pitch(size.width() * sizeof(RGBA32))
, m_client_process(&process)
{
size_t size_in_bytes = size.width() * size.height() * sizeof(RGBA32);
auto vmo = VMObject::create_anonymous(size_in_bytes);
m_client_region = process.allocate_region_with_vmo(LinearAddress(), size_in_bytes, vmo.copyRef(), 0, "GraphicsBitmap (client)", true, true);
m_client_region->commit(process);
{
auto& server = WSEventLoop::the().server_process();
InterruptDisabler disabler;
m_server_region = server.allocate_region_with_vmo(LinearAddress(), size_in_bytes, move(vmo), 0, "GraphicsBitmap (server)", true, true);
}
m_data = (RGBA32*)m_server_region->linearAddress.asPtr();
}
#endif
RetainPtr<GraphicsBitmap> GraphicsBitmap::create_wrapper(const Size& size, RGBA32* data)
{
return adopt(*new GraphicsBitmap(size, data));
}
GraphicsBitmap::GraphicsBitmap(const Size& size, RGBA32* data)
: m_size(size)
, m_data(data)
, m_pitch(size.width() * sizeof(RGBA32))
{
}
GraphicsBitmap::~GraphicsBitmap()
{
#ifdef KERNEL
if (m_client_region)
m_client_process->deallocate_region(*m_client_region);
if (m_server_region)
WSEventLoop::the().server_process().deallocate_region(*m_server_region);
#endif
m_data = nullptr;
}

View file

@ -0,0 +1,58 @@
#pragma once
#include "Color.h"
#include "Size.h"
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#ifdef KERNEL
#include "Process.h"
#endif
class GraphicsBitmap : public Retainable<GraphicsBitmap> {
public:
#ifdef KERNEL
static RetainPtr<GraphicsBitmap> create(Process&, const Size&);
#endif
static RetainPtr<GraphicsBitmap> create_wrapper(const Size&, RGBA32*);
~GraphicsBitmap();
RGBA32* scanline(int y);
const RGBA32* scanline(int y) const;
Size size() const { return m_size; }
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
size_t pitch() const { return m_pitch; }
#ifdef KERNEL
Region* client_region() { return m_client_region; }
Region* server_region() { return m_server_region; }
#endif
private:
#ifdef KERNEL
GraphicsBitmap(Process&, const Size&);
#endif
GraphicsBitmap(const Size&, RGBA32*);
Size m_size;
RGBA32* m_data { nullptr };
size_t m_pitch { 0 };
#ifdef KERNEL
Process* m_client_process { nullptr };
Region* m_client_region { nullptr };
Region* m_server_region { nullptr };
#endif
};
inline RGBA32* GraphicsBitmap::scanline(int y)
{
return reinterpret_cast<RGBA32*>((((byte*)m_data) + (y * m_pitch)));
}
inline const RGBA32* GraphicsBitmap::scanline(int y) const
{
return reinterpret_cast<const RGBA32*>((((const byte*)m_data) + (y * m_pitch)));
}

262
SharedGraphics/Painter.cpp Normal file
View file

@ -0,0 +1,262 @@
#include "Painter.h"
#include "Font.h"
#include "GraphicsBitmap.h"
#include <AK/Assertions.h>
#include <AK/StdLibExtras.h>
#ifdef LIBGUI
#include <LibGUI/Widget.h>
#endif
#define DEBUG_WIDGET_UNDERDRAW
Painter::Painter(GraphicsBitmap& bitmap)
{
m_font = &Font::default_font();
m_target = &bitmap;
m_clip_rect = { { 0, 0 }, bitmap.size() };
}
#ifdef LIBGUI
Painter::Painter(Widget& widget)
: m_font(&widget.font())
{
m_target = widget.backing();
ASSERT(m_target);
m_window = widget.window();
m_translation.move_by(widget.relativePosition());
// NOTE: m_clip_rect is in Window coordinates since we are painting into its backing store.
m_clip_rect = widget.relativeRect();
#ifdef DEBUG_WIDGET_UNDERDRAW
// If the widget is not opaque, let's not mess it up with debugging color.
if (widget.fillWithBackgroundColor())
fill_rect(widget.rect(), Color::Red);
#endif
}
#endif
Painter::~Painter()
{
}
void Painter::fill_rect(const Rect& a_rect, Color color)
{
auto rect = a_rect;
rect.move_by(m_translation);
rect.intersect(m_clip_rect);
RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
const unsigned dst_skip = m_target->width();
for (int i = rect.height() - 1; i >= 0; --i) {
fast_dword_fill(dst, color.value(), rect.width());
dst += dst_skip;
}
}
void Painter::draw_rect(const Rect& rect, Color color)
{
Rect r = rect;
r.move_by(m_translation);
int min_y = max(r.top(), m_clip_rect.top());
int max_y = min(r.bottom(), m_clip_rect.bottom());
int min_x = max(r.left(), m_clip_rect.left());
int max_x = min(r.right(), m_clip_rect.right());
if (r.top() >= min_y && r.top() <= max_y) {
fast_dword_fill(m_target->scanline(r.top()) + min_x, color.value(), max_x - min_x + 1);
++min_y;
}
if (r.bottom() <= max_y && r.bottom() >= min_y) {
fast_dword_fill(m_target->scanline(r.bottom()) + min_x, color.value(), max_x - min_x + 1);
--max_y;
}
bool draw_left_side = r.left() >= m_clip_rect.left() && r.left() <= m_clip_rect.right();
bool draw_right_side = r.right() >= m_clip_rect.left() && r.right() <= m_clip_rect.right();
if (draw_left_side && draw_right_side) {
// Specialized loop when drawing both sides.
for (int y = min_y; y <= max_y; ++y) {
auto* bits = m_target->scanline(y);
bits[r.left()] = color.value();
bits[r.right()] = color.value();
}
} else {
for (int y = min_y; y <= max_y; ++y) {
auto* bits = m_target->scanline(y);
if (draw_left_side)
bits[r.left()] = color.value();
if (draw_right_side)
bits[r.right()] = color.value();
}
}
}
void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
{
Point point = p;
point.move_by(m_translation);
for (unsigned row = 0; row < bitmap.height(); ++row) {
int y = point.y() + row;
if (y < m_clip_rect.top() || y > m_clip_rect.bottom())
break;
auto* bits = m_target->scanline(y);
for (unsigned j = 0; j < bitmap.width(); ++j) {
int x = point.x() + j;
if (x < m_clip_rect.left() || x > m_clip_rect.right())
break;
char fc = bitmap.bits()[row * bitmap.width() + j];
if (fc == '#')
bits[x] = color.value();
}
}
}
void Painter::draw_glyph(const Point& point, char ch, Color color)
{
auto* bitmap = font().glyph_bitmap(ch);
if (!bitmap) {
dbgprintf("Font doesn't have 0x%b ('%c')\n", (byte)ch, ch);
ASSERT_NOT_REACHED();
}
int x = point.x();
int y = point.y();
draw_bitmap({ x, y }, *bitmap, color);
}
void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alignment, Color color)
{
Point point;
if (alignment == TextAlignment::TopLeft) {
point = rect.location();
} else if (alignment == TextAlignment::CenterLeft) {
point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
} else if (alignment == TextAlignment::Center) {
int textWidth = text.length() * font().glyph_width();
point = rect.center();
point.move_by(-(textWidth / 2), -(font().glyph_height() / 2));
} else {
ASSERT_NOT_REACHED();
}
for (unsigned i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) {
byte ch = text[i];
if (ch == ' ')
continue;
draw_glyph(point, ch, color);
}
}
void Painter::set_pixel(const Point& p, Color color)
{
auto point = p;
point.move_by(m_translation);
if (!m_clip_rect.contains(point))
return;
m_target->scanline(point.y())[point.x()] = color.value();
}
ALWAYS_INLINE void Painter::set_pixel_with_draw_op(dword& pixel, const Color& color)
{
if (m_draw_op == DrawOp::Copy)
pixel = color.value();
else if (m_draw_op == DrawOp::Xor)
pixel ^= color.value();
}
void Painter::draw_line(const Point& p1, const Point& p2, Color color)
{
auto point1 = p1;
point1.move_by(m_translation);
auto point2 = p2;
point2.move_by(m_translation);
// Special case: vertical line.
if (point1.x() == point2.x()) {
const int x = point1.x();
if (x < m_clip_rect.left() || x > m_clip_rect.right())
return;
if (point1.y() > point2.y())
swap(point1, point2);
int min_y = max(point1.y(), m_clip_rect.top());
int max_y = min(point2.y(), m_clip_rect.bottom());
for (int y = min_y; y <= max_y; ++y)
set_pixel_with_draw_op(m_target->scanline(y)[x], color);
return;
}
if (point1.x() > point2.x())
swap(point1, point2);
// Special case: horizontal line.
if (point1.y() == point2.y()) {
const int y = point1.y();
if (y < m_clip_rect.top() || y > m_clip_rect.bottom())
return;
if (point1.x() > point2.x())
swap(point1, point2);
int min_x = max(point1.x(), m_clip_rect.left());
int max_x = min(point2.x(), m_clip_rect.right());
auto* pixels = m_target->scanline(point1.y());
if (m_draw_op == DrawOp::Copy) {
fast_dword_fill(pixels + min_x, color.value(), max_x - min_x + 1);
} else {
for (int x = min_x; x <= max_x; ++x)
set_pixel_with_draw_op(pixels[x], color);
}
return;
}
// FIXME: Implement clipping below.
ASSERT_NOT_REACHED();
#if 0
const double dx = point2.x() - point1.x();
const double dy = point2.y() - point1.y();
const double delta_error = fabs(dy / dx);
double error = 0;
const double yStep = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
int y = point1.y();
for (int x = point1.x(); x <= point2.x(); ++x) {
m_target->scanline(y)[x] = color.value();
error += delta_error;
if (error >= 0.5) {
y = (double)y + yStep;
error -= 1.0;
}
}
#endif
}
void Painter::draw_focus_rect(const Rect& rect)
{
Rect focus_rect = rect;
focus_rect.move_by(1, 1);
focus_rect.set_width(focus_rect.width() - 2);
focus_rect.set_height(focus_rect.height() - 2);
draw_rect(focus_rect, Color(96, 96, 192));
}
void Painter::blit(const Point& position, const GraphicsBitmap& source)
{
Rect dst_rect(position, source.size());
dst_rect.intersect(m_clip_rect);
RGBA32* dst = m_target->scanline(position.y()) + dst_rect.x();
const RGBA32* src= source.scanline(0) + (dst_rect.x() - position.x());
const unsigned dst_skip = m_target->width();
const unsigned src_skip = source.width();
for (int i = dst_rect.height() - 1; i >= 0; --i) {
fast_dword_copy(dst, src, dst_rect.width());
dst += dst_skip;
src += src_skip;
}
}

47
SharedGraphics/Painter.h Normal file
View file

@ -0,0 +1,47 @@
#pragma once
#include "Color.h"
#include "Point.h"
#include "Rect.h"
#include "Size.h"
#include <AK/AKString.h>
class CharacterBitmap;
class GraphicsBitmap;
class Font;
class Widget;
class Window;
class Painter {
public:
explicit Painter(Widget&);
explicit Painter(GraphicsBitmap&);
~Painter();
void fill_rect(const Rect&, Color);
void draw_rect(const Rect&, Color);
void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());
void set_pixel(const Point&, Color);
void draw_line(const Point&, const Point&, Color);
void draw_focus_rect(const Rect&);
void blit(const Point&, const GraphicsBitmap&);
enum class TextAlignment { TopLeft, CenterLeft, Center };
void draw_text(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
void draw_glyph(const Point&, char, Color);
const Font& font() const { return *m_font; }
enum class DrawOp { Copy, Xor };
void set_draw_op(DrawOp op) { m_draw_op = op; }
DrawOp draw_op() const { return m_draw_op; }
private:
void set_pixel_with_draw_op(dword& pixel, const Color&);
const Font* m_font;
Point m_translation;
Rect m_clip_rect;
RetainPtr<GraphicsBitmap> m_target;
Window* m_window { nullptr };
DrawOp m_draw_op { DrawOp::Copy };
};

1049
SharedGraphics/Peanut8x10.h Normal file

File diff suppressed because it is too large Load diff

861
SharedGraphics/Peanut8x8.h Normal file
View file

@ -0,0 +1,861 @@
#pragma once
namespace Peanut8x8 {
static constexpr char first_character = '!';
static constexpr char last_character = '~';
static constexpr byte font_width = 8;
static constexpr byte font_height = 8;
static constexpr const char* font[] {
" ## "
" ## "
" ## "
" ## "
" ## "
" "
" ## "
" ",
" ## ## "
" ## ## "
" # # "
" "
" "
" "
" "
" ",
" # # "
" # # "
" ###### "
" # # "
" ###### "
" # # "
" # # "
" ",
" #### "
" # # # "
" # # "
" #### "
" # # "
" # # # "
" #### "
" ",
" ## # "
" ## ## "
" ## "
" ## "
" ## "
" ## ## "
" # ## "
" ",
" "
" ## "
" # # "
" ### "
" # # #"
" # ## "
" ### #"
" ",
" ## "
" ## "
" # "
" "
" "
" "
" "
" ",
" # "
" # "
" # "
" # "
" # "
" # "
" # "
" ",
" # "
" # "
" # "
" # "
" # "
" # "
" # "
" ",
" "
" "
" # "
" ##### "
" # "
" # # "
" "
" ",
" "
" "
" # "
" # "
" ##### "
" # "
" # "
" ",
" "
" "
" "
" "
" "
" ## "
" ## "
" ",
" "
" "
" "
" "
" ##### "
" "
" "
" ",
" "
" "
" "
" "
" "
" ## "
" ## "
" ",
" # "
" ## "
" ## "
" ## "
" ## "
" ## "
" # "
" ",
" #### "
" # ## "
" # # # "
" # # # "
" # # # "
" ## # "
" #### "
" ",
" # "
" ## "
" # # "
" # "
" # "
" # "
" ### "
" ",
" #### "
" # # "
" # # "
" # "
" # "
" # "
" ###### "
" ",
" ###### "
" # "
" # "
" ### "
" # "
" # # "
" #### "
" ",
" ### "
" # # "
" # # "
" ###### "
" # "
" # "
" # "
" ",
" ###### "
" # "
" # "
" ##### "
" # "
" # # "
" #### "
" ",
" ## "
" ## "
" # "
" ##### "
" # # "
" # # "
" #### "
" ",
" ###### "
" # # "
" # "
" # "
" # "
" # "
" # "
" ",
" #### "
" # # "
" # # "
" #### "
" # # "
" # # "
" #### "
" ",
" #### "
" # # "
" # # "
" ##### "
" # "
" ## "
" ## "
" ",
" "
" "
" ## "
" ## "
" "
" ## "
" ## "
" ",
" "
" "
" ## "
" ## "
" "
" ## "
" ## "
" ",
" ## "
" # "
" # "
" # "
" # "
" # "
" ## "
" ",
" "
" "
" ###### "
" "
" ###### "
" "
" "
" ",
" ## "
" # "
" # "
" # "
" # "
" # "
" ## "
" ",
" #### "
" # # "
" # "
" # "
" ## "
" "
" ## "
" ",
" #### "
" # # "
" # ### "
" # # # "
" # ### "
" # "
" #### "
" ",
" #### "
" # # "
" # # "
" ###### "
" # # "
" # # "
"### ###"
" ",
" ##### "
" # # "
" # # "
" ###### "
" # # "
" # # "
" ##### "
" ",
" #### "
" # # "
" # "
" # "
" # "
" # # "
" #### "
" ",
" ##### "
" # # "
" # # "
" # # "
" # # "
" # # "
" ##### "
" ",
" ###### "
" # # "
" # "
" #### "
" # "
" # # "
" ###### "
" ",
" ###### "
" # # "
" # "
" ### "
" # "
" # "
" ### "
" ",
" #### "
" # # "
" # "
" # ###"
" # # "
" # # "
" #### "
" ",
" # # "
" # # "
" # # "
" ###### "
" # # "
" # # "
" # # "
" ",
" ### "
" # "
" # "
" # "
" # "
" # "
" ### "
" ",
" ##### "
" # # "
" # "
" # "
" # "
" # # "
" ### "
" ",
" ### ###"
" # # "
" # # "
" ### "
" # # "
" # # "
" ### ###"
" ",
" ### "
" # "
" # "
" # "
" # "
" # # "
" ###### "
" ",
" # # "
" ## ## "
" # ## # "
" # # "
" # # "
" # # "
"### ###"
" ",
" # # "
" ## # "
" # # # "
" # # # "
" # ## "
" # # "
" # # "
" ",
" #### "
" # # "
" # # "
" # # "
" # # "
" # # "
" #### "
" ",
" ##### "
" # # "
" # # "
" #### "
" # "
" # "
" ### "
" ",
" #### "
" # # "
" # # "
" # # "
" # # # "
" # # "
" ### # "
" ",
" ##### "
" # # "
" # # "
" #### "
" # # "
" # # "
" ### # "
" ",
" #### "
" # # "
" # "
" #### "
" # "
" # # "
" #### "
" ",
"####### "
"# # # "
" # "
" # "
" # "
" # "
" ### "
" ",
"### ###"
" # # "
" # # "
" # # "
" # # "
" # # "
" #### "
" ",
"### ### "
" # # "
" # # "
" # # "
" # # "
" # "
" # "
" ",
"### ###"
" # # "
" # # "
" # # "
" # ## # "
" ## ## "
" # # "
" ",
"## ## "
" # # "
" # # "
" # "
" # # "
" # # "
"## ## "
" ",
"## ## "
" # # "
" # # "
" # "
" # "
" # "
" ### "
" ",
" ###### "
" # # "
" # "
" # "
" # "
" # # "
" ###### "
" ",
" #### "
" # "
" # "
" # "
" # "
" # "
" #### "
" ",
" # "
" ## "
" ## "
" ## "
" ## "
" ## "
" # "
" ",
" #### "
" # "
" # "
" # "
" # "
" # "
" #### "
" ",
" # "
" # # "
" # # "
" "
" "
" "
" "
" ",
" "
" "
" "
" "
" "
" "
" ###### "
" ",
" ## "
" ## "
" "
" "
" "
" "
" "
" ",
" "
" "
" #### "
" # # "
" # # "
" # ## "
" ### # "
" ",
" # "
" # "
" ##### "
" # # "
" # # "
" # # "
" ##### "
" ",
" "
" "
" #### "
" # # "
" # "
" # # "
" #### "
" ",
" # "
" # "
" ##### "
" # # "
" # # "
" # # "
" ##### "
" ",
" "
" "
" #### "
" # # "
" ###### "
" # "
" #### "
" ",
" ### "
" # "
" #### "
" # "
" # "
" # "
" ### "
" ",
" "
" "
" #### "
" # # "
" # # "
" #### "
" # "
" #### ",
" # "
" # "
" ##### "
" # # "
" # # "
" # # "
" # # "
" ",
" # "
" "
" ### "
" # "
" # "
" # "
" ### "
" ",
" # "
" "
" ### "
" # "
" # "
" # "
" # "
" ## ",
"### "
" # "
" # ## "
" # # "
" ## # "
" # # "
"### # "
" ",
" ## "
" # "
" # "
" # "
" # "
" # "
" ### "
" ",
" "
" "
" ## ## "
" # # #"
" # # #"
" # #"
" # #"
" ",
" "
" "
" ##### "
" # # "
" # # "
" # # "
" # # "
" ",
" "
" "
" #### "
" # # "
" # # "
" # # "
" #### "
" ",
" "
" "
" ##### "
" # # "
" # # "
" ##### "
" # "
"### ",
" "
" "
" ##### "
" # # "
" # # "
" ##### "
" # "
" ###",
" "
" "
" #### "
" # # "
" # "
" # "
"### "
" ",
" "
" "
" #### "
" ## # "
" ## "
" # ## "
" #### "
" ",
" # "
" # "
" #### "
" # "
" # "
" # "
" ### "
" ",
" "
" "
" # # "
" # # "
" # # "
" # # "
" #### #"
" ",
" "
" "
" # # "
" # # "
" # # "
" # # "
" ## "
" ",
" "
" "
" # # #"
" # # #"
" # # #"
" # # #"
" ## ###"
" ",
" "
" "
" # # "
" # # "
" # "
" # # "
" # # "
" ",
" "
" "
" # # "
" # # "
" # # "
" ##### "
" # "
" #### ",
" "
" "
" ###### "
" # "
" ## "
" # "
" ###### "
" ",
" ### "
" # "
" # "
" # "
" # "
" # "
" ### "
" ",
" # "
" # "
" # "
" # "
" # "
" # "
" # "
" ",
" ### "
" # "
" # "
" # "
" # "
" # "
" ### "
" ",
" "
" "
" "
" ## # "
" # ## "
" "
" "
" ",
};
}

47
SharedGraphics/Point.h Normal file
View file

@ -0,0 +1,47 @@
#pragma once
class Rect;
struct GUI_Point;
class Point {
public:
Point() { }
Point(int x, int y) : m_x(x) , m_y(y) { }
Point(const GUI_Point&);
int x() const { return m_x; }
int y() const { return m_y; }
void set_x(int x) { m_x = x; }
void set_y(int y) { m_y = y; }
void move_by(int dx, int dy)
{
m_x += dx;
m_y += dy;
}
void move_by(const Point& delta)
{
move_by(delta.x(), delta.y());
}
void constrain(const Rect&);
bool operator==(const Point& other) const
{
return m_x == other.m_x
&& m_y == other.m_y;
}
bool operator!=(const Point& other) const
{
return !(*this == other);
}
operator GUI_Point() const;
private:
int m_x { 0 };
int m_y { 0 };
};

35
SharedGraphics/Rect.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "Rect.h"
#include <AK/StdLibExtras.h>
void Rect::intersect(const Rect& other)
{
int l = max(left(), other.left());
int r = min(right(), other.right());
int t = max(top(), other.top());
int b = min(bottom(), other.bottom());
if (l >= r || t >= b) {
m_location = { };
m_size = { };
return;
}
m_location.set_x(l);
m_location.set_y(t);
m_size.set_width((r - l) + 1);
m_size.set_height((b - t) + 1);
}
Rect Rect::united(const Rect& other) const
{
if (is_null())
return other;
if (other.is_null())
return *this;
Rect rect;
rect.set_left(min(left(), other.left()));
rect.set_top(min(top(), other.top()));
rect.set_right(max(right(), other.right()));
rect.set_bottom(max(bottom(), other.bottom()));
return rect;
}

162
SharedGraphics/Rect.h Normal file
View file

@ -0,0 +1,162 @@
#pragma once
#include "Point.h"
#include "Size.h"
struct GUI_Rect;
class Rect {
public:
Rect() { }
Rect(int x, int y, int width, int height)
: m_location(x, y)
, m_size(width, height)
{
}
Rect(const Point& location, const Size& size)
: m_location(location)
, m_size(size)
{
}
Rect(const GUI_Rect&);
bool is_null() const
{
return width() == 0 && height() == 0;
}
bool is_empty() const
{
return width() <= 0 || height() <= 0;
}
void move_by(int dx, int dy)
{
m_location.move_by(dx, dy);
}
void move_by(const Point& delta)
{
m_location.move_by(delta);
}
Point center() const
{
return { x() + width() / 2, y() + height() / 2 };
}
void inflate(int w, int h)
{
set_x(x() - w / 2);
set_width(width() + w);
set_y(y() - h / 2);
set_height(height() + h);
}
void shrink(int w, int h)
{
set_x(x() + w / 2);
set_width(width() - w);
set_y(y() + h / 2);
set_height(height() - h);
}
bool contains(int x, int y) const
{
return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
}
bool contains(const Point& point) const
{
return contains(point.x(), point.y());
}
bool contains(const Rect& other) const
{
return left() <= other.left()
&& right() >= other.right()
&& top() <= other.top()
&& bottom() >= other.bottom();
}
int left() const { return x(); }
int right() const { return x() + width() - 1; }
int top() const { return y(); }
int bottom() const { return y() + height() - 1; }
void set_left(int left)
{
set_x(left);
}
void set_top(int top)
{
set_y(top);
}
void set_right(int right)
{
set_width(right - x() + 1);
}
void set_bottom(int bottom)
{
set_height(bottom - y() + 1);
}
bool intersects(const Rect& other) const
{
return left() <= other.right()
&& other.left() <= right()
&& top() <= other.bottom()
&& other.top() <= bottom();
}
int x() const { return location().x(); }
int y() const { return location().y(); }
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
void set_x(int x) { m_location.set_x(x); }
void set_y(int y) { m_location.set_y(y); }
void set_width(int width) { m_size.set_width(width); }
void set_height(int height) { m_size.set_height(height); }
Point location() const { return m_location; }
Size size() const { return m_size; }
operator GUI_Rect() const;
bool operator==(const Rect& other) const
{
return m_location == other.m_location
&& m_size == other.m_size;
}
void intersect(const Rect&);
static Rect intersection(const Rect& a, const Rect& b)
{
Rect r(a);
r.intersect(b);
return r;
}
Rect united(const Rect&) const;
private:
Point m_location;
Size m_size;
};
inline void Point::constrain(const Rect& rect)
{
if (x() < rect.left())
set_x(rect.left());
else if (x() > rect.right())
set_x(rect.right());
if (y() < rect.top())
set_y(rect.top());
else if (y() > rect.bottom())
set_y(rect.bottom());
}

31
SharedGraphics/Size.h Normal file
View file

@ -0,0 +1,31 @@
#pragma once
struct GUI_Size;
class Size {
public:
Size() { }
Size(int w, int h) : m_width(w), m_height(h) { }
Size(const GUI_Size&);
bool is_empty() const { return !m_width || !m_height; }
int width() const { return m_width; }
int height() const { return m_height; }
void set_width(int w) { m_width = w; }
void set_height(int h) { m_height = h; }
bool operator==(const Size& other) const
{
return m_width == other.m_width &&
m_height == other.m_height;
}
operator GUI_Size() const;
private:
int m_width { 0 };
int m_height { 0 };
};