Cleaned up font code to always use uint32 instead of the slightly more ambiguous "unsigned int" for codepoints

This commit is contained in:
Alex Szpakowski
2013-06-16 19:48:53 -03:00
parent d48d1fd441
commit 76618f0328
17 changed files with 60 additions and 63 deletions
+3 -2
View File
@@ -25,6 +25,7 @@
#include "Rasterizer.h" #include "Rasterizer.h"
#include "image/ImageData.h" #include "image/ImageData.h"
#include "common/Module.h" #include "common/Module.h"
#include "common/int.h"
// STD // STD
#include <string> #include <string>
@@ -41,9 +42,9 @@ public:
virtual Rasterizer *newRasterizer(Data *data, int size) = 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, const std::string &glyphs) = 0;
virtual Rasterizer *newRasterizer(love::image::ImageData *data, unsigned int *glyphs, int length) = 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, const std::string &glyph) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, unsigned int glyph) = 0; virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph) = 0;
// Implement Module // Implement Module
virtual const char *getName() const = 0; virtual const char *getName() const = 0;
+2 -2
View File
@@ -32,7 +32,7 @@ namespace love
namespace font namespace font
{ {
GlyphData::GlyphData(unsigned int glyph, GlyphMetrics glyphMetrics, GlyphData::Format f) GlyphData::GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, GlyphData::Format f)
: glyph(glyph) : glyph(glyph)
, metrics(glyphMetrics) , metrics(glyphMetrics)
, data(0) , data(0)
@@ -88,7 +88,7 @@ int GlyphData::getWidth() const
return metrics.width; return metrics.width;
} }
unsigned int GlyphData::getGlyph() const uint32 GlyphData::getGlyph() const
{ {
return glyph; return glyph;
} }
+5 -4
View File
@@ -26,6 +26,7 @@
#include "common/Data.h" #include "common/Data.h"
#include "common/Exception.h" #include "common/Exception.h"
#include "common/StringMap.h" #include "common/StringMap.h"
#include "common/int.h"
// stdlib // stdlib
#include <string> #include <string>
@@ -62,7 +63,7 @@ public:
FORMAT_MAX_ENUM FORMAT_MAX_ENUM
}; };
GlyphData(unsigned int glyph, GlyphMetrics glyphMetrics, Format f); GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, Format f);
virtual ~GlyphData(); virtual ~GlyphData();
// Implements Data. // Implements Data.
@@ -80,9 +81,9 @@ public:
virtual int getWidth() const; virtual int getWidth() const;
/** /**
* Gets the glyph itself. * Gets the glyph codepoint itself.
**/ **/
unsigned int getGlyph() const; uint32 getGlyph() const;
/** /**
* Gets the glyph as a UTF-8 string (instead of a UTF-8 code point.) * Gets the glyph as a UTF-8 string (instead of a UTF-8 code point.)
@@ -135,7 +136,7 @@ public:
private: private:
// The glyph codepoint itself. // The glyph codepoint itself.
unsigned int glyph; uint32 glyph;
// Glyph metrics. // Glyph metrics.
GlyphMetrics metrics; GlyphMetrics metrics;
+9 -9
View File
@@ -34,7 +34,7 @@ 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); return (a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
} }
ImageRasterizer::ImageRasterizer(love::image::ImageData *data, unsigned int *glyphs, int numglyphs) ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
: imageData(data) : imageData(data)
, glyphs(glyphs) , glyphs(glyphs)
, numglyphs(numglyphs) , numglyphs(numglyphs)
@@ -53,13 +53,13 @@ int ImageRasterizer::getLineHeight() const
return getHeight(); return getHeight();
} }
GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
{ {
GlyphMetrics gm; GlyphMetrics gm;
memset(&gm, 0, sizeof(GlyphMetrics)); memset(&gm, 0, sizeof(GlyphMetrics));
// Set relevant glyph metrics if the glyph is in this ImageFont // Set relevant glyph metrics if the glyph is in this ImageFont
std::map<unsigned int, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph); std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
if (it != imageGlyphs.end()) if (it != imageGlyphs.end())
{ {
gm.width = it->second.width; gm.width = it->second.width;
@@ -95,8 +95,8 @@ void ImageRasterizer::load()
{ {
love::image::pixel *pixels = (love::image::pixel *) imageData->getData(); love::image::pixel *pixels = (love::image::pixel *) imageData->getData();
unsigned int imgw = (unsigned int) imageData->getWidth(); int imgw = imageData->getWidth();
unsigned int imgh = (unsigned int) imageData->getHeight(); int imgh = imageData->getHeight();
// Set the only metric that matters // Set the only metric that matters
metrics.height = imgh; metrics.height = imgh;
@@ -104,10 +104,10 @@ void ImageRasterizer::load()
// Reading texture data begins // Reading texture data begins
spacer = pixels[0]; spacer = pixels[0];
unsigned int start = 0; int start = 0;
unsigned int end = 0; int end = 0;
for (unsigned int i = 0; i < numglyphs; ++i) for (int i = 0; i < numglyphs; ++i)
{ {
start = end; start = end;
@@ -151,7 +151,7 @@ int ImageRasterizer::getGlyphCount() const
return numglyphs; return numglyphs;
} }
bool ImageRasterizer::hasGlyph(unsigned int glyph) const bool ImageRasterizer::hasGlyph(uint32 glyph) const
{ {
return imageGlyphs.find(glyph) != imageGlyphs.end(); return imageGlyphs.find(glyph) != imageGlyphs.end();
} }
+9 -9
View File
@@ -39,14 +39,14 @@ namespace font
class ImageRasterizer : public Rasterizer class ImageRasterizer : public Rasterizer
{ {
public: public:
ImageRasterizer(love::image::ImageData *imageData, unsigned int *glyphs, int numglyphs); ImageRasterizer(love::image::ImageData *imageData, uint32 *glyphs, int numglyphs);
virtual ~ImageRasterizer(); virtual ~ImageRasterizer();
// Implement Rasterizer // Implement Rasterizer
virtual int getLineHeight() const; virtual int getLineHeight() const;
virtual GlyphData *getGlyphData(unsigned int glyph) const; virtual GlyphData *getGlyphData(uint32 glyph) const;
virtual int getGlyphCount() const; virtual int getGlyphCount() const;
virtual bool hasGlyph(unsigned int glyph) const; virtual bool hasGlyph(uint32 glyph) const;
private: private:
// Load all the glyph positions into memory // Load all the glyph positions into memory
@@ -56,20 +56,20 @@ private:
love::image::ImageData *imageData; love::image::ImageData *imageData;
// The glyphs in the font // The glyphs in the font
unsigned int *glyphs; uint32 *glyphs;
// Number of glyphs in the font // Number of glyphs in the font
unsigned int numglyphs; int numglyphs;
// Information about a glyph in the ImageData // Information about a glyph in the ImageData
struct ImageGlyphData struct ImageGlyphData
{ {
unsigned int x; int x;
unsigned int width; int width;
unsigned int spacing; int spacing;
}; };
std::map<unsigned int, ImageGlyphData> imageGlyphs; std::map<uint32, ImageGlyphData> imageGlyphs;
// Color used to identify glyph separation in the source ImageData // Color used to identify glyph separation in the source ImageData
love::image::pixel spacer; love::image::pixel spacer;
+2 -2
View File
@@ -55,7 +55,7 @@ int Rasterizer::getDescent() const
GlyphData *Rasterizer::getGlyphData(const std::string &text) const GlyphData *Rasterizer::getGlyphData(const std::string &text) const
{ {
unsigned int codepoint = 0; uint32 codepoint = 0;
try try
{ {
@@ -74,7 +74,7 @@ bool Rasterizer::hasGlyph(const std::string &text) const
if (text.size() == 0) if (text.size() == 0)
return false; return false;
unsigned int codepoint = 0; uint32 codepoint = 0;
try try
{ {
+3 -2
View File
@@ -23,6 +23,7 @@
// LOVE // LOVE
#include "common/Object.h" #include "common/Object.h"
#include "common/int.h"
#include "GlyphData.h" #include "GlyphData.h"
namespace love namespace love
@@ -79,7 +80,7 @@ public:
* Gets a specific glyph. * Gets a specific glyph.
* @param glyph The (UNICODE) glyph codepoint to get data for. * @param glyph The (UNICODE) glyph codepoint to get data for.
**/ **/
virtual GlyphData *getGlyphData(unsigned int glyph) const = 0; virtual GlyphData *getGlyphData(uint32 glyph) const = 0;
/** /**
* Gets a specific glyph. * Gets a specific glyph.
@@ -96,7 +97,7 @@ public:
* Gets whether this Rasterizer has a specific glyph. * Gets whether this Rasterizer has a specific glyph.
* @param glyph The (UNICODE) glyph codepoint. * @param glyph The (UNICODE) glyph codepoint.
**/ **/
virtual bool hasGlyph(unsigned int glyph) const = 0; virtual bool hasGlyph(uint32 glyph) const = 0;
/** /**
* Gets whether this Rasterizer has a specific glyph. * Gets whether this Rasterizer has a specific glyph.
+4 -4
View File
@@ -53,7 +53,7 @@ Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string
size_t strlen = text.size(); size_t strlen = text.size();
size_t numglyphs = 0; size_t numglyphs = 0;
unsigned int *glyphs = new unsigned int[strlen]; uint32 *glyphs = new uint32[strlen];
try try
{ {
@@ -75,14 +75,14 @@ Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string
return r; return r;
} }
Rasterizer *Font::newRasterizer(love::image::ImageData *data, unsigned int *glyphs, int numglyphs) Rasterizer *Font::newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
{ {
return new ImageRasterizer(data, glyphs, numglyphs); return new ImageRasterizer(data, glyphs, numglyphs);
} }
GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text) GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text)
{ {
unsigned int codepoint = 0; uint32 codepoint = 0;
try try
{ {
@@ -96,7 +96,7 @@ GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text)
return r->getGlyphData(codepoint); return r->getGlyphData(codepoint);
} }
GlyphData *Font::newGlyphData(Rasterizer *r, unsigned int glyph) GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph)
{ {
return r->getGlyphData(glyph); return r->getGlyphData(glyph);
} }
+2 -2
View File
@@ -56,9 +56,9 @@ public:
// Implements Font // Implements Font
Rasterizer *newRasterizer(Data *data, int size); Rasterizer *newRasterizer(Data *data, int size);
Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &text); Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &text);
Rasterizer *newRasterizer(love::image::ImageData *data, unsigned int *glyphs, int numglyphs); Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs);
GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph); GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph);
GlyphData *newGlyphData(Rasterizer *r, unsigned int glyph); GlyphData *newGlyphData(Rasterizer *r, uint32 glyph);
// Implement Module // Implement Module
const char *getName() const; const char *getName() const;
@@ -63,7 +63,7 @@ int TrueTypeRasterizer::getLineHeight() const
return (int)(getHeight() * 1.25); return (int)(getHeight() * 1.25);
} }
GlyphData *TrueTypeRasterizer::getGlyphData(unsigned int glyph) const GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const
{ {
love::font::GlyphMetrics glyphMetrics; love::font::GlyphMetrics glyphMetrics;
FT_Glyph ftglyph; FT_Glyph ftglyph;
@@ -111,7 +111,7 @@ int TrueTypeRasterizer::getGlyphCount() const
return face->num_glyphs; return face->num_glyphs;
} }
bool TrueTypeRasterizer::hasGlyph(unsigned int glyph) const bool TrueTypeRasterizer::hasGlyph(uint32 glyph) const
{ {
return FT_Get_Char_Index(face, glyph) != 0; return FT_Get_Char_Index(face, glyph) != 0;
} }
@@ -50,9 +50,9 @@ public:
// Implement Rasterizer // Implement Rasterizer
virtual int getLineHeight() const; virtual int getLineHeight() const;
virtual GlyphData *getGlyphData(unsigned int glyph) const; virtual GlyphData *getGlyphData(uint32 glyph) const;
virtual int getGlyphCount() const; virtual int getGlyphCount() const;
virtual bool hasGlyph(unsigned int glyph) const; virtual bool hasGlyph(uint32 glyph) const;
private: private:
+1 -1
View File
@@ -88,7 +88,7 @@ int w_newGlyphData(lua_State *L)
} }
else else
{ {
unsigned int g = (unsigned int) luaL_checknumber(L, 2); uint32 g = (uint32) luaL_checknumber(L, 2);
t = instance->newGlyphData(r, g); t = instance->newGlyphData(r, g);
} }
+1 -1
View File
@@ -55,7 +55,7 @@ int w_GlyphData_getDimensions(lua_State *L)
int w_GlyphData_getGlyph(lua_State *L) int w_GlyphData_getGlyph(lua_State *L)
{ {
GlyphData *t = luax_checkglyphdata(L, 1); GlyphData *t = luax_checkglyphdata(L, 1);
unsigned int glyph = t->getGlyph(); uint32 glyph = t->getGlyph();
lua_pushnumber(L, (lua_Number) glyph); lua_pushnumber(L, (lua_Number) glyph);
return 1; return 1;
} }
+2 -2
View File
@@ -82,7 +82,7 @@ int w_Rasterizer_getGlyphData(lua_State *L)
} }
else else
{ {
unsigned int glyph = (unsigned int) luaL_checknumber(L, 2); uint32 glyph = (uint32) luaL_checknumber(L, 2);
g = t->getGlyphData(glyph); g = t->getGlyphData(glyph);
} }
} }
@@ -113,7 +113,7 @@ int w_Rasterizer_hasGlyph(lua_State *L)
if (lua_type(L, 2) == LUA_TSTRING) if (lua_type(L, 2) == LUA_TSTRING)
hasglyph = t->hasGlyph(luax_checkstring(L, 2)); hasglyph = t->hasGlyph(luax_checkstring(L, 2));
else else
hasglyph = t->hasGlyph((unsigned int) luaL_checknumber(L, 2)); hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2));
} }
catch (love::Exception &e) catch (love::Exception &e)
{ {
+7 -12
View File
@@ -172,7 +172,7 @@ void Font::createTexture()
setFilter(filter); setFilter(filter);
} }
Font::Glyph *Font::addGlyph(unsigned int glyph) Font::Glyph *Font::addGlyph(uint32 glyph)
{ {
love::font::GlyphData *gd = rasterizer->getGlyphData(glyph); love::font::GlyphData *gd = rasterizer->getGlyphData(glyph);
int w = gd->getWidth(); int w = gd->getWidth();
@@ -243,7 +243,7 @@ Font::Glyph *Font::addGlyph(unsigned int glyph)
return g; return g;
} }
Font::Glyph *Font::findGlyph(unsigned int glyph) Font::Glyph *Font::findGlyph(uint32 glyph)
{ {
Glyph *g = glyphs[glyph]; Glyph *g = glyphs[glyph];
if (!g) if (!g)
@@ -283,7 +283,7 @@ void Font::print(const std::string &text, float x, float y, float letter_spacing
while (i != end) while (i != end)
{ {
unsigned int g = *i++; uint32 g = *i++;
if (g == '\n') if (g == '\n')
{ {
@@ -392,7 +392,7 @@ int Font::getWidth(const std::string &str)
utf8::iterator<std::string::const_iterator> end(line.end(), line.begin(), line.end()); utf8::iterator<std::string::const_iterator> end(line.end(), line.begin(), line.end());
while (i != end) while (i != end)
{ {
unsigned int c = *i++; uint32 c = *i++;
g = findGlyph(c); g = findGlyph(c);
width += static_cast<int>(g->spacing * mSpacing); width += static_cast<int>(g->spacing * mSpacing);
} }
@@ -409,12 +409,7 @@ int Font::getWidth(const std::string &str)
return max_width; return max_width;
} }
int Font::getWidth(const char *str) int Font::getWidth(char character)
{
return this->getWidth(std::string(str));
}
int Font::getWidth(unsigned int character)
{ {
Glyph *g = findGlyph(character); Glyph *g = findGlyph(character);
return g->spacing; return g->spacing;
@@ -526,7 +521,7 @@ bool Font::loadVolatile()
void Font::unloadVolatile() void Font::unloadVolatile()
{ {
// nuke everything from orbit // nuke everything from orbit
std::map<unsigned int, Glyph *>::iterator it = glyphs.begin(); std::map<uint32, Glyph *>::iterator it = glyphs.begin();
Glyph *g; Glyph *g;
while (it != glyphs.end()) while (it != glyphs.end())
{ {
@@ -559,7 +554,7 @@ float Font::getBaseline() const
return (type == FONT_TRUETYPE) ? floor(getHeight() / 1.25f + 0.5f) : 0.0f; return (type == FONT_TRUETYPE) ? floor(getHeight() / 1.25f + 0.5f) : 0.0f;
} }
bool Font::hasGlyph(unsigned int glyph) const bool Font::hasGlyph(uint32 glyph) const
{ {
return rasterizer->hasGlyph(glyph); return rasterizer->hasGlyph(glyph);
} }
+5 -6
View File
@@ -81,14 +81,13 @@ public:
* @param str A string of text. * @param str A string of text.
**/ **/
int getWidth(const std::string &str); int getWidth(const std::string &str);
int getWidth(const char *str);
/** /**
* Returns the width of the passed character. * Returns the width of the passed character.
* *
* @param character A character. * @param character A character.
**/ **/
int getWidth(unsigned int character); int getWidth(char character);
/** /**
* Returns the maximal width of a wrapped string * Returns the maximal width of a wrapped string
@@ -138,7 +137,7 @@ public:
int getDescent() const; int getDescent() const;
float getBaseline() const; float getBaseline() const;
bool hasGlyph(unsigned int glyph) const; bool hasGlyph(uint32 glyph) const;
bool hasGlyph(const std::string &text) const; bool hasGlyph(const std::string &text) const;
private: private:
@@ -194,7 +193,7 @@ private:
std::vector<GLuint> textures; std::vector<GLuint> textures;
// maps glyphs to glyph texture information // maps glyphs to glyph texture information
std::map<unsigned int, Glyph *> glyphs; std::map<uint32, Glyph *> glyphs;
FontType type; FontType type;
Image::Filter filter; Image::Filter filter;
@@ -210,8 +209,8 @@ private:
bool initializeTexture(GLint format); bool initializeTexture(GLint format);
void createTexture(); void createTexture();
Glyph *addGlyph(unsigned int glyph); Glyph *addGlyph(uint32 glyph);
Glyph *findGlyph(unsigned int glyph); Glyph *findGlyph(uint32 glyph);
}; // Font }; // Font
} // opengl } // opengl
+1 -1
View File
@@ -162,7 +162,7 @@ int w_Font_hasGlyph(lua_State *L)
if (lua_type(L, 2) == LUA_TSTRING) if (lua_type(L, 2) == LUA_TSTRING)
hasglyph = t->hasGlyph(luax_checkstring(L, 2)); hasglyph = t->hasGlyph(luax_checkstring(L, 2));
else else
hasglyph = t->hasGlyph((unsigned int) luaL_checknumber(L, 2)); hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2));
} }
catch (love::Exception &e) catch (love::Exception &e)
{ {