Skip to content

Commit e930bff

Browse files
committed
First working implementation
1 parent 8c3d94b commit e930bff

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed

ExtendedChars.cpp

Whitespace-only changes.

ExtendedChars.h

Whitespace-only changes.

include/ExtendedChars.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include <Arduino.h>
3+
4+
// Structure for individual Unicode characters
5+
struct UnicodeChar {
6+
uint16_t code;
7+
const char* description;
8+
};
9+
10+
class ExtendedChars {
11+
public:
12+
static String extendChars(const String &str);
13+
14+
// Supported character sets
15+
static const UnicodeChar ADDITIONAL_CHARS[];
16+
static const uint8_t ADDITIONAL_CHARS_COUNT;
17+
18+
private:
19+
ExtendedChars() = delete;
20+
};

library.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
"authors": [
55
{
66
"name": "David Pöttler",
7-
"email": "david.e.poettler@gmail.com"
7+
"email": "david.e.poettler@gmail.com",
8+
"maintainer": true
89
}
910
],
1011
"description": "ExtendedChars is an extension library for the Adafruit GFX library adding support for UTF8 characters.",
1112
"keywords": ["display", "UTF8", "Adafruit", "GFX"],
1213
"license": "GPL-3.0",
1314
"repository": {
1415
"type": "git",
15-
"url": "https://github.com/dpoettler/ExtendedChars"
16+
"url": "https://github.com/dpoettler/ExtendedChars.git"
1617
},
1718
"frameworks": "arduino",
1819
"platforms": "*",

src/ExtendedChars.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "ExtendedChars.h"
2+
3+
// Define the static members
4+
const UnicodeChar ExtendedChars::ADDITIONAL_CHARS[] = {
5+
{0x00C4, "Ä"}, // Upper Ä umlaut
6+
{0x00D6, "Ö"}, // Upper Ö umlaut
7+
{0x00DC, "Ü"}, // Upper Ü umlaut
8+
{0x00E4, "ä"}, // Lower ä umlaut
9+
{0x00F6, "ö"}, // Lower ö umlaut
10+
{0x00FC, "ü"}, // Lower ü umlaut
11+
{0x00DF, "ß"}, // Sharp ß
12+
};
13+
14+
const uint8_t ExtendedChars::ADDITIONAL_CHARS_COUNT = sizeof(ADDITIONAL_CHARS) / sizeof(ADDITIONAL_CHARS[0]);
15+
16+
namespace {
17+
bool isInSupportedChars(uint16_t unicode) {
18+
for (uint8_t i = 0; i < ExtendedChars::ADDITIONAL_CHARS_COUNT; i++) {
19+
if (unicode == ExtendedChars::ADDITIONAL_CHARS[i].code) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
26+
uint16_t extractUnicode(char firstByte, char secondByte) {
27+
return ((firstByte & 0x1F) << 6) | (secondByte & 0x3F);
28+
}
29+
}
30+
31+
String ExtendedChars::extendChars(const String &str) {
32+
String result;
33+
result.reserve(str.length());
34+
35+
for (size_t i = 0; i < str.length(); i++) {
36+
if ((str[i] & 0x80) == 0) {
37+
result += str[i];
38+
}
39+
else if ((str[i] & 0xE0) == 0xC0) {
40+
uint16_t unicode = extractUnicode(str[i], str[i + 1]);
41+
if (isInSupportedChars(unicode)) {
42+
result += (char)unicode;
43+
}
44+
i++;
45+
}
46+
else if ((str[i] & 0xF0) == 0xE0) {
47+
result += str.substring(i, i + 3);
48+
i += 2;
49+
}
50+
}
51+
return result;
52+
}

0 commit comments

Comments
 (0)