From 76b0537eff90785f1cc822a98daf2ac1cd8c58b1 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 23 Jan 2013 06:00:28 -0400 Subject: [PATCH] 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.) --- src/modules/font/ImageRasterizer.cpp | 53 ++++++++++++---------------- src/modules/font/ImageRasterizer.h | 9 ++--- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/modules/font/ImageRasterizer.cpp b/src/modules/font/ImageRasterizer.cpp index 2cce08d3f..1de515bc7 100644 --- a/src/modules/font/ImageRasterizer.cpp +++ b/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::const_iterator it = imageGlyphData.find(glyph); - if (it != imageGlyphData.end()) + std::map::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; } } diff --git a/src/modules/font/ImageRasterizer.h b/src/modules/font/ImageRasterizer.h index 1623a14f7..89ebeeb2f 100644 --- a/src/modules/font/ImageRasterizer.h +++ b/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 imageGlyphData; + std::map imageGlyphs; + + // Color used to identify glyph separation in the source ImageData + love::image::pixel spacer; }; // ImageRasterizer