Parcourir la source

ImageFonts now replace the spacer color with transparency when generating a glyph instead of when first parsing ImageData (source ImageData used to generate an ImageFont is now unmolested.)

Alex Szpakowski il y a 12 ans
Parent
commit
76b0537eff
2 fichiers modifiés avec 28 ajouts et 34 suppressions
  1. 23 30
      src/modules/font/ImageRasterizer.cpp
  2. 5 4
      src/modules/font/ImageRasterizer.h

+ 23 - 30
src/modules/font/ImageRasterizer.cpp

@@ -59,8 +59,8 @@ GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const
 	memset(&gm, 0, sizeof(GlyphMetrics));
 
 	// Set relevant glyph metrics if the glyph is in this ImageFont
-	std::map<unsigned int, ImageGlyphData>::const_iterator it = imageGlyphData.find(glyph);
-	if (it != imageGlyphData.end())
+	std::map<unsigned int, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
+	if (it != imageGlyphs.end())
 	{
 		gm.width = it->second.width;
 		gm.advance = it->second.width + it->second.spacing;
@@ -76,14 +76,21 @@ GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const
 	unsigned char *gd = (unsigned char *) g->getData();
 	love::image::pixel *pixels = (love::image::pixel *) imageData->getData();
 
-	// copy glyph from imagedata to glyphdata
+	// copy glyph pixels from imagedata to glyphdata
 	for (unsigned int i = 0; i < gm.width * (unsigned int) getHeight(); i++)
 	{
 		love::image::pixel p = pixels[ it->second.x + (i % gm.width) + (imageData->getWidth() * (i / gm.width)) ];
-		gd[i*4+0] = p.r;
-		gd[i*4+1] = p.g;
-		gd[i*4+2] = p.b;
-		gd[i*4+3] = p.a;
+
+		// Replace spacer color with an empty pixel
+		if (equal(p, spacer))
+			gd[i*4+0] = gd[i*4+1] = gd[i*4+2] = gd[i*4+3] = 0;
+		else
+		{
+			gd[i*4+0] = p.r;
+			gd[i*4+1] = p.g;
+			gd[i*4+2] = p.b;
+			gd[i*4+3] = p.a;
+		}
 	}
 	
 	return g;
@@ -91,17 +98,16 @@ GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const
 
 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();
 	unsigned int imgh = (unsigned int) imageData->getHeight();
-	unsigned int imgs = imgw*imgh;
 
 	// Set the only metric that matters
 	metrics.height = imgh;
 
 	// Reading texture data begins
-	love::image::pixel spacer = pixels[0];
+	spacer = pixels[0];
 
 	unsigned int start = 0;
 	unsigned int end = 0;
@@ -115,8 +121,8 @@ void ImageRasterizer::load()
 			++start;
 
 		// set previous glyph's spacing
-		if (i > 0 && imageGlyphData.size() > 0)
-			imageGlyphData[glyphs[i - 1]].spacing = (start > end) ? (start - end) : 0;
+		if (i > 0 && imageGlyphs.size() > 0)
+			imageGlyphs[glyphs[i - 1]].spacing = (start > end) ? (start - end) : 0;
 
 		end = start;
 
@@ -127,12 +133,11 @@ void ImageRasterizer::load()
 		if (start >= end)
 			break;
 
-		ImageGlyphData igd;
-		igd.x = start;
-		igd.y = 0; // todo?
-		igd.width = end - start;
+		ImageGlyphData imageGlyph;
+		imageGlyph.x = start;
+		imageGlyph.width = end - start;
 
-		imageGlyphData[glyphs[i]] = igd;
+		imageGlyphs[glyphs[i]] = imageGlyph;
 	}
 
 	// Find spacing of last glyph
@@ -142,19 +147,7 @@ void ImageRasterizer::load()
 		while (start < imgw && equal(pixels[start], spacer))
 			++start;
 
-		imageGlyphData[glyphs[numglyphs - 1]].spacing = (start > end) ? (start - end) : 0;
-	}
-
-	// Replace spacer color with an empty pixel
-	for (unsigned int i = 0; i < imgs; ++i)
-	{
-		if (equal(pixels[i], spacer))
-		{
-			pixels[i].r = 0;
-			pixels[i].g = 0;
-			pixels[i].b = 0;
-			pixels[i].a = 0;
-		}
+		imageGlyphs[glyphs[numglyphs - 1]].spacing = (start > end) ? (start - end) : 0;
 	}
 }
 

+ 5 - 4
src/modules/font/ImageRasterizer.h

@@ -47,8 +47,6 @@ public:
 	virtual GlyphData *getGlyphData(unsigned int glyph) const;
 	virtual int getNumGlyphs() const;
 
-	static const unsigned int MAX_CHARS = 256;
-
 private:
 	// Load all the glyph positions into memory
 	void load();
@@ -65,12 +63,15 @@ private:
 	// Information about a glyph in the ImageData
 	struct ImageGlyphData
 	{
-		unsigned int x, y;
+		unsigned int x;
 		unsigned int width;
 		unsigned int spacing;
 	};
 
-	std::map<unsigned int, ImageGlyphData> imageGlyphData;
+	std::map<unsigned int, ImageGlyphData> imageGlyphs;
+
+	// Color used to identify glyph separation in the source ImageData
+	love::image::pixel spacer;
 
 }; // ImageRasterizer