libepaper 2.0.0
A C++23 library for controlling Waveshare e-paper displays on Raspberry Pi, featuring transparent sleep/wake management and a fluent builder API.
Loading...
Searching...
No Matches
/mnt/nas/libepaper/include/epaper/graphics/font.hpp

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:

Character Addressing:

Factory Methods:

// Using built-in font
const Font& font = Font::font16();
std::cout << "Font size: " << font.width() << "x" << font.height() << std::endl;
// Accessing character bitmap
auto bitmap = font.char_data('A');
std::cout << "'A' bitmap size: " << bitmap.size() << " bytes" << std::endl;
// Custom font from external data
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>
namespace epaper {
struct FontMetrics {
std::uint16_t width;
std::uint16_t height;
};
class Font {
public:
constexpr Font(const std::uint8_t *table, std::uint16_t width, std::uint16_t height)
: table_(table), width_(width), height_(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_);
}
// Factory methods for built-in fonts
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_;
};
} // namespace epaper
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
Definition color.hpp:5
std::uint16_t height
Definition font.hpp:101
std::uint16_t width
Definition font.hpp:100