Added an optional spacing argument to love.graphics.newImageFont, allowing additional spacing (positive or negative) to be applied to all glyphs rendered using that font.

This commit is contained in:
Alex Szpakowski
2015-03-11 18:28:22 -03:00
parent b1f4beac56
commit a119e6a862
6 changed files with 28 additions and 19 deletions
+4 -4
View File
@@ -54,7 +54,7 @@ Rasterizer *Font::newBMFontRasterizer(love::filesystem::FileData *fontdef, const
return new BMFontRasterizer(fontdef, images);
}
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text)
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text, int extraspacing)
{
std::vector<uint32> glyphs;
glyphs.reserve(text.size());
@@ -72,12 +72,12 @@ Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::st
throw love::Exception("UTF-8 decoding error: %s", e.what());
}
return newImageRasterizer(data, &glyphs[0], (int) glyphs.size());
return newImageRasterizer(data, &glyphs[0], (int) glyphs.size(), extraspacing);
}
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs, int extraspacing)
{
return new ImageRasterizer(data, glyphs, numglyphs);
return new ImageRasterizer(data, glyphs, numglyphs, extraspacing);
}
GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text)
+2 -2
View File
@@ -52,8 +52,8 @@ public:
virtual Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images);
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &glyphs);
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int length);
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &glyphs, int extraspacing);
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int length, int extraspacing);
virtual GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph);
virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph);
+3 -2
View File
@@ -34,10 +34,11 @@ inline bool equal(const love::image::pixel &a, const love::image::pixel &b)
return (a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
}
ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs, int extraspacing)
: imageData(data)
, glyphs(glyphs)
, numglyphs(numglyphs)
, extraSpacing(extraspacing)
{
load();
}
@@ -60,7 +61,7 @@ GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
if (it != imageGlyphs.end())
{
gm.width = it->second.width;
gm.advance = it->second.width;
gm.advance = it->second.width + extraSpacing;
}
gm.height = metrics.height;
+10 -7
View File
@@ -39,7 +39,7 @@ namespace font
class ImageRasterizer : public Rasterizer
{
public:
ImageRasterizer(love::image::ImageData *imageData, uint32 *glyphs, int numglyphs);
ImageRasterizer(love::image::ImageData *imageData, uint32 *glyphs, int numglyphs, int extraspacing);
virtual ~ImageRasterizer();
// Implement Rasterizer
@@ -49,6 +49,14 @@ public:
virtual bool hasGlyph(uint32 glyph) const;
private:
// Information about a glyph in the ImageData
struct ImageGlyphData
{
int x;
int width;
};
// Load all the glyph positions into memory
void load();
@@ -61,12 +69,7 @@ private:
// Number of glyphs in the font
int numglyphs;
// Information about a glyph in the ImageData
struct ImageGlyphData
{
int x;
int width;
};
int extraSpacing;
std::map<uint32, ImageGlyphData> imageGlyphs;
+2 -1
View File
@@ -162,8 +162,9 @@ int w_newImageRasterizer(lua_State *L)
image::ImageData *d = luax_checktype<image::ImageData>(L, 1, IMAGE_IMAGE_DATA_ID);
std::string glyphs = luax_checkstring(L, 2);
int extraspacing = luaL_optint(L, 3, 0);
luax_catchexcept(L, [&](){ t = instance()->newImageRasterizer(d, glyphs); });
luax_catchexcept(L, [&](){ t = instance()->newImageRasterizer(d, glyphs, extraspacing); });
luax_pushtype(L, FONT_RASTERIZER_ID, t);
t->release();
@@ -339,9 +339,13 @@ int w_newImageFont(lua_State *L)
// Convert to Rasterizer if necessary.
if (!luax_istype(L, 1, FONT_RASTERIZER_ID))
{
luaL_checkstring(L, 2);
int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newImageRasterizer");
luaL_checktype(L, 2, LUA_TSTRING);
std::vector<int> idxs;
for (int i = 0; i < lua_gettop(L); i++)
idxs.push_back(i + 1);
luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newImageRasterizer");
}
love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, FONT_RASTERIZER_ID);