correctly skip utf-8 characters

This commit is contained in:
Pavol Rusnak 2015-02-13 17:42:23 +01:00
parent 7fd1e894f5
commit 7d3196a057
6 changed files with 45 additions and 27 deletions

View File

@ -252,9 +252,9 @@ void layoutAddress(const char *address)
oledDrawString(68, 3 * 9, str[3]);
static const char *btnYes = "Continue";
oledDrawString(OLED_WIDTH - fontCharWidth('\xff') - 1, OLED_HEIGHT - 8, "\xff");
oledDrawString(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\xff') - 3, OLED_HEIGHT - 8, btnYes);
oledInvert(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\xff') - 4, OLED_HEIGHT - 9, OLED_WIDTH - 1, OLED_HEIGHT - 1);
oledDrawString(OLED_WIDTH - fontCharWidth('\x06') - 1, OLED_HEIGHT - 8, "\x06");
oledDrawString(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\x06') - 3, OLED_HEIGHT - 8, btnYes);
oledInvert(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\x06') - 4, OLED_HEIGHT - 9, OLED_WIDTH - 1, OLED_HEIGHT - 1);
oledRefresh();
}

View File

@ -7,7 +7,7 @@ const uint8_t *font_data[256] = {
/* 0x03 _ */ (uint8_t *)"\x01\x00",
/* 0x04 _ */ (uint8_t *)"\x01\x00",
/* 0x05 _ */ (uint8_t *)"\x01\x00",
/* 0x06 _ */ (uint8_t *)"\x01\x00",
/* 0x06 _ */ (uint8_t *)"\x07\x18\x1c\x0e\x18\x30\x40\x80",
/* 0x07 _ */ (uint8_t *)"\x01\x00",
/* 0x08 _ */ (uint8_t *)"\x01\x00",
/* 0x09 _ */ (uint8_t *)"\x01\x00",
@ -22,7 +22,7 @@ const uint8_t *font_data[256] = {
/* 0x12 _ */ (uint8_t *)"\x01\x00",
/* 0x13 _ */ (uint8_t *)"\x01\x00",
/* 0x14 _ */ (uint8_t *)"\x01\x00",
/* 0x15 _ */ (uint8_t *)"\x01\x00",
/* 0x15 _ */ (uint8_t *)"\x07\x44\xee\x7c\x38\x7c\xee\x44",
/* 0x16 _ */ (uint8_t *)"\x01\x00",
/* 0x17 _ */ (uint8_t *)"\x01\x00",
/* 0x18 _ */ (uint8_t *)"\x01\x00",
@ -255,8 +255,8 @@ const uint8_t *font_data[256] = {
/* 0xfb _ */ (uint8_t *)"\x01\x00",
/* 0xfc _ */ (uint8_t *)"\x01\x00",
/* 0xfd _ */ (uint8_t *)"\x01\x00",
/* 0xfe _ */ (uint8_t *)"\x07\x44\xee\x7c\x38\x7c\xee\x44",
/* 0xff _ */ (uint8_t *)"\x07\x18\x1c\x0e\x18\x30\x40\x80",
/* 0xfe _ */ (uint8_t *)"\x01\x00",
/* 0xff _ */ (uint8_t *)"\x01\x00",
};
int fontCharWidth(char c) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -20,15 +20,15 @@ class Img(object):
img = Img('font.png')
cur = ''
idx = 0
for i in range(img.w):
if img.pixel(i, 0) == None:
cur = '\\x%02x' % (len(cur) / 4) + cur
ch = chr(idx) if idx >= 32 and idx <= 126 else '_'
print '\t/* 0x%02x %c */ (uint8_t *)"%s",' % (idx, ch , cur)
cur = ''
idx += 1
continue
val = img.pixel(i, 0) + img.pixel(i, 1) + img.pixel(i, 2) + img.pixel(i, 3) + img.pixel(i, 4) + img.pixel(i, 5) + img.pixel(i, 6) + img.pixel(i, 7)
cur += '\\x%02x' % int(val, 2)
for i in range(256):
x = (i % 16) * 10
y = (i / 16) * 10
cur = ''
while img.pixel(x, y) != None:
val = ''.join(img.pixel(x, y + j) for j in range(8))
x += 1
cur += '\\x%02x' % int(val, 2)
cur = '\\x%02x' % (len(cur) / 4) + cur
ch = chr(i) if i >= 32 and i <= 126 else '_'
print '\t/* 0x%02x %c */ (uint8_t *)"%s",' % (i, ch , cur)

View File

@ -67,14 +67,14 @@ void layoutDialog(LayoutDialogIcon icon, const char *btnNo, const char *btnYes,
}
}
if (btnNo) {
oledDrawString(1, OLED_HEIGHT - 8, "\xfe");
oledDrawString(fontCharWidth('\xfe') + 3, OLED_HEIGHT - 8, btnNo);
oledInvert(0, OLED_HEIGHT - 9, fontCharWidth('\xfe') + oledStringWidth(btnNo) + 2, OLED_HEIGHT - 1);
oledDrawString(1, OLED_HEIGHT - 8, "\x15");
oledDrawString(fontCharWidth('\x15') + 3, OLED_HEIGHT - 8, btnNo);
oledInvert(0, OLED_HEIGHT - 9, fontCharWidth('\x15') + oledStringWidth(btnNo) + 2, OLED_HEIGHT - 1);
}
if (btnYes) {
oledDrawString(OLED_WIDTH - fontCharWidth('\xff') - 1, OLED_HEIGHT - 8, "\xff");
oledDrawString(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\xff') - 3, OLED_HEIGHT - 8, btnYes);
oledInvert(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\xff') - 4, OLED_HEIGHT - 9, OLED_WIDTH - 1, OLED_HEIGHT - 1);
oledDrawString(OLED_WIDTH - fontCharWidth('\x06') - 1, OLED_HEIGHT - 8, "\x06");
oledDrawString(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\x06') - 3, OLED_HEIGHT - 8, btnYes);
oledInvert(OLED_WIDTH - oledStringWidth(btnYes) - fontCharWidth('\x06') - 4, OLED_HEIGHT - 9, OLED_WIDTH - 1, OLED_HEIGHT - 1);
}
oledRefresh();
}

24
oled.c
View File

@ -207,11 +207,25 @@ void oledDrawChar(int x, int y, char c)
}
}
char oledConvertChar(const char c) {
uint8_t a = c;
if (a < 0x80) return c;
// UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Description
// bytes 11xxxxxx are first byte of UTF-8 characters
// bytes 10xxxxxx are successive UTF-8 characters
if (a >= 0xC0) return '_';
return 0;
}
int oledStringWidth(const char *text) {
if (!text) return 0;
int l = 0;
char c;
for (; *text; text++) {
l += fontCharWidth(*text) + 1;
c = oledConvertChar(*text);
if (c) {
l += fontCharWidth(c) + 1;
}
}
return l;
}
@ -220,9 +234,13 @@ void oledDrawString(int x, int y, const char* text)
{
if (!text) return;
int l = 0;
char c;
for (; *text; text++) {
oledDrawChar(x + l, y, *text);
l += fontCharWidth(*text) + 1;
c = oledConvertChar(*text);
if (c) {
oledDrawChar(x + l, y, c);
l += fontCharWidth(c) + 1;
}
}
}