diff --git a/OLEDDisplay.cpp b/OLEDDisplay.cpp index f8fc226519b37df85862d0ff3e72bd26a4691eaa..e55683f3b19345add7bb7f93822adc882bac4b62 100644 --- a/OLEDDisplay.cpp +++ b/OLEDDisplay.cpp @@ -612,12 +612,17 @@ size_t OLEDDisplay::write(uint8_t c) { // Don't waste space on \r\n line endings, dropping \r if (c == 13) return 1; + // convert UTF-8 character to font table index + c = (this->fontTableLookupFunction)(c); + // drop unknown character + if (c == 0) return 1; + bool maxLineNotReached = this->logBufferLine < this->logBufferMaxLines; bool bufferNotFull = this->logBufferFilled < this->logBufferSize; // Can we write to the buffer? if (bufferNotFull && maxLineNotReached) { - this->logBuffer[logBufferFilled] = utf8ascii(c); + this->logBuffer[logBufferFilled] = c; this->logBufferFilled++; // Keep track of lines written if (c == 10) this->logBufferLine++; @@ -760,27 +765,6 @@ void inline OLEDDisplay::drawInternal(int16_t xMove, int16_t yMove, int16_t widt } } -// Code form http://playground.arduino.cc/Main/Utf8ascii -uint8_t OLEDDisplay::utf8ascii(byte ascii) { - static uint8_t LASTCHAR; - - if ( ascii < 128 ) { // Standard ASCII-set 0..0x7F handling - LASTCHAR = 0; - return ascii; - } - - uint8_t last = LASTCHAR; // get last char - LASTCHAR = ascii; - - switch (last) { // conversion depnding on first UTF8-character - case 0xC2: return (ascii); break; - case 0xC3: return (ascii | 0xC0); break; - case 0x82: if (ascii == 0xAC) return (0x80); // special case Euro-symbol - } - - return 0; // otherwise: return zero, if character has to be ignored -} - // You need to free the char! char* OLEDDisplay::utf8ascii(String str) { uint16_t k = 0; @@ -797,7 +781,7 @@ char* OLEDDisplay::utf8ascii(String str) { length--; for (uint16_t i=0; i < length; i++) { - char c = utf8ascii(s[i]); + char c = (this->fontTableLookupFunction)(s[i]); if (c!=0) { s[k++]=c; } @@ -808,3 +792,7 @@ char* OLEDDisplay::utf8ascii(String str) { // This will leak 's' be sure to free it in the calling function. return s; } + +void OLEDDisplay::setFontTableLookupFunction(FontTableLookupFunction function) { + this->fontTableLookupFunction = function; +} \ No newline at end of file diff --git a/OLEDDisplay.h b/OLEDDisplay.h index 81537a24fa54ba017715d4971fb094f6f6b3e791..b46e7edcc828156d3ff28a24f2c4828a19cb1cd1 100644 --- a/OLEDDisplay.h +++ b/OLEDDisplay.h @@ -107,6 +107,7 @@ enum OLEDDISPLAY_TEXT_ALIGNMENT { TEXT_ALIGN_CENTER_BOTH = 3 }; +typedef byte (*FontTableLookupFunction)(const byte ch); class OLEDDisplay : public Print { public: @@ -186,6 +187,9 @@ class OLEDDisplay : public Print { // ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24 void setFont(const char *fontData); + // Set the function that will convert utf-8 to font table index + void setFontTableLookupFunction(FontTableLookupFunction function); + /* Display functions */ // Turn the display on @@ -256,13 +260,33 @@ class OLEDDisplay : public Print { void sendInitCommands(); // converts utf8 characters to extended ascii - static char* utf8ascii(String s); - static byte utf8ascii(byte ascii); + char* utf8ascii(String s); void inline drawInternal(int16_t xMove, int16_t yMove, int16_t width, int16_t height, const char *data, uint16_t offset, uint16_t bytesInData) __attribute__((always_inline)); void drawStringInternal(int16_t xMove, int16_t yMove, char* text, uint16_t textLength, uint16_t textWidth); + // UTF-8 to font table index converter + // Code form http://playground.arduino.cc/Main/Utf8ascii + FontTableLookupFunction fontTableLookupFunction = [](const byte ch) { + static uint8_t LASTCHAR; + + if (ch < 128) { // Standard ASCII-set 0..0x7F handling + LASTCHAR = 0; + return ch; + } + + uint8_t last = LASTCHAR; // get last char + LASTCHAR = ch; + + switch (last) { // conversion depnding on first UTF8-character + case 0xC2: return (uint8_t) ch; break; + case 0xC3: return (uint8_t) (ch | 0xC0); break; + case 0x82: if (ch == 0xAC) return (uint8_t) 0x80; // special case Euro-symbol + } + + return (uint8_t) 0; // otherwise: return zero, if character has to be ignored + }; }; #endif