i desire a function that, given a string, a width constraint, and a Font, returns a copy of the string with word wrapping applied. although Font:getWrap helpfully returns the number of lines that the text was wrapped to, it does not return the wrapped text itself. do you have a solution?
i have already accomplished this by modifying the source code for the getWrap function like so:
Code: Select all
/*modules/graphics/opengl/wrap_Font.cpp*/
int w_Font_getWrap(lua_State *L)
{
Font *t = luax_checkfont(L, 1);
const char *str = luaL_checkstring(L, 2);
float wrap = (float) luaL_checknumber(L, 3);
int max_width = 0, numlines = 0;
std::vector<std::string> lines;
luax_catchexcept(L, [&]() {
lines = t->getWrap(str, wrap, &max_width);
numlines = lines.size();
});
// return max width and number of lines
lua_pushinteger(L, max_width);
lua_pushinteger(L, numlines);
// my addition: return a list of the lines
lua_newtable(L);
for (int i = 0; i<numlines; i++){
lua_pushinteger(L, i+1);
lua_pushstring(L, lines[i].c_str());
lua_settable(L, -3);
}
return 3;
}
Code: Select all
myFont = love.graphics.newFont(18)
width, numLines, lines = myFont:getWrap("The quick brown fox jumps over the lazy dog.", 200)
print(lines[1]) # The quick brown fox
for your curiosity: my main purpose for this is to create text boxes that have vertical margins and hide any lines of text that would flow beyond the margins.