Using Custom Fonts

FTDI/Bridgetek EVE2 & EVE3 & EVE4 SPI TFT Series by Matrix Orbital

Moderator: Mods

amaul
LCD Geek
Posts: 21
Joined: Mon Jun 08, 2020 5:29 pm

Using Custom Fonts

Post by amaul »

Hello Support,

I'm trying to figure out the proper procedure to use a custom font using the Matrix Orbital EVE3 development set and Rudolf Riedel's excellent code library. Here's the procedure I've followed:

1. Using EVE Asset Builder v1.4:
- Input desired font library and selected output folder
- Font Size of 30, no Manual Kerning
- Legacy Format, BT81X, SETFONT, RAM_G + 1000
- Printable ASCII Characters

2. Font is converted, and data from ...L1.rawh is copied into my program as an array:

const uint8_t appfont[13300] PROGMEM =
{
/*The expected raw bitmap size is 13300 Bytes */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ... 0,0,0};

3. At this point, I think I need to inflate the data into GRAM?

#define MEM_FONT 0x00000000

EVE_cmd_inflate(MEM_FONT, appfont, sizeof(appfont));

It's at this point after compiling and downloading the code the application crashes. I haven't made it past this point.

4. Use the font:

EVE_cmd_dl(DL_COLOR_RGB | BLUE);
EVE_cmd_dl(DL_BEGIN | EVE_BITMAPS);
EVE_cmd_setbitmap(MEM_FONT, EVE_L1, 32, 35);
EVE_cmd_setfont(1000, MEM_FONT);
EVE_cmd_text(100, 300, 1000, 0, "TEST FONT");
EVE_cmd_dl(DL_END);

I've seen a handful of examples but most of them use a file from a file and I need to load them from my micro's flash using Rudolph's library. Any pointers would be helpful.

Thanks!

Alex

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

Thank you for the kind words.

1. Why use EAB 1.4 when 1.6 is released? Does not matter much in this context though
You have an EVE3, why bother with legacy format? Why SETFONT and not SETFONT2?
You can even flash the font to the external FLASH of the EVE3.

So I would use Extended Format with ASTC and usually "Full Unicode Characters".
You only need to be a little carefull about your fonts as these could include a lot of glyphs.
There is https://fontdrop.info/ for example to check fonts.
And something like https://www.fontsquirrel.com/tools/webfont-generator could be used to reduce the amount of glyphs in a font.

I did a couple of tests recently and needed a font with not so many glyphs for which 32 pixel version in ASTC would
fit into RAM_G.
One of the fonts I then used was ABeeZee-Regular.otf which has 265 glyphs.
At 32 pixel in ASTC the result is this:
ABeeZee-Regular_32_ASTC.glyph : 4096 : 479808
ABeeZee-Regular_32_ASTC.xfont : 483904 : 4928

Combined with a unified.blob with the EAB "Flash Utilities" the result is a 477kB ABeeZee-Regular_32_ASTC.bin flash-image.

3.Raw data is just that, you would need to copy it to RAM_G, not inflate it.
However, EAB has an "Asset Compressor" that can be used to, well, compress data in the format CMD_INFLATE needs.
And fonts can be very well compressed, even in ASTC format.

To continue from 1) the 477kB ABeeZee-Regular_32_ASTC.bin can be compressed to 24kB.
To convert this binary to source-code I am currently using the export function of "Hex Workshop", not pretty, but does the job well enough.

And of course a different font with less glyphs results in smaller files.
GlacialIndifference-Regular.otf -> 189 glyphs -> 32 pixel ASTC -> 234kB image -> 15kB compressed

4.
Now to use this, have this in the init-function before you load anything else:
EVE_cmd_inflate(0, flash, sizeof(flash)); /* de-compress flash-image to RAM_G */
EVE_cmd_flashupdate(0,0,491520); /* size must be a multiple of 4096 */
EVE_init_flash();

The comment on the second line is important, the image file in this example is exactly 488960 bytes uncompressed.
A call num = EVE_cmd_getptr() would show this number after the EVE_cmd_inflate() to RAM_G and a destination of 0x000.
448960 / 4096 = 119.375
120 * 4096 = 491520 - hence the size for the EVE_cmd_flashupdate()

After this has been executed at least one time the first two lines can be of course removed and the "flash" file commented out or deleted from the project.

Next we need to copy the .xfont file from the external flash to somewhere in RAM_G.
EVE_cmd_flashread(0, 483904, 4928); /* copy .xfont from FLASH to G-RAM */

How do we know where it is in the FLASH and how long it is?
The ABeeZee-Regular_32_ASTC.map file that EAB also generated has this info:
unified.blob : 0 : 4096
ABeeZee-Regular_32_ASTC.glyph : 4096 : 479808
ABeeZee-Regular_32_ASTC.xfont : 483904 : 4928

Note, when you put the files like this into your flash image, with the .glyph first and the .xfont next, you can safely ignore the numeric parameter for "Address of Glyph Data" in EABs font converter as the flash-utility will modify the .xfont file accordingly.

The .xfont does not need to be copied to 0x0000 of RAM_G, this is just my lazy test code.

So after the first run it could look like this:
//EVE_cmd_inflate(0, flash, sizeof(flash)); /* de-compress flash-image to RAM_G */
//EVE_cmd_flashupdate(0,0,491520); /* size must be a multiple of 4096 */
EVE_init_flash();
EVE_cmd_flashread(0, 483904, 4928); /* copy .xfont from FLASH to G-RAM */

Now to use this font there is EVE_cmd_setfont2():
EVE_cmd_setfont2(12,0,32); /* assign bitmap handle to a custom font */

And if you setup your Editor to save your source-files in UTF-8 format you can do this then:
EVE_cmd_text(10,190,12,0, "The quick brown fox 0123456789 äüö");


There is a limit in my currently released library though that I only removed a couple of days ago
for what will be V5 of my library.

If you put the display-list generation inside a EVE_start_cmd_burst(); / EVE_end_cmd_burst(); pair,
you can not use EVE_cmd_setfont2().
I use my initStaticBackground() function for this that generates the static portion of the display list that is included in the generated
list with EVE_cmd_append().

amaul
LCD Geek
Posts: 21
Joined: Mon Jun 08, 2020 5:29 pm

Re: Using Custom Fonts

Post by amaul »

What an incredibly generous response. I will digest and retry, thanks! I'm new to the EVE front, so any help is welcomed.

dood
LCD Geek
Posts: 20
Joined: Fri Oct 30, 2020 5:10 pm

Re: Using Custom Fonts

Post by dood »

Hi,

I'm working on a personnal project (motorcycle laptimer/dataloger) since a long time ago and would like to use EVE tft screens in the next revision :)
I give a try with a 4.3 [EDIT :)] EVE3 TFT screen and Rudolph's library which run on an Arduino M0.

I'm stuck with custom fonts, this thread helps me a lot (what an answer !) but I don't understand where I'm wrong.

Here is some parts of code :

Code: Select all

#define MEM_PIC2  0x00000000
#define MEM_FONT2 0x00215168
Init :

Code: Select all

EVE_init_flash();
EVE_cmd_flashread(MEM_PIC2, 4096, 130560); /* copy from FLASH (read 130560 bits starting offset 4096) to G-RAM (MEM_PIC2) */
EVE_cmd_flashread(MEM_FONT2, 215168, 4288);
initStaticBackground();
display :

Code: Select all

EVE_cmd_setfont2(12, MEM_FONT2, 32);
EVE_cmd_button(20, 20, 80, 30, 12, toggle_state, "Touch 2 !");
The background is working fine but as soon as I'm using custom font everything screw up.

Here is the flash map :
unified.blob : 0 : 4096
878 - Copie_480x272_COMPRESSED_RGBA_ASTC_4x4_KHR.raw : 4096 : 130560
ARCADE_R_12_ASTC.glyph : 134656 : 80512
ARCADE_R_12_ASTC.xfont : 215168 : 4288

I'm using EVE Asset Builder 1.6.0 without "Asset Compressor".
I understand that glyph file could stay in flash but how the program know that, I'm probably missing something.

Thanks BTW for this amazing library :)
Last edited by dood on Sun Nov 01, 2020 6:56 am, edited 1 time in total.

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

I just tried it with a different font again using my latest simple demo code as a starting point.
Of course using an EVE3-43G and not something from that other company you mentioned and no-one ever heard of before. :-)

And going over your post now I found the issue:
#define MEM_FONT2 0x00215168

This is after RAM_G, try:
#define MEM_FONT2 0x0001fe00
or
#define MEM_FONT2 130560

There are alignment requirements in place which I can not find right now.
130560 is aligned to 512 bytes which is a coincidence here as a result of the length of the image.
130561 does not work, or 130562, or 130563.
A value of 130564 does work however which means that .xfont data has to be stored in RAM_G with 4 bytes alignment.

>I understand that glyph file could stay in flash but how the program know that, I'm probably missing something.

As I wrote above, when you add fonts to a flash-image the EVE Asset Builder modifys the .xfont file.
The .xfont file has the offsets to all the character pictures in the .glyph file, so that is where the magic happens.

>Thanks BTW for this amazing library :)

Thank you, I only hope everyone is having at least as much fun with EVE as I do. :-)

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

going over my post above my code looks a little different now:

Code: Select all

#if 0
/* this is only needed once to transfer the flash-image to the external flash */
uint32_t datasize;
EVE_cmd_inflate(0, flash, sizeof(flash)); /* de-compress flash-image to RAM_G */
datasize = EVE_cmd_getptr(); /* we unpacked to RAM_G address 0x0000, so the first address after the unpacked data also is the size */
EVE_cmd_flashupdate(0,0,4096); /* write blob first */
EVE_init_flash();
EVE_cmd_flashupdate(0,0,(datasize|4095)+1); /* size must be a multiple of 4096, so set the lower 12 bits and add 1 */
#endif
EVE_init_flash();
EVE_cmd_flashread(MEM_FONT, 363008, 5056); /* copy .xfont from FLASH to RAM_G, offset and length are from the .map file */
The block that updates the data in the external flash from the controllers memory does not need to be changed anymore
when using a different flash-file.
And it writes the data at full speed even to a freshly erased FLASH as it writes the unified.blob first.
This leaves two obvious limits, the zlib compressed data needs to fit in your controller and the unpacked data needs to be smaller than 1MB.

The flashread line to copy the .xfont to RAM_G needs to be adjusted manually though.

Hmm, I wonder if I should add the project folder to the examples on Github.

dood
LCD Geek
Posts: 20
Joined: Fri Oct 30, 2020 5:10 pm

Re: Using Custom Fonts

Post by dood »

Ok I get it !

Two mistakes :
- I didn't pay attention to "Address Of Glyph Data" in EAB : Glyph data stays on flash and is not copied in RAM, so the address specified here has to be the same when generating flash image.
- My test was inside a EVE_start_cmd_burst(); / EVE_end_cmd_burst(); pair ... yes I know you wrote it in your last post :D

I just saw you release v5 with :
- changed EVE_cmd_setfont2() to a display-list command and added EVE_cmd_setfont2_burst()

Looks like interesting !

dood
LCD Geek
Posts: 20
Joined: Fri Oct 30, 2020 5:10 pm

Re: Using Custom Fonts

Post by dood »

Damn you're answering fast, I didn't see your 2 last posts before writing mine !

Anyway, here is more information about my working example.

Code: Select all

#define MEM_FONT_01 0x00000000
#define MEM_PICT_01 0x00004288

Code: Select all

EVE_init_flash();
EVE_cmd_flashread(MEM_FONT_01, 84608, 4288); /* copy from FLASH (read 4288 bits starting offset 84608) to G-RAM (MEM_FONT_01/.xfont) */
EVE_cmd_flashread(MEM_PICT_01, 88896, 130560);
initStaticBackground();
Inside the initStaticBackground() :

Code: Select all

EVE_cmd_setfont2(12, MEM_FONT_01, 32);
EVE_cmd_text(10,190,12,0, "The quick brown fox");
For beginners like me maybe it could help, here are screenshots of EAB for my example :
Image
Image
Image
Image

My Github, no EVE code on this revision (6.x) but as soon as I get something working, I'll update everything !
Arduino laptimer/datalogger
Last edited by dood on Mon Nov 02, 2020 4:16 pm, edited 1 time in total.

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

- I didn't pay attention to "Address Of Glyph Data" in EAB : Glyph data stays on flash and is not copied in RAM, so the address specified here has to be the same when generating flash image.
Again, no, EAB takes care of that when you put the .glyph and .xfont in a flash file by modifying the .xfont.
I just saw you release v5 with :
- changed EVE_cmd_setfont2() to a display-list command and added EVE_cmd_setfont2_burst()
Yes, when working on the refactoring for V5 I realised that re-setting the font within a display list
is something one might would like to do. :-)
Arduino laptimer/datalogger
Interesting.
Please consider adding .pdf files for the schematics for those out there that do not use EAGLE.

dood
LCD Geek
Posts: 20
Joined: Fri Oct 30, 2020 5:10 pm

Re: Using Custom Fonts

Post by dood »

Rudolph wrote:
Sat Oct 31, 2020 1:23 pm
Again, no, EAB takes care of that when you put the .glyph and .xfont in a flash file by modifying the .xfont.
That's a good news !
Rudolph wrote:
Sat Oct 31, 2020 1:23 pm
Please consider adding .pdf files for the schematics for those out there that do not use EAGLE.
I will add this asap.

I've just installed v5 and give a try with basics but there's something wrong. Hope that's not something stupid, it's late here, will double check tomorrow.

Based on my simplified TFT_display(void) :

Code: Select all

void TFT_display(void) {
  uint16_t display_list_size = 0;

  if (tft_active != 0) {
    display_list_size = EVE_memRead16(REG_CMD_DL);
        EVE_start_cmd_burst(); /* start writing to the cmd-fifo as one stream of bytes, only sending the address once */

        EVE_cmd_dl_burst(CMD_DLSTART); /* start the display list */
        EVE_cmd_dl_burst(DL_CLEAR_RGB | WHITE); /* set the default clear color to white */
        EVE_cmd_dl_burst(DL_CLEAR | CLR_COL | CLR_STN | CLR_TAG); /* clear the screen - this and the previous prevent artifacts between lists, Attributes are the color, stencil and tag buffers */
        EVE_cmd_dl_burst(TAG(0));

        EVE_cmd_append_burst(MEM_DL_STATIC, num_dl_static); /* insert static part of display-list from copy in gfx-mem */
        /* display a button */
        EVE_cmd_dl_burst(DL_COLOR_RGB | WHITE);
        EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
        EVE_cmd_dl_burst(TAG(10)); /* assign tag-value '10' to the button that follows */
        //EVE_cmd_button_burst(20, 20, 80, 30, 28, toggle_state, "Touch!");
        EVE_cmd_dl_burst(TAG(0)); /* no touch */

        EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
        EVE_cmd_text_burst(10, 100, 12, 0, "LOOP - The quick brown fox");

        EVE_cmd_dl_burst(DL_DISPLAY); /* instruct the graphics processor to show the list */
        EVE_cmd_dl_burst(CMD_SWAP); /* make this list active */

        EVE_end_cmd_burst(); /* stop writing to the cmd-fifo, the cmd-FIFO will be executed automatically after this or when DMA is done */
  }
}
This is ok (button only, default example) :

Code: Select all

...
        EVE_cmd_button_burst(20, 20, 80, 30, 28, toggle_state, "Touch!");
        EVE_cmd_dl_burst(TAG(0)); /* no touch */
        //EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
        //EVE_cmd_text_burst(10, 100, 12, 0, "LOOP - The quick brown fox");
This is ok (no button and text with standard font)

Code: Select all

...
        EVE_cmd_button_burst(20, 20, 80, 30, 28, toggle_state, "Touch!");
        EVE_cmd_dl_burst(TAG(0)); /* no touch */
        //EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
        EVE_cmd_text_burst(10, 100, 28, 0, "LOOP - The quick brown fox");
This is not ok (no button and text with specific font) - not freezing but text is not printed on screen

Code: Select all

...
        //EVE_cmd_button_burst(20, 20, 80, 30, 28, toggle_state, "Touch!");
        EVE_cmd_dl_burst(TAG(0)); /* no touch */
        EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
        EVE_cmd_text_burst(10, 100, 12, 0, "LOOP - The quick brown fox");
This is freezing (button and text with specific font)

Code: Select all

...
        EVE_cmd_button_burst(20, 20, 80, 30, 28, toggle_state, "Touch!");
        EVE_cmd_dl_burst(TAG(0)); /* no touch */
        EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
        EVE_cmd_text_burst(10, 100, 12, 0, "LOOP - The quick brown fox");
This is freezing (button and text with standard font)

Code: Select all

...
        EVE_cmd_button_burst(20, 20, 80, 30, 28, toggle_state, "Touch!");
        EVE_cmd_dl_burst(TAG(0)); /* no touch */
        //EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
        EVE_cmd_text_burst(10, 100, 28, 0, "LOOP - The quick brown fox");
The text with specific font is working fine when it's in the initStaticBackground (no burst) :

Code: Select all

void initStaticBackground() {
  EVE_cmd_dl(CMD_DLSTART); /* Start the display list */
  EVE_cmd_dl(TAG(0)); /* do not use the following objects for touch-detection */
  EVE_cmd_bgcolor(0x00c0c0c0); /* light grey */
  EVE_cmd_dl(VERTEX_FORMAT(0)); /* reduce precision for VERTEX2F to 1 pixel instead of 1/16 pixel default */

  /* display the logo */
  EVE_cmd_dl(DL_COLOR_RGB | WHITE);
  EVE_cmd_dl(DL_BEGIN | EVE_BITMAPS);
  EVE_cmd_setbitmap(MEM_PICT_01, EVE_COMPRESSED_RGBA_ASTC_4x4_KHR, 480, 272);
  EVE_cmd_dl(VERTEX2F(0, 0));
  EVE_cmd_setfont2(12, MEM_FONT_01, 32);
  EVE_cmd_text(10, 190, 12, 0, "INIT - The quick brown fox");
  EVE_cmd_dl(DL_END);

  while (EVE_busy());
  num_dl_static = EVE_memRead16(REG_CMD_DL);

  EVE_cmd_memcpy(MEM_DL_STATIC, EVE_RAM_DL, num_dl_static);
  while (EVE_busy());
}

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

I changed my EVE_cmd_setfont2() from the static part to a EVE_cmd_setfont2_burst() in the generated list and it stopped working for me as well.

Turned out that EVE_cmd_setfont2_burst() was using CMD_SETFONT instead of CMD_SETFONT2.
I just pushed a fixed version to Github, thank you for pointing out the issue!

dood
LCD Geek
Posts: 20
Joined: Fri Oct 30, 2020 5:10 pm

Re: Using Custom Fonts

Post by dood »

Well done, thanks !
Custom font works in burst mode ... but ... still freezing when I place a button at the same time.

I removed all the touch part to get somehting very simple like this (based on your example)

Code: Select all

EVE_cmd_button_burst(20, 20, 80, 30, 28, 0, "Touch!");
EVE_cmd_setfont2_burst(12, MEM_FONT_01, 32);
EVE_cmd_text_burst(10, 100, 12, 0, "LOOP - The quick brown fox");
Font without button is ok, button without font is ok.

Can you give a try on your side just to be sure ? maybe I broke something or do something wrong.

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

If you are doing something wrong, I do not see it.

I have been using this:

Code: Select all

...
EVE_color_rgb_burst(WHITE);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(10)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(20,20,80,30, 28, toggle_state,"Touch!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */
EVE_cmd_dl_burst(DL_COLOR_RGB | BLACK);
EVE_cmd_setfont2_burst(12, MEM_FONT, 32);
EVE_cmd_text_burst(10,80,12,0, "Hello UTF-8: €µäöü"); //€µ°");
...
And right now I have this up an running:

Code: Select all

EVE_color_rgb_burst(WHITE);
EVE_cmd_fgcolor_burst(0x00c0c0c0); /* some grey */
EVE_cmd_dl_burst(TAG(10)); /* assign tag-value '10' to the button that follows */
EVE_cmd_button_burst(20,20,80,30, 28, toggle_state,"Touch!");
EVE_cmd_dl_burst(TAG(0)); /* no touch */
EVE_cmd_dl_burst(DL_COLOR_RGB | BLACK);
EVE_cmd_button_burst(20, 120, 80, 30, 28, 0, "Touch!");
EVE_cmd_setfont2_burst(12, MEM_FONT, 32);
EVE_cmd_text_burst(10, 150, 12, 0, "LOOP - The quick brown fox");
EVE_cmd_text_burst(10,80,12,0, "Hello UTF-8: €µäöü"); //€µ°");
I only changed the Y-values when I added your two lines to have them separately.

Okay, this is the complete project that I am currently testing with and that I have happily up
and running on an EVE3-43G right now:
EVE_Test_SAMC21_EVE3-43G_UTF-8.zip
(88.42 KiB) Downloaded 262 times
Here, this is what it looks like:
EVE3-43G_UTF-8.jpg
EVE3-43G_UTF-8.jpg (27.87 KiB) Viewed 3993 times
The font is a notosans-regular that I converted down to 222 glyphs and that was converted with EAB to 24 pixels ASTC.

dood
LCD Geek
Posts: 20
Joined: Fri Oct 30, 2020 5:10 pm

Re: Using Custom Fonts

Post by dood »

That's really strange.

I try with another font, with your TFT_display()/TFT_init() and something is still wrong here.
I'm using SPI with an SDcard, I'll try to remove everything else related to only keep the tft part.

Stay tuned :)

Rudolph
LCD Guru
Posts: 67
Joined: Wed Feb 28, 2018 11:09 am

Re: Using Custom Fonts

Post by Rudolph »

Today at work I had the idea that I might have introduced a bug in the Arduino support.
So I just gave it a spin and it is now up and running an a Seeduino V4.2 with ATMega328P.
It just does run a lot slower but this is to be expected without DMA.
I even re-activated the spinning picture from the sample code.

Here is the project:
EVE_Test_Arduino_PlatformIO_EVE3-43G_UTF-8.zip
(78.46 KiB) Downloaded 266 times
This is practically the same as the one above, only not with a main.c for ATSAMC21 but with a src.ino.

Now I am converting ARCADE_R.ttf.

Post Reply