1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:17:34 +00:00

Libraries: Make CharacterBitmap instances at compile-time

`CharacterBitmap` instances are generated at run-time and put on the
heap, but they can be created in a `constexpr` context and stored in
static memory.

Also, remove additional `width` and `height` `static` values in favor
of using the `constexpr` member functions of `CharacterBitmap`.

These changes also include the removal of some initialization code
which tests if the `CharacterBitmap` is created since it is always
created and removes function-local `static` values which cause
run-time branches to ensure it is initialized each time the function
is called.
This commit is contained in:
Lenny Maiorani 2022-02-28 19:21:12 -07:00 committed by Linus Groh
parent dc518404ce
commit d5fdc6096c
9 changed files with 97 additions and 135 deletions

View file

@ -6,7 +6,6 @@ set(SOURCES
BitmapFont.cpp
BMPLoader.cpp
BMPWriter.cpp
CharacterBitmap.cpp
ClassicStylePainter.cpp
ClassicWindowTheme.cpp
Color.cpp

View file

@ -1,26 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CharacterBitmap.h"
namespace Gfx {
CharacterBitmap::CharacterBitmap(const char* ascii_data, unsigned width, unsigned height)
: m_bits(ascii_data)
, m_size(width, height)
{
}
CharacterBitmap::~CharacterBitmap()
{
}
NonnullRefPtr<CharacterBitmap> CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height)
{
return adopt_ref(*new CharacterBitmap(asciiData, width, height));
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -9,26 +10,31 @@
#include "Size.h"
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
namespace Gfx {
class CharacterBitmap : public RefCounted<CharacterBitmap> {
class CharacterBitmap {
public:
static NonnullRefPtr<CharacterBitmap> create_from_ascii(const char* asciiData, unsigned width, unsigned height);
~CharacterBitmap();
CharacterBitmap() = delete;
constexpr CharacterBitmap(StringView ascii_data, unsigned width, unsigned height)
: m_bits(ascii_data)
, m_size(width, height)
{
}
bool bit_at(unsigned x, unsigned y) const { return m_bits[y * width() + x] == '#'; }
const char* bits() const { return m_bits; }
constexpr ~CharacterBitmap() = default;
IntSize size() const { return m_size; }
unsigned width() const { return m_size.width(); }
unsigned height() const { return m_size.height(); }
constexpr bool bit_at(unsigned x, unsigned y) const { return m_bits[y * width() + x] == '#'; }
constexpr StringView bits() const { return m_bits; }
constexpr IntSize size() const { return m_size; }
constexpr unsigned width() const { return m_size.width(); }
constexpr unsigned height() const { return m_size.height(); }
private:
CharacterBitmap(const char* b, unsigned w, unsigned h);
const char* m_bits { nullptr };
IntSize m_size;
StringView m_bits {};
IntSize m_size {};
};
}

View file

@ -2,6 +2,7 @@
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Sarah Taube <metalflakecobaltpaint@gmail.com>
* Copyright (c) 2021, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -391,7 +392,7 @@ void ClassicStylePainter::paint_radio_button(Painter& painter, IntRect const& re
painter.blit(rect.location(), bitmap, bitmap.rect());
}
static char const* s_checked_bitmap_data = {
static constexpr Gfx::CharacterBitmap s_checked_bitmap {
" "
" # "
" ## "
@ -400,13 +401,10 @@ static char const* s_checked_bitmap_data = {
" ##### "
" ### "
" # "
" "
" ",
9, 9
};
static Gfx::CharacterBitmap* s_checked_bitmap;
static int const s_checked_bitmap_width = 9;
static int const s_checked_bitmap_height = 9;
void ClassicStylePainter::paint_check_box(Painter& painter, IntRect const& rect, Palette const& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
{
painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
@ -418,9 +416,7 @@ void ClassicStylePainter::paint_check_box(Painter& painter, IntRect const& rect,
}
if (is_checked) {
if (!s_checked_bitmap)
s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
painter.draw_bitmap(rect.shrunken(4, 4).location(), *s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
painter.draw_bitmap(rect.shrunken(4, 4).location(), s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
}
}