Alex Szpakowski avatar Alex Szpakowski committed 624351c

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

Comments (0)

Files changed (17)

src/modules/font/Font.h

 #include "Rasterizer.h"
 #include "image/ImageData.h"
 #include "common/Module.h"
+#include "common/int.h"
 
 // STD
 #include <string>
 
 	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;

src/modules/font/GlyphData.cpp

 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)
 	return metrics.width;
 }
 
-unsigned int GlyphData::getGlyph() const
+uint32 GlyphData::getGlyph() const
 {
 	return glyph;
 }

src/modules/font/GlyphData.h

 #include "common/Data.h"
 #include "common/Exception.h"
 #include "common/StringMap.h"
+#include "common/int.h"
 
 // stdlib
 #include <string>
 		FORMAT_MAX_ENUM
 	};
 
-	GlyphData(unsigned int glyph, GlyphMetrics glyphMetrics, Format f);
+	GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, Format f);
 	virtual ~GlyphData();
 
 	// Implements Data.
 	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.)
 private:
 
 	// The glyph codepoint itself.
-	unsigned int glyph;
+	uint32 glyph;
 
 	// Glyph metrics.
 	GlyphMetrics metrics;

src/modules/font/ImageRasterizer.cpp

 	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)
 	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<unsigned int, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
+	std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
 	if (it != imageGlyphs.end())
 	{
 		gm.width = it->second.width;
 {
 	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;
 	// 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;
 
 	return numglyphs;
 }
 
-bool ImageRasterizer::hasGlyph(unsigned int glyph) const
+bool ImageRasterizer::hasGlyph(uint32 glyph) const
 {
 	return imageGlyphs.find(glyph) != imageGlyphs.end();
 }

src/modules/font/ImageRasterizer.h

 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
 	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<unsigned int, ImageGlyphData> imageGlyphs;
+	std::map<uint32, ImageGlyphData> imageGlyphs;
 
 	// Color used to identify glyph separation in the source ImageData
 	love::image::pixel spacer;

src/modules/font/Rasterizer.cpp

 
 GlyphData *Rasterizer::getGlyphData(const std::string &text) const
 {
-	unsigned int codepoint = 0;
+	uint32 codepoint = 0;
 
 	try
 	{
 	if (text.size() == 0)
 		return false;
 
-	unsigned int codepoint = 0;
+	uint32 codepoint = 0;
 
 	try
 	{

src/modules/font/Rasterizer.h

 
 // LOVE
 #include "common/Object.h"
+#include "common/int.h"
 #include "GlyphData.h"
 
 namespace love
 	 * 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.
 	 * 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.

src/modules/font/freetype/Font.cpp

 	size_t strlen = text.size();
 	size_t numglyphs = 0;
 
-	unsigned int *glyphs = new unsigned int[strlen];
+	uint32 *glyphs = new uint32[strlen];
 
 	try
 	{
 	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
 	{
 	return r->getGlyphData(codepoint);
 }
 
-GlyphData *Font::newGlyphData(Rasterizer *r, unsigned int glyph)
+GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph)
 {
 	return r->getGlyphData(glyph);
 }

src/modules/font/freetype/Font.h

 	// 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;

src/modules/font/freetype/TrueTypeRasterizer.cpp

 	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;
 	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;
 }

src/modules/font/freetype/TrueTypeRasterizer.h

 
 	// 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:
 

src/modules/font/freetype/wrap_Font.cpp

 	}
 	else
 	{
-		unsigned int g = (unsigned int) luaL_checknumber(L, 2);
+		uint32 g = (uint32) luaL_checknumber(L, 2);
 		t = instance->newGlyphData(r, g);
 	}
 

src/modules/font/wrap_GlyphData.cpp

 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;
 }

src/modules/font/wrap_Rasterizer.cpp

 		}
 		else
 		{
-			unsigned int glyph = (unsigned int) luaL_checknumber(L, 2);
+			uint32 glyph = (uint32) luaL_checknumber(L, 2);
 			g = t->getGlyphData(glyph);
 		}
 	}
 		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)
 	{

src/modules/graphics/opengl/Font.cpp

 	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();
 	return g;
 }
 
-Font::Glyph *Font::findGlyph(unsigned int glyph)
+Font::Glyph *Font::findGlyph(uint32 glyph)
 {
 	Glyph *g = glyphs[glyph];
 	if (!g)
 
 		while (i != end)
 		{
-			unsigned int g = *i++;
+			uint32 g = *i++;
 
 			if (g == '\n')
 			{
 			utf8::iterator<std::string::const_iterator> end(line.end(), line.begin(), line.end());
 			while (i != end)
 			{
-				unsigned int c = *i++;
+				uint32 c = *i++;
 				g = findGlyph(c);
 				width += static_cast<int>(g->spacing * mSpacing);
 			}
 	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;
 void Font::unloadVolatile()
 {
 	// nuke everything from orbit
-	std::map<unsigned int, Glyph *>::iterator it = glyphs.begin();
+	std::map<uint32, Glyph *>::iterator it = glyphs.begin();
 	Glyph *g;
 	while (it != glyphs.end())
 	{
 	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);
 }

src/modules/graphics/opengl/Font.h

 	 * @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
 	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:
 	std::vector<GLuint> textures;
 
 	// maps glyphs to glyph texture information
-	std::map<unsigned int, Glyph *> glyphs;
+	std::map<uint32, Glyph *> glyphs;
 
 	FontType type;
 	Image::Filter filter;
 
 	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

src/modules/graphics/opengl/wrap_Font.cpp

 		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)
 	{
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.