Partially rework fonts (again)

This commit is contained in:
2026-02-13 23:57:42 +01:00
parent 2d03316910
commit 3668854cd4
12 changed files with 324 additions and 277 deletions
+40 -21
View File
@@ -2521,11 +2521,10 @@ int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t dp, int32_t poX, int32_t
}
void TFT_eSPI::setTextFont(uint8_t f) {
textfont = (f > 7) ? 1 : f; // Don't allow font > 7
textfont = (f > FONT_COUNT) ? 1 : f; // Don't allow font > 7
}
void TFT_eSPI::loadFont(const uint8_t array[], uint8_t font)
{
void TFT_eSPI::loadFont(const uint8_t array[], uint8_t font) {
if (array == nullptr) return;
fontPtr = (uint8_t*)array;
unloadFont(font);
@@ -2593,45 +2592,50 @@ void TFT_eSPI::loadMetrics(uint8_t font)
gFonts[font].spaceWidth = (gFonts[font].ascent + gFonts[font].descent) * 2/7; // Guess at space width
}
void TFT_eSPI::unloadFont(uint8_t font)
{
void TFT_eSPI::unloadFont(uint8_t font) {
if (!fontOwned[font]) {
gUnicode[font] = NULL;
gHeight[font] = NULL;
gWidth[font] = NULL;
gxAdvance[font] = NULL;
gdY[font] = NULL;
gdX[font] = NULL;
gBitmap[font] = NULL;
gFonts[font].gArray = nullptr;
return;
}
if (gUnicode[font]) {
free(gUnicode[font]);
gUnicode[font] = NULL;
}
if (gHeight[font])
{
if (gHeight[font]) {
free(gHeight[font]);
gHeight[font] = NULL;
}
if (gWidth[font])
{
if (gWidth[font]) {
free(gWidth[font]);
gWidth[font] = NULL;
}
if (gxAdvance[font])
{
if (gxAdvance[font]) {
free(gxAdvance[font]);
gxAdvance[font] = NULL;
}
if (gdY[font])
{
if (gdY[font]) {
free(gdY[font]);
gdY[font] = NULL;
}
if (gdX[font])
{
if (gdX[font]) {
free(gdX[font]);
gdX[font] = NULL;
}
if (gBitmap[font])
{
if (gBitmap[font]) {
free(gBitmap[font]);
gBitmap[font] = NULL;
}
@@ -3079,7 +3083,7 @@ TFT_eSprite::TFT_eSprite(TFT_eSPI *tft) {
_iwidth = 0; // Initialise width and height to 0 (it does not exist yet)
_iheight = 0;
_bpp = 16;
_swapBytes = true; // Do not swap pushImage colour bytes by default
_swapBytes = true;
_created = false;
_vpOoB = true;
@@ -3306,8 +3310,7 @@ void TFT_eSprite::deleteSprite() {
}
}
void TFT_eSprite::pushSprite(int32_t x, int32_t y)
{
void TFT_eSprite::pushSprite(int32_t x, int32_t y) {
if (!_created) return;
if (_bpp == 16) {
@@ -4335,4 +4338,20 @@ void TFT_eSprite::drawGlyph(uint16_t code, uint16_t font) {
}
bg_cursor_x = cursor_x;
last_cursor_x = cursor_x;
}
}
void TFT_eSprite::copyFontFromTFT(uint8_t source, uint8_t destination) {
unloadFont(destination); // Make sure there is nothing there
gUnicode[destination] = _tft->gUnicode[source];
gHeight[destination] = _tft->gHeight[source];
gWidth[destination] = _tft->gWidth[source];
gxAdvance[destination] = _tft->gxAdvance[source];
gdY[destination] = _tft->gdY[source];
gdX[destination] = _tft->gdX[source];
gBitmap[destination] = _tft->gBitmap[source];
memcpy(&gFonts[destination], &_tft->gFonts[source], sizeof(fontMetrics));
fontOwned[destination] = false;
}
+13 -8
View File
@@ -1,5 +1,7 @@
#pragma once
#define FONT_COUNT 7
#include <Arduino.h>
#define SPI_FREQUENCY 7500000
@@ -398,7 +400,7 @@ class TFT_eSPI { friend class TFT_eSprite;
uint16_t maxDescent; // Maximum descent found in font
} fontMetrics;
fontMetrics gFonts[7] = {
fontMetrics gFonts[FONT_COUNT] = {
{ nullptr, 0, 0, 0, 0, 0, 0, 0 },
{ nullptr, 0, 0, 0, 0, 0, 0, 0 },
{ nullptr, 0, 0, 0, 0, 0, 0, 0 },
@@ -409,13 +411,15 @@ class TFT_eSPI { friend class TFT_eSprite;
};
// These are for the metrics for each individual glyph (so we don't need to seek this in file and waste time)
uint16_t* gUnicode[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //UTF-16 code, the codes are searched so do not need to be sequential
uint8_t* gHeight[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //cheight
uint8_t* gWidth[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //cwidth
uint8_t* gxAdvance[7] = {NULL, NULL, NULL, NULL, NULL, NULL}; //setWidth
int16_t* gdY[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //topExtent
int8_t* gdX[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //leftExtent
uint32_t* gBitmap[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //file pointer to greyscale bitmap
uint16_t* gUnicode[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //UTF-16 code, the codes are searched so do not need to be sequential
uint8_t* gHeight[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //cheight
uint8_t* gWidth[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //cwidth
uint8_t* gxAdvance[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL}; //setWidth
int16_t* gdY[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //topExtent
int8_t* gdX[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //leftExtent
uint32_t* gBitmap[FONT_COUNT] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; //file pointer to greyscale bitmap
bool fontOwned[FONT_COUNT] = {false};
uint8_t getTouchRaw(uint16_t *x, uint16_t *y);
uint16_t getTouchRawZ();
@@ -539,6 +543,7 @@ class TFT_eSprite : public TFT_eSPI {
int16_t width(),
height();
void drawGlyph(uint16_t code, uint16_t font);
void copyFontFromTFT(uint8_t source, uint8_t destination);
private: