Creating my own font library

Hello,

for my still-ongoing Arduino trip computer project, I am in the process of creating my own RGB565 font libraries to be used with an ESP32, which will have well enough memory to accomodate a certain number of different fonts in flash memory in 16-bit color. They need to be anti-aliased (most importantly) and in full color, so the standard Arduino or ST7735 font libraries simply won't do for this project

As with a few things on this project, setting up my very own font library is kind of uncharted territory for me.

At the moment, my working hypothesis is that it might work like this:

typedef struct{

char character;
uint8_t* charID;
uint16_t charData;
byte height;
byte width;
}charData;


const uint16_t myFontNr1[][] PROGMEM = {

{0x2b},
{0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 


// [...]

    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff}

{0x2c},
{0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
   

// [...]

    0xffff, 0xffff, 0xfdad, 0x8841, 0x884d, 0xdfff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xec87, 0x8841, 0xa49b, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xfffb, 0xc9e1, 0x884d, 0xdfff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 
    0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff}


//..... and many more different characters for every font

}

//... and then a lookup array using the struct as defined above:


const charData Font_array[][] = {

{"+", 0x2b, 32, 29},      // Actual character, character ID in font pane array, pixel height, pixel width

{",", 0x2c, 32, 8},

// ...and do this for every character in the character set

}

I would then use a function like this to print the text out on the 1.8'' ST7735 TFT display:

void printMyText(String myText, String whichFont, byte x_pos, byte y_pos);

in which the text string would be split up into a character array, and then for each element in this chararcter array, I would go through my Font_array[][], find the corresponding character ID in it, and then go to my font pane array where I would retrieve the 16-bit RGB565 pixel color definitions that correspond to the respective character ID, and push those pixels straight onto the screen, one character after another.

Any thoughts on this approach?

  • carguy

Go for it. Don't use the String class though.

so there's nothing fundamentally wrong with doing it this way?

aarg:
Go for it. Don't use the String class though.

So how would you do that bit?

carguy:
so there's nothing fundamentally wrong with doing it this way?

It's a pointless question, just code it up and try. You're not too far from a test program.

carguy:
So how would you do that bit?

Use cstring objects and methods everywhere you would use String objects and methods.
For your information:
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html#ga81577c743915e4fb8759ef9081f10838

OK so I have given my approach a bit of fine tuning, and one of the results has been the .h file that you can see attached at the bottom of this post.

Now what's the best way to actually access an array element when I want to print out the characters one-by-one?

Do I have to loop through my const charData_SF19PtRed SF19PtRed[][] at the bottom of my .h file each time to match my character against the corresponding array element number?

SF19PtRed.h (73 KB)