Files
cardboy/Firmware/main/include/font16x8.hpp
2025-10-08 21:21:33 +02:00

62 lines
1.9 KiB
C++

#pragma once
#include "Fonts.hpp"
#include "app_framework.hpp"
#include <array>
#include <cctype>
#include <string_view>
namespace font16x8 {
constexpr int kGlyphWidth = 8;
constexpr int kGlyphHeight = 16;
constexpr unsigned char kFallbackChar = '?';
inline unsigned char normalizeChar(char ch) {
unsigned char uc = static_cast<unsigned char>(ch);
if (uc >= 'a' && uc <= 'z')
uc = static_cast<unsigned char>(std::toupper(static_cast<unsigned char>(uc)));
if (!std::isprint(static_cast<unsigned char>(uc)))
return kFallbackChar;
return uc;
}
inline const std::array<uint8_t, kGlyphHeight>& glyphBitmap(char ch) {
unsigned char uc = normalizeChar(ch);
return fonts_Terminess_Powerline[uc];
}
inline void drawGlyph(IFramebuffer& fb, int x, int y, char ch, int scale = 1, bool on = true) {
const auto& rows = glyphBitmap(ch);
for (int row = 0; row < kGlyphHeight; ++row) {
const uint8_t rowBits = rows[row];
for (int col = 0; col < kGlyphWidth; ++col) {
const uint8_t mask = static_cast<uint8_t>(1u << (kGlyphWidth - 1 - col));
if (rowBits & mask) {
for (int sx = 0; sx < scale; ++sx)
for (int sy = 0; sy < scale; ++sy)
fb.drawPixel(x + col * scale + sx, y + row * scale + sy, on);
}
}
}
}
inline int measureText(std::string_view text, int scale = 1, int letterSpacing = 1) {
if (text.empty())
return 0;
const int advance = (kGlyphWidth + letterSpacing) * scale;
return static_cast<int>(text.size()) * advance - letterSpacing * scale;
}
inline void drawText(IFramebuffer& fb, int x, int y, std::string_view text, int scale = 1, bool on = true,
int letterSpacing = 1) {
int cursor = x;
for (char ch: text) {
drawGlyph(fb, cursor, y, ch, scale, on);
cursor += (kGlyphWidth + letterSpacing) * scale;
}
}
} // namespace font16x8