Print custom characters and diacritics
What this example shows
Printing German characters (ä, ö, ü, ß, Ä, Ö, Ü) with a custom font. The text renderer looks up each glyph by a single byte, so characters above 0xFF can never be printed with print(). A character typed directly into a string literal is stored as two bytes and prints two wrong glyphs. The fix in the example is to generate a font header with the Soldered Font Converter, which places the extra characters at byte codes 0x7F to 0x85, and to write those codes into your strings.
Example code
Trimmed from the pinned example: only the file header comment is removed. The sketch needs FreeSansGerman18pt7b.h from the same example folder, so open the source link below and keep both files together.
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif
#include "Inkplate.h"
#include "FreeSansGerman18pt7b.h" // FreeSans18pt7b + ä ö ü ß Ä Ö Ü at 0x7F-0x85
// Byte codes the extra glyphs were placed at inside FreeSansGerman18pt7b.h
// (shown by the Font Converter tool when the font was generated - see header
// comment above). Used as adjacent string literals, e.g. CH_AE "rger" -> "\x83" "rger".
#define CH_ae "\x7f" // ä U+00E4
#define CH_oe "\x80" // ö U+00F6
#define CH_ue "\x81" // ü U+00FC
#define CH_sz "\x82" // ß U+00DF
#define CH_AE "\x83" // Ä U+00C4
#define CH_OE "\x84" // Ö U+00D6
#define CH_UE "\x85" // Ü U+00DC
Inkplate display(INKPLATE_1BIT);
void setup()
{
display.begin();
display.clearDisplay();
display.setTextColor(BLACK);
display.setFont(&FreeSansGerman18pt7b);
// A German sentence exercising every extra glyph: Ö, ä, ü (x2), ö, ß, Ü, Ä.
display.setCursor(10, 60);
display.print(F(CH_OE "sterreichische M" CH_ae "dchen " CH_ue "ben ihre Gr" CH_oe CH_sz "e."));
display.setCursor(10, 100);
display.print(F(CH_UE "ber " CH_AE "rger spricht niemand gern."));
// Full legend of every extra glyph, upper- then lower-case.
display.setCursor(10, 150);
display.print(F(CH_AE CH_OE CH_UE " " CH_ae CH_oe CH_ue CH_sz));
display.display();
}
void loop()
{
}
Functions used
Inkplate(uint8_t mode)returns NoneCreates the display object. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` gives 8 levels of grey but only full updates.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | mode | INKPLATE_1BIT or INKPLATE_3BIT. |
display.begin()returns voidInitializes the library and the display. Call it once, at the start of `setup()`.
ReturnsNothing.
display.clearDisplay()returns voidClears the frame buffer in memory. It does not change what the panel shows until you call `display()`.
ReturnsNothing.
display.setTextColor(uint16_t c)returns voidSets the text color.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | c | Text color, for example BLACK. |
display.setFont(const GFXfont *f = NULL)returns voidSelects the font used by `print()`. The default `NULL` argument is the font the library uses when you set none.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
const GFXfont * | f | Pointer to a font, for example &FreeSansGerman18pt7b. Defaults to NULL. |
display.setCursor(int16_t x, int16_t y)returns voidSets where the next `print()` starts, in pixels from the top left corner.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int16_t | x | Horizontal position in pixels. |
int16_t | y | Vertical position in pixels. |
display.print(value)returns size_tPrints text or a number at the cursor. The `print` functions come from the Arduino `Print` class that `Adafruit_GFX` builds on. For a number, an optional second argument sets the decimal places of a float or the number base of an integer.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text, number or char | value | What to print. |
int | second argument (optional) | Decimal places for a float, or the base for an integer, for example DEC. |
F(string_literal)returns const __FlashStringHelper *Arduino macro that keeps a string literal in flash instead of RAM. The example passes the result to `display.print()`.
ReturnsThe flash string.
Parameters
| Type | Name | Description |
|---|---|---|
string literal | string_literal | The text. |
display.display(bool _leaveOn = 0)returns voidDoes a full refresh: sends the frame buffer to the panel. If `_leaveOn` is true, the panel supply stays on after the refresh, which saves time on the next update.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | Keeps the panel supply on after the update when true. Defaults to 0. |
What to expect
The display shows two German sentences, Österreichische Mädchen üben ihre Größe. and Über Ärger spricht niemand gern., followed by a line with all the extra characters: ÄÖÜ äöüß.
To make your own font, open the Font Converter at https://tools.soldered.com/tools/font-converter/, upload a .ttf file that contains the characters you need, set the size and a font name, list the characters in "Extra characters", convert, and put the downloaded header next to your sketch. The converter shows the byte code of each extra character, which you then use in your strings the way the example does with its CH_ defines.