BRT Community

Please login or register.

Login with username, password and session length
Advanced search  


Welcome to the Bridgetek Community!

Please read our Welcome Note

Technical Support enquires
please contact the team
@ Bridgetek Support

Please refer to our website for detailed information on all our products - Bridgetek - Bridging Technology

Author Topic: Calculate String Width in Custom Font  (Read 429 times)


  • Newbie
  • *
  • Posts: 32
    • View Profile
Calculate String Width in Custom Font
« on: December 16, 2022, 02:47:49 AM »


I'm using 1024 x 600 7" display and using NotoSansCJK custom font with size 37

Font Conversion completed!

Generated Folder: NotoSansCJK-Regular_37_Extend
Format:           COMPRESSED_RGBA_ASTC_8x6_KHR
Compressed:       thorough
Layout Width:     80
Layout Height:    8
Pixel Width:      40
Pixel Height:     48
Number of characters in xfont file:      65408
Number of characters in user input file: 354
   Success: 349
   Fail:    5
   Fail code point: 0x5f00, 0x8a00, 0x9000, 0x9700, 0x9600
There are non-graphic characters filtered out.
NotoSansCJK-Regular_37_ASTC.xfont and NotoSansCJK-Regular_37_ASTC.glyph are generated successfully.


json file
{"name": "NotoSansCJK-Regular_37_ASTC", "type": "extendedfont", "astc_block_footprints": "8x6", "size": 37, "base_line": 33, "caps_height": 28, "mid_line": 20, "x_width": 21, "x_offset": 0, "address_type": "FLASH", "address_glyph": 3925440}

I try to calculate the width of the string from the command


From the information above

"CONNECT FLUID BAGS, UNCLAMP LINES" = 33 characters and based on the converted log pixel width is 40.

33 * 40 = 1320 pixel. This seem not right for me because the display width is 1024 pixels. I'm not sure how the EAB convert the font size and the width for each character.

My goal is try to highlight and change the color of a word in a string (see the attachment) when a flag is set.

If I can get the correct string width in pixels then I can figure out the x/y position of each character so I can draw rects box and change text color dynamically.

Is there any command to get the true width size of a text string or can we set custom font have the same width for each character?

« Last Edit: December 16, 2022, 03:00:26 AM by vdc »


  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Calculate String Width in Custom Font
« Reply #1 on: December 21, 2022, 01:14:28 PM »

Where I can get support this?


  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Calculate String Width in Custom Font
« Reply #2 on: December 22, 2022, 06:59:08 AM »

I am using the width values for each letter which can be found in the xFont file. I read them from RAM and then add them together to get the width of the whole string.
In the programming guide ( you can find the information in "5.4.3 Extended Font Metrics Block". It is the last line of the table.
Those values work very well for me.


  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Calculate String Width in Custom Font
« Reply #3 on: December 22, 2022, 05:21:11 PM »

Hi David,

How do you read each letter in xFont file? You display a single character at one and read from RAM_G to get the width and manually calculate all at time? I confuse the psesudo code from the programming guide.

BRT Community

  • Administrator
  • Hero Member
  • *****
  • Posts: 650
    • View Profile
Re: Calculate String Width in Custom Font
« Reply #4 on: December 23, 2022, 09:22:25 AM »


Here is a small example code to check the width and the address of the characters. The structure has some defines added compared to the programmers guide as the structure is shown for illustration but would not compile directly as it has more than one array in it. We have updated the Programmers Guide for this and are also releasing a short application note soon with this code and some background explanation (as well as a similar application note on doing the same with standard ASCII).

You can cycle through each character getting its width and add them up. You can then calculate the start and end points for your highlight within this data.

Best Regards, BRT Community

Code: [Select]
#define XF_GPTR(xf)                     ( (unsigned int*)&(((int*)xf)[10]) )
#define XF_WPTR(xf)                     ( (unsigned int*) &(((char*)xf)[40 + 4 * (xf->number_of_characters / 128)]))
#define XF_WIDTH(xf)                    ( (unsigned char*)&(((char*)xf)[0]))
typedef struct
       uint32_t signature;// Must be 0x0100AAFF
       uint32_t size;// Total size of the font block, in bytes
       uint32_t format;// Bitmap format, as defined in BITMAP_EXT_FORMAT, except TextVGA,Tex
       uint32_t swizzle;// Bitmap swizzle value
       uint32_t layout_width;//Font bitmap line stride, in bytes
       uint32_t layout_height;//Font bitmap height, in pixels
       uint32_t pixel_width;//Font screen width, in pixels
       uint32_t pixel_height;//Font screen height, in pixels
       uint32_t start_of_graphic_data;//Pointer to font graphic data in memory, including flash.
       uint32_t number_of_characters;//Total number of characters in font: N(multiple of 128)
       //uint32_t gptr[];//Offsets to glyph data
       //uint32_t wptr[];//Offsets to width data
       //uint8_t width_data[];//Width data, one byte per character

uint8_t cp_width(const XFONT_EXTENDED * xf, uint32_t cp)
    uint32_t offset = XF_WPTR(xf)[cp / 128] + (cp % 128);
    return XF_WIDTH(xf)[offset];

uint32_t cp_address(const XFONT_EXTENDED * xf, uint32_t cp)
uint32_t bytes_per_glyph;
bytes_per_glyph = xf->layout_width * xf->layout_height;

if (xf->start_of_graphic_data >= 0x800000)
//if the graphic data is in flash
return (xf->start_of_graphic_data +
(XF_GPTR(xf)[cp / 128] +
bytes_per_glyph * (cp % 128)) / 32);
//if the graphic data is in RAM_G
return (xf->start_of_graphic_data +
XF_GPTR(xf)[cp / 128] +
bytes_per_glyph * (cp % 128));

    // Load the glyph data to RAM_G + 4096
    EVE_LIB_WriteDataToRAMG(glyph_data, sizeof(glyph_data), 4096);
    // Load the xfont data to RAM_G + 0
    EVE_LIB_WriteDataToRAMG(xfont_data, sizeof(xfont_data), 0);

    // Apply the xf structure to the xfont data array
    const XFONT_EXTENDED *xf = (const XFONT_EXTENDED *)xfont_data;

    cp_width_G = cp_width(xf, 0x0047); // letter G = 0x0047
    cp_address_G = cp_address(xf, 0x0047); // Letter G = 0x0047

« Last Edit: December 23, 2022, 04:35:15 PM by BRT Community »