Added support for loading BMFont bitmap font files with love.graphics.newFont (see issue #190.) Some esoteric BMFont features are not supported.

Moved the Font module Lua wrapper code out of the freetype implementation folder.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2014-09-28 16:00:40 -03:00
parent 34688e929e
commit 8edb90a719
18 changed files with 714 additions and 199 deletions
+1 -1
View File
@@ -474,7 +474,7 @@ bool Filesystem::remove(const char *file)
return true;
}
Data *Filesystem::read(const char *filename, int64 size) const
FileData *Filesystem::read(const char *filename, int64 size) const
{
File file(filename);
+1 -1
View File
@@ -202,7 +202,7 @@ public:
* @param filename The name of the file to read from.
* @param size The size in bytes of the data to read.
**/
Data *read(const char *filename, int64 size = File::ALL) const;
FileData *read(const char *filename, int64 size = File::ALL) const;
/**
* Write data to a file.
+317
View File
@@ -0,0 +1,317 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "BMFontRasterizer.h"
#include "filesystem/physfs/Filesystem.h"
#include "image/Image.h"
// C++
#include <sstream>
#include <vector>
// C
#include <cstdlib>
#include <cstring>
namespace love
{
namespace font
{
namespace
{
/**
* Helper class for parsing lines in BMFont definition files.
* NOTE: Does not properly handle multi-value attributes (e.g. 'padding' or
* 'spacing'.)
**/
class BMFontLine
{
public:
BMFontLine(const std::string &line);
const std::string &getTag() const { return tag; }
int getAttributeInt(const char *name) const;
std::string getAttributeString(const char *name) const;
private:
std::string tag;
std::map<std::string, std::string> attributes;
};
// This is not entirely robust...
BMFontLine::BMFontLine(const std::string &line)
{
// The tag name should always be at the start of the line.
tag = line.substr(0, line.find(' '));
size_t startpos = 0;
while (startpos < line.length())
{
// Find the next '=', which indicates a key-value pair.
size_t fpos = line.find('=', startpos);
if (fpos == std::string::npos || fpos + 1 >= line.length())
break;
// The key should be between a space character and the '='.
size_t keystart = line.rfind(' ', fpos);
if (keystart == std::string::npos)
break;
keystart++;
std::string key = line.substr(keystart, fpos - keystart);
size_t valstart = fpos + 1;
size_t valend = valstart + 1;
if (line[valstart] == '"')
{
// Values can be surrounded by quotes (a literal string.)
valstart++;
valend = line.find('"', valstart) - 1;
}
else
{
// Otherwise look for the next space character after the '='.
valend = line.find(' ', valstart + 1) - 1;
}
valend = std::min(valend, line.length() - 1);
attributes[key] = line.substr(valstart, valend - valstart + 1);
startpos = valend + 1;
}
}
int BMFontLine::getAttributeInt(const char *name) const
{
auto it = attributes.find(name);
if (it == attributes.end())
return 0;
return (int) strtol(it->second.c_str(), nullptr, 10);
}
std::string BMFontLine::getAttributeString(const char *name) const
{
auto it = attributes.find(name);
if (it == attributes.end())
return "";
return it->second;
}
} // anonymous namespace
BMFontRasterizer::BMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &imagelist)
: fontSize(0)
, unicode(false)
, lineHeight(0)
{
const std::string &filename = fontdef->getFilename();
size_t separatorpos = filename.rfind('/');
if (separatorpos != std::string::npos)
fontFolder = filename.substr(0, separatorpos);
// The parseConfig function will try to load any missing page images.
for (size_t i = 0; i < imagelist.size(); i++)
images[i] = imagelist[i];
std::string configtext((const char *) fontdef->getData(), fontdef->getSize());
parseConfig(configtext);
}
BMFontRasterizer::~BMFontRasterizer()
{
}
void BMFontRasterizer::parseConfig(const std::string &configtext)
{
std::stringstream ss(configtext);
std::string line;
while (std::getline(ss, line))
{
BMFontLine cline(line);
const std::string &tag = cline.getTag();
if (tag == "info")
{
fontSize = cline.getAttributeInt("size");
unicode = cline.getAttributeInt("unicode") > 0;
}
else if (tag == "common")
{
lineHeight = cline.getAttributeInt("lineHeight");
metrics.ascent = cline.getAttributeInt("base");
metrics.height = lineHeight;
}
else if (tag == "page")
{
size_t pageindex = (size_t) cline.getAttributeInt("id");
std::string filename = cline.getAttributeString("file");
// The file name is relative to the font file's folder.
if (!fontFolder.empty())
filename = fontFolder + "/" + filename;
// Load the page file from disk into an ImageData, if necessary.
if (images[pageindex].get() == nullptr)
{
using namespace love::filesystem::physfs;
Filesystem *filesystem = Module::getInstance<Filesystem>(Module::M_FILESYSTEM);
image::Image *imagemodule = Module::getInstance<image::Image>(Module::M_IMAGE);
if (!filesystem)
throw love::Exception("Filesystem module not loaded!");
if (!imagemodule)
throw love::Exception("Image module not loaded!");
// Release these variables right away since StrongRef retains.
StrongRef<filesystem::FileData> data = filesystem->read(filename.c_str());
data->release();
images[pageindex].set(imagemodule->newImageData(data.get()));
images[pageindex]->release();
}
}
else if (tag == "char")
{
BMFontCharacter c = {};
uint32 id = (uint32) cline.getAttributeInt("id");
c.x = cline.getAttributeInt("x");
c.y = cline.getAttributeInt("y");
c.page = cline.getAttributeInt("page");
c.metrics.width = cline.getAttributeInt("width");
c.metrics.height = cline.getAttributeInt("height");
c.metrics.bearingX = cline.getAttributeInt("xoffset");
c.metrics.bearingY = -cline.getAttributeInt("yoffset");
c.metrics.advance = cline.getAttributeInt("xadvance");
characters[id] = c;
}
else if (tag == "kerning")
{
// TODO
}
}
if (lineHeight == 0)
throw love::Exception("Invalid BMFont file (missing line height attribute?)");
if (characters.size() == 0)
throw love::Exception("Invalid BMFont file (no character definitions?)");
// Verify the glyph character attributes.
for (const auto &cpair : characters)
{
const BMFontCharacter &c = cpair.second;
if (!unicode && cpair.first > 127)
throw love::Exception("Invalid BMFont character id (only unicode and ASCII are supported)");
if (c.page < 0 || images[c.page].get() == nullptr)
throw love::Exception("Invalid BMFont character page id: %d", c.page);
const image::ImageData *id = images[c.page].get();
if (!id->inside(c.x, c.y) || !id->inside(c.x + c.metrics.width, c.y + c.metrics.height))
throw love::Exception("Invalid BMFont character coordinates.");
}
}
int BMFontRasterizer::getLineHeight() const
{
return lineHeight;
}
GlyphData *BMFontRasterizer::getGlyphData(uint32 glyph) const
{
auto it = characters.find(glyph);
// Return an empty GlyphData if we don't have the glyph character.
if (it == characters.end())
return new GlyphData(glyph, GlyphMetrics(), GlyphData::FORMAT_RGBA);
const BMFontCharacter &c = it->second;
GlyphData *g = new GlyphData(glyph, c.metrics, GlyphData::FORMAT_RGBA);
const auto &imagepair = images.find(c.page);
if (imagepair == images.end())
{
g->release();
return new GlyphData(glyph, GlyphMetrics(), GlyphData::FORMAT_RGBA);
}
image::ImageData *imagedata = imagepair->second.get();
image::pixel *pixels = (image::pixel *) g->getData();
const image::pixel *ipixels = (const image::pixel *) imagedata->getData();
love::thread::Lock lock(imagedata->getMutex());
// Copy the subsection of the texture from the ImageData to the GlyphData.
for (int y = 0; y < c.metrics.height; y++)
{
size_t idindex = (c.y + y) * imagedata->getWidth() + c.x;
memcpy(&pixels[y * c.metrics.width], &ipixels[idindex], sizeof(image::pixel) * c.metrics.width);
}
return g;
}
int BMFontRasterizer::getGlyphCount() const
{
return (int) characters.size();
}
bool BMFontRasterizer::hasGlyph(uint32 glyph) const
{
return characters.find(glyph) != characters.end();
}
bool BMFontRasterizer::accepts(love::filesystem::FileData *fontdef)
{
const char *data = (const char *) fontdef->getData();
// Check if the "info" tag is at the start of the file. This is a truly
// crappy test. Is the tag even guaranteed to be at the start?
return fontdef->getSize() > 4 && memcmp(data, "info", 4) == 0;
}
} // font
} // love
+85
View File
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_BMFONT_RASTERIZER_H
#define LOVE_FONT_BMFONT_RASTERIZER_H
// LOVE
#include "Rasterizer.h"
#include "image/ImageData.h"
// C++
#include <map>
#include <vector>
namespace love
{
namespace font
{
/**
* Rasterizer for BMFont bitmap fonts.
**/
class BMFontRasterizer : public Rasterizer
{
public:
BMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &imagelist);
virtual ~BMFontRasterizer();
// Implements Rasterizer.
int getLineHeight() const override;
GlyphData *getGlyphData(uint32 glyph) const override;
int getGlyphCount() const override;
bool hasGlyph(uint32 glyph) const override;
static bool accepts(love::filesystem::FileData *fontdef);
private:
struct BMFontCharacter
{
int x;
int y;
int page;
GlyphMetrics metrics;
};
void parseConfig(const std::string &config);
std::string fontFolder;
// Image pages, indexed by their page id.
std::map<int, StrongRef<image::ImageData>> images;
// Glyph characters, indexed by their glyph id.
std::map<uint32, BMFontCharacter> characters;
int fontSize;
bool unicode;
int lineHeight;
}; // BMFontRasterizer
} // font
} // love
#endif // LOVE_FONT_BMFONT_RASTERIZER_H
+13 -7
View File
@@ -24,11 +24,13 @@
// LOVE
#include "Rasterizer.h"
#include "image/ImageData.h"
#include "filesystem/FileData.h"
#include "common/Module.h"
#include "common/int.h"
// STD
// C++
#include <string>
#include <vector>
namespace love
{
@@ -42,16 +44,20 @@ public:
virtual ~Font() {}
// Implements Module.
virtual ModuleType getModuleType() const { return M_FONT; }
virtual Rasterizer *newRasterizer(love::filesystem::FileData *data) = 0;
virtual Rasterizer *newTrueTypeRasterizer(love::filesystem::FileData *data, int size) = 0;
virtual Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images) = 0;
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;
virtual Rasterizer *newRasterizer(Data *data, int size) = 0;
virtual Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
virtual Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph) = 0;
// Implement Module
// Implement Module.
virtual ModuleType getModuleType() const { return M_FONT; }
virtual const char *getName() const = 0;
}; // Font
+1 -2
View File
@@ -53,8 +53,7 @@ int ImageRasterizer::getLineHeight() const
GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
{
GlyphMetrics gm;
memset(&gm, 0, sizeof(GlyphMetrics));
GlyphMetrics gm = {};
// Set relevant glyph metrics if the glyph is in this ImageFont
std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
+23 -13
View File
@@ -22,6 +22,7 @@
#include "TrueTypeRasterizer.h"
#include "font/ImageRasterizer.h"
#include "font/BMFontRasterizer.h"
#include "libraries/utf8/utf8.h"
@@ -35,7 +36,7 @@ namespace freetype
Font::Font()
{
if (FT_Init_FreeType(&library))
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed\n");
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed");
}
Font::~Font()
@@ -43,17 +44,30 @@ Font::~Font()
FT_Done_FreeType(library);
}
Rasterizer *Font::newRasterizer(Data *data, int size)
Rasterizer *Font::newRasterizer(love::filesystem::FileData *data)
{
if (TrueTypeRasterizer::accepts(library, data))
return newTrueTypeRasterizer(data, 12);
else if (BMFontRasterizer::accepts(data))
return newBMFontRasterizer(data, std::vector<image::ImageData *>());
throw love::Exception("Invalid font file: %s", data->getFilename().c_str());
}
Rasterizer *Font::newTrueTypeRasterizer(love::filesystem::FileData *data, int size)
{
return new TrueTypeRasterizer(library, data, size);
}
Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string &text)
Rasterizer *Font::newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images)
{
size_t strlen = text.size();
size_t numglyphs = 0;
return new BMFontRasterizer(fontdef, images);
}
uint32 *glyphs = new uint32[strlen];
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text)
{
std::vector<uint32> glyphs;
glyphs.reserve(text.size());
try
{
@@ -61,21 +75,17 @@ Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string
utf8::iterator<std::string::const_iterator> end(text.end(), text.begin(), text.end());
while (i != end)
glyphs[numglyphs++] = *i++;
glyphs.push_back(*i++);
}
catch (utf8::exception &e)
{
delete [] glyphs;
throw love::Exception("Decoding error: %s", e.what());
}
Rasterizer *r = newRasterizer(data, glyphs, numglyphs);
delete [] glyphs;
return r;
return newImageRasterizer(data, &glyphs[0], glyphs.size());
}
Rasterizer *Font::newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
{
return new ImageRasterizer(data, glyphs, numglyphs);
}
+10 -11
View File
@@ -25,11 +25,7 @@
#include "font/Font.h"
// FreeType2
#ifdef LOVE_MACOSX_USE_FRAMEWORKS
#include <freetype/ft2build.h>
#else
#include <ft2build.h>
#endif
#include FT_FREETYPE_H
#include FT_GLYPH_H
@@ -45,16 +41,18 @@ class Font : public love::font::Font
public:
Font();
/**
* Destructor.
**/
virtual ~Font();
// Implements Font
Rasterizer *newRasterizer(Data *data, int size);
Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &text);
Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs);
Rasterizer *newRasterizer(love::filesystem::FileData *data);
Rasterizer *newTrueTypeRasterizer(love::filesystem::FileData *data, int size);
Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images);
Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &text);
Rasterizer *newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs);
GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph);
GlyphData *newGlyphData(Rasterizer *r, uint32 glyph);
@@ -65,6 +63,7 @@ private:
// FreeType library
FT_Library library;
}; // Font
} // freetype
@@ -30,7 +30,7 @@ namespace font
namespace freetype
{
TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, Data *data, int size)
TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, love::filesystem::FileData *data, int size)
: data(data)
{
FT_Error err = FT_Err_Ok;
@@ -150,6 +150,15 @@ bool TrueTypeRasterizer::hasGlyph(uint32 glyph) const
return FT_Get_Char_Index(face, glyph) != 0;
}
bool TrueTypeRasterizer::accepts(FT_Library library, love::filesystem::FileData *data)
{
const FT_Byte *fbase = (const FT_Byte *) data->getData();
FT_Long fsize = (FT_Long) data->getSize();
// Pasing in -1 for the face index lets us test if the data is valid.
return FT_New_Memory_Face(library, fbase, fsize, -1, nullptr) == 0;
}
} // freetype
} // font
} // love
@@ -25,7 +25,7 @@
#include "filesystem/File.h"
#include "font/Rasterizer.h"
// TrueType2
// FreeType2
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
@@ -43,7 +43,8 @@ namespace freetype
class TrueTypeRasterizer : public Rasterizer
{
public:
TrueTypeRasterizer(FT_Library library, Data *data, int size);
TrueTypeRasterizer(FT_Library library, love::filesystem::FileData *data, int size);
virtual ~TrueTypeRasterizer();
// Implement Rasterizer
@@ -52,14 +53,17 @@ public:
virtual int getGlyphCount() const;
virtual bool hasGlyph(uint32 glyph) const;
static bool accepts(FT_Library library, love::filesystem::FileData *data);
private:
// TrueType face
FT_Face face;
// File data
Object::StrongRef<Data> data;
}; // FreetypeRasterizer
StrongRef<love::filesystem::FileData> data;
}; // TrueTypeRasterizer
} // freetype
} // font
-128
View File
@@ -1,128 +0,0 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_Font.h"
#include "Font.h"
#include "font/wrap_GlyphData.h"
#include "font/wrap_Rasterizer.h"
#include "filesystem/wrap_Filesystem.h"
#include "TrueTypeRasterizer.h"
namespace love
{
namespace font
{
namespace freetype
{
#define instance() (Module::getInstance<Font>(Module::M_FONT))
int w_newRasterizer(lua_State *L)
{
Rasterizer *t = nullptr;
if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T))
{
love::image::ImageData *d = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
const char *g = luaL_checkstring(L, 2);
std::string glyphs(g);
luax_catchexcept(L, [&](){ t = instance()->newRasterizer(d, glyphs); });
}
else if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
{
love::filesystem::FileData *d = love::filesystem::luax_getfiledata(L, 1);
int size = luaL_checkint(L, 2);
luax_catchexcept(L,
[&]() { t = instance()->newRasterizer(d, size); },
[&]() { d->release(); }
);
}
else
return luaL_argerror(L, 1, "expected ImageData, filename, or FileData");
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
t->release();
return 1;
}
int w_newGlyphData(lua_State *L)
{
Rasterizer *r = luax_checkrasterizer(L, 1);
GlyphData *t = nullptr;
// newGlyphData accepts a unicode character or a codepoint number.
if (lua_type(L, 2) == LUA_TSTRING)
{
std::string glyph = luax_checkstring(L, 2);
luax_catchexcept(L, [&](){ t = instance()->newGlyphData(r, glyph); });
}
else
{
uint32 g = (uint32) luaL_checknumber(L, 2);
t = instance()->newGlyphData(r, g);
}
luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, t);
t->release();
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "newRasterizer", w_newRasterizer },
{ "newGlyphData", w_newGlyphData },
{ 0, 0 }
};
static const lua_CFunction types[] =
{
luaopen_glyphdata,
luaopen_rasterizer,
0
};
extern "C" int luaopen_love_font(lua_State *L)
{
Font *instance = instance();
if (instance == nullptr)
{
luax_catchexcept(L, [&](){ instance = new Font(); });
}
else
instance->retain();
WrappedModule w;
w.module = instance;
w.name = "font";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
return luax_register_module(L, w);
}
} // freetype
} // font
} // love
+209
View File
@@ -0,0 +1,209 @@
/**
* Copyright (c) 2006-2014 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_Font.h"
#include "Font.h"
#include "freetype/Font.h"
#include "wrap_GlyphData.h"
#include "wrap_Rasterizer.h"
#include "filesystem/wrap_Filesystem.h"
namespace love
{
namespace font
{
#define instance() (Module::getInstance<Font>(Module::M_FONT))
int w_newRasterizer(lua_State *L)
{
if (lua_isnoneornil(L, 2))
{
// Single argument: call Font::newRasterizer.
Rasterizer *t = nullptr;
filesystem::FileData *d = filesystem::luax_getfiledata(L, 1);
luax_catchexcept(L,
[&]() { t = instance()->newRasterizer(d); },
[&]() { d->release(); }
);
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
t->release();
return 1;
}
else if (lua_type(L, 2) == LUA_TNUMBER)
{
// Second argument is a number: call newTrueTypeRasterizer.
return w_newTrueTypeRasterizer(L);
}
else
{
// Otherwise call newBMFontRasterizer.
return w_newBMFontRasterizer(L);
}
}
int w_newTrueTypeRasterizer(lua_State *L)
{
Rasterizer *t = nullptr;
filesystem::FileData *d = filesystem::luax_getfiledata(L, 1);
int size = luaL_optint(L, 2, 12);
luax_catchexcept(L,
[&]() { t = instance()->newTrueTypeRasterizer(d, size); },
[&]() { d->release(); }
);
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
t->release();
return 1;
}
static void convimagedata(lua_State *L, int idx)
{
if (lua_isstring(L, idx) || luax_istype(L, idx, FILESYSTEM_FILE_T) || luax_istype(L, idx, FILESYSTEM_FILE_DATA_T))
luax_convobj(L, idx, "image", "newImageData");
}
int w_newBMFontRasterizer(lua_State *L)
{
Rasterizer *t = nullptr;
filesystem::FileData *d = filesystem::luax_getfiledata(L, 1);
std::vector<image::ImageData *> images;
if (lua_istable(L, 2))
{
for (int i = 1; i <= (int) lua_objlen(L, 2); i++)
{
lua_rawgeti(L, 2, i);
convimagedata(L, -1);
image::ImageData *id = luax_checktype<image::ImageData>(L, -1, "ImageData", IMAGE_IMAGE_DATA_T);
images.push_back(id);
id->retain();
lua_pop(L, 1);
}
}
else
{
for (int i = 2; i <= lua_gettop(L); i++)
{
convimagedata(L, i);
image::ImageData *id = luax_checktype<image::ImageData>(L, i, "ImageData", IMAGE_IMAGE_DATA_T);
images.push_back(id);
id->retain();
}
}
luax_catchexcept(L,
[&]() { t = instance()->newBMFontRasterizer(d, images); },
[&]() { d->release(); for (auto id : images) id->release(); }
);
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
t->release();
return 1;
}
int w_newImageRasterizer(lua_State *L)
{
Rasterizer *t = nullptr;
convimagedata(L, 1);
image::ImageData *d = luax_checktype<image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
std::string glyphs = luax_checkstring(L, 2);
luax_catchexcept(L, [&](){ t = instance()->newImageRasterizer(d, glyphs); });
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
t->release();
return 1;
}
int w_newGlyphData(lua_State *L)
{
Rasterizer *r = luax_checkrasterizer(L, 1);
GlyphData *t = nullptr;
// newGlyphData accepts a unicode character or a codepoint number.
if (lua_type(L, 2) == LUA_TSTRING)
{
std::string glyph = luax_checkstring(L, 2);
luax_catchexcept(L, [&](){ t = instance()->newGlyphData(r, glyph); });
}
else
{
uint32 g = (uint32) luaL_checknumber(L, 2);
t = instance()->newGlyphData(r, g);
}
luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, t);
t->release();
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "newRasterizer", w_newRasterizer },
{ "newTrueTypeRasterizer", w_newTrueTypeRasterizer },
{ "newBMFontRasterizer", w_newBMFontRasterizer },
{ "newImageRasterizer", w_newImageRasterizer },
{ "newGlyphData", w_newGlyphData },
{ 0, 0 }
};
static const lua_CFunction types[] =
{
luaopen_glyphdata,
luaopen_rasterizer,
0
};
extern "C" int luaopen_love_font(lua_State *L)
{
Font *instance = instance();
if (instance == nullptr)
{
luax_catchexcept(L, [&](){ instance = new freetype::Font(); });
}
else
instance->retain();
WrappedModule w;
w.module = instance;
w.name = "font";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
return luax_register_module(L, w);
}
} // font
} // love
@@ -18,8 +18,8 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_FREETYPE_WRAP_FONT_H
#define LOVE_FONT_FREETYPE_WRAP_FONT_H
#ifndef LOVE_FONT_WRAP_FONT_H
#define LOVE_FONT_WRAP_FONT_H
// LOVE
#include "common/config.h"
@@ -29,15 +29,15 @@ namespace love
{
namespace font
{
namespace freetype
{
int w_newRasterizer(lua_State *L);
int w_newTrueTypeRasterizer(lua_State *L);
int w_newBMFontRasterizer(lua_State *L);
int w_newImageRasterizer(lua_State *L);
int w_newGlyphData(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_font(lua_State *L);
} // freetype
} // font
} // love
#endif // LOVE_FONT_FREETYPE_WRAP_FONT_H
#endif // LOVE_FONT_WRAP_FONT_H
+11 -16
View File
@@ -258,23 +258,24 @@ int w_newQuad(lua_State *L)
int w_newFont(lua_State *L)
{
Font *font = nullptr;
// Convert to Rasterizer, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
if (!luax_istype(L, 1, FONT_RASTERIZER_T))
{
int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newRasterizer");
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", "newRasterizer");
}
love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
Font *font = 0;
luax_catchexcept(L, [&]() {
font = instance()->newFont(rasterizer, instance()->getDefaultFilter()); }
);
if (font == 0)
return luaL_error(L, "Could not load font.");
// Push the type.
luax_pushtype(L, "Font", GRAPHICS_FONT_T, font);
font->release();
@@ -287,9 +288,7 @@ int w_newImageFont(lua_State *L)
Texture::Filter filter = instance()->getDefaultFilter();
// Convert to ImageData if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
luax_convobj(L, 1, "image", "newImageData");
else if (luax_istype(L, 1, GRAPHICS_IMAGE_T))
if (luax_istype(L, 1, GRAPHICS_IMAGE_T))
{
Image *i = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
filter = i->getFilter();
@@ -301,11 +300,11 @@ int w_newImageFont(lua_State *L)
}
// Convert to Rasterizer if necessary.
if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T))
if (!luax_istype(L, 1, FONT_RASTERIZER_T))
{
luaL_checkstring(L, 2);
int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newRasterizer");
luax_convobj(L, idxs, 2, "font", "newImageRasterizer");
}
love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, "Rasterizer", FONT_RASTERIZER_T);
@@ -313,13 +312,9 @@ int w_newImageFont(lua_State *L)
// Create the font.
Font *font = instance()->newFont(rasterizer, filter);
if (font == 0)
return luaL_error(L, "Could not load font.");
// Push the type.
luax_pushtype(L, "Font", GRAPHICS_FONT_T, font);
font->release();
return 1;
}