mirror of
https://github.com/love2d/love.git
synced 2026-08-19 20:19:42 +02:00
Move wrapping to Font::getWrap and make printf use that (fixes issue #230)
This commit is contained in:
@@ -28,6 +28,8 @@
|
|||||||
#include <common/Matrix.h>
|
#include <common/Matrix.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include <algorithm> // for max
|
#include <algorithm> // for max
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
@@ -221,45 +223,54 @@ namespace opengl
|
|||||||
return g->spacing;
|
return g->spacing;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Font::getWrap(const std::string & line, float wrap, int * lines)
|
std::vector<std::string> Font::getWrap(const std::string text, float wrap, int * max_width)
|
||||||
{
|
{
|
||||||
if(line.size() == 0) return 0;
|
using namespace std;
|
||||||
|
const float width_space = static_cast<float>(getWidth(' '));
|
||||||
|
vector<string> lines_to_draw;
|
||||||
int maxw = 0;
|
int maxw = 0;
|
||||||
int linen = 1;
|
|
||||||
int temp = 0;
|
|
||||||
std::string text;
|
|
||||||
Glyph * g;
|
|
||||||
|
|
||||||
|
//split text at newlines
|
||||||
utf8::iterator<std::string::const_iterator> i (line.begin(), line.begin(), line.end());
|
istringstream iss( text );
|
||||||
utf8::iterator<std::string::const_iterator> end (line.end(), line.begin(), line.end());
|
string line;
|
||||||
while (i != end) {
|
while (getline(iss, line, '\n')) {
|
||||||
if(temp > wrap && text.find(" ") != std::string::npos)
|
// split line into words
|
||||||
{
|
vector<string> words;
|
||||||
unsigned int space = text.find_last_of(' ');
|
istringstream word_iss(line);
|
||||||
std::string tmp = text.substr(0, space);
|
copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
|
||||||
int w = getWidth(tmp);
|
back_inserter< vector<string> >(words));
|
||||||
if(w > maxw) maxw = w;
|
|
||||||
text = text.substr(space+1);
|
// put words back together until a wrap occurs
|
||||||
temp = getWidth(text);
|
float width = 0.0f;
|
||||||
linen++;
|
float oldwidth = 0.0f;
|
||||||
|
ostringstream string_builder;
|
||||||
|
vector<string>::const_iterator word_iter;
|
||||||
|
for (word_iter = words.begin(); word_iter != words.end(); ++word_iter) {
|
||||||
|
string word( *word_iter );
|
||||||
|
width += getWidth( word );
|
||||||
|
|
||||||
|
// on wordwrap, push line to line buffer and clear string builder
|
||||||
|
if (width >= wrap && oldwidth > 0) {
|
||||||
|
if (width > maxw)
|
||||||
|
maxw = width;
|
||||||
|
lines_to_draw.push_back( string_builder.str() );
|
||||||
|
string_builder.str( "" );
|
||||||
|
width = static_cast<float>(getWidth( word ));
|
||||||
|
}
|
||||||
|
string_builder << word << " ";
|
||||||
|
width += width_space;
|
||||||
|
oldwidth = width;
|
||||||
}
|
}
|
||||||
int c = *i++;
|
// push last line
|
||||||
g = glyphs[c];
|
if (width > maxw)
|
||||||
if (!g) g = addGlyph(c);
|
maxw = width;
|
||||||
temp += static_cast<int>(g->spacing * mSpacing);
|
lines_to_draw.push_back( string_builder.str() );
|
||||||
utf8::append(c, text.end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(temp > maxw) maxw = temp;
|
if (max_width)
|
||||||
if(lines) *lines = linen;
|
*max_width = maxw;
|
||||||
|
|
||||||
return maxw;
|
return lines_to_draw;
|
||||||
}
|
|
||||||
|
|
||||||
int Font::getWrap(const char * line, float wrap, int * lines)
|
|
||||||
{
|
|
||||||
return getWrap(std::string(line), wrap, lines);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Font::setLineHeight(float height)
|
void Font::setLineHeight(float height)
|
||||||
|
|||||||
@@ -135,12 +135,12 @@ namespace opengl
|
|||||||
* Returns the maximal width of a wrapped string
|
* Returns the maximal width of a wrapped string
|
||||||
* and optionally the number of lines
|
* and optionally the number of lines
|
||||||
*
|
*
|
||||||
* @param line A line of text
|
* @param text The input text
|
||||||
* @param wrap The number of pixels to wrap at
|
* @param wrap The number of pixels to wrap at
|
||||||
* @param lines Optional output of the number of lines needed
|
* @param max_width Optional output of the maximum width
|
||||||
|
* Returns a vector with the lines.
|
||||||
**/
|
**/
|
||||||
int getWrap(const std::string & line, float wrap, int *lines = 0);
|
std::vector<std::string> getWrap(const std::string text, float wrap, int * max_width = 0);
|
||||||
int getWrap(const char * line, float wrap, int *lines = 0);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the line height (which should be a number to multiply the font size by,
|
* Sets the line height (which should be a number to multiply the font size by,
|
||||||
|
|||||||
@@ -803,41 +803,7 @@ namespace opengl
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
string text(str);
|
string text(str);
|
||||||
const float width_space = static_cast<float>(currentFont->getWidth(' '));
|
vector<string> lines_to_draw = currentFont->getWrap(text, wrap);
|
||||||
vector<string> lines_to_draw;
|
|
||||||
|
|
||||||
//split text at newlines
|
|
||||||
istringstream iss( text );
|
|
||||||
string line;
|
|
||||||
while (getline(iss, line, '\n')) {
|
|
||||||
// split line into words
|
|
||||||
vector<string> words;
|
|
||||||
istringstream word_iss(line);
|
|
||||||
copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
|
|
||||||
back_inserter< vector<string> >(words));
|
|
||||||
|
|
||||||
// put words back together until a wrap occurs
|
|
||||||
float width = 0.0f;
|
|
||||||
float oldwidth = 0.0f;
|
|
||||||
ostringstream string_builder;
|
|
||||||
vector<string>::const_iterator word_iter;
|
|
||||||
for (word_iter = words.begin(); word_iter != words.end(); ++word_iter) {
|
|
||||||
string word( *word_iter );
|
|
||||||
width += currentFont->getWidth( word );
|
|
||||||
|
|
||||||
// on wordwrap, push line to line buffer and clear string builder
|
|
||||||
if (width >= wrap && oldwidth > 0) {
|
|
||||||
lines_to_draw.push_back( string_builder.str() );
|
|
||||||
string_builder.str( "" );
|
|
||||||
width = static_cast<float>(currentFont->getWidth( word ));
|
|
||||||
}
|
|
||||||
string_builder << word << " ";
|
|
||||||
width += width_space;
|
|
||||||
oldwidth = width;
|
|
||||||
}
|
|
||||||
// push last line
|
|
||||||
lines_to_draw.push_back( string_builder.str() );
|
|
||||||
}
|
|
||||||
|
|
||||||
// now for the actual printing
|
// now for the actual printing
|
||||||
vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
|
vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
|
||||||
|
|||||||
@@ -52,9 +52,10 @@ namespace opengl
|
|||||||
Font * t = luax_checkfont(L, 1);
|
Font * t = luax_checkfont(L, 1);
|
||||||
const char * str = luaL_checkstring(L, 2);
|
const char * str = luaL_checkstring(L, 2);
|
||||||
float wrap = (float) luaL_checknumber(L, 3);
|
float wrap = (float) luaL_checknumber(L, 3);
|
||||||
int lines = 0;
|
int max_width = 0;
|
||||||
lua_pushinteger(L, t->getWrap(str, wrap, &lines));
|
std::vector<std::string> lines = t->getWrap(str, wrap, &max_width);
|
||||||
lua_pushinteger(L, lines);
|
lua_pushinteger(L, max_width);
|
||||||
|
lua_pushinteger(L, lines.size());
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user