Commits

Alex Szpakowski  committed 6ce3cd1

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.)

  • Participants
  • Parent commits 33cac91

Comments (0)

Files changed (2)

File src/modules/font/ImageRasterizer.cpp

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

File src/modules/font/ImageRasterizer.h

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