1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:07:36 +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

@ -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 {};
};
}