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

This commit is contained in:
Alex Szpakowski
2013-01-23 06:00:28 -04:00
parent 6247e7b60b
commit 76b0537eff
2 changed files with 28 additions and 34 deletions
+23 -30
View File
@@ -59,8 +59,8 @@ GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const
memset(&gm, 0, sizeof(GlyphMetrics)); memset(&gm, 0, sizeof(GlyphMetrics));
// Set relevant glyph metrics if the glyph is in this ImageFont // Set relevant glyph metrics if the glyph is in this ImageFont
std::map<unsigned int, ImageGlyphData>::const_iterator it = imageGlyphData.find(glyph); std::map<unsigned int, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
if (it != imageGlyphData.end()) if (it != imageGlyphs.end())
{ {
gm.width = it->second.width; gm.width = it->second.width;
gm.advance = it->second.width + it->second.spacing; 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(); unsigned char *gd = (unsigned char *) g->getData();
love::image::pixel *pixels = (love::image::pixel *) imageData->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++) 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)) ]; 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; // Replace spacer color with an empty pixel
gd[i*4+2] = p.b; if (equal(p, spacer))
gd[i*4+3] = p.a; 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; return g;
@@ -91,17 +98,16 @@ GlyphData *ImageRasterizer::getGlyphData(unsigned int glyph) const
void ImageRasterizer::load() 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 imgw = (unsigned int) imageData->getWidth();
unsigned int imgh = (unsigned int) imageData->getHeight(); unsigned int imgh = (unsigned int) imageData->getHeight();
unsigned int imgs = imgw*imgh;
// Set the only metric that matters // Set the only metric that matters
metrics.height = imgh; metrics.height = imgh;
// Reading texture data begins // Reading texture data begins
love::image::pixel spacer = pixels[0]; spacer = pixels[0];
unsigned int start = 0; unsigned int start = 0;
unsigned int end = 0; unsigned int end = 0;
@@ -115,8 +121,8 @@ void ImageRasterizer::load()
++start; ++start;
// set previous glyph's spacing // set previous glyph's spacing
if (i > 0 && imageGlyphData.size() > 0) if (i > 0 && imageGlyphs.size() > 0)
imageGlyphData[glyphs[i - 1]].spacing = (start > end) ? (start - end) : 0; imageGlyphs[glyphs[i - 1]].spacing = (start > end) ? (start - end) : 0;
end = start; end = start;
@@ -127,12 +133,11 @@ void ImageRasterizer::load()
if (start >= end) if (start >= end)
break; break;
ImageGlyphData igd; ImageGlyphData imageGlyph;
igd.x = start; imageGlyph.x = start;
igd.y = 0; // todo? imageGlyph.width = end - start;
igd.width = end - start;
imageGlyphData[glyphs[i]] = igd; imageGlyphs[glyphs[i]] = imageGlyph;
} }
// Find spacing of last glyph // Find spacing of last glyph
@@ -142,19 +147,7 @@ void ImageRasterizer::load()
while (start < imgw && equal(pixels[start], spacer)) while (start < imgw && equal(pixels[start], spacer))
++start; ++start;
imageGlyphData[glyphs[numglyphs - 1]].spacing = (start > end) ? (start - end) : 0; imageGlyphs[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;
}
} }
} }
+5 -4
View File
@@ -47,8 +47,6 @@ public:
virtual GlyphData *getGlyphData(unsigned int glyph) const; virtual GlyphData *getGlyphData(unsigned int glyph) const;
virtual int getNumGlyphs() const; virtual int getNumGlyphs() const;
static const unsigned int MAX_CHARS = 256;
private: private:
// Load all the glyph positions into memory // Load all the glyph positions into memory
void load(); void load();
@@ -65,12 +63,15 @@ private:
// Information about a glyph in the ImageData // Information about a glyph in the ImageData
struct ImageGlyphData struct ImageGlyphData
{ {
unsigned int x, y; unsigned int x;
unsigned int width; unsigned int width;
unsigned int spacing; 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 }; // ImageRasterizer