Use of Manual Word Wrap Function by Shiffman
in
Programming Questions
•
10 months ago
I came across this function in my search for a MANUAL word wrap function:
It would be perfect for my application, except it does not work. If anyone can take a look at why it creates a null pointer exception apparently at this line:
w += textWidth(c); // accumulate widthit would save me countless hours and possibly days of trying to figure out my own version, as I am terrible at fixing other people's code. Below is the example function set-up to test for convenience sake:
String test;
void setup() {
size(screen.width, screen.height);
test = "Work function, work. Please.";
}
void draw() {
println(wordWrap(test, screen.width));
noLoop();
}
ArrayList wordWrap(String s, int maxWidth) {
// Make an empty ArrayList
ArrayList a = new ArrayList();
float w = 0; // Accumulate width of chars
int i = 0; // Count through chars
int rememberSpace = 0; // Remember where the last space was
// As long as we are not at the end of the String
while (i < s.length()) {
// Current char
char c = s.charAt(i);
w += textWidth(c); // accumulate width
if (c == ' ') rememberSpace = i; // Are we a blank space?
if (w > maxWidth) { // Have we reached the end of a line?
String sub = s.substring(0,rememberSpace); // Make a substring
// Chop off space at beginning
if (sub.length() > 0 && sub.charAt(0) == ' ') sub = sub.substring(1,sub.length());
// Add substring to the list
a.add(sub);
// Reset everything
s = s.substring(rememberSpace,s.length());
i = 0;
w = 0;
}
else {
i++; // Keep going!
}
}
// Take care of the last remaining line
if (s.length() > 0 && s.charAt(0) == ' ') s = s.substring(1,s.length());
a.add(s);
return a;
}
1