Skip to content

Commit c54fa9c

Browse files
cytownptoal
authored andcommitted
🐛 Fix wide glyph characters display (MarlinFirmware#22237)
1 parent e0fd33b commit c54fa9c

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

Marlin/src/lcd/dogm/marlinui_DOGM.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,12 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
369369
void MenuItem_static::draw(const uint8_t row, PGM_P const pstr, const uint8_t style/*=SS_DEFAULT*/, const char * const vstr/*=nullptr*/) {
370370

371371
if (mark_as_selected(row, style & SS_INVERT)) {
372-
373372
pixel_len_t n = LCD_PIXEL_WIDTH; // pixel width of string allowed
374-
375-
const int8_t plen = pstr ? utf8_strlen_P(pstr) : 0,
376-
vlen = vstr ? utf8_strlen(vstr) : 0;
373+
374+
const int plen = pstr ? calculateWidth(pstr) : 0,
375+
vlen = vstr ? utf8_strlen(vstr) : 0;
377376
if (style & SS_CENTER) {
378-
int8_t pad = (LCD_WIDTH - plen - vlen) / 2;
377+
int pad = (LCD_PIXEL_WIDTH - plen - vlen * MENU_FONT_WIDTH) / MENU_FONT_WIDTH / 2;
379378
while (--pad >= 0) n -= lcd_put_wchar(' ');
380379
}
381380

@@ -400,8 +399,9 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
400399
if (mark_as_selected(row, sel)) {
401400
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen((char*)inStr)),
402401
pixelwidth = (pgm ? uxg_GetUtf8StrPixelWidthP(u8g.getU8g(), inStr) : uxg_GetUtf8StrPixelWidth(u8g.getU8g(), (char*)inStr));
402+
const u8g_uint_t prop = USE_WIDE_GLYPH ? 2 : 1;
403403

404-
pixel_len_t n = lcd_put_u8str_ind_P(pstr, itemIndex, itemString, LCD_WIDTH - 2 - vallen) * (MENU_FONT_WIDTH);
404+
pixel_len_t n = lcd_put_u8str_ind_P(pstr, itemIndex, itemString, LCD_WIDTH - 2 - vallen * prop) * (MENU_FONT_WIDTH);
405405
if (vallen) {
406406
lcd_put_wchar(':');
407407
while (n > MENU_FONT_WIDTH) n -= lcd_put_wchar(' ');
@@ -414,15 +414,16 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
414414
void MenuEditItemBase::draw_edit_screen(PGM_P const pstr, const char * const value/*=nullptr*/) {
415415
ui.encoder_direction_normal();
416416

417+
const u8g_uint_t prop = USE_WIDE_GLYPH ? 2 : 1;
417418
const u8g_uint_t labellen = utf8_strlen_P(pstr), vallen = utf8_strlen(value);
418-
bool extra_row = labellen > LCD_WIDTH - 2 - vallen;
419+
bool extra_row = labellen * prop > LCD_WIDTH - 2 - vallen * prop;
419420

420421
#if ENABLED(USE_BIG_EDIT_FONT)
421422
// Use the menu font if the label won't fit on a single line
422423
constexpr u8g_uint_t lcd_edit_width = (LCD_PIXEL_WIDTH) / (EDIT_FONT_WIDTH);
423424
u8g_uint_t lcd_chr_fit, one_chr_width;
424-
if (labellen <= lcd_edit_width - 1) {
425-
if (labellen + vallen + 1 > lcd_edit_width) extra_row = true;
425+
if (labellen * prop <= lcd_edit_width - 1) {
426+
if (labellen * prop + vallen * prop + 1 > lcd_edit_width) extra_row = true;
426427
lcd_chr_fit = lcd_edit_width + 1;
427428
one_chr_width = EDIT_FONT_WIDTH;
428429
ui.set_font(FONT_EDIT);
@@ -454,7 +455,7 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
454455
onpage = PAGE_CONTAINS(baseline - (EDIT_FONT_ASCENT - 1), baseline);
455456
}
456457
if (onpage) {
457-
lcd_put_wchar(((lcd_chr_fit - 1) - (vallen + 1)) * one_chr_width, baseline, ' '); // Right-justified, padded, add a leading space
458+
lcd_put_wchar(((lcd_chr_fit - 1) - (vallen * prop + 1)) * one_chr_width, baseline, ' '); // Right-justified, padded, add a leading space
458459
lcd_put_u8str(value);
459460
}
460461
}
@@ -478,8 +479,7 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
478479
void MenuItem_confirm::draw_select_screen(PGM_P const yes, PGM_P const no, const bool yesno, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
479480
ui.draw_select_screen_prompt(pref, string, suff);
480481
draw_boxed_string(1, LCD_HEIGHT - 1, no, !yesno);
481-
const u8g_uint_t xpos = (LCD_WIDTH) / (USE_WIDE_GLYPH ? 2 : 1);
482-
draw_boxed_string(xpos - (utf8_strlen_P(yes) + 1), LCD_HEIGHT - 1, yes, yesno);
482+
draw_boxed_string(LCD_WIDTH - (utf8_strlen_P(yes) * (USE_WIDE_GLYPH ? 2 : 1) + 1), LCD_HEIGHT - 1, yes, yesno);
483483
}
484484

485485
#if ENABLED(SDSUPPORT)

Marlin/src/lcd/lcdprint.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* * displays 'E1'...'E11' for indexes 0 - 10 (By default. Uses LCD_FIRST_TOOL)
4242
*/
4343
lcd_uint_t lcd_put_u8str_ind_P(PGM_P const pstr, const int8_t ind, PGM_P const inStr/*=nullptr*/, const lcd_uint_t maxlen/*=LCD_WIDTH*/) {
44+
const uint8_t prop = USE_WIDE_GLYPH ? 2 : 1;
4445
uint8_t *p = (uint8_t*)pstr;
4546
int8_t n = maxlen;
4647
while (n > 0) {
@@ -73,10 +74,27 @@ lcd_uint_t lcd_put_u8str_ind_P(PGM_P const pstr, const int8_t ind, PGM_P const i
7374
}
7475
else {
7576
lcd_put_wchar(ch);
76-
n--;
77+
n -= ch > 255 ? prop : 1;
7778
}
7879
}
7980
return n;
8081
}
8182

83+
// Calculate UTF8 width with a simple check
84+
int calculateWidth(PGM_P const pstr) {
85+
if (!USE_WIDE_GLYPH) return utf8_strlen_P(pstr) * MENU_FONT_WIDTH;
86+
const uint8_t prop = 2;
87+
uint8_t *p = (uint8_t*)pstr;
88+
int n = 0;
89+
90+
do {
91+
wchar_t ch;
92+
p = get_utf8_value_cb(p, read_byte_rom, &ch);
93+
if (!ch) break;
94+
n += (ch > 255) ? prop : 1;
95+
} while (1);
96+
97+
return n * MENU_FONT_WIDTH;
98+
}
99+
82100
#endif // HAS_WIRED_LCD

Marlin/src/lcd/lcdprint.h

+2
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,5 @@ inline int lcd_put_wchar(const lcd_uint_t col, const lcd_uint_t row, const wchar
172172
lcd_moveto(col, row);
173173
return lcd_put_wchar(c);
174174
}
175+
176+
int calculateWidth(PGM_P const pstr);

0 commit comments

Comments
 (0)