Add support to different width fonts
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include "./fonts/5x5_font.h"
|
||||
#include "./fonts/7linedigital_font.h"
|
||||
#include "./fonts/BMplain_font.h"
|
||||
#include "./fonts/Blokus_font.h"
|
||||
#include "./fonts/HISKYF21_font.h"
|
||||
@@ -15,12 +14,52 @@
|
||||
#include "./fonts/haiku_font.h"
|
||||
#include "./fonts/sloth_font.h"
|
||||
#include "./fonts/zxpix_font.h"
|
||||
#include "./fonts/Commo-Monospaced_font.h"
|
||||
#include "./fonts/7linedigital_font.h"
|
||||
#include "./fonts/BMSPA_font.h"
|
||||
#include "./fonts/HUNTER_font.h"
|
||||
#include "./fonts/Raumsond_font.h"
|
||||
#include "./fonts/bubblesstandard_font.h"
|
||||
#include "./fonts/formplex12_font.h"
|
||||
#include "./fonts/homespun_font.h"
|
||||
#include "./fonts/Minimum_1_font.h"
|
||||
#include "./fonts/m38_font.h"
|
||||
#include "./fonts/pzim3x5_font.h"
|
||||
#include "./fonts/renew_font.h"
|
||||
#include "./fonts/tama_mini02_font.h"
|
||||
|
||||
// Font object definitions
|
||||
Font font_5x5_obj(reinterpret_cast<const unsigned char*>(font_5x5), 96, 6, 8);
|
||||
Font font_7linedigital_obj(reinterpret_cast<const unsigned char*>(font_7linedigital), 96, 4, 8);
|
||||
Font font_acme_5_outlines_obj(reinterpret_cast<const unsigned char*>(font_acme_5_outlines), 96, 6, 8);
|
||||
Font font_aztech_obj(reinterpret_cast<const unsigned char*>(font_aztech), 96, 6, 8);
|
||||
Font font_BMplain_obj(reinterpret_cast<const unsigned char*>(font_BMplain), 96, 6, 8);
|
||||
Font font_BMSPA_obj(reinterpret_cast<const unsigned char*>(font_BMSPA), 96, 8, 8);
|
||||
Font font_Blokus_obj(reinterpret_cast<const unsigned char*>(font_Blokus), 96, 6, 8);
|
||||
Font font_bubblesstandard_obj(reinterpret_cast<const unsigned char*>(font_bubblesstandard), 96, 7, 8);
|
||||
Font font_Commo_Monospaced_obj(reinterpret_cast<const unsigned char*>(font_Commo_Monospaced), 96, 8, 8);
|
||||
Font font_crackers_obj(reinterpret_cast<const unsigned char*>(font_crackers), 96, 6, 8);
|
||||
Font font_formplex12_obj(reinterpret_cast<const unsigned char*>(font_formplex12), 96, 8, 8);
|
||||
Font font_haiku_obj(reinterpret_cast<const unsigned char*>(font_haiku), 96, 6, 8);
|
||||
Font font_HISKYF21_obj(reinterpret_cast<const unsigned char*>(font_HISKYF21), 96, 6, 8);
|
||||
Font font_homespun_obj(reinterpret_cast<const unsigned char*>(font_homespun), 96, 7, 8);
|
||||
Font font_HUNTER_obj(reinterpret_cast<const unsigned char*>(font_HUNTER), 96, 8, 8);
|
||||
Font font_m38_obj(reinterpret_cast<const unsigned char*>(font_m38), 96, 8, 8);
|
||||
Font font_Minimum_obj(reinterpret_cast<const unsigned char*>(font_Minimum), 96, 6, 8);
|
||||
Font font_Minimum_1_obj(reinterpret_cast<const unsigned char*>(font_Minimum_1), 96, 7, 8);
|
||||
Font font_pzim3x5_obj(reinterpret_cast<const unsigned char*>(font_pzim3x5), 96, 3, 8);
|
||||
Font font_Raumsond_obj(reinterpret_cast<const unsigned char*>(font_Raumsond), 96, 5, 8);
|
||||
Font font_renew_obj(reinterpret_cast<const unsigned char*>(font_renew), 96, 7, 8);
|
||||
Font font_sloth_obj(reinterpret_cast<const unsigned char*>(font_sloth), 96, 6, 8);
|
||||
Font font_SUPERDIG_obj(reinterpret_cast<const unsigned char*>(font_SUPERDIG), 96, 6, 8);
|
||||
Font font_tama_mini02_obj(reinterpret_cast<const unsigned char*>(font_tama_mini02), 96, 5, 8);
|
||||
Font font_zxpix_obj(reinterpret_cast<const unsigned char*>(font_zxpix), 96, 6, 8);
|
||||
|
||||
LowLevelRenderer::LowLevelRenderer(uint8_t* buffer, int width, int height)
|
||||
: bit_buffer(buffer), V_WIDTH(width), V_HEIGHT(height), current_font(&font_acme_5_outlines),
|
||||
: bit_buffer(buffer), V_WIDTH(width), V_HEIGHT(height), current_font(nullptr),
|
||||
clipping_enabled(false), clip_x(0), clip_y(0), clip_width(width), clip_height(height), text_color(true) {}
|
||||
|
||||
void LowLevelRenderer::set_font(const unsigned char (*font)[96][6]) {
|
||||
void LowLevelRenderer::set_font(const Font* font) {
|
||||
current_font = font;
|
||||
}
|
||||
|
||||
@@ -488,19 +527,26 @@ void LowLevelRenderer::draw_filled_circle(int x, int y, int radius, bool on)
|
||||
|
||||
void LowLevelRenderer::draw_char_vcol(int x, int y, char c)
|
||||
{
|
||||
if (!current_font) return;
|
||||
|
||||
// The font table starts at space (ASCII 32)
|
||||
if (c < 32 || c > 127)
|
||||
return;
|
||||
int font_idx = c - 32;
|
||||
|
||||
for (int col = 0; col < 6; col++)
|
||||
{
|
||||
unsigned char column_byte = (*current_font)[font_idx][col];
|
||||
const unsigned char* char_data = current_font->get_char_data(font_idx);
|
||||
if (!char_data) return;
|
||||
|
||||
for (int row = 0; row < 8; row++)
|
||||
int bytes_per_char = current_font->get_bytes_per_char();
|
||||
int char_height = current_font->get_char_height();
|
||||
|
||||
for (int col = 0; col < bytes_per_char; col++)
|
||||
{
|
||||
unsigned char column_byte = char_data[col];
|
||||
|
||||
for (int row = 0; row < char_height; row++)
|
||||
{
|
||||
// Check if the bit for this row is set
|
||||
// Most of these 1-bit fonts use bit 0 as the top pixel
|
||||
if (column_byte & (1 << row))
|
||||
{
|
||||
set_pixel(x + col, y + row, text_color);
|
||||
@@ -511,26 +557,35 @@ void LowLevelRenderer::draw_char_vcol(int x, int y, char c)
|
||||
|
||||
void LowLevelRenderer::draw_string(int x, int y, const std::string &text, int spacing)
|
||||
{
|
||||
if (!current_font) return;
|
||||
int char_width = current_font->get_bytes_per_char();
|
||||
for (size_t i = 0; i < text.length(); i++)
|
||||
{
|
||||
draw_char_vcol(x + (i * (6 + spacing)), y, text[i]);
|
||||
draw_char_vcol(x + (i * (char_width + spacing)), y, text[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void LowLevelRenderer::draw_char_scaled(int x, int y, char c, int scale)
|
||||
{
|
||||
if (!current_font) return;
|
||||
|
||||
if (c < 32 || c > 127)
|
||||
return;
|
||||
if (scale < 1)
|
||||
scale = 1; // Safety check
|
||||
|
||||
int font_idx = c - 32;
|
||||
const unsigned char* char_data = current_font->get_char_data(font_idx);
|
||||
if (!char_data) return;
|
||||
|
||||
for (int col = 0; col < 6; col++)
|
||||
int bytes_per_char = current_font->get_bytes_per_char();
|
||||
int char_height = current_font->get_char_height();
|
||||
|
||||
for (int col = 0; col < bytes_per_char; col++)
|
||||
{
|
||||
unsigned char column_byte = (*current_font)[font_idx][col];
|
||||
unsigned char column_byte = char_data[col];
|
||||
|
||||
for (int row = 0; row < 8; row++)
|
||||
for (int row = 0; row < char_height; row++)
|
||||
{
|
||||
if (column_byte & (1 << row))
|
||||
{
|
||||
@@ -551,11 +606,13 @@ void LowLevelRenderer::draw_char_scaled(int x, int y, char c, int scale)
|
||||
|
||||
void LowLevelRenderer::draw_string_scaled(int x, int y, const char* text, int scale, int spacing)
|
||||
{
|
||||
if (!current_font) return;
|
||||
int char_width = current_font->get_bytes_per_char();
|
||||
int i = 0;
|
||||
while(text[i] != '\0')
|
||||
{
|
||||
// We multiply the character width (6) and spacing by the scale
|
||||
int next_x = x + (i * (6 + spacing) * scale);
|
||||
// We multiply the character width and spacing by the scale
|
||||
int next_x = x + (i * (char_width + spacing) * scale);
|
||||
draw_char_scaled(next_x, y, text[i], scale);
|
||||
i++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user