Word Wrap Text in a window

The question is simple. Which function or what exactly do I have to do to allow you to enter in a rather long string of text and I word wrap it for you. This clearly seems to be a built in feature so how do I take advantage of it? I would like to be able to wrap sentences that are longer then a specific width specified to drawText or drawTextEx ... 

Ideas? (Sample code too please if possible
I use this, personally, to resize the text itself:

Code:
resizeText = function(text, maxWidth, measure) {    if(!maxWidth) {      throw new Error("No max width given!");    }    if(!(measure instanceof Bitmap)) {      measure = new Bitmap(1, 1);    }    var words = text.split(" ");    var lines = [];    var line = "";    var length = 0;    for(var i = 0; i < words.length; i++) {      var word = words[i];      if(measure.measureTextWidth(this.realText(word)) > maxWidth) {        for(var j = 0; j < word.length; j++) {          if(length + measure.measureTextWidth(this.realText(word[j]) + "-") > maxWidth) {            lines.push(line + "-");            line = word[j];            length = measure.measureTextWidth(this.realText(line));          } else {            length += measure.measureTextWidth(this.realText(word[j]));            line += word[j];          }        }      } else if(length + measure.measureTextWidth(this.realText(word) + " ") > maxWidth) {        lines.push(line.trim());        line = word + " ";        length = measure.measureTextWidth(this.realText(line));      } else {        length += measure.measureTextWidth(this.realText(word) + " ");        line += word + " ";      }    }    lines.push(line);    return lines;  }
Why do you say it's a built-in feature? It's not, as far as I know, unless you're looking at something particular in the script.


One of the plugins from either Yanfly or Yami has word wrapping. If you don't want to use their plugins, you could at least take a look and see how they handle it.
I assumed it was built in.

@Zalerinian Your method is a mess and very hard to read. I feel like it would be very fragile.

I might just look at yanflys word wrapping and steal the concept, that or make you put in short names, Like I do for currency descriptions
Yeah, word wrapping has never been built in.  Some people believe it's there because of that line in the Show Text box when you enter text, but that's got nothing to do with word wrapping - that is there to show you how much text you can have on a line before it starts disappearing.  It moves, depending on whether you have a face or not.
then Ill do what I did with my currencies script, where I said that if you do the description too long it will disappear off the window. 😀
@Zalerinian Your method is a mess and very hard to read. I feel like it would be very fragile.
Care to explain how it's a mess? I've included the function again, along with the documentation for it (Didn't do it earlier because why spend the time copying extra stuff when there's Fallout 4 waiting):

Spoiler
Code:
/** * ========================================================================== * * resizeText(string text, int maxWidth, [bitmap measure]) * * @param text The text to resize * @param maxWidth The max width that the text should fit in * @param measure A bitmap with which to meaure text size. Optional. * * @note *  Takes the given text and resizes it by splitting it by word, or letter *  if absolutely needed, to fit within the given width. The result is *  returned as an array. * * @return An array of strings. * * ========================================================================== */resizeText = function(text, maxWidth, measure) {  if(!maxWidth) {    throw new Error("No max width given!");  }  if(!(measure instanceof Bitmap)) {    measure = new Bitmap(1, 1);  }  var words = text.split(" ");  var lines = [];  var line = "";  var length = 0;  for(var i = 0; i < words.length; i++) {    var word = words[i];    if(measure.measureTextWidth(this.realText(word)) > maxWidth) {      for(var j = 0; j < word.length; j++) {        if(length + measure.measureTextWidth(this.realText(word[j]) + "-") > maxWidth) {          lines.push(line + "-");          line = word[j];          length = measure.measureTextWidth(this.realText(line));        } else {          length += measure.measureTextWidth(this.realText(word[j]));          line += word[j];        }      }    } else if(length + measure.measureTextWidth(this.realText(word) + " ") > maxWidth) {      lines.push(line.trim());      line = word + " ";      length = measure.measureTextWidth(this.realText(line));    } else {      length += measure.measureTextWidth(this.realText(word) + " ");      line += word + " ";    }  }  lines.push(line);  return lines;} 

Original thread: https://forums.rpgmakerweb.com/threads/word-wrap-text-in-a-window.50114/