From f5a683a3cc953b1dcf518a8514af64c492864cd2 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 9 Nov 2023 14:48:36 +0000 Subject: [PATCH] Graphics: wrapString will now wrap lines on comma, dot and dash --- ChangeLog | 1 + libs/graphics/jswrap_graphics.c | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index f6f4402b40..5ebf9cbdc2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,6 +22,7 @@ Bangle.js2: Fix spurious tap events when HRM enabled by changing threshold Reinstate `if (0);"test"` fix after f87a53c accidentally reverted it Bangle.js2: Do a soft reset every time we start the SPL06 pressure sensor (stops occasional sensor lockup) + Graphics: wrapString will now wrap lines on comma, dot and dash 2v19 : Fix Object.values/entries for numeric keys after 2v18 regression (fix #2375) nRF52: for SD>5 use static buffers for advertising and scan response data (#2367) diff --git a/libs/graphics/jswrap_graphics.c b/libs/graphics/jswrap_graphics.c index c478c7d4ca..4cfd11ca95 100644 --- a/libs/graphics/jswrap_graphics.c +++ b/libs/graphics/jswrap_graphics.c @@ -2355,6 +2355,7 @@ JsVar *jswrap_graphics_wrapString(JsVar *parent, JsVar *str, int maxWidth) { int spaceWidth = _jswrap_graphics_getCharWidth(&gfx, &info, ' '); int wordWidth = 0; int lineWidth = 0; + bool lineHasSpaceAfter = false; int wordStartIdx = 0; int wordIdxAtMaxWidth = 0; // index just before the word width>maxWidth int wordWidthAtMaxWidth = 0; // index just before the word width>maxWidth @@ -2366,32 +2367,36 @@ JsVar *jswrap_graphics_wrapString(JsVar *parent, JsVar *str, int maxWidth) { while (jsvStringIteratorHasChar(&it) || endOfText) { int ch = jsvStringIteratorGetUTF8CharAndNext(&it); - if (endOfText || ch=='\n' || ch==' ') { // newline or space + bool canBreakOnCh = endOfText || ch=='\n' || ch==' '; + bool canBreakAfterCh = ch==',' || ch=='.' || ch=='-'; + if (canBreakOnCh || canBreakAfterCh) { // newline or space int currentPos = (int)jsvStringIteratorGetIndex(&it); + bool includeCh = canBreakAfterCh; if ((lineWidth + spaceWidth + wordWidth <= maxWidth) && !wasNewLine) { // all on one line - if (lineWidth) { + if (lineHasSpaceAfter) { jsvAppendString(currentLine, " "); lineWidth += spaceWidth; } - jsvAppendStringVar(currentLine, str, wordStartIdx, currentPos-(wordStartIdx+1)); + jsvAppendStringVar(currentLine, str, wordStartIdx, currentPos-(wordStartIdx+ (includeCh?0:1))); + lineHasSpaceAfter = !canBreakAfterCh; lineWidth += wordWidth; - } else { // doesn't fit one one line - move to new line + } else { // doesn't fit on one line - move to new line lineWidth = wordWidth; if (jsvGetStringLength(currentLine) || wasNewLine) { jsvArrayPush(lines, currentLine); } jsvUnLock(currentLine); if (wordIdxAtMaxWidth) { - // word is too long to fit on a line - currentLine = jsvNewFromStringVar(str, wordStartIdx, wordIdxAtMaxWidth-(wordStartIdx+1)); + // word is too long to fit on a line, split it // jsvNewFromStringVar will create a unicode string is str was a unicode string - jsvArrayPushAndUnLock(lines, currentLine); + jsvArrayPushAndUnLock(lines, jsvNewFromStringVar(str, wordStartIdx, wordIdxAtMaxWidth-(wordStartIdx+1))); wordStartIdx = wordIdxAtMaxWidth-1; lineWidth -= wordWidthAtMaxWidth; } currentLine = jsvNewFromStringVar(str, wordStartIdx, currentPos-(wordStartIdx+1)); + lineHasSpaceAfter = !canBreakAfterCh; // jsvNewFromStringVar will create a unicode string is str was a unicode string } wordWidth = 0;