scrolling text scrolling, unicode-aware strlen

This commit is contained in:
Frank
2025-11-20 18:05:53 +01:00
parent 45f793b338
commit 4a4887aaba
3 changed files with 27 additions and 4 deletions

View File

@@ -10,6 +10,10 @@
#include "FX.h"
#include "fcn_declare.h"
#if defined(WLED_ENABLE_FULL_FONTS)
#include "src/font/codepages.h"
#endif
#ifdef WLEDMM_FASTPATH
#undef SEGMENT
#undef SEGENV
@@ -6861,11 +6865,16 @@ uint16_t mode_2Dscrollingtext(void) {
else if ((!strncmp_P(text,PSTR("#AMP"),4)) || (!strncmp_P(text,PSTR("#POW"),4))) sprintf_P(text, PSTR("%3.1fA"), float(strip.currentMilliamps)/1000.0f); // WLEDMM
else sprintf_P(text, PSTR("%s %d, %d %d:%02d%s"), monthShortStr(month(localTime)), day(localTime), year(localTime), AmPmHour, minute(localTime), sec);
} else drawShadow = false; // static text does not require shadow
const int numberOfLetters = strlen(text);
const int numberOfChars = strlen(text);
#if defined(WLED_ENABLE_FULL_FONTS)
const int numberOfLetters = strlenUC((unsigned char *)text); // get the mumber of unicode letters
#else
const int numberOfLetters = numberOfChars;
#endif
long delayTime = long(strip.now) - long(SEGENV.step);
if ((delayTime >= 0) || (abs(delayTime) > 1500)) { // WLEDMM keep on scrolling if timebase jumps (supersync, or brightness off, or wifi delay)
//WLEDMM ToDO: adjust scolling logic for unicode (single UTF-8 letters need up to 4 chars)
if ((numberOfLetters * letterWidth) > cols) ++SEGENV.aux0 %= (numberOfLetters * letterWidth) + cols; // offset
else SEGENV.aux0 = (cols + (numberOfLetters * letterWidth))/2;
SEGENV.aux1 = (SEGENV.aux1 + 1) & 0xFF; // color shift // WLEDMM changed to prevent overflow
@@ -6900,12 +6909,12 @@ uint16_t mode_2Dscrollingtext(void) {
col1 = SEGCOLOR(0);
col2 = SEGCOLOR(2);
}
SEGMENT.drawText((unsigned char*)text, maxLen, numberOfLetters, int(cols) - int(SEGENV.aux0), yoffset, letterWidth, letterHeight, col1, col2, drawShadow);
SEGMENT.drawText((unsigned char*)text, maxLen, numberOfChars, int(cols) - int(SEGENV.aux0), yoffset, letterWidth, letterHeight, col1, col2, drawShadow);
#endif
return FRAMETIME;
}
static const char _data_FX_MODE_2DSCROLLTEXT[] PROGMEM = "Scrolling Text@!,Y Offset,Trail,Font size,,Gradient,Overlay;!,!,Gradient;!;2;ix=128,c1=0,rev=0,mi=0,rY=0,mY=0";
static const char _data_FX_MODE_2DSCROLLTEXT[] PROGMEM = "Scrolling Text@!,Y Offset,Trail,Font size,,Gradient,Overlay,Soft;!,!,Gradient;!;2;ix=128,c1=0,rev=0,mi=0,rY=0,mY=0";
////////////////////////////

View File

@@ -16,6 +16,9 @@ uint16_t unicodeToWchar16(const unsigned char* utf8, size_t maxLen); /
// return nullptr at end of input
const unsigned char* nextUnicode(const unsigned char* utf8, size_t maxLen); // unicodetool.cpp
// unicode-aware string length
size_t strlenUC(const unsigned char* utf8);
// translates unicode 2-byte (UTF-16) "code point" into corresponding character in codepage 437 (IBM PC aka PC-8)
uint16_t wchar16ToCodepage437(uint16_t wideChar); // codepage437.cpp

View File

@@ -72,4 +72,15 @@ const unsigned char* nextUnicode(const unsigned char* utf8, size_t maxLen) {
else return utf8 + codeLength; // success: advance stream
}
// unicode-aware string length
size_t strlenUC(const unsigned char* utf8) {
if ((utf8 == nullptr) || (utf8[0] == '\0')) return 0;
size_t maxLen = strlen((const char *)utf8);
size_t letters = 0;
for(const unsigned char* now = utf8; now != nullptr && now[0] != '\0'; now = nextUnicode(now, maxLen)) // iterates over utf-8 and count letters
letters++;
return letters;
}
#endif