Skip to content

Commit

Permalink
🩹 Followup for lchar_t
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jul 4, 2022
1 parent e94fa7d commit f39e2bc
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 74 deletions.
8 changes: 4 additions & 4 deletions Marlin/src/lcd/HD44780/lcdprint_hd44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,10 @@ static int lcd_put_u8str_max_cb(const char * utf8_str, read_byte_cb_t cb_read_by
pixel_len_t ret = 0;
const uint8_t *p = (uint8_t *)utf8_str;
while (ret < max_length) {
lchar_t ch;
p = get_utf8_value_cb(p, cb_read_byte, ch);
if (!ch) break;
ret += lcd_put_lchar_max(ch, max_length - ret);
lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!wc) break;
ret += lcd_put_lchar_max(wc, max_length - ret);
}
return (int)ret;
}
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/lcd/TFTGLCD/lcdprint_TFTGLCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,10 @@ static int lcd_put_u8str_max_cb(const char * utf8_str, read_byte_cb_t cb_read_by
pixel_len_t ret = 0;
const uint8_t *p = (uint8_t *)utf8_str;
while (ret < max_length) {
lchar_t ch;
p = get_utf8_value_cb(p, cb_read_byte, ch);
if (!ch) break;
ret += lcd_put_lchar_max(ch, max_length - ret);
lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!wc) break;
ret += lcd_put_lchar_max(wc, max_length - ret);
}
return (int)ret;
}
Expand Down
14 changes: 7 additions & 7 deletions Marlin/src/lcd/dogm/u8g_fontutf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ static void fontgroup_drawwchar(font_group_t *group, const font_t *fnt_default,
static void fontgroup_drawstring(font_group_t *group, const font_t *fnt_default, const char *utf8_msg, read_byte_cb_t cb_read_byte, void * userdata, fontgroup_cb_draw_t cb_draw_ram) {
const uint8_t *p = (uint8_t*)utf8_msg;
for (;;) {
lchar_t ch;
p = get_utf8_value_cb(p, cb_read_byte, ch);
if (!ch) break;
fontgroup_drawwchar(group, fnt_default, ch, userdata, cb_draw_ram);
lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!wc) break;
fontgroup_drawwchar(group, fnt_default, wc, userdata, cb_draw_ram);
}
}

Expand Down Expand Up @@ -154,14 +154,14 @@ static int fontgroup_cb_draw_u8g(void *userdata, const font_t *fnt_current, cons
* @param pu8g : U8G pointer
* @param x : position x axis
* @param y : position y axis
* @param ch : the lchar_t
* @param wc : the lchar_t
* @param max_width : the pixel width of the string allowed
*
* @return number of pixels advanced
*
* Draw a UTF-8 string at the specified position
*/
unsigned int uxg_DrawWchar(u8g_t *pu8g, unsigned int x, unsigned int y, const lchar_t &ch, pixel_len_t max_width) {
unsigned int uxg_DrawWchar(u8g_t *pu8g, unsigned int x, unsigned int y, const lchar_t &wc, pixel_len_t max_width) {
struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g);
Expand All @@ -176,7 +176,7 @@ unsigned int uxg_DrawWchar(u8g_t *pu8g, unsigned int x, unsigned int y, const lc
data.adv = 0;
data.max_width = max_width;
data.fnt_prev = nullptr;
fontgroup_drawwchar(group, fnt_default, ch, (void*)&data, fontgroup_cb_draw_u8g);
fontgroup_drawwchar(group, fnt_default, wc, (void*)&data, fontgroup_cb_draw_u8g);
u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);

return data.adv;
Expand Down
28 changes: 14 additions & 14 deletions Marlin/src/lcd/e3v2/marlinui/dwin_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ uint8_t read_byte(const uint8_t *byte) { return *byte; }
* @ displays an axis name such as XYZUVW, or E for an extruder
*/
void DWIN_String::add(const char *tpl, const int8_t index, const char *cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/) {
lchar_t ch;
lchar_t wc;

while (*tpl) {
tpl = get_utf8_value_cb(tpl, read_byte, ch);
if (ch > 255) ch |= 0x0080;
const uint8_t ch = uint8_t(ch & 0x00FF);
tpl = get_utf8_value_cb(tpl, read_byte, wc);
if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(wc & 0x00FF);

if (ch == '=' || ch == '~' || ch == '*') {
if (index >= 0) {
Expand All @@ -80,32 +80,32 @@ void DWIN_String::add(const char *tpl, const int8_t index, const char *cstr/*=nu
}

void DWIN_String::add(const char *cstr, uint8_t max_len/*=MAX_STRING_LENGTH*/) {
lchar_t ch;
lchar_t wc;
while (*cstr && max_len) {
cstr = get_utf8_value_cb(cstr, read_byte, ch);
cstr = get_utf8_value_cb(cstr, read_byte, wc);
/*
if (ch > 255) ch |= 0x0080;
uint8_t ch = uint8_t(ch & 0x00FF);
if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(wc & 0x00FF);
add_character(ch);
*/
add(ch);
add(wc);
max_len--;
}
eol();
}

void DWIN_String::add(const lchar_t &ch) {
void DWIN_String::add(const lchar_t &wc) {
int ret;
size_t idx = 0;
dwin_charmap_t pinval;
dwin_charmap_t *copy_address = nullptr;
pinval.uchar = ch;
pinval.uchar = wc;
pinval.idx = -1;

// For 8-bit ASCII just print the single ch
// For 8-bit ASCII just print the single character
char str[] = { '?', 0 };
if (ch < 255) {
str[0] = (char)ch;
if (wc < 255) {
str[0] = (char)wc;
}
else {
copy_address = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/lcd/e3v2/marlinui/dwin_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ class DWIN_String {
/**
* @brief Append a UTF-8 character
*
* @param ch The UTF-8 character
* @param wc The UTF-8 character
*/
static void add(const lchar_t &ch);
static void set(const lchar_t &ch) { set(); add(ch); }
static void add(const lchar_t &wc);
static void set(const lchar_t &wc) { set(); add(wc); }

/**
* @brief Append / Set C-string
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/lcd/e3v2/marlinui/lcdprint_dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ static int lcd_put_u8str_max_cb(const char * utf8_str, read_byte_cb_t cb_read_by
const uint8_t *p = (uint8_t *)utf8_str;
dwin_string.set();
while (dwin_string.length < max_length) {
lchar_t ch;
p = get_utf8_value_cb(p, cb_read_byte, ch);
if (!ch) break;
dwin_string.add(ch);
lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!wc) break;
dwin_string.add(wc);
}
DWIN_Draw_String(dwin_font.solid, dwin_font.index, dwin_font.fg, dwin_font.bg, cursor.x, cursor.y, dwin_string.string());
lcd_advance_cursor(dwin_string.length);
Expand Down
2 changes: 0 additions & 2 deletions Marlin/src/lcd/fontutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

#include "../inc/MarlinConfig.h"

#define MAX_UTF8_CHAR_SIZE 4

#if HAS_WIRED_LCD
#include "marlinui.h"
#include "../MarlinCore.h"
Expand Down
14 changes: 11 additions & 3 deletions Marlin/src/lcd/fontutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,23 @@

#define MAX_UTF8_CHAR_SIZE 4

// Use a longer character type (if needed) because wchar_t is only 16 bits wide
#ifdef MAX_UTF8_CHAR_SIZE
#if MAX_UTF8_CHAR_SIZE > 2
typedef uint32_t lchar_t;
#else
typedef wchar_t lchar_t;
#endif
#else
#define wchar_t uint32_t
#endif

// read a byte from ROM or RAM
typedef uint8_t (*read_byte_cb_t)(const uint8_t * str);

uint8_t read_byte_ram(const uint8_t *str);
uint8_t read_byte_rom(const uint8_t *str);

// An extra long character type because wchar_t is only 2 bytes
typedef uint32_t lchar_t;

typedef uint16_t pixel_len_t;
#define PIXEL_LEN_NOLIMIT ((pixel_len_t)(-1))

Expand Down
30 changes: 15 additions & 15 deletions Marlin/src/lcd/lcdprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ lcd_uint_t lcd_put_u8str_P(PGM_P const ptpl, const int8_t ind, const char *cstr/
const uint8_t *p = (uint8_t*)ptpl;
int8_t n = maxlen;
while (n > 0) {
lchar_t ch;
p = get_utf8_value_cb(p, read_byte_rom, ch);
if (!ch) break;
if (ch == '=' || ch == '~' || ch == '*') {
lchar_t wc;
p = get_utf8_value_cb(p, read_byte_rom, wc);
if (!wc) break;
if (wc == '=' || wc == '~' || wc == '*') {
if (ind >= 0) {
if (ch == '*') { lcd_put_lchar('E'); n--; }
if (wc == '*') { lcd_put_lchar('E'); n--; }
if (n) {
int8_t inum = ind + ((ch == '=') ? 0 : LCD_FIRST_TOOL);
int8_t inum = ind + ((wc == '=') ? 0 : LCD_FIRST_TOOL);
if (inum >= 10) {
lcd_put_lchar('0' + (inum / 10)); n--;
inum %= 10;
Expand All @@ -71,19 +71,19 @@ lcd_uint_t lcd_put_u8str_P(PGM_P const ptpl, const int8_t ind, const char *cstr/
break;
}
}
else if (ch == '$' && fstr) {
else if (wc == '$' && fstr) {
n -= lcd_put_u8str_max_P(FTOP(fstr), n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH);
}
else if (ch == '$' && cstr) {
else if (wc == '$' && cstr) {
n -= lcd_put_u8str_max(cstr, n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH);
}
else if (ch == '@') {
else if (wc == '@') {
lcd_put_lchar(AXIS_CHAR(ind));
n--;
}
else {
lcd_put_lchar(ch);
n -= ch > 255 ? prop : 1;
lcd_put_lchar(wc);
n -= wc > 255 ? prop : 1;
}
}
return n;
Expand All @@ -97,10 +97,10 @@ int calculateWidth(PGM_P const pstr) {
int n = 0;

do {
lchar_t ch;
p = get_utf8_value_cb(p, read_byte_rom, ch);
if (!ch) break;
n += (ch > 255) ? prop : 1;
lchar_t wc;
p = get_utf8_value_cb(p, read_byte_rom, wc);
if (!wc) break;
n += (wc > 255) ? prop : 1;
} while (1);

return n * MENU_FONT_WIDTH;
Expand Down
20 changes: 10 additions & 10 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,26 +417,26 @@ void MarlinUI::init() {
};

const uint8_t *p = (uint8_t*)string;
lchar_t ch;
lchar_t wc;
if (wordwrap) {
const uint8_t *wrd = nullptr;
uint8_t c = 0;
// find the end of the part
for (;;) {
if (!wrd) wrd = p; // Get word start /before/ advancing
p = get_utf8_value_cb(p, cb_read_byte, ch);
const bool eol = !ch; // zero ends the string
p = get_utf8_value_cb(p, cb_read_byte, wc);
const bool eol = !wc; // zero ends the string
// End or a break between phrases?
if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
if (eol || wc == ' ' || wc == '-' || wc == '+' || wc == '.') {
if (!c && wc == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
// Past the right and the word is not too long?
if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
c += !eol; // +1 so the space will be printed
col += c; // advance col to new position
while (c) { // character countdown
--c; // count down to zero
wrd = get_utf8_value_cb(wrd, cb_read_byte, ch); // get characters again
lcd_put_lchar(ch); // character to the LCD
wrd = get_utf8_value_cb(wrd, cb_read_byte, wc); // get characters again
lcd_put_lchar(wc); // character to the LCD
}
if (eol) break; // all done!
wrd = nullptr; // set up for next word
Expand All @@ -446,9 +446,9 @@ void MarlinUI::init() {
}
else {
for (;;) {
p = get_utf8_value_cb(p, cb_read_byte, ch);
if (!ch) break;
lcd_put_lchar(ch);
p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!wc) break;
lcd_put_lchar(wc);
col++;
if (col >= LCD_WIDTH) _newline();
}
Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/lcd/tft/tft_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ void TFT_String::set() {
* @ displays an axis name such as XYZUVW, or E for an extruder
*/
void TFT_String::add(const char *tpl, const int8_t index, const char *cstr/*=nullptr*/, FSTR_P const fstr/*=nullptr*/) {
lchar_t ch;
lchar_t wc;

while (*tpl) {
tpl = get_utf8_value_cb(tpl, read_byte_ram, ch);
if (ch > 255) ch |= 0x0080;
const uint8_t ch = uint8_t(ch & 0x00FF);
tpl = get_utf8_value_cb(tpl, read_byte_ram, wc);
if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(wc & 0x00FF);

if (ch == '=' || ch == '~' || ch == '*') {
if (index >= 0) {
Expand All @@ -124,11 +124,11 @@ void TFT_String::add(const char *tpl, const int8_t index, const char *cstr/*=nul
}

void TFT_String::add(const char *cstr, uint8_t max_len/*=MAX_STRING_LENGTH*/) {
lchar_t ch;
lchar_t wc;
while (*cstr && max_len) {
cstr = get_utf8_value_cb(cstr, read_byte_ram, ch);
if (ch > 255) ch |= 0x0080;
const uint8_t ch = uint8_t(ch & 0x00FF);
cstr = get_utf8_value_cb(cstr, read_byte_ram, wc);
if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(wc & 0x00FF);
add_character(ch);
max_len--;
}
Expand Down

0 comments on commit f39e2bc

Please sign in to comment.