diff options
author | Lenny Maiorani <lenny@serenityos.org> | 2022-02-28 19:21:12 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-03-04 17:41:08 +0100 |
commit | d5fdc6096cd549e5571a5f97eedc6f2450826569 (patch) | |
tree | e33f3175efe4f5b70158ab7af597be5512f467c7 /Userland/Libraries/LibGfx/CharacterBitmap.h | |
parent | dc518404ce57673050c7895ffd0a76533d60b6e1 (diff) | |
download | serenity-d5fdc6096cd549e5571a5f97eedc6f2450826569.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibGfx/CharacterBitmap.h')
-rw-r--r-- | Userland/Libraries/LibGfx/CharacterBitmap.h | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/Userland/Libraries/LibGfx/CharacterBitmap.h b/Userland/Libraries/LibGfx/CharacterBitmap.h index 26b9a0ab86..94c4da2a58 100644 --- a/Userland/Libraries/LibGfx/CharacterBitmap.h +++ b/Userland/Libraries/LibGfx/CharacterBitmap.h @@ -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; } -private: - CharacterBitmap(const char* b, unsigned w, unsigned h); + constexpr IntSize size() const { return m_size; } + constexpr unsigned width() const { return m_size.width(); } + constexpr unsigned height() const { return m_size.height(); } - const char* m_bits { nullptr }; - IntSize m_size; +private: + StringView m_bits {}; + IntSize m_size {}; }; } |