FTDI Community

Please login or register.

Login with username, password and session length.
Advanced Search  

News:

Welcome to the FTDI Community!

Please read our Welcome Note

Technical Support enquires
please contact the team
@ FTDI Support


New Bridgetek Community is now open

Please note that we have created the Bridgetek Community to discuss all Bridgetek products e.g. EVE, MCU.

Please follow this link and create a new user account to get started.

Bridgetek Community

Author Topic: FT813 Custome Font Text  (Read 2567 times)

jberkhout

  • Newbie
  • *
  • Posts: 35
    • View Profile
FT813 Custome Font Text
« on: September 07, 2019, 09:09:59 PM »

Hi, I use a custom font.
The library I use is based on the one from Rudolph Riedel (Thank you very much!):
https://github.com/RudolphRiedel/FT800-FT813

I use STM32H7 (480MHz) and drive the display using Quad-SPI (4-bit).
For the CD card, I use the SDMMC (4-bit) interface, and FATFS.

I created the font with command:
fnt_cvt.exe -i SourceCodePro-Regular.ttf -s 40 -u characters.txt -d 1000
The file characters.txt contains this string (excluding the first and last double-quote):
" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[|]^_`abcdefghijklmnopqrstuvwxyz{|}~"

I load the raw file (SourceCodePro-Regular.ttf_40_L4.raw) into RAM_G.
When printing strings, all works fine until the printed character value reaches 96, after which the characters are printed over each other (so the cursor-position didn't increment to the next horizontal character position). To illustrate it, I have attached a photo from the screen.

In the top three lines, I first print the characters individually on a calculated position. That works, all characters are printable.

The fourth line should read as: "0123456789 The quick brown fox jumps over the lazy dog", but problems start beyond character value 96, with the lower case characters, being printed over each other.
The 7th line should read as "abcdefghijklmnopqrstuvwxyz", but shows those characters all printed over each other on one place.
In the last line I take a string with the character 96 somewhere near the end ("XYZ[|]^_`abc"). The first part goes well until character 96.

My code:

Code: [Select]
/*
 * This example shows the use of the SETFONT2 command.
 * Simpler method to load RAM font. Use the font conversion utility to convert the desired
 * subset of the ASCII characters, load font data, and use cmd_setfont2 command.
 * In characters.txt
 *  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[|]^_`abcdefghijklmnopqrstuvwxyz{|}~
 */
void Ft813Setfont40(void)
{
  /*
   * The converted font file will have a starting address of
   * RAM_G or 0th byte mark
   */
  uint16_t fontFileAddress = RAM_G + 1000;
  Ft813DLstart();
  Ft813DL(CLEAR_COLOR_RGB(0, 0, 0));
  Ft813DL(CLEAR(1, 1, 1));
  Ft813DL(COLOR_RGB(255, 255, 255));
  Ft813LoadRawFile(fontFileAddress, "SourceCodePro-Regular.ttf_40_L4.raw");      /* Load font data */
  /*
   *
   * Data consists of font metric block and font data from the FTDI font conversion utility. The
   * file has been packaged with the font metric block at the beginning of the font data.
   */
  Ft813DL(BITMAP_HANDLE(1));    /* Associate to font handle 1 */
  /*
   * The starting address for the font file 148 bytes into the font file because the
   * first 148 bytes are font metric informational block.
   */
  Ft813DL(BITMAP_SOURCE(fontFileAddress + 148));    /* The starting address for

  /* Font stride, width and height metrics can be found in the .rawh file */
  uint16_t stride = 12;
  uint16_t width = 24;
  uint16_t height = 50;
  Ft813DL(BITMAP_LAYOUT(L4, stride, height));
  Ft813DL(BITMAP_SIZE(NEAREST, BORDER, BORDER, width, height));
  Ft813SetFont2(1, fontFileAddress, 31);
  /*
   * Set ram font to font handle 1, font metric block starts at fontFileAddress(RAM_G),
   * and set the starting character to the 32th character in the set.
   */
  uint16_t x = 10;
  for(uint8_t i = 0; i < 32; i++)
  {
    char str[2];
    str[0] = i + 0x20;
    str[1] = 0;
    Ft813Text(x, 10, 1, 0, str);
    str[0] = i + 0x20 + 32;
    str[1] = 0;
    Ft813Text(x, 10 + height*1, 1, 0, str);
    if (i + 0x20 + 64 < 127)    /* Do not print space character (0x20) */
    {
      str[0] = i + 0x20 + 64;
      str[1] = 0;
      Ft813Text(x, 10 + height*2, 1, 0, str);
    }
    x += width;
  }
  Ft813Text(10, 10 + height*4, 1, 0, "0123456789 The quick brown fox jumps over the lazy dog");
  Ft813Text(10, 10 + height*5, 1, 0, "The quick brown fox jumps over the lazy dog");
  Ft813Text(10, 10 + height*6, 1, 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  Ft813Text(10, 10 + height*7, 1, 0, "abcdefghijklmnopqrstuvwxyz");
  Ft813Text(10, 10 + height*8, 1, 0, "XYZ[|]^_`abc");

  Ft813DL(DISPLAY());         /* Display the image */
  Ft813Swap();                /* Swap the current display list */
  Ft813Flush_Co_Buffer();     /* Download the commands into fifo */
  Ft813WaitCmdFifoEmpty();    /* Wait till coprocessor completes the operation */
}


The function Ft813Text:

Code: [Select]
/* Draw Text: FT81x Series Programmers Guide Section 5.41 */
void Ft813Text(int16_t x, int16_t y, int16_t font, uint16_t options, const char* s)
{
  Ft813StartFunc(FT_CMD_SIZE * 3 + strlen(s) + 1);
  Ft813SendCmd(CMD_TEXT); // 4294967052 = FFFFFF0C
  Ft813SendCmd((((uint32_t) y << 16) | (x & 0xffff)));
  Ft813SendCmd((((uint32_t) options << 16) | (uint32_t) font));
  Ft813SendStr(s);
  Ft813EndFunc();
}
Probably, I'm overlooking something.
Note, in the function Ft813SetFont2, I specified 31 as first character, perhaps there is a limit on the character value of 127.


Update: I tried font-size 200, characters "+,-./0123456789:"

Code: [Select]
  uint16_t fontFileAddress = RAM_G + 0;
  uint16_t stride = 60;
  uint16_t width = 120;
  uint16_t height = 251;
  Ft813DL(BITMAP_LAYOUT(L4, stride, height));
  Ft813DL(BITMAP_SIZE(NEAREST, BORDER, BORDER, width, height));
  Ft813SetFont2(1, fontFileAddress, 42); //set ram font to font handle 1, font

Now the numbers 0-9 are printed over each other, so past 127 character value is not the problem.

I also noticed that for Ft813LoadRawFile, I have to use address  RAM_G (+0), while for font-size 40 I have to use address RAM_G + 1000.
I found out by experimenting this. The code is otherwise the same as above.

That function looks like:

Code: [Select]
int Ft813LoadRawFile(uint32_t address, const char *filename)
{
unsigned int bufferSize = FT813_MAX_FILE_BLOCK_SIZE;
uint32_t filesize = 0;
uint16_t blocklen = 0;

_address = address;
uint32_t addr = address;

#ifdef USE_POSIX
FILE *fp = fopen(filename, "rb");
if (fp == NULL)
{
return 0;
}

// Get file size
fseek(fp, 0, SEEK_END);// End of file
filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);// Beginning of file
#endif

#ifdef USE_FATFS
FIL fil; /* File object */
FRESULT res; /* API result code */
res = f_open(&fil, filename, FA_READ);
if (res != FR_OK)
{
return 0;
}

// Get file size
filesize = f_size(&fil);
#endif

// Keep track of of the start addresses of loaded images
// Increment _address
_addresses[_bitmap_count] = _address;
_bitmap_count++;
_address += (uint32_t) filesize;
_addresses[_bitmap_count] = _address;

char* pbuff = (char*) malloc(bufferSize);
while (filesize > 0)
{
blocklen = filesize > bufferSize ? bufferSize : filesize;

#ifdef USE_POSIX
fread(pbuff, 1, blocklen, fp);
#endif

#ifdef USE_FATFS
UINT bytesRead = 1; /* Bytes read */
res = f_read(&fil, pbuff, blocklen, &bytesRead);
#endif

filesize -= blocklen;
Ft813WrMem(addr, (uint8_t *) pbuff, blocklen);
addr += blocklen;
//        WrCmdBuf((uint8_t *)pbuff, blocklen); //alignment is already taken care by this api
}

#ifdef USE_POSIX
fclose(fp); /* close the opened jpg file */
#endif

#ifdef USE_FATFS
res = f_close(&fil);
#endif

free(pbuff);

return 1;
}

So my questions are:
How can I make printing a string working correctly beyond character 96 (apart from making a function using a loop and keeping track of character positions)?
What is the reason behind the required difference in loading address for different fonts, and how can I calculate the correct address?
Why are characters in a string printed over each other on the same cursor position, using the Ft813Text function above?

Has anybody used custom characters successfully, without these issues?

« Last Edit: September 08, 2019, 06:30:05 PM by jberkhout »
Logged

FTDI Community

  • Administrator
  • Sr. Member
  • *****
  • Posts: 341
    • View Profile
Re: FT813 Custome Font Text
« Reply #1 on: September 10, 2019, 04:09:11 PM »

Hello,

Rudolph may be able to advise you on his Library's operation, but it looks like maybe a font width issue.

You can find a draft of an upcoming application note which covers the use of special font characters at the following:
BRT_AN_042.zip

Please let me know if this helps.

Best Regards,
FTDI Community

EDIT: the hyperlink seems to be messing with the URL, if you copy it into your browser it should work fine.
« Last Edit: September 13, 2019, 03:12:43 PM by FTDI Community »
Logged

jberkhout

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: FT813 Custome Font Text
« Reply #2 on: September 10, 2019, 06:04:18 PM »

Thank you. The link to BRT_AN_042.zip does not work unfortunately.

I don't understand this answer.
As one easily can notice, Rudolph follows the standard FTDI API, and does not handle the width of individual characters in function Ft813Text.
Therefore his code can't be blamed anyway.

That's why I posted here, as I did not expect problems with Rudolph library, but with FTDI's font convert utility instead.

Looking a bit further, the below confirms that FTDI's font convert utility is actually the culprit:
In SourceCodePro-Regular.ttf_40_L4.rawh, the below line exactly explains the problem:

/* Widths */
0,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,
24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,
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,

It has invalid width for many glyphs (width = 0)!

The same problem is in the .raw file.
After editing the raw file with a hex-editor and fixing the wrong font-widths, it works!
I changed the values inside the red box from 0x00 to the correct width 0x18 (mono-space font here).

Perhaps the font convert utility can be corrected?
In the mean time I will create my own font convert utility (with GUI).
« Last Edit: September 11, 2019, 09:16:52 AM by jberkhout »
Logged

FTDI Community

  • Administrator
  • Sr. Member
  • *****
  • Posts: 341
    • View Profile
Re: FT813 Custome Font Text
« Reply #3 on: September 11, 2019, 10:52:11 AM »

Hello,

Copying the URL into your browser should work as noted in the last post.
It covers pulling the font widths from the data and printing the characters correctly with these widths.

I will pass this on to the development team to see if they can adjust the font convertor utility accordingly.

Best Regards,
FTDI Community
Logged

jberkhout

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: FT813 Custome Font Text
« Reply #4 on: September 11, 2019, 06:12:23 PM »

Hi thank you very much!
BTW, pasting the URL helps, except it then asks for a username and password, and it doesn't accept what I use over here.
Do you have any ideas?
Logged

FTDI Community

  • Administrator
  • Sr. Member
  • *****
  • Posts: 341
    • View Profile
Re: FT813 Custome Font Text
« Reply #5 on: September 12, 2019, 12:19:49 PM »

Hello,

Sorry, must have pasted the link back in without the password after a couple minutes of fighting with the hyperlink.
This one should work now:
BRT_AN_042.zip

Best Regards,
FTDI Community
« Last Edit: September 13, 2019, 03:13:12 PM by FTDI Community »
Logged

jberkhout

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: FT813 Custome Font Text
« Reply #6 on: October 04, 2019, 09:19:03 AM »

Hi, I tested the utility used in the upcoming application note BRT_AN_042.zip, and it has exactly the same bug, I think the same utility/code is used.
Logged

FTDI Community

  • Administrator
  • Sr. Member
  • *****
  • Posts: 341
    • View Profile
Re: FT813 Custome Font Text
« Reply #7 on: October 07, 2019, 01:26:36 PM »

Hello,

I will pass this on to the development team so they can look into it.

Best Regards,
FTDI Community
Logged