function string_split_by_width(_str, _line_width, _font=draw_get_font()) {
if (_str == undefined) return;
if (_line_width < 0) _line_width = 10000000;
var _prev_font = draw_get_font();
draw_set_font(_font);
var _whitespace = " ";
var _newline = chr(0x0a);
var _newline2 = chr(0x0d);
var _arr = [];
var _arr_index = 0;
var _length = string_length(_str);
var _new_str = _str;
var lastChar = string_char_at(_new_str, 0);
var _start = 0;
var _end = 0;
while (_start < _length)
{
var total = 0;
if (_line_width == 10000000)
{
while (_end < _length && string_char_at(_new_str, _end) != _newline && string_char_at(_new_str, _end) != _newline2)
{
_end++;
if (_end < _length) lastChar = string_char_at(_new_str, _end); else lastChar = chr(0x0);
}
var c;
if (_end < _length) c = string_char_at(_new_str, _end); else c = String.fromCharCode(0x0);
if ((_newline == lastChar) && (_newline2 == string_char_at(_new_str, _end))) { _end++; continue; }
if ((_newline2 == lastChar) && (_newline == string_char_at(_new_str, _end))) { _end++; continue; }
lastChar = string_char_at(_new_str, _end);
_arr[_arr_index++] = string_copy(_new_str, _start, _end-_start);
}
else
{
while (_end < _length && total < _line_width)
{
c = string_char_at(_new_str, _end);
if (string_char_at(_new_str, _end) != _whitespace) break;
total += string_width(c);
_end++;
}
while (_end < _length && total < _line_width)
{
c = string_char_at(_new_str, _end);
if (c == _newline) break;
total += string_width(c);
_end++;
}
if (total > _line_width)
{
_end--;
total -= string_width(string_char_at(_new_str, _end));
}
if (string_char_at(_new_str, _end) == _newline)
{
_arr[_arr_index++] = string_copy(_new_str, _start, _end-_start);
} else
{
if (_end == _start)
{
draw_set_font(_prev_font);
return _arr;
}
if (_end != _length)
{
if ((string_char_at(_new_str, _end) != _whitespace) || (string_char_at(_new_str, _end) != _whitespace && string_char_at(_new_str, _end + 1) != _whitespace))
{
var e = _end;
while (_end > _start)
{
if (string_char_at(_new_str, --e) == _whitespace) break;
}
if(e!=_start)
{
_end = e;
}
else {
while(string_char_at(_new_str, _end)!=_whitespace)
_end++;
}
}
}
var __end = _end;
if (__end > _start)
{
while (string_char_at(_new_str, __end - 1) == _whitespace && __end>0)
{
__end--;
}
}
if(__end!=_start)
_arr[_arr_index++] = string_copy(_new_str, _start, __end-_start);
}
}
_start = ++_end;
}
draw_set_font(_prev_font);
retur