mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-28 23:27:49 +01:00
64 lines
2.0 KiB
C++
64 lines
2.0 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];
|
|
}
|
|
|
|
template<typename Framebuffer>
|
|
inline void drawGlyph(Framebuffer& 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;
|
|
}
|
|
|
|
template<typename Framebuffer>
|
|
inline void drawText(Framebuffer& 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
|