Bitmap font wrapper.
Bitmap font wrapper.Encapsulates raw font data and provides access to character bitmaps. Supports fixed-width bitmap fonts in Waveshare format.
Design:
- Value type: Cheap to copy (just pointer + dimensions)
- Non-owning: Font data must outlive Font instance (typically static)
- Thread-safe: Immutable after construction (const methods only)
Character Addressing:
- ASCII printable range: 0x20 (space) to 0x7E (~) = 95 characters
- Invalid characters (< 0x20 or > 0x7E) return empty span
- Offset calculation: (ascii_code - 0x20) × bytes_per_char()
Factory Methods:
- font8(), font12(), font16(), font20(), font24(): Built-in fonts
- Return const references to static instances (zero allocation)
- Thread-safe initialization (C++11 static local guarantee)
const Font& font = Font::font16();
std::cout << "Font size: " << font.width() << "x" << font.height() << std::endl;
auto bitmap = font.char_data('A');
std::cout << "'A' bitmap size: " << bitmap.size() << " bytes" << std::endl;
extern const std::uint8_t custom_font_table[];
Font custom{custom_font_table, 12, 16};
- See also
- FontMetrics, Graphics::draw_text()
#pragma once
#include <cstddef>
#include <cstdint>
#include <span>
struct FontMetrics {
};
class Font {
public:
constexpr Font(
const std::uint8_t *table, std::uint16_t
width, std::uint16_t
height)
[[nodiscard]]
constexpr auto metrics()
const noexcept -> FontMetrics {
return {.width = width_, .height = height_}; }
[[nodiscard]]
constexpr auto width()
const noexcept -> std::uint16_t {
return width_; }
[[nodiscard]]
constexpr auto height()
const noexcept -> std::uint16_t {
return height_; }
[[nodiscard]]
auto char_data(
char c)
const -> std::span<const std::uint8_t>;
[[nodiscard]]
constexpr auto bytes_per_char()
const noexcept -> std::size_t {
const auto width_bytes = static_cast<std::size_t>((width_ % 8 == 0) ? (width_ / 8) : ((width_ / 8) + 1));
return width_bytes * static_cast<std::size_t>(height_);
}
static auto font8() ->
const Font &;
static auto font12() ->
const Font &;
static auto font16() ->
const Font &;
static auto font20() ->
const Font &;
static auto font24() ->
const Font &;
private:
const std::uint8_t *table_;
std::uint16_t width_;
std::uint16_t height_;
};
}
constexpr auto height() const noexcept -> std::uint16_t
Get character height in pixels.
Definition font.hpp:194
auto char_data(char c) const -> std::span< const std::uint8_t >
Get bitmap data for a specific character.
Definition font.cpp:11
constexpr auto width() const noexcept -> std::uint16_t
Get character width in pixels.
Definition font.hpp:188
static auto font8() -> const Font &
Get 8-pixel font.
Definition font.cpp:40
static auto font24() -> const Font &
Get 24-pixel font.
Definition font.cpp:64
static auto font12() -> const Font &
Get 12-pixel font.
Definition font.cpp:46
static auto font16() -> const Font &
Get 16-pixel font.
Definition font.cpp:52
constexpr auto bytes_per_char() const noexcept -> std::size_t
Calculate storage size for a single character.
Definition font.hpp:261
constexpr auto metrics() const noexcept -> FontMetrics
Get font dimensions.
Definition font.hpp:182
static auto font20() -> const Font &
Get 20-pixel font.
Definition font.cpp:58
std::uint16_t height
Definition font.hpp:101
std::uint16_t width
Definition font.hpp:100