idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
font.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
23#include <cstddef>
24#include <cstdint>
25#include <string_view>
26
31namespace idfxx::font {
32
41struct mono_font {
42 uint8_t width;
43 uint8_t height;
45 char last_char;
46 const uint8_t* bitmap;
47
49 [[nodiscard]] constexpr size_t bytes_per_row() const noexcept { return (width + 7u) / 8u; }
50
52 [[nodiscard]] constexpr size_t bytes_per_glyph() const noexcept { return bytes_per_row() * height; }
53
55 [[nodiscard]] constexpr bool contains(char c) const noexcept { return c >= first_char && c <= last_char; }
56
62 [[nodiscard]] constexpr const uint8_t* glyph(char c) const noexcept {
63 return bitmap + static_cast<size_t>(c - first_char) * bytes_per_glyph();
64 }
65
77 [[nodiscard]] constexpr bool ink_at(char c, size_t col, size_t row) const noexcept {
78 return glyph(c)[row * bytes_per_row() + col / 8] & (0x80u >> (col % 8));
79 }
80
90 [[nodiscard]] constexpr size_t advance(char /*c*/) const noexcept { return width; }
91};
92
104[[nodiscard]] constexpr size_t text_width(const mono_font& font, std::string_view text, unsigned scale = 1) noexcept {
105 if (scale == 0) {
106 scale = 1;
107 }
108 size_t width = 0;
109 for (char c : text) {
110 width += font.advance(c);
111 }
112 return width * scale;
113}
114
// end of idfxx_font
116
117} // namespace idfxx::font
constexpr size_t text_width(const mono_font &font, std::string_view text, unsigned scale=1) noexcept
Returns the width in pixels of text rendered in font.
Definition font.hpp:104
Bitmap font types, text metrics, and bundled fonts.
Definition font.hpp:31
A fixed-cell monochrome bitmap font.
Definition font.hpp:41
char first_char
First character in the glyph range.
Definition font.hpp:44
const uint8_t * bitmap
Glyph data (see struct description for layout).
Definition font.hpp:46
constexpr size_t advance(char) const noexcept
Returns the horizontal distance the cursor moves after c.
Definition font.hpp:90
constexpr const uint8_t * glyph(char c) const noexcept
Returns a pointer to the glyph bitmap for c.
Definition font.hpp:62
constexpr bool ink_at(char c, size_t col, size_t row) const noexcept
Returns true if the glyph for c has ink at (col, row).
Definition font.hpp:77
uint8_t width
Glyph cell width in pixels.
Definition font.hpp:42
constexpr size_t bytes_per_glyph() const noexcept
Returns the number of bytes in one glyph.
Definition font.hpp:52
constexpr size_t bytes_per_row() const noexcept
Returns the number of bytes in one glyph row.
Definition font.hpp:49
constexpr bool contains(char c) const noexcept
Returns true if the font has a glyph for c.
Definition font.hpp:55
char last_char
Last character in the glyph range (inclusive).
Definition font.hpp:45
uint8_t height
Glyph cell height in pixels.
Definition font.hpp:43