GRIM/EMI: Text wrapping fixes

94699b8 fixes a buffer overflow when the text to be wrapped does not contain spaces. Previously there was a loop that never terminated, which eventually caused an out of bounds string buffer access. The original code was quite confusing, so I cleaned it up a bit to make it more readable.