correct for narrow chars on fonts
This commit is contained in:
@@ -525,22 +525,34 @@ void LowLevelRenderer::draw_filled_circle(int x, int y, int radius, bool on)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LowLevelRenderer::draw_char_vcol(int x, int y, char c)
|
int LowLevelRenderer::draw_char_vcol(int x, int y, char c)
|
||||||
{
|
{
|
||||||
if (!current_font) return;
|
if (!current_font) return 0;
|
||||||
|
|
||||||
// The font table starts at space (ASCII 32)
|
// The font table starts at space (ASCII 32)
|
||||||
if (c < 32 || c > 127)
|
if (c < 32 || c > 127)
|
||||||
return;
|
return 0;
|
||||||
int font_idx = c - 32;
|
int font_idx = c - 32;
|
||||||
|
|
||||||
const unsigned char* char_data = current_font->get_char_data(font_idx);
|
const unsigned char* char_data = current_font->get_char_data(font_idx);
|
||||||
if (!char_data) return;
|
if (!char_data) return 0;
|
||||||
|
|
||||||
int bytes_per_char = current_font->get_bytes_per_char();
|
int bytes_per_char = current_font->get_bytes_per_char();
|
||||||
int char_height = current_font->get_char_height();
|
int char_height = current_font->get_char_height();
|
||||||
|
|
||||||
for (int col = 0; col < bytes_per_char; col++)
|
// Find the actual width by skipping trailing empty columns
|
||||||
|
int actual_width = 0;
|
||||||
|
for (int col = bytes_per_char - 1; col >= 0; col--)
|
||||||
|
{
|
||||||
|
if (char_data[col] != 0)
|
||||||
|
{
|
||||||
|
actual_width = col + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw only up to the actual width
|
||||||
|
for (int col = 0; col < actual_width; col++)
|
||||||
{
|
{
|
||||||
unsigned char column_byte = char_data[col];
|
unsigned char column_byte = char_data[col];
|
||||||
|
|
||||||
@@ -553,35 +565,50 @@ void LowLevelRenderer::draw_char_vcol(int x, int y, char c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return actual_width;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LowLevelRenderer::draw_string(int x, int y, const std::string &text, int spacing)
|
void LowLevelRenderer::draw_string(int x, int y, const std::string &text, int spacing)
|
||||||
{
|
{
|
||||||
if (!current_font) return;
|
if (!current_font) return;
|
||||||
int char_width = current_font->get_bytes_per_char();
|
int current_x = x;
|
||||||
for (size_t i = 0; i < text.length(); i++)
|
for (size_t i = 0; i < text.length(); i++)
|
||||||
{
|
{
|
||||||
draw_char_vcol(x + (i * (char_width + spacing)), y, text[i]);
|
int char_width = draw_char_vcol(current_x, y, text[i]);
|
||||||
|
current_x += char_width + spacing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LowLevelRenderer::draw_char_scaled(int x, int y, char c, int scale)
|
int LowLevelRenderer::draw_char_scaled(int x, int y, char c, int scale)
|
||||||
{
|
{
|
||||||
if (!current_font) return;
|
if (!current_font) return 0;
|
||||||
|
|
||||||
if (c < 32 || c > 127)
|
if (c < 32 || c > 127)
|
||||||
return;
|
return 0;
|
||||||
if (scale < 1)
|
if (scale < 1)
|
||||||
scale = 1; // Safety check
|
scale = 1; // Safety check
|
||||||
|
|
||||||
int font_idx = c - 32;
|
int font_idx = c - 32;
|
||||||
const unsigned char* char_data = current_font->get_char_data(font_idx);
|
const unsigned char* char_data = current_font->get_char_data(font_idx);
|
||||||
if (!char_data) return;
|
if (!char_data) return 0;
|
||||||
|
|
||||||
int bytes_per_char = current_font->get_bytes_per_char();
|
int bytes_per_char = current_font->get_bytes_per_char();
|
||||||
int char_height = current_font->get_char_height();
|
int char_height = current_font->get_char_height();
|
||||||
|
|
||||||
for (int col = 0; col < bytes_per_char; col++)
|
// Find the actual width by skipping trailing empty columns
|
||||||
|
int actual_width = 0;
|
||||||
|
for (int col = bytes_per_char - 1; col >= 0; col--)
|
||||||
|
{
|
||||||
|
if (char_data[col] != 0)
|
||||||
|
{
|
||||||
|
actual_width = col + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw only up to the actual width, scaled
|
||||||
|
for (int col = 0; col < actual_width; col++)
|
||||||
{
|
{
|
||||||
unsigned char column_byte = char_data[col];
|
unsigned char column_byte = char_data[col];
|
||||||
|
|
||||||
@@ -602,18 +629,19 @@ void LowLevelRenderer::draw_char_scaled(int x, int y, char c, int scale)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return actual_width * scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LowLevelRenderer::draw_string_scaled(int x, int y, const char* text, int scale, int spacing)
|
void LowLevelRenderer::draw_string_scaled(int x, int y, const char* text, int scale, int spacing)
|
||||||
{
|
{
|
||||||
if (!current_font) return;
|
if (!current_font) return;
|
||||||
int char_width = current_font->get_bytes_per_char();
|
int current_x = x;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(text[i] != '\0')
|
while(text[i] != '\0')
|
||||||
{
|
{
|
||||||
// We multiply the character width and spacing by the scale
|
int char_width = draw_char_scaled(current_x, y, text[i], scale);
|
||||||
int next_x = x + (i * (char_width + spacing) * scale);
|
current_x += char_width + (spacing * scale);
|
||||||
draw_char_scaled(next_x, y, text[i], scale);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,9 +140,9 @@ public:
|
|||||||
|
|
||||||
void draw_circle(int x, int y, int radius, bool on);
|
void draw_circle(int x, int y, int radius, bool on);
|
||||||
void draw_filled_circle(int x, int y, int radius, bool on);
|
void draw_filled_circle(int x, int y, int radius, bool on);
|
||||||
void draw_char_vcol(int x, int y, char c);
|
int draw_char_vcol(int x, int y, char c);
|
||||||
void draw_string(int x, int y, const std::string &text, int spacing = 1);
|
void draw_string(int x, int y, const std::string &text, int spacing = 1);
|
||||||
void draw_char_scaled(int x, int y, char c, int scale);
|
int draw_char_scaled(int x, int y, char c, int scale);
|
||||||
void draw_string_scaled(int x, int y, const char* text, int scale, int spacing = 1);
|
void draw_string_scaled(int x, int y, const char* text, int scale, int spacing = 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
2
main.cpp
2
main.cpp
@@ -149,7 +149,7 @@ int main()
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Text with different fonts
|
// Text with different fonts
|
||||||
renderer.set_font(&font_HISKYF21_obj);
|
renderer.set_font(&font_acme_5_outlines_obj);
|
||||||
renderer.draw_string_scaled(10, 10, "Drawing Demo", 2);
|
renderer.draw_string_scaled(10, 10, "Drawing Demo", 2);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user