Search code examples
unicodetouchstm32stm32f7

Issues with subsript/superscript characters (TouchGFX)


I need subscript/superscript characters for the correct display of O₂,CO₂, m² ,m³....

The superscript characters are working (m³,.. no Problem), but if I want to have a subscript two, either the display shows just the "O" instead of O₂ or a "hyroglyphic" letter behind the O.

I checked the wildcard ranges, it is 0x02 to 0x2084. The Unicode for the subscript 2 is 0x2082.
I added the ₂ also into the wildcard characters.

My code:

void Screen1View::button_down_clicked()  
{
    Unicode::UnicodeChar list[] = {0xB2, 0};
    counter--;
    Unicode::snprintf(test_textBuffer, TEST_TEXT_SIZE,  "%d m%s", counter, list);  
    test_text.invalidate();  
}

Please don't wonder about the UnicodeChar list. I'm planning to add more than one Unicode-Character to the sprintf.

The 0xB2 ist the Unicode for ². This works well. But if I write 0x2082 or 0x2083 it fails.

If anyone has an idea please let me know :)


Solution

  • You may want to use Unicode::strncpy() instead since it allows you to simply copy one UnicodeChar buffer to another. I think you'll have some undefined behavior since Unicode::snprintf expects you to respect the format you specify.

    I specified subscript-2 (0x2082) for my typography as a wildcard character which gave me the following generated unicode (in generated/fonts/src/Font_verdana_20_4bpp.cpp). Note that the font file you use must support the unicode points you specify.

     FONT_LOCATION_FLASH_PRAGMA
    KEEP extern const uint8_t unicodes_verdana_20_4bpp[] FONT_LOCATION_FLASH_ATTRIBUTE =
    {
    ...
        0xDF, 0xDF, 0x00, 0xE8, 0xDF, 0x07, 0xDF,
        // Unicode: [0x2082, ]
        0x00, 0x10, 0x00, 0x00, 0xE9, 0xFF, 0x9F, 0x00, 0x9E, 0x55, 0xFA, 0x09, 0x00, 0x00, 0xE0, 0x0D,
        0x00, 0x00, 0xF0, 0x0B, 0x00, 0x00, 0xFB, 0x03, 0x00, 0xC2, 0x4E, 0x00, 0x60, 0xBF, 0x01, 0x00,
        0xFC, 0x6C, 0x66, 0x26, 0xFF, 0xFF, 0xFF, 0x7F
    };
    

    and wrote the following code for Screen 1:

    void Screen1View::setupScreen()
    {
        Screen1ViewBase::setupScreen();
    
        Unicode::UnicodeChar buf[2] = {0x2082, 0};
        Unicode::strncpy(textArea1Buffer, buf, 5);  
        textArea1.invalidate();  
    }
    

    Which gives me the following output in my simulator:

    enter image description here

    If i add a wildcard "range" instead, like you did:

    enter image description here

    .. i still get code for the unicode-point generated:

    ...
    // Unicode: [0x2080, ]
    0x00, 0xE8, 0xDF, 0x07, 0x00, 0x80, 0x7F, 0x83, 0x6F, 0x00, 0xE0, 0x09, 0x00, 0xCC, 0x00, 0xF1,
    0x06, 0x00, 0xF9, 0x00, 0xF2, 0x05, 0x00, 0xF8, 0x00, 0xF2, 0x06, 0x00, 0xF9, 0x00, 0xF0, 0x09,
    0x00, 0xCC, 0x00, 0x80, 0x6F, 0x83, 0x5F, 0x00, 0x00, 0xE8, 0xDF, 0x06, 0x00,
    // Unicode: [0x2081, ]
    0x00, 0xD2, 0x0C, 0x00, 0xF2, 0xFF, 0x0C, 0x00, 0x20, 0xD2, 0x0C, 0x00, 0x00, 0xC0, 0x0C, 0x00,
    0x00, 0xC0, 0x0C, 0x00, 0x00, 0xC0, 0x0C, 0x00, 0x00, 0xC0, 0x0C, 0x00, 0x20, 0xD2, 0x2C, 0x02,
    0xF1, 0xFF, 0xFF, 0x0E,
    // Unicode: [0x2082, ]
    0x00, 0x10, 0x00, 0x00, 0xE9, 0xFF, 0x9F, 0x00, 0x9E, 0x55, 0xFA, 0x09, 0x00, 0x00, 0xE0, 0x0D,
    0x00, 0x00, 0xF0, 0x0B, 0x00, 0x00, 0xFB, 0x03, 0x00, 0xC2, 0x4E, 0x00, 0x60, 0xBF, 0x01, 0x00,
    0xFC, 0x6C, 0x66, 0x26, 0xFF, 0xFF, 0xFF, 0x7F,
    // Unicode: [0x2083, ]
    0x00, 0x00, 0x00, 0x00, 0xD7, 0xFF, 0xBF, 0x02, 0xAC, 0x46, 0xF7, 0x0D, 0x01, 0x00, 0xA0, 0x0F,
    0x00, 0x11, 0xE4, 0x09, 0x00, 0xFF, 0xAF, 0x01, 0x00, 0x44, 0xD6, 0x1E, 0x00, 0x00, 0x60, 0x4F,
    0x9E, 0x67, 0xE8, 0x1E, 0xD9, 0xFF, 0xAE, 0x02,
    // Unicode: [0x2084, ]
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3, 0x0C, 0x00, 0x00, 0x10, 0xFE, 0x0C, 0x00, 0x00,
    0xC0, 0xBC, 0x0C, 0x00, 0x00, 0xEA, 0xA1, 0x0C, 0x00, 0x70, 0x2F, 0xA0, 0x0C, 0x00, 0xF2, 0x05,
    0xA0, 0x0C, 0x00, 0xF4, 0xFF, 0xFF, 0xFF, 0x05, 0x30, 0x33, 0xC3, 0x3C, 0x01, 0x00, 0x00, 0xB0,
    0x0C, 0x00