From 76618f03280272fe7af47605ebc28574b9c362d9 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 16 Jun 2013 19:48:53 -0300 Subject: [PATCH] Cleaned up font code to always use uint32 instead of the slightly more ambiguous "unsigned int" for codepoints --- src/modules/font/Font.h | 5 +++-- src/modules/font/GlyphData.cpp | 4 ++-- src/modules/font/GlyphData.h | 9 +++++---- src/modules/font/ImageRasterizer.cpp | 18 +++++++++--------- src/modules/font/ImageRasterizer.h | 18 +++++++++--------- src/modules/font/Rasterizer.cpp | 4 ++-- src/modules/font/Rasterizer.h | 5 +++-- src/modules/font/freetype/Font.cpp | 8 ++++---- src/modules/font/freetype/Font.h | 4 ++-- .../font/freetype/TrueTypeRasterizer.cpp | 4 ++-- .../font/freetype/TrueTypeRasterizer.h | 4 ++-- src/modules/font/freetype/wrap_Font.cpp | 2 +- src/modules/font/wrap_GlyphData.cpp | 2 +- src/modules/font/wrap_Rasterizer.cpp | 4 ++-- src/modules/graphics/opengl/Font.cpp | 19 +++++++------------ src/modules/graphics/opengl/Font.h | 11 +++++------ src/modules/graphics/opengl/wrap_Font.cpp | 2 +- 17 files changed, 60 insertions(+), 63 deletions(-) diff --git a/src/modules/font/Font.h b/src/modules/font/Font.h index b7029fa84..f52d76477 100644 --- a/src/modules/font/Font.h +++ b/src/modules/font/Font.h @@ -25,6 +25,7 @@ #include "Rasterizer.h" #include "image/ImageData.h" #include "common/Module.h" +#include "common/int.h" // STD #include @@ -41,9 +42,9 @@ public: 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, 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, unsigned int glyph) = 0; + virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph) = 0; // Implement Module virtual const char *getName() const = 0; diff --git a/src/modules/font/GlyphData.cpp b/src/modules/font/GlyphData.cpp index 15f903fc4..17702343a 100644 --- a/src/modules/font/GlyphData.cpp +++ b/src/modules/font/GlyphData.cpp @@ -32,7 +32,7 @@ namespace love namespace font { -GlyphData::GlyphData(unsigned int glyph, GlyphMetrics glyphMetrics, GlyphData::Format f) +GlyphData::GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, GlyphData::Format f) : glyph(glyph) , metrics(glyphMetrics) , data(0) @@ -88,7 +88,7 @@ int GlyphData::getWidth() const return metrics.width; } -unsigned int GlyphData::getGlyph() const +uint32 GlyphData::getGlyph() const { return glyph; } diff --git a/src/modules/font/GlyphData.h b/src/modules/font/GlyphData.h index 079c6717b..0bdb92c4e 100644 --- a/src/modules/font/GlyphData.h +++ b/src/modules/font/GlyphData.h @@ -26,6 +26,7 @@ #include "common/Data.h" #include "common/Exception.h" #include "common/StringMap.h" +#include "common/int.h" // stdlib #include @@ -62,7 +63,7 @@ public: FORMAT_MAX_ENUM }; - GlyphData(unsigned int glyph, GlyphMetrics glyphMetrics, Format f); + GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, Format f); virtual ~GlyphData(); // Implements Data. @@ -80,9 +81,9 @@ public: 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.) @@ -135,7 +136,7 @@ public: private: // The glyph codepoint itself. - unsigned int glyph; + uint32 glyph; // Glyph metrics. GlyphMetrics metrics; diff --git a/src/modules/font/ImageRasterizer.cpp b/src/modules/font/ImageRasterizer.cpp index 3d3cf210c..18cf2fe91 100644 --- a/src/modules/font/ImageRasterizer.cpp +++ b/src/modules/font/ImageRasterizer.cpp @@ -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); } -ImageRasterizer::ImageRasterizer(love::image::ImageData *data, unsigned int *glyphs, int numglyphs) +ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs) : imageData(data) , glyphs(glyphs) , numglyphs(numglyphs) @@ -53,13 +53,13 @@ int ImageRasterizer::getLineHeight() const return getHeight(); } -GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const +GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const { GlyphMetrics gm; memset(&gm, 0, sizeof(GlyphMetrics)); // Set relevant glyph metrics if the glyph is in this ImageFont - std::map::const_iterator it = imageGlyphs.find(glyph); + std::map::const_iterator it = imageGlyphs.find(glyph); if (it != imageGlyphs.end()) { gm.width = it->second.width; @@ -95,8 +95,8 @@ void ImageRasterizer::load() { love::image::pixel *pixels = (love::image::pixel *) imageData->getData(); - unsigned int imgw = (unsigned int) imageData->getWidth(); - unsigned int imgh = (unsigned int) imageData->getHeight(); + int imgw = imageData->getWidth(); + int imgh = imageData->getHeight(); // Set the only metric that matters metrics.height = imgh; @@ -104,10 +104,10 @@ void ImageRasterizer::load() // Reading texture data begins spacer = pixels[0]; - unsigned int start = 0; - unsigned int end = 0; + int start = 0; + int end = 0; - for (unsigned int i = 0; i < numglyphs; ++i) + for (int i = 0; i < numglyphs; ++i) { start = end; @@ -151,7 +151,7 @@ int ImageRasterizer::getGlyphCount() const return numglyphs; } -bool ImageRasterizer::hasGlyph(unsigned int glyph) const +bool ImageRasterizer::hasGlyph(uint32 glyph) const { return imageGlyphs.find(glyph) != imageGlyphs.end(); } diff --git a/src/modules/font/ImageRasterizer.h b/src/modules/font/ImageRasterizer.h index 8bf40e135..43a34ebdd 100644 --- a/src/modules/font/ImageRasterizer.h +++ b/src/modules/font/ImageRasterizer.h @@ -39,14 +39,14 @@ namespace font class ImageRasterizer : public Rasterizer { public: - ImageRasterizer(love::image::ImageData *imageData, unsigned int *glyphs, int numglyphs); + ImageRasterizer(love::image::ImageData *imageData, uint32 *glyphs, int numglyphs); virtual ~ImageRasterizer(); // Implement Rasterizer virtual int getLineHeight() const; - virtual GlyphData *getGlyphData(unsigned int glyph) const; + virtual GlyphData *getGlyphData(uint32 glyph) const; virtual int getGlyphCount() const; - virtual bool hasGlyph(unsigned int glyph) const; + virtual bool hasGlyph(uint32 glyph) const; private: // Load all the glyph positions into memory @@ -56,20 +56,20 @@ private: love::image::ImageData *imageData; // The glyphs in the font - unsigned int *glyphs; + uint32 *glyphs; // Number of glyphs in the font - unsigned int numglyphs; + int numglyphs; // Information about a glyph in the ImageData struct ImageGlyphData { - unsigned int x; - unsigned int width; - unsigned int spacing; + int x; + int width; + int spacing; }; - std::map imageGlyphs; + std::map imageGlyphs; // Color used to identify glyph separation in the source ImageData love::image::pixel spacer; diff --git a/src/modules/font/Rasterizer.cpp b/src/modules/font/Rasterizer.cpp index 0e4c8abd7..b8bb01fc4 100644 --- a/src/modules/font/Rasterizer.cpp +++ b/src/modules/font/Rasterizer.cpp @@ -55,7 +55,7 @@ int Rasterizer::getDescent() const GlyphData *Rasterizer::getGlyphData(const std::string &text) const { - unsigned int codepoint = 0; + uint32 codepoint = 0; try { @@ -74,7 +74,7 @@ bool Rasterizer::hasGlyph(const std::string &text) const if (text.size() == 0) return false; - unsigned int codepoint = 0; + uint32 codepoint = 0; try { diff --git a/src/modules/font/Rasterizer.h b/src/modules/font/Rasterizer.h index b98a23bdf..11c59356e 100644 --- a/src/modules/font/Rasterizer.h +++ b/src/modules/font/Rasterizer.h @@ -23,6 +23,7 @@ // LOVE #include "common/Object.h" +#include "common/int.h" #include "GlyphData.h" namespace love @@ -79,7 +80,7 @@ public: * Gets a specific glyph. * @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. @@ -96,7 +97,7 @@ public: * Gets whether this Rasterizer has a specific glyph. * @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. diff --git a/src/modules/font/freetype/Font.cpp b/src/modules/font/freetype/Font.cpp index c9496e35b..a9c6f38c7 100644 --- a/src/modules/font/freetype/Font.cpp +++ b/src/modules/font/freetype/Font.cpp @@ -53,7 +53,7 @@ Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string size_t strlen = text.size(); size_t numglyphs = 0; - unsigned int *glyphs = new unsigned int[strlen]; + uint32 *glyphs = new uint32[strlen]; try { @@ -75,14 +75,14 @@ Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string 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); } GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text) { - unsigned int codepoint = 0; + uint32 codepoint = 0; try { @@ -96,7 +96,7 @@ GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text) return r->getGlyphData(codepoint); } -GlyphData *Font::newGlyphData(Rasterizer *r, unsigned int glyph) +GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph) { return r->getGlyphData(glyph); } diff --git a/src/modules/font/freetype/Font.h b/src/modules/font/freetype/Font.h index d9c390c7d..abe9436c7 100644 --- a/src/modules/font/freetype/Font.h +++ b/src/modules/font/freetype/Font.h @@ -56,9 +56,9 @@ public: // Implements Font Rasterizer *newRasterizer(Data *data, int size); 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, unsigned int glyph); + GlyphData *newGlyphData(Rasterizer *r, uint32 glyph); // Implement Module const char *getName() const; diff --git a/src/modules/font/freetype/TrueTypeRasterizer.cpp b/src/modules/font/freetype/TrueTypeRasterizer.cpp index a63265107..744c1727c 100644 --- a/src/modules/font/freetype/TrueTypeRasterizer.cpp +++ b/src/modules/font/freetype/TrueTypeRasterizer.cpp @@ -63,7 +63,7 @@ int TrueTypeRasterizer::getLineHeight() const return (int)(getHeight() * 1.25); } -GlyphData *TrueTypeRasterizer::getGlyphData(unsigned int glyph) const +GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const { love::font::GlyphMetrics glyphMetrics; FT_Glyph ftglyph; @@ -111,7 +111,7 @@ int TrueTypeRasterizer::getGlyphCount() const 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; } diff --git a/src/modules/font/freetype/TrueTypeRasterizer.h b/src/modules/font/freetype/TrueTypeRasterizer.h index 8e0be69d5..71c2b0090 100644 --- a/src/modules/font/freetype/TrueTypeRasterizer.h +++ b/src/modules/font/freetype/TrueTypeRasterizer.h @@ -50,9 +50,9 @@ public: // Implement Rasterizer virtual int getLineHeight() const; - virtual GlyphData *getGlyphData(unsigned int glyph) const; + virtual GlyphData *getGlyphData(uint32 glyph) const; virtual int getGlyphCount() const; - virtual bool hasGlyph(unsigned int glyph) const; + virtual bool hasGlyph(uint32 glyph) const; private: diff --git a/src/modules/font/freetype/wrap_Font.cpp b/src/modules/font/freetype/wrap_Font.cpp index fa791d00c..12002fcf0 100644 --- a/src/modules/font/freetype/wrap_Font.cpp +++ b/src/modules/font/freetype/wrap_Font.cpp @@ -88,7 +88,7 @@ int w_newGlyphData(lua_State *L) } else { - unsigned int g = (unsigned int) luaL_checknumber(L, 2); + uint32 g = (uint32) luaL_checknumber(L, 2); t = instance->newGlyphData(r, g); } diff --git a/src/modules/font/wrap_GlyphData.cpp b/src/modules/font/wrap_GlyphData.cpp index 6b6066b65..55bf4c767 100644 --- a/src/modules/font/wrap_GlyphData.cpp +++ b/src/modules/font/wrap_GlyphData.cpp @@ -55,7 +55,7 @@ int w_GlyphData_getDimensions(lua_State *L) int w_GlyphData_getGlyph(lua_State *L) { GlyphData *t = luax_checkglyphdata(L, 1); - unsigned int glyph = t->getGlyph(); + uint32 glyph = t->getGlyph(); lua_pushnumber(L, (lua_Number) glyph); return 1; } diff --git a/src/modules/font/wrap_Rasterizer.cpp b/src/modules/font/wrap_Rasterizer.cpp index df9f539da..862ed576c 100644 --- a/src/modules/font/wrap_Rasterizer.cpp +++ b/src/modules/font/wrap_Rasterizer.cpp @@ -82,7 +82,7 @@ int w_Rasterizer_getGlyphData(lua_State *L) } else { - unsigned int glyph = (unsigned int) luaL_checknumber(L, 2); + uint32 glyph = (uint32) luaL_checknumber(L, 2); g = t->getGlyphData(glyph); } } @@ -113,7 +113,7 @@ int w_Rasterizer_hasGlyph(lua_State *L) if (lua_type(L, 2) == LUA_TSTRING) hasglyph = t->hasGlyph(luax_checkstring(L, 2)); else - hasglyph = t->hasGlyph((unsigned int) luaL_checknumber(L, 2)); + hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2)); } catch (love::Exception &e) { diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index 16c04a194..a3c799ba0 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -172,7 +172,7 @@ void Font::createTexture() setFilter(filter); } -Font::Glyph *Font::addGlyph(unsigned int glyph) +Font::Glyph *Font::addGlyph(uint32 glyph) { love::font::GlyphData *gd = rasterizer->getGlyphData(glyph); int w = gd->getWidth(); @@ -243,7 +243,7 @@ Font::Glyph *Font::addGlyph(unsigned int glyph) return g; } -Font::Glyph *Font::findGlyph(unsigned int glyph) +Font::Glyph *Font::findGlyph(uint32 glyph) { Glyph *g = glyphs[glyph]; if (!g) @@ -283,7 +283,7 @@ void Font::print(const std::string &text, float x, float y, float letter_spacing while (i != end) { - unsigned int g = *i++; + uint32 g = *i++; if (g == '\n') { @@ -392,7 +392,7 @@ int Font::getWidth(const std::string &str) utf8::iterator end(line.end(), line.begin(), line.end()); while (i != end) { - unsigned int c = *i++; + uint32 c = *i++; g = findGlyph(c); width += static_cast(g->spacing * mSpacing); } @@ -409,12 +409,7 @@ int Font::getWidth(const std::string &str) return max_width; } -int Font::getWidth(const char *str) -{ - return this->getWidth(std::string(str)); -} - -int Font::getWidth(unsigned int character) +int Font::getWidth(char character) { Glyph *g = findGlyph(character); return g->spacing; @@ -526,7 +521,7 @@ bool Font::loadVolatile() void Font::unloadVolatile() { // nuke everything from orbit - std::map::iterator it = glyphs.begin(); + std::map::iterator it = glyphs.begin(); Glyph *g; while (it != glyphs.end()) { @@ -559,7 +554,7 @@ float Font::getBaseline() const 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); } diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index 8a96d32f8..440b8ffad 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/src/modules/graphics/opengl/Font.h @@ -81,14 +81,13 @@ public: * @param str A string of text. **/ int getWidth(const std::string &str); - int getWidth(const char *str); /** * Returns the width of the passed character. * * @param character A character. **/ - int getWidth(unsigned int character); + int getWidth(char character); /** * Returns the maximal width of a wrapped string @@ -138,7 +137,7 @@ public: int getDescent() const; float getBaseline() const; - bool hasGlyph(unsigned int glyph) const; + bool hasGlyph(uint32 glyph) const; bool hasGlyph(const std::string &text) const; private: @@ -194,7 +193,7 @@ private: std::vector textures; // maps glyphs to glyph texture information - std::map glyphs; + std::map glyphs; FontType type; Image::Filter filter; @@ -210,8 +209,8 @@ private: bool initializeTexture(GLint format); void createTexture(); - Glyph *addGlyph(unsigned int glyph); - Glyph *findGlyph(unsigned int glyph); + Glyph *addGlyph(uint32 glyph); + Glyph *findGlyph(uint32 glyph); }; // Font } // opengl diff --git a/src/modules/graphics/opengl/wrap_Font.cpp b/src/modules/graphics/opengl/wrap_Font.cpp index cc6dce784..a14d92b2d 100644 --- a/src/modules/graphics/opengl/wrap_Font.cpp +++ b/src/modules/graphics/opengl/wrap_Font.cpp @@ -162,7 +162,7 @@ int w_Font_hasGlyph(lua_State *L) if (lua_type(L, 2) == LUA_TSTRING) hasglyph = t->hasGlyph(luax_checkstring(L, 2)); else - hasglyph = t->hasGlyph((unsigned int) luaL_checknumber(L, 2)); + hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2)); } catch (love::Exception &e) {