Browse Source

Cleaned up the GlyphData code.

Alex Szpakowski 9 years ago
parent
commit
a3ff90c94c
2 changed files with 34 additions and 27 deletions
  1. 23 26
      src/modules/font/GlyphData.cpp
  2. 11 1
      src/modules/font/GlyphData.h

+ 23 - 26
src/modules/font/GlyphData.cpp

@@ -40,18 +40,7 @@ GlyphData::GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, GlyphData::Format
 	, format(f)
 	, format(f)
 {
 {
 	if (metrics.width > 0 && metrics.height > 0)
 	if (metrics.width > 0 && metrics.height > 0)
-	{
-		switch (f)
-		{
-		case GlyphData::FORMAT_LUMINANCE_ALPHA:
-			data = new unsigned char[metrics.width * metrics.height * 2];
-			break;
-		case GlyphData::FORMAT_RGBA:
-		default:
-			data = new unsigned char[metrics.width * metrics.height * 4];
-			break;
-		}
-	}
+		data = new uint8[metrics.width * metrics.height * getPixelSize()];
 }
 }
 
 
 GlyphData::~GlyphData()
 GlyphData::~GlyphData()
@@ -61,22 +50,30 @@ GlyphData::~GlyphData()
 
 
 void *GlyphData::getData() const
 void *GlyphData::getData() const
 {
 {
-	return (void *) data;
+	return data;
 }
 }
 
 
-size_t GlyphData::getSize() const
+size_t GlyphData::getPixelSize() const
 {
 {
 	switch (format)
 	switch (format)
 	{
 	{
-	case GlyphData::FORMAT_LUMINANCE_ALPHA:
-		return size_t(getWidth() * getHeight() * 2);
-		break;
-	case GlyphData::FORMAT_RGBA:
+	case FORMAT_LUMINANCE_ALPHA:
+		return 2;
+	case FORMAT_RGBA:
 	default:
 	default:
-		return size_t(getWidth() * getHeight() * 4);
-		break;
+		return 4;
 	}
 	}
+}
 
 
+void *GlyphData::getData(int x, int y) const
+{
+	size_t offset = (y * getWidth() + x) * getPixelSize();
+	return data + offset;
+}
+
+size_t GlyphData::getSize() const
+{
+	return size_t(getWidth() * getHeight()) * getPixelSize();
 }
 }
 
 
 int GlyphData::getHeight() const
 int GlyphData::getHeight() const
@@ -133,22 +130,22 @@ int GlyphData::getBearingY() const
 
 
 int GlyphData::getMinX() const
 int GlyphData::getMinX() const
 {
 {
-	return this->getBearingX();
+	return getBearingX();
 }
 }
 
 
 int GlyphData::getMinY() const
 int GlyphData::getMinY() const
 {
 {
-	return this->getHeight() - this->getBearingY();
+	return getHeight() - getBearingY();
 }
 }
 
 
 int GlyphData::getMaxX() const
 int GlyphData::getMaxX() const
 {
 {
-	return this->getBearingX() + this->getWidth();
+	return getBearingX() + getWidth();
 }
 }
 
 
 int GlyphData::getMaxY() const
 int GlyphData::getMaxY() const
 {
 {
-	return this->getBearingY();
+	return getBearingY();
 }
 }
 
 
 GlyphData::Format GlyphData::getFormat() const
 GlyphData::Format GlyphData::getFormat() const
@@ -168,8 +165,8 @@ bool GlyphData::getConstant(GlyphData::Format in, const char *&out)
 
 
 StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM>::Entry GlyphData::formatEntries[] =
 StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM>::Entry GlyphData::formatEntries[] =
 {
 {
-	{"luminancealpha", GlyphData::FORMAT_LUMINANCE_ALPHA},
-	{"rgba", GlyphData::FORMAT_RGBA},
+	{"luminancealpha", FORMAT_LUMINANCE_ALPHA},
+	{"rgba", FORMAT_RGBA},
 };
 };
 
 
 StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM> GlyphData::formats(GlyphData::formatEntries, sizeof(GlyphData::formatEntries));
 StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM> GlyphData::formats(GlyphData::formatEntries, sizeof(GlyphData::formatEntries));

+ 11 - 1
src/modules/font/GlyphData.h

@@ -69,6 +69,16 @@ public:
 	void *getData() const;
 	void *getData() const;
 	size_t getSize() const;
 	size_t getSize() const;
 
 
+	/**
+	 * Gets the data starting at a specific pixel in the glyph.
+	 **/
+	void *getData(int x, int y) const;
+
+	/**
+	 * Gets the size in bytes of each pixel in the glyph.
+	 **/
+	size_t getPixelSize() const;
+
 	/**
 	/**
 	 * Gets the height of the glyph.
 	 * Gets the height of the glyph.
 	 **/
 	 **/
@@ -141,7 +151,7 @@ private:
 	GlyphMetrics metrics;
 	GlyphMetrics metrics;
 
 
 	// Glyph texture data.
 	// Glyph texture data.
-	unsigned char *data;
+	uint8 *data;
 
 
 	// The format the data's in.
 	// The format the data's in.
 	Format format;
 	Format format;