From 372d4afa4a9da3ad61ddc15ca32a149d5c4da40a Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Wed, 31 Jan 2024 14:40:22 +0000 Subject: [PATCH] Fix g.wrapString lockup if wrap width is less than the character width --- ChangeLog | 1 + libs/graphics/jswrap_graphics.c | 3 ++- tests/test_graphics_wrapString.js | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index c0b0c8783..75ae24d69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,7 @@ Add jsvGet...Child functions to replace common call patterns - saves 280 bytes on Pico Fix issue with g.wrapString when running on flash-based strings Fix lock leak when using flat/flash/native strings as object indices + Fix g.wrapString lockup if wrap width is less than the character width 2v20 : Ensure String.charCodeAt returns NaN for out of bounds chars Bangle.js2: When rendering overlays, *do not* use the current FG/BG color for 1 bit overlays diff --git a/libs/graphics/jswrap_graphics.c b/libs/graphics/jswrap_graphics.c index a53b7547e..0c0ecd1ab 100644 --- a/libs/graphics/jswrap_graphics.c +++ b/libs/graphics/jswrap_graphics.c @@ -2428,7 +2428,8 @@ JsVar *jswrap_graphics_wrapString(JsVar *parent, JsVar *str, int maxWidth) { while (wordStartIdx < currentPos) { char wordCh = jsvGetCharInString(str, wordStartIdx); int w = _jswrap_graphics_getCharWidth(&gfx, &info, wordCh); - if (width+w < maxWidth) { + if (width+w < maxWidth || !width) { // add while it fits OR it's the first character + // !width stops us locking up if char width>split width wordStartIdx++; wordWidth -= w; lineWidth -= w; diff --git a/tests/test_graphics_wrapString.js b/tests/test_graphics_wrapString.js index c07d756e7..3139ffb54 100644 --- a/tests/test_graphics_wrapString.js +++ b/tests/test_graphics_wrapString.js @@ -40,6 +40,11 @@ g.clear().setFont("4x6"); lines = g.wrapString("X", 10); SHOULD_BE(lines, ["X"]); +// wrapping when the area is too small to show even one char +g.clear().setFont("4x6"); +lines = g.wrapString("XYZ", 2); +SHOULD_BE(lines, ["X","Y","Z"]); + // wrap a long word to multiple lines g.clear().setFont("4x6"); lines = g.wrapString("ABCDEFGHIJ", 10); @@ -114,4 +119,6 @@ require("Storage").erase("x") SHOULD_BE(lines, ["Compacting...","Takes approx","1 minute"]); + + result = ok;