78 lines
2.2 KiB
C++
78 lines
2.2 KiB
C++
|
|
#pragma once
|
|
|
|
#if !defined(BAD_APPLE_OS_PSF_HPP_INCLUDED)
|
|
#define BAD_APPLE_OS_PSF_HPP_INCLUDED
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
namespace psf
|
|
{
|
|
inline const uint32_t MAGIC = 0x864ab572;
|
|
|
|
struct Header
|
|
{
|
|
uint32_t magic; /* magic bytes to identify PSF */
|
|
uint32_t version; /* zero */
|
|
uint32_t headersize; /* offset of bitmaps in file, 32 */
|
|
uint32_t flags; /* 0 if there's no unicode table */
|
|
uint32_t numglyph; /* number of glyphs */
|
|
uint32_t bytesperglyph; /* size of each glyph */
|
|
uint32_t height; /* height in pixels */
|
|
uint32_t width; /* width in pixels */
|
|
};
|
|
|
|
class Font
|
|
{
|
|
private:
|
|
const Header* mMemory = nullptr;
|
|
size_t mSize = 0;
|
|
public:
|
|
Font() noexcept = default;
|
|
Font(const Font&) = default;
|
|
private:
|
|
Font(const Header* memory, size_t size) noexcept : mMemory(memory), mSize(size) {}
|
|
public:
|
|
Font& operator=(const Font&) noexcept = default;
|
|
|
|
[[nodiscard]] uint32_t getGlyphWidth() const noexcept { return mMemory->width; }
|
|
[[nodiscard]] uint32_t getGlyphHeight() const noexcept { return mMemory->height; }
|
|
template<typename TColor, typename TDrawFn>
|
|
void writeGlyph(char chr, const TColor& fgColor, const TColor& bgColor, TDrawFn draw) const noexcept;
|
|
public:
|
|
[[nodiscard]] static bool create(const void* data, size_t size, Font& outFont) noexcept;
|
|
};
|
|
|
|
template<typename TColor, typename TDrawFn>
|
|
void Font::writeGlyph(char chr, const TColor& fgColor, const TColor& bgColor, TDrawFn draw) const noexcept
|
|
{
|
|
if (chr > mMemory->numglyph) {
|
|
chr = 0;
|
|
}
|
|
|
|
const uint8_t* glyphData = reinterpret_cast<const uint8_t*>(mMemory) + mMemory->headersize + chr * mMemory->bytesperglyph;
|
|
|
|
for (unsigned y = 0; y < mMemory->height; ++y)
|
|
{
|
|
for (unsigned x = 0; x < mMemory->width; ++x)
|
|
{
|
|
const unsigned idx = y * mMemory->width + x;
|
|
const unsigned byte = idx / 8;
|
|
const unsigned bit = idx % 8;
|
|
|
|
if (glyphData[byte] & (0b10000000 >> bit))
|
|
{
|
|
draw(x, y, fgColor);
|
|
}
|
|
else
|
|
{
|
|
draw(x, y, bgColor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif // !defined(BAD_APPLE_OS_PSF_HPP_INCLUDED)
|