0
0
Fork 0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2025-01-19 16:16:13 +00:00

🩹 Followup for lchar_t

This commit is contained in:
Scott Lahteine 2022-07-03 23:44:06 -05:00
parent e94fa7d5dc
commit f39e2bc1e4
11 changed files with 80 additions and 74 deletions

View file

@ -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; pixel_len_t ret = 0;
const uint8_t *p = (uint8_t *)utf8_str; const uint8_t *p = (uint8_t *)utf8_str;
while (ret < max_length) { while (ret < max_length) {
lchar_t ch; lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, ch); p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!ch) break; if (!wc) break;
ret += lcd_put_lchar_max(ch, max_length - ret); ret += lcd_put_lchar_max(wc, max_length - ret);
} }
return (int)ret; return (int)ret;
} }

View file

@ -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; pixel_len_t ret = 0;
const uint8_t *p = (uint8_t *)utf8_str; const uint8_t *p = (uint8_t *)utf8_str;
while (ret < max_length) { while (ret < max_length) {
lchar_t ch; lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, ch); p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!ch) break; if (!wc) break;
ret += lcd_put_lchar_max(ch, max_length - ret); ret += lcd_put_lchar_max(wc, max_length - ret);
} }
return (int)ret; return (int)ret;
} }

View file

@ -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) { 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; const uint8_t *p = (uint8_t*)utf8_msg;
for (;;) { for (;;) {
lchar_t ch; lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, ch); p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!ch) break; if (!wc) break;
fontgroup_drawwchar(group, fnt_default, ch, userdata, cb_draw_ram); fontgroup_drawwchar(group, fnt_default, wc, userdata, cb_draw_ram);
} }
} }
@ -154,14 +154,14 @@ static int fontgroup_cb_draw_u8g(void *userdata, const font_t *fnt_current, cons
* @param pu8g : U8G pointer * @param pu8g : U8G pointer
* @param x : position x axis * @param x : position x axis
* @param y : position y 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 * @param max_width : the pixel width of the string allowed
* *
* @return number of pixels advanced * @return number of pixels advanced
* *
* Draw a UTF-8 string at the specified position * 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; struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root; font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g); const font_t *fnt_default = uxg_GetFont(pu8g);
@ -176,7 +176,7 @@ unsigned int uxg_DrawWchar(u8g_t *pu8g, unsigned int x, unsigned int y, const lc
data.adv = 0; data.adv = 0;
data.max_width = max_width; data.max_width = max_width;
data.fnt_prev = nullptr; 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); u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);
return data.adv; return data.adv;

View file

@ -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 * @ 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*/) { 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) { while (*tpl) {
tpl = get_utf8_value_cb(tpl, read_byte, ch); tpl = get_utf8_value_cb(tpl, read_byte, wc);
if (ch > 255) ch |= 0x0080; if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(ch & 0x00FF); const uint8_t ch = uint8_t(wc & 0x00FF);
if (ch == '=' || ch == '~' || ch == '*') { if (ch == '=' || ch == '~' || ch == '*') {
if (index >= 0) { if (index >= 0) {
@ -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*/) { void DWIN_String::add(const char *cstr, uint8_t max_len/*=MAX_STRING_LENGTH*/) {
lchar_t ch; lchar_t wc;
while (*cstr && max_len) { 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; if (wc > 255) wc |= 0x0080;
uint8_t ch = uint8_t(ch & 0x00FF); const uint8_t ch = uint8_t(wc & 0x00FF);
add_character(ch); add_character(ch);
*/ */
add(ch); add(wc);
max_len--; max_len--;
} }
eol(); eol();
} }
void DWIN_String::add(const lchar_t &ch) { void DWIN_String::add(const lchar_t &wc) {
int ret; int ret;
size_t idx = 0; size_t idx = 0;
dwin_charmap_t pinval; dwin_charmap_t pinval;
dwin_charmap_t *copy_address = nullptr; dwin_charmap_t *copy_address = nullptr;
pinval.uchar = ch; pinval.uchar = wc;
pinval.idx = -1; pinval.idx = -1;
// For 8-bit ASCII just print the single ch // For 8-bit ASCII just print the single character
char str[] = { '?', 0 }; char str[] = { '?', 0 };
if (ch < 255) { if (wc < 255) {
str[0] = (char)ch; str[0] = (char)wc;
} }
else { else {
copy_address = nullptr; copy_address = nullptr;

View file

@ -69,10 +69,10 @@ class DWIN_String {
/** /**
* @brief Append a UTF-8 character * @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 add(const lchar_t &wc);
static void set(const lchar_t &ch) { set(); add(ch); } static void set(const lchar_t &wc) { set(); add(wc); }
/** /**
* @brief Append / Set C-string * @brief Append / Set C-string

View file

@ -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; const uint8_t *p = (uint8_t *)utf8_str;
dwin_string.set(); dwin_string.set();
while (dwin_string.length < max_length) { while (dwin_string.length < max_length) {
lchar_t ch; lchar_t wc;
p = get_utf8_value_cb(p, cb_read_byte, ch); p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!ch) break; if (!wc) break;
dwin_string.add(ch); 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()); 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); lcd_advance_cursor(dwin_string.length);

View file

@ -31,8 +31,6 @@
#include "../inc/MarlinConfig.h" #include "../inc/MarlinConfig.h"
#define MAX_UTF8_CHAR_SIZE 4
#if HAS_WIRED_LCD #if HAS_WIRED_LCD
#include "marlinui.h" #include "marlinui.h"
#include "../MarlinCore.h" #include "../MarlinCore.h"

View file

@ -38,15 +38,23 @@
#define MAX_UTF8_CHAR_SIZE 4 #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 // read a byte from ROM or RAM
typedef uint8_t (*read_byte_cb_t)(const uint8_t * str); typedef uint8_t (*read_byte_cb_t)(const uint8_t * str);
uint8_t read_byte_ram(const uint8_t *str); uint8_t read_byte_ram(const uint8_t *str);
uint8_t read_byte_rom(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; typedef uint16_t pixel_len_t;
#define PIXEL_LEN_NOLIMIT ((pixel_len_t)(-1)) #define PIXEL_LEN_NOLIMIT ((pixel_len_t)(-1))

View file

@ -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; const uint8_t *p = (uint8_t*)ptpl;
int8_t n = maxlen; int8_t n = maxlen;
while (n > 0) { while (n > 0) {
lchar_t ch; lchar_t wc;
p = get_utf8_value_cb(p, read_byte_rom, ch); p = get_utf8_value_cb(p, read_byte_rom, wc);
if (!ch) break; if (!wc) break;
if (ch == '=' || ch == '~' || ch == '*') { if (wc == '=' || wc == '~' || wc == '*') {
if (ind >= 0) { if (ind >= 0) {
if (ch == '*') { lcd_put_lchar('E'); n--; } if (wc == '*') { lcd_put_lchar('E'); n--; }
if (n) { if (n) {
int8_t inum = ind + ((ch == '=') ? 0 : LCD_FIRST_TOOL); int8_t inum = ind + ((wc == '=') ? 0 : LCD_FIRST_TOOL);
if (inum >= 10) { if (inum >= 10) {
lcd_put_lchar('0' + (inum / 10)); n--; lcd_put_lchar('0' + (inum / 10)); n--;
inum %= 10; inum %= 10;
@ -71,19 +71,19 @@ lcd_uint_t lcd_put_u8str_P(PGM_P const ptpl, const int8_t ind, const char *cstr/
break; break;
} }
} }
else if (ch == '$' && fstr) { else if (wc == '$' && fstr) {
n -= lcd_put_u8str_max_P(FTOP(fstr), n * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH); 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); 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)); lcd_put_lchar(AXIS_CHAR(ind));
n--; n--;
} }
else { else {
lcd_put_lchar(ch); lcd_put_lchar(wc);
n -= ch > 255 ? prop : 1; n -= wc > 255 ? prop : 1;
} }
} }
return n; return n;
@ -97,10 +97,10 @@ int calculateWidth(PGM_P const pstr) {
int n = 0; int n = 0;
do { do {
lchar_t ch; lchar_t wc;
p = get_utf8_value_cb(p, read_byte_rom, ch); p = get_utf8_value_cb(p, read_byte_rom, wc);
if (!ch) break; if (!wc) break;
n += (ch > 255) ? prop : 1; n += (wc > 255) ? prop : 1;
} while (1); } while (1);
return n * MENU_FONT_WIDTH; return n * MENU_FONT_WIDTH;

View file

@ -417,26 +417,26 @@ void MarlinUI::init() {
}; };
const uint8_t *p = (uint8_t*)string; const uint8_t *p = (uint8_t*)string;
lchar_t ch; lchar_t wc;
if (wordwrap) { if (wordwrap) {
const uint8_t *wrd = nullptr; const uint8_t *wrd = nullptr;
uint8_t c = 0; uint8_t c = 0;
// find the end of the part // find the end of the part
for (;;) { for (;;) {
if (!wrd) wrd = p; // Get word start /before/ advancing if (!wrd) wrd = p; // Get word start /before/ advancing
p = get_utf8_value_cb(p, cb_read_byte, ch); p = get_utf8_value_cb(p, cb_read_byte, wc);
const bool eol = !ch; // zero ends the string const bool eol = !wc; // zero ends the string
// End or a break between phrases? // End or a break between phrases?
if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') { if (eol || wc == ' ' || wc == '-' || wc == '+' || wc == '.') {
if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces if (!c && wc == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
// Past the right and the word is not too long? // Past the right and the word is not too long?
if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap? if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
c += !eol; // +1 so the space will be printed c += !eol; // +1 so the space will be printed
col += c; // advance col to new position col += c; // advance col to new position
while (c) { // character countdown while (c) { // character countdown
--c; // count down to zero --c; // count down to zero
wrd = get_utf8_value_cb(wrd, cb_read_byte, ch); // get characters again wrd = get_utf8_value_cb(wrd, cb_read_byte, wc); // get characters again
lcd_put_lchar(ch); // character to the LCD lcd_put_lchar(wc); // character to the LCD
} }
if (eol) break; // all done! if (eol) break; // all done!
wrd = nullptr; // set up for next word wrd = nullptr; // set up for next word
@ -446,9 +446,9 @@ void MarlinUI::init() {
} }
else { else {
for (;;) { for (;;) {
p = get_utf8_value_cb(p, cb_read_byte, ch); p = get_utf8_value_cb(p, cb_read_byte, wc);
if (!ch) break; if (!wc) break;
lcd_put_lchar(ch); lcd_put_lchar(wc);
col++; col++;
if (col >= LCD_WIDTH) _newline(); if (col >= LCD_WIDTH) _newline();
} }

View file

@ -94,12 +94,12 @@ void TFT_String::set() {
* @ displays an axis name such as XYZUVW, or E for an extruder * @ 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*/) { 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) { while (*tpl) {
tpl = get_utf8_value_cb(tpl, read_byte_ram, ch); tpl = get_utf8_value_cb(tpl, read_byte_ram, wc);
if (ch > 255) ch |= 0x0080; if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(ch & 0x00FF); const uint8_t ch = uint8_t(wc & 0x00FF);
if (ch == '=' || ch == '~' || ch == '*') { if (ch == '=' || ch == '~' || ch == '*') {
if (index >= 0) { if (index >= 0) {
@ -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*/) { void TFT_String::add(const char *cstr, uint8_t max_len/*=MAX_STRING_LENGTH*/) {
lchar_t ch; lchar_t wc;
while (*cstr && max_len) { while (*cstr && max_len) {
cstr = get_utf8_value_cb(cstr, read_byte_ram, ch); cstr = get_utf8_value_cb(cstr, read_byte_ram, wc);
if (ch > 255) ch |= 0x0080; if (wc > 255) wc |= 0x0080;
const uint8_t ch = uint8_t(ch & 0x00FF); const uint8_t ch = uint8_t(wc & 0x00FF);
add_character(ch); add_character(ch);
max_len--; max_len--;
} }