Created
December 9, 2019 21:47
-
-
Save companje/d7ad4dacf47b31b69298ed5d71793eb6 to your computer and use it in GitHub Desktop.
Wordwrap, ofxWordwrap
This file contains 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
string ofxWordWrap(string input, int maxWidth, ofTrueTypeFont *font) { | |
vector<string> lines = ofSplitString(input,"\n"); | |
for (int l=0; l<lines.size(); l++) { | |
vector<string> words = ofSplitString(lines[l]," "); | |
int strWidth=0; | |
for (int w=0; w<words.size(); w++) { | |
int nextWidth = font ? font->stringWidth(words[w]+"i") : words[w].length()+1; | |
if (strWidth+nextWidth < maxWidth) { | |
strWidth+=nextWidth; | |
} else { | |
strWidth=nextWidth; | |
words[w] = "\n" + words[w]; | |
} | |
} | |
lines[l] = ofJoinString(words, " "); | |
} | |
return ofJoinString(lines, "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment