TinyCircuits

Can I use display.print for unicode characters?

koc

Can I use display.print for unicode characters?
« on: September 28, 2017, 01:58:54 PM »
I'm new to Arduino. Since I want to print some unicode characters on the screen I read some tutorials and source code. If I understood correctly, I should add some bitmap font into the font.h file.

In the font.h file, it said:
Quote
Generated by The Dot Factory, written by Eran Duchan, currently available at
http://www.eran.io/the-dot-factory-an-lcd-font-and-image-generator/
using the configuration xml included with this library. Just put the
configuration file in the same directory as the executable, and copy and paste
the output to the end of this file. Fonts that are not used are not included by
the compiler and do not take up any space on the microcontroller flash.

Therefore I downloaded  The Dot Factory. I generated some codes for one non-ascii characters, pasted it to the end of font.h but it won't show up. If I change nothing, but just the non-ascii character to the ascii character, it will work.

The non-ascii version of code is
Code: [Select]
/*
**  Font data for Arial 8pt
*/

/* Character bitmaps for Arial 8pt */
static const unsigned char PROGMEM arial_8ptBitmaps[] =
{
/* @0 '啊' (11 pixels wide) */
//   #########
//    #      #
//   #########
//           
// ###########
//   #   ##  #
//    ###  ###
//     ##### 
// #   #   # #
// ###########
//           #
0x08, 0x08, 0xAC, 0xEA, 0xAB, 0xAB, 0xAD, 0xAD, 0xAB, 0xAA, 0xEE,
0xC0, 0x40, 0x40, 0x40, 0xC0, 0x40, 0x40, 0x40, 0xC0, 0x40, 0xE0,
};

/* Character descriptors for Arial 8pt */
/* { [Char width in bits], [Offset into arial_8ptCharBitmaps in bytes] } */
static const FONT_CHAR_INFO PROGMEM arial_8ptDescriptors[] =
{
{11, 0}, /* 啊 */
};

/* Font information for Arial 8pt */
static const FONT_INFO arial_8ptFontInfo =
{
11, /*  Character height */
21834, /*  Start character */
21834, /*  End character */
arial_8ptDescriptors, /*  Character descriptor array */
arial_8ptBitmaps, /*  Character bitmap array */
};


The ascii version of it is
Code: [Select]
/*
**  Font data for Arial 8pt
*/

/* Character bitmaps for Arial 8pt */
static const unsigned char PROGMEM arial_8ptBitmaps[] =
{
/* @0 'a' (5 pixels wide) */
//  ## #
// #  # #
// #  # #
//  # # #
// #####
0x68, 0x98, 0x88, 0x78, 0x88, 0x70,
};

/* Character descriptors for Arial 8pt */
/* { [Char width in bits], [Offset into arial_8ptCharBitmaps in bytes] } */
static const FONT_CHAR_INFO PROGMEM arial_8ptDescriptors[] =
{
{5, 0}, /* a */
};

/* Font information for Arial 8pt */
static const FONT_INFO arial_8ptFontInfo =
{
6, /*  Character height */
'a', /*  Start character */
'a', /*  End character */
arial_8ptDescriptors, /*  Character descriptor array */
arial_8ptBitmaps, /*  Character bitmap array */
};


The main process's code is
Code: [Select]
#include <Wire.h>
#include <SPI.h>
#include <TinyScreen.h>

TinyScreen display = TinyScreen(TinyScreenPlus);
void setup(void) {
  Wire.begin();//initialize I2C before we can initialize TinyScreen- not needed for TinyScreen+
  display.begin();
  display.setBrightness(10);
}

void loop() {
  display.clearScreen();
  display.setFont(arial_8ptFontInfo);
  display.setCursor(20,20);
  display.print("a"); // or display.print("啊")
}
« Last Edit: September 28, 2017, 02:00:39 PM by koc »

Re: Can I use display.print for unicode characters?
« Reply #1 on: October 30, 2017, 04:42:20 PM »
I've added this to the list of useful things we should really add to the TinyScreen library now that the majority of users are on the 32 bit TinyScreen+ platform with flash to store Unicode characters.

This is a good attempt, but the current library only handles ASCII numbers which fit into 8 bit values. This particular unicode character seems to be decimal 21834, which would be split into 3 UTF-8 bytes, which will all be discarded by the code expecting ASCII.

A workaround to get it to display would be to replace the 'start' and 'end' characters that are currently 21834 with 'a' like the ASCII example to 'trick' the code to use a different bitmap. This could be extended to a few characters tacked on to the end of your ASCII bitmap set, but would be pretty annoying for many characters.