Cleaned up some duplicated pixel format-related code in Font files.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-12-29 02:45:27 -04:00
parent 4a3889ae48
commit 8aa6725039
15 changed files with 88 additions and 105 deletions
+8 -3
View File
@@ -291,17 +291,17 @@ GlyphData *BMFontRasterizer::getGlyphData(uint32 glyph) const
// Return an empty GlyphData if we don't have the glyph character.
if (it == characters.end())
return new GlyphData(glyph, GlyphMetrics(), GlyphData::FORMAT_RGBA);
return new GlyphData(glyph, GlyphMetrics(), PIXELFORMAT_RGBA8);
const BMFontCharacter &c = it->second;
GlyphData *g = new GlyphData(glyph, c.metrics, GlyphData::FORMAT_RGBA);
GlyphData *g = new GlyphData(glyph, c.metrics, PIXELFORMAT_RGBA8);
const auto &imagepair = images.find(c.page);
if (imagepair == images.end())
{
g->release();
return new GlyphData(glyph, GlyphMetrics(), GlyphData::FORMAT_RGBA);
return new GlyphData(glyph, GlyphMetrics(), PIXELFORMAT_RGBA8);
}
image::ImageData *imagedata = imagepair->second.get();
@@ -343,6 +343,11 @@ float BMFontRasterizer::getKerning(uint32 leftglyph, uint32 rightglyph) const
return 0.0f;
}
Rasterizer::DataType BMFontRasterizer::getDataType() const
{
return DATA_IMAGE;
}
bool BMFontRasterizer::accepts(love::filesystem::FileData *fontdef)
{
const char *data = (const char *) fontdef->getData();
+1
View File
@@ -51,6 +51,7 @@ public:
int getGlyphCount() const override;
bool hasGlyph(uint32 glyph) const override;
float getKerning(uint32 leftglyph, uint32 rightglyph) const override;
DataType getDataType() const override;
static bool accepts(love::filesystem::FileData *fontdef);
+6 -28
View File
@@ -35,12 +35,15 @@ namespace font
love::Type GlyphData::type("GlyphData", &Data::type);
GlyphData::GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, GlyphData::Format f)
GlyphData::GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, PixelFormat f)
: glyph(glyph)
, metrics(glyphMetrics)
, data(nullptr)
, format(f)
{
if (f != PIXELFORMAT_LA8 && f != PIXELFORMAT_RGBA8)
throw love::Exception("Invalid GlyphData pixel format.");
if (metrics.width > 0 && metrics.height > 0)
data = new uint8[metrics.width * metrics.height * getPixelSize()];
}
@@ -57,14 +60,7 @@ void *GlyphData::getData() const
size_t GlyphData::getPixelSize() const
{
switch (format)
{
case FORMAT_LUMINANCE_ALPHA:
return 2;
case FORMAT_RGBA:
default:
return 4;
}
return getPixelFormatSize(format);
}
void *GlyphData::getData(int x, int y) const
@@ -150,28 +146,10 @@ int GlyphData::getMaxY() const
return getBearingY();
}
GlyphData::Format GlyphData::getFormat() const
PixelFormat GlyphData::getFormat() const
{
return format;
}
bool GlyphData::getConstant(const char *in, GlyphData::Format &out)
{
return formats.find(in, out);
}
bool GlyphData::getConstant(GlyphData::Format in, const char *&out)
{
return formats.find(in, out);
}
StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM>::Entry GlyphData::formatEntries[] =
{
{"luminancealpha", FORMAT_LUMINANCE_ALPHA},
{"rgba", FORMAT_RGBA},
};
StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM> GlyphData::formats(GlyphData::formatEntries, sizeof(GlyphData::formatEntries));
} // font
} // love
+4 -16
View File
@@ -27,6 +27,7 @@
#include "common/Exception.h"
#include "common/StringMap.h"
#include "common/int.h"
#include "common/pixelformat.h"
// stdlib
#include <string>
@@ -57,14 +58,7 @@ public:
static love::Type type;
enum Format
{
FORMAT_LUMINANCE_ALPHA,
FORMAT_RGBA,
FORMAT_MAX_ENUM
};
GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, Format f);
GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, PixelFormat f);
virtual ~GlyphData();
// Implements Data.
@@ -139,10 +133,7 @@ public:
/**
* Gets the format of the glyph data.
**/
Format getFormat() const;
static bool getConstant(const char *in, Format &out);
static bool getConstant(Format in, const char *&out);
PixelFormat getFormat() const;
private:
@@ -156,10 +147,7 @@ private:
uint8 *data;
// The format the data's in.
Format format;
static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
static StringMap<Format, FORMAT_MAX_ENUM> formats;
PixelFormat format;
}; // GlyphData
+6 -1
View File
@@ -69,7 +69,7 @@ GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
gm.height = metrics.height;
GlyphData *g = new GlyphData(glyph, gm, GlyphData::FORMAT_RGBA);
GlyphData *g = new GlyphData(glyph, gm, PIXELFORMAT_RGBA8);
if (gm.width == 0)
return g;
@@ -149,5 +149,10 @@ bool ImageRasterizer::hasGlyph(uint32 glyph) const
return imageGlyphs.find(glyph) != imageGlyphs.end();
}
Rasterizer::DataType ImageRasterizer::getDataType() const
{
return DATA_IMAGE;
}
} // font
} // love
+6 -4
View File
@@ -43,10 +43,12 @@ public:
virtual ~ImageRasterizer();
// Implement Rasterizer
virtual int getLineHeight() const;
virtual GlyphData *getGlyphData(uint32 glyph) const;
virtual int getGlyphCount() const;
virtual bool hasGlyph(uint32 glyph) const;
int getLineHeight() const override;
GlyphData *getGlyphData(uint32 glyph) const override;
int getGlyphCount() const override;
bool hasGlyph(uint32 glyph) const override;
DataType getDataType() const override;
private:
+8
View File
@@ -49,6 +49,12 @@ class Rasterizer : public Object
{
public:
enum DataType
{
DATA_TRUETYPE,
DATA_IMAGE,
};
static love::Type type;
virtual ~Rasterizer();
@@ -112,6 +118,8 @@ public:
**/
virtual float getKerning(uint32 leftglyph, uint32 rightglyph) const;
virtual DataType getDataType() const = 0;
protected:
FontMetrics metrics;
@@ -108,7 +108,7 @@ GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const
glyphMetrics.width = bitmap.width;
glyphMetrics.advance = (int) (ftglyph->advance.x >> 16);
GlyphData *glyphData = new GlyphData(glyph, glyphMetrics, GlyphData::FORMAT_LUMINANCE_ALPHA);
GlyphData *glyphData = new GlyphData(glyph, glyphMetrics, PIXELFORMAT_LA8);
const uint8 *pixels = bitmap.buffer;
uint8 *dest = (uint8 *) glyphData->getData();
@@ -176,6 +176,11 @@ float TrueTypeRasterizer::getKerning(uint32 leftglyph, uint32 rightglyph) const
return float(kerning.x >> 6);
}
Rasterizer::DataType TrueTypeRasterizer::getDataType() const
{
return DATA_TRUETYPE;
}
bool TrueTypeRasterizer::accepts(FT_Library library, love::Data *data)
{
const FT_Byte *fbase = (const FT_Byte *) data->getData();
@@ -48,11 +48,12 @@ public:
virtual ~TrueTypeRasterizer();
// Implement Rasterizer
virtual int getLineHeight() const;
virtual GlyphData *getGlyphData(uint32 glyph) const;
virtual int getGlyphCount() const;
virtual bool hasGlyph(uint32 glyph) const;
virtual float getKerning(uint32 leftglyph, uint32 rightglyph) const;
int getLineHeight() const override;
GlyphData *getGlyphData(uint32 glyph) const override;
int getGlyphCount() const override;
bool hasGlyph(uint32 glyph) const override;
float getKerning(uint32 leftglyph, uint32 rightglyph) const override;
DataType getDataType() const override;
static bool accepts(FT_Library library, love::Data *data);
+1 -1
View File
@@ -108,7 +108,7 @@ int w_GlyphData_getFormat(lua_State *L)
GlyphData *t = luax_checkglyphdata(L, 1);
const char *str;
if (!GlyphData::getConstant(t->getFormat(), str))
if (!getConstant(t->getFormat(), str))
return luaL_error(L, "unknown GlyphData format.");
lua_pushstring(L, str);
+16 -36
View File
@@ -78,7 +78,7 @@ Font::Font(love::font::Rasterizer *r, const Texture::Filter &filter)
}
love::font::GlyphData *gd = r->getGlyphData(32); // Space character.
fontType = (gd->getFormat() == font::GlyphData::FORMAT_LUMINANCE_ALPHA) ? FONT_TRUETYPE : FONT_IMAGE;
pixelFormat = gd->getFormat();
gd->release();
if (!r->hasGlyph(9)) // No tab character in the Rasterizer.
@@ -114,31 +114,6 @@ Font::TextureSize Font::getNextTextureSize() const
return size;
}
GLenum Font::getTextureFormat(FontType fontType, GLenum *internalformat) const
{
GLenum format = fontType == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA;
GLenum iformat = fontType == FONT_TRUETYPE ? GL_LUMINANCE8_ALPHA8 : GL_RGBA8;
if (format == GL_RGBA && isGammaCorrect())
{
// In ES2, the internalformat and format params of TexImage must match.
// ES3 doesn't allow "GL_SRGB_ALPHA" as the internal format. But it also
// requires GL_LUMINANCE_ALPHA rather than GL_LUMINANCE8_ALPHA8 as the
// internal format, for LA.
if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0)
format = iformat = GL_SRGB_ALPHA;
else
iformat = GL_SRGB8_ALPHA8;
}
else if (GLAD_ES_VERSION_2_0)
iformat = format;
if (internalformat != nullptr)
*internalformat = iformat;
return format;
}
void Font::createTexture()
{
auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
@@ -146,7 +121,7 @@ void Font::createTexture()
OpenGL::TempDebugGroup debuggroup("Font create texture");
size_t bpp = fontType == FONT_TRUETYPE ? 2 : 4;
size_t bpp = getPixelFormatSize(pixelFormat);
size_t prevmemsize = textureMemorySize;
if (prevmemsize > 0)
@@ -180,8 +155,8 @@ void Font::createTexture()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLenum internalformat = GL_RGBA;
GLenum format = getTextureFormat(fontType, &internalformat);
bool sRGB = isGammaCorrect();
OpenGL::TextureFormat fmt = gl.convertPixelFormat(pixelFormat, false, sRGB);
// Initialize the texture with transparent black.
std::vector<GLubyte> emptydata(size.width * size.height * bpp, 0);
@@ -189,8 +164,8 @@ void Font::createTexture()
// Clear errors before initializing.
while (glGetError() != GL_NO_ERROR);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, size.width, size.height, 0,
format, GL_UNSIGNED_BYTE, &emptydata[0]);
glTexImage2D(GL_TEXTURE_2D, 0, fmt.internalformat, size.width, size.height,
0, fmt.externalformat, fmt.type, &emptydata[0]);
if (glGetError() != GL_NO_ERROR)
{
@@ -233,7 +208,7 @@ love::font::GlyphData *Font::getRasterizerGlyphData(uint32 glyph)
if (glyph == 9 && useSpacesAsTab)
{
love::font::GlyphData *spacegd = rasterizers[0]->getGlyphData(32);
love::font::GlyphData::Format fmt = spacegd->getFormat();
PixelFormat fmt = spacegd->getFormat();
love::font::GlyphMetrics gm = {};
gm.advance = spacegd->getAdvance() * SPACES_PER_TAB;
@@ -289,12 +264,14 @@ const Font::Glyph &Font::addGlyph(uint32 glyph)
// don't waste space for empty glyphs. also fixes a divide by zero bug with ATI drivers
if (w > 0 && h > 0)
{
GLenum format = getTextureFormat(fontType);
bool isSRGB = isGammaCorrect();
OpenGL::TextureFormat fmt = gl.convertPixelFormat(pixelFormat, false, isSRGB);
g.texture = textures.back();
gl.bindTextureToUnit(g.texture, 0, false);
glTexSubImage2D(GL_TEXTURE_2D, 0, textureX, textureY, w, h,
format, GL_UNSIGNED_BYTE, gd->getData());
fmt.externalformat, fmt.type, gd->getData());
double tX = (double) textureX, tY = (double) textureY;
double tWidth = (double) textureWidth, tHeight = (double) textureHeight;
@@ -979,7 +956,10 @@ int Font::getDescent() const
float Font::getBaseline() const
{
// 1.25 is magic line height for true type fonts
return (fontType == FONT_TRUETYPE) ? floorf(getHeight() / 1.25f + 0.5f) : 0.0f;
if (rasterizers[0]->getDataType() == font::Rasterizer::DATA_TRUETYPE)
return floorf(getHeight() / 1.25f + 0.5f);
else
return 0.0f;
}
bool Font::hasGlyph(uint32 glyph) const
@@ -1023,7 +1003,7 @@ void Font::setFallbacks(const std::vector<Font *> &fallbacks)
{
for (const Font *f : fallbacks)
{
if (f->fontType != this->fontType)
if (f->rasterizers[0]->getDataType() != this->rasterizers[0]->getDataType())
throw love::Exception("Font fallbacks must be of the same font type.");
}
+2 -9
View File
@@ -188,13 +188,6 @@ public:
private:
enum FontType
{
FONT_TRUETYPE,
FONT_IMAGE,
FONT_UNKNOWN
};
struct Glyph
{
GLuint texture;
@@ -209,7 +202,6 @@ private:
};
TextureSize getNextTextureSize() const;
GLenum getTextureFormat(FontType fontType, GLenum *internalformat = nullptr) const;
void createTexture();
love::font::GlyphData *getRasterizerGlyphData(uint32 glyph);
const Glyph &addGlyph(uint32 glyph);
@@ -234,7 +226,8 @@ private:
// map of left/right glyph pairs to horizontal kerning.
std::unordered_map<uint64, float> kerning;
FontType fontType;
PixelFormat pixelFormat;
Texture::Filter filter;
int textureX, textureY;
+13 -1
View File
@@ -812,6 +812,12 @@ OpenGL::TextureFormat OpenGL::convertPixelFormat(PixelFormat pixelformat, bool r
f.type = GL_FLOAT;
break;
case PIXELFORMAT_LA8:
f.internalformat = GL_LUMINANCE8_ALPHA8;
f.externalformat = GL_LUMINANCE_ALPHA;
f.type = GL_UNSIGNED_BYTE;
break;
case PIXELFORMAT_RGBA4:
f.internalformat = GL_RGBA4;
f.externalformat = GL_RGBA;
@@ -965,8 +971,11 @@ OpenGL::TextureFormat OpenGL::convertPixelFormat(PixelFormat pixelformat, bool r
if (!isPixelFormatCompressed(pixelformat))
{
if (GLAD_ES_VERSION_2_0 && !GLAD_ES_VERSION_3_0 && !renderbuffer)
if (GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 && pixelformat != PIXELFORMAT_LA8)
&& !renderbuffer)
{
f.internalformat = f.externalformat;
}
if (pixelformat != PIXELFORMAT_sRGBA8)
isSRGB = false;
@@ -1048,6 +1057,9 @@ bool OpenGL::isPixelFormatSupported(PixelFormat pixelformat, bool rendertarget,
else
return false;
case PIXELFORMAT_LA8:
return !rendertarget;
case PIXELFORMAT_RGBA4:
case PIXELFORMAT_RGB5A1:
return true;