Given “This is some long text to split” and a value of say 20 as the maximum number of characters per line I would want it split like this:
["This is some text", "to split"]
Splitting on the closest space character less than the max. This should work for a string that would result in any number of lines. I have made some progress but I’m getting errors in the code.
function minv(compare_value, arr) = arr[len(arr)-1] > compare_value ?
max([for (i = [0:len(arr)-1]) if (arr[i] <= compare_value) i]) : undef;
function sub(a, b) = a-b;
function subarr(arr, b) = [for(i=[b:len(arr)-1]) arr[i]];
function rap(s, r, b, arr) = l > 0 ? let(m=search(" ", s, 0)[0], l=r,
_rap=function(idxa, l) is_undef(idxa) != true && len(idxa) > 0 ?
let(idx=minv(r, idxa), index = m[idx],
_r=function(index) !is_undef(index) ? concat(arr,substr(s, b, index)) : concat(arr,substr(s, b, len(s))),
_r(idx),
idx < len(m) ? _rap(subarr(idxa, idx+1), l+r) : undef
) : undef
)
_rap(m, l);
It’s a work in progress but I was hoping someone could help with the error or a better way to do this altogether. Or has someone already solved this? Ideally it would split based on a maximum width in mm using the character widths, but one step at a time. ????
1