Skip to content

Commit

Permalink
Graphics: wrapString will now wrap lines on comma, dot and dash
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Nov 9, 2023
1 parent afffbd7 commit f5a683a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions libs/graphics/jswrap_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit f5a683a

Please sign in to comment.