2018-10-11 23:45:57 +02:00
|
|
|
#include "Font.h"
|
|
|
|
#include "Peanut8x10.h"
|
2019-01-25 15:12:23 +01:00
|
|
|
#include "Liza8x10.h"
|
2019-02-02 17:42:16 +01:00
|
|
|
#include <AK/kmalloc.h>
|
2019-01-25 15:12:23 +01:00
|
|
|
|
|
|
|
#define DEFAULT_FONT_NAME Liza8x10
|
2019-01-10 23:19:29 +01:00
|
|
|
|
2019-02-02 08:05:14 +01:00
|
|
|
static const byte error_glyph_width = 8;
|
|
|
|
static const byte error_glyph_height = 10;
|
|
|
|
static constexpr const char* error_glyph {
|
|
|
|
" #### "
|
|
|
|
" # # "
|
|
|
|
" # # "
|
|
|
|
" # ## # "
|
|
|
|
" # ## # "
|
|
|
|
" #### "
|
|
|
|
" ## "
|
|
|
|
" ###### "
|
|
|
|
" ## "
|
|
|
|
" ## ",
|
|
|
|
};
|
|
|
|
|
2019-01-10 23:19:29 +01:00
|
|
|
static Font* s_default_font;
|
|
|
|
|
|
|
|
void Font::initialize()
|
|
|
|
{
|
|
|
|
s_default_font = nullptr;
|
|
|
|
}
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2019-01-16 17:54:06 +01:00
|
|
|
Font& Font::default_font()
|
2018-10-11 23:45:57 +02:00
|
|
|
{
|
2019-01-10 23:19:29 +01:00
|
|
|
if (!s_default_font)
|
2019-01-31 17:31:23 +01:00
|
|
|
s_default_font = adopt(*new Font(DEFAULT_FONT_NAME::glyphs, DEFAULT_FONT_NAME::glyph_width, DEFAULT_FONT_NAME::glyph_height, DEFAULT_FONT_NAME::first_glyph, DEFAULT_FONT_NAME::last_glyph)).leak_ref();
|
2019-01-10 23:19:29 +01:00
|
|
|
return *s_default_font;
|
2018-10-11 23:45:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-02 08:05:14 +01:00
|
|
|
RetainPtr<Font> Font::clone() const
|
|
|
|
{
|
|
|
|
size_t bytes_per_glyph = glyph_width() * glyph_height();
|
|
|
|
// FIXME: This is leaked!
|
2019-02-02 17:42:16 +01:00
|
|
|
char** new_glyphs = static_cast<char**>(kmalloc(sizeof(char*) * 256));
|
2019-02-02 08:05:14 +01:00
|
|
|
for (unsigned i = 0; i < 256; ++i) {
|
2019-02-02 17:42:16 +01:00
|
|
|
new_glyphs[i] = static_cast<char*>(kmalloc(bytes_per_glyph));
|
2019-02-02 08:05:14 +01:00
|
|
|
if (i >= m_first_glyph && i <= m_last_glyph) {
|
|
|
|
memcpy(new_glyphs[i], m_glyphs[i - m_first_glyph], bytes_per_glyph);
|
|
|
|
} else {
|
|
|
|
memset(new_glyphs[i], ' ', bytes_per_glyph);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adopt(*new Font(new_glyphs, m_glyph_width, m_glyph_height, 0, 255));
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:54:06 +01:00
|
|
|
Font::Font(const char* const* glyphs, byte glyph_width, byte glyph_height, byte first_glyph, byte last_glyph)
|
2018-10-11 23:45:57 +02:00
|
|
|
: m_glyphs(glyphs)
|
2019-01-16 17:54:06 +01:00
|
|
|
, m_glyph_width(glyph_width)
|
|
|
|
, m_glyph_height(glyph_height)
|
|
|
|
, m_first_glyph(first_glyph)
|
|
|
|
, m_last_glyph(last_glyph)
|
2018-10-11 23:45:57 +02:00
|
|
|
{
|
2019-02-02 08:05:14 +01:00
|
|
|
ASSERT(m_glyph_width == error_glyph_width);
|
|
|
|
ASSERT(m_glyph_height == error_glyph_height);
|
|
|
|
m_error_bitmap = CharacterBitmap::create_from_ascii(error_glyph, error_glyph_width, error_glyph_height);
|
2019-01-27 05:14:15 +01:00
|
|
|
for (unsigned ch = 0; ch < 256; ++ch) {
|
|
|
|
if (ch < m_first_glyph || ch > m_last_glyph) {
|
2019-01-31 17:31:23 +01:00
|
|
|
m_bitmaps[ch] = m_error_bitmap.copy_ref();
|
2019-01-27 05:14:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char* data = m_glyphs[(unsigned)ch - m_first_glyph];
|
|
|
|
m_bitmaps[ch] = CharacterBitmap::create_from_ascii(data, m_glyph_width, m_glyph_height);
|
|
|
|
}
|
2018-10-11 23:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::~Font()
|
|
|
|
{
|
|
|
|
}
|