Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function StringInsertNewlines(_string, _lineWidth) {
- // "String" macros
- #macro S_LINE "\n"
- #macro S_SPACE " "
- #macro S_EMPTY ""
- _string += S_SPACE; // Add a space at end to ensure last word isn't missed
- var _stringOut = S_EMPTY, _word = S_EMPTY, _chr,
- _totalWidth = 0,
- _wordWidth = 0;
- for (var _c = 1, _count = string_length(_string) + 1, _chr; _c < _count; _c++)
- {
- // Get next character and add to current word
- _chr = string_char_at(_string, _c);
- _word += _chr;
- if (_chr == S_SPACE) // If a space add the word and compare width
- {
- _wordWidth = string_width(_word);
- if (_totalWidth > 0)
- {
- _totalWidth += _wordWidth;
- if (_totalWidth >= _lineWidth)
- { // Line would be too long, so add a newline before word
- _stringOut += S_LINE + _word;
- _totalWidth = _wordWidth;
- }
- else _stringOut += _word; // Add current word to entire string
- }
- else
- { // First word on line should never have a newline
- _totalWidth += _wordWidth;
- _stringOut += _word;
- }
- // Reset the current word
- _word = S_EMPTY;
- }
- }
- return _stringOut;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement