Created
May 5, 2017 05:07
-
-
Save Yanrishatum/cf77f241ab44968491ed61ec7011bb64 to your computer and use it in GitHub Desktop.
Simple WordWrap sample for Miaut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wordWrap(text, width, spaceSize) | |
{ | |
var lines = text.split("\n"); // In case input have newlines, we want to preserve them. | |
var output = []; | |
for (var line of lines) | |
{ | |
var newLine = ""; | |
var words = line.split(" "); | |
var lineSize = 0; | |
for (var word of words) | |
{ | |
var size = word.length * 8; // There may be as well function that calculates word size. | |
// This is a bit basic, with unnecessary space inserted, you probably want to trim it. | |
if (lineSize + size >= width) | |
{ | |
lineSize = size + spaceSize; // reset size, add \n before word. | |
newLine += "\n" + word + " "; | |
} | |
else | |
{ | |
lineSize += size + spaceSize; // Just add word to line. | |
newLine += word + " "; | |
} | |
} | |
output.push(newLine); | |
} | |
return output.join("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment