imported Löve GLES branch (changeset 1ba9037e558b)

This commit is contained in:
Martin Felis
2013-12-05 18:05:13 +01:00
parent 41b2db04a7
commit 2644d1ee18
615 changed files with 150300 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_FONT_H
#define LOVE_FONT_FONT_H
// LOVE
#include "Rasterizer.h"
#include "image/ImageData.h"
#include "common/Module.h"
#include "common/int.h"
// STD
#include <string>
namespace love
{
namespace font
{
class Font : public Module
{
public:
virtual Rasterizer *newRasterizer(Data *data, int size) = 0;
virtual Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
virtual Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph) = 0;
// Implement Module
virtual const char *getName() const = 0;
}; // Font
} // font
} // love
#endif // LOVE_FONT_FONT_H
+178
View File
@@ -0,0 +1,178 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "GlyphData.h"
// UTF-8
#include "libraries/utf8/utf8.h"
// stdlib
#include <iostream>
#include <cstddef>
namespace love
{
namespace font
{
GlyphData::GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, GlyphData::Format f)
: glyph(glyph)
, metrics(glyphMetrics)
, data(0)
, format(f)
{
if (metrics.width > 0 && metrics.height > 0)
{
switch (f)
{
case GlyphData::FORMAT_LUMINANCE_ALPHA:
data = new unsigned char[metrics.width * metrics.height * 2];
break;
case GlyphData::FORMAT_RGBA:
default:
data = new unsigned char[metrics.width * metrics.height * 4];
break;
}
}
}
GlyphData::~GlyphData()
{
delete[] data;
}
void *GlyphData::getData() const
{
return (void *) data;
}
int GlyphData::getSize() const
{
switch (format)
{
case GlyphData::FORMAT_LUMINANCE_ALPHA:
return getWidth() * getHeight() * 2;
break;
case GlyphData::FORMAT_RGBA:
default:
return getWidth() * getHeight() * 4;
break;
}
}
int GlyphData::getHeight() const
{
return metrics.height;
}
int GlyphData::getWidth() const
{
return metrics.width;
}
uint32 GlyphData::getGlyph() const
{
return glyph;
}
std::string GlyphData::getGlyphString() const
{
char u[5] = {0, 0, 0, 0, 0};
ptrdiff_t length = 0;
try
{
char *end = utf8::append(glyph, u);
length = end - u;
}
catch (utf8::exception &e)
{
throw love::Exception("Decoding error: %s", e.what());
}
// Just in case...
if (length < 0)
return "";
return std::string(u, length);
}
int GlyphData::getAdvance() const
{
return metrics.advance;
}
int GlyphData::getBearingX() const
{
return metrics.bearingX;
}
int GlyphData::getBearingY() const
{
return metrics.bearingY;
}
int GlyphData::getMinX() const
{
return this->getBearingX();
}
int GlyphData::getMinY() const
{
return this->getHeight() - this->getBearingY();
}
int GlyphData::getMaxX() const
{
return this->getBearingX() + this->getWidth();
}
int GlyphData::getMaxY() const
{
return this->getBearingY();
}
GlyphData::Format 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[] =
{
{"luminance alpha", GlyphData::FORMAT_LUMINANCE_ALPHA},
{"rgba", GlyphData::FORMAT_RGBA},
};
StringMap<GlyphData::Format, GlyphData::FORMAT_MAX_ENUM> GlyphData::formats(GlyphData::formatEntries, sizeof(GlyphData::formatEntries));
} // font
} // love
+158
View File
@@ -0,0 +1,158 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_GLYPH_DATA_H
#define LOVE_FONT_GLYPH_DATA_H
// LOVE
#include "common/config.h"
#include "common/Data.h"
#include "common/Exception.h"
#include "common/StringMap.h"
#include "common/int.h"
// stdlib
#include <string>
namespace love
{
namespace font
{
/**
* Holds the specific glyph data.
**/
struct GlyphMetrics
{
int height;
int width;
int advance;
int bearingX;
int bearingY;
int spacing;
};
/**
* Holds data for a specic glyph object.
**/
class GlyphData : public Data
{
public:
enum Format
{
FORMAT_LUMINANCE_ALPHA,
FORMAT_RGBA,
FORMAT_MAX_ENUM
};
GlyphData(uint32 glyph, GlyphMetrics glyphMetrics, Format f);
virtual ~GlyphData();
// Implements Data.
void *getData() const;
int getSize() const;
/**
* Gets the height of the glyph.
**/
virtual int getHeight() const;
/**
* Gets the width of the glyph.
**/
virtual int getWidth() const;
/**
* Gets the glyph codepoint itself.
**/
uint32 getGlyph() const;
/**
* Gets the glyph as a UTF-8 string (instead of a UTF-8 code point.)
**/
std::string getGlyphString() const;
/**
* Gets the advance (the space the glyph takes up) of the glyph.
**/
int getAdvance() const;
/**
* Gets bearing (the spacing from origin) along the x-axis of the glyph.
**/
int getBearingX() const;
/**
* Gets bearing (the spacing from origin) along the y-axis of the glyph.
**/
int getBearingY() const;
/**
* Gets the min x value of the glyph.
**/
int getMinX() const;
/**
* Gets the min y value of the glyph.
**/
int getMinY() const;
/**
* Gets the max x value of the glyph.
**/
int getMaxX() const;
/**
* Gets the max y value of the glyph.
**/
int getMaxY() const;
/**
* 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);
private:
// The glyph codepoint itself.
uint32 glyph;
// Glyph metrics.
GlyphMetrics metrics;
// Glyph texture data.
unsigned char *data;
// The format the data's in.
Format format;
static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
static StringMap<Format, FORMAT_MAX_ENUM> formats;
}; // GlyphData
} // font
} // love
#endif // LOVE_FONT_GLYPH_DATA_H
@@ -0,0 +1,166 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "ImageRasterizer.h"
#include "common/Exception.h"
#include <string.h>
namespace love
{
namespace font
{
inline bool equal(const love::image::pixel &a, const love::image::pixel &b)
{
return (a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
}
ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
: imageData(data)
, glyphs(glyphs)
, numglyphs(numglyphs)
{
imageData->retain();
load();
}
ImageRasterizer::~ImageRasterizer()
{
imageData->release();
}
int ImageRasterizer::getLineHeight() const
{
return getHeight();
}
GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
{
GlyphMetrics gm;
memset(&gm, 0, sizeof(GlyphMetrics));
// Set relevant glyph metrics if the glyph is in this ImageFont
std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
if (it != imageGlyphs.end())
{
gm.width = it->second.width;
gm.advance = it->second.width + it->second.spacing;
}
gm.height = metrics.height;
GlyphData *g = new GlyphData(glyph, gm, GlyphData::FORMAT_RGBA);
if (gm.width == 0)
return g;
// We don't want another thread modifying our ImageData mid-copy.
love::thread::Lock lock(imageData->getMutex());
love::image::pixel *gdpixels = (love::image::pixel *) g->getData();
love::image::pixel *imagepixels = (love::image::pixel *) imageData->getData();
// copy glyph pixels from imagedata to glyphdata
for (int i = 0; i < g->getWidth() * g->getHeight(); i++)
{
love::image::pixel p = imagepixels[it->second.x + (i % gm.width) + (imageData->getWidth() * (i / gm.width))];
// Use transparency instead of the spacer color
if (equal(p, spacer))
gdpixels[i].r = gdpixels[i].g = gdpixels[i].b = gdpixels[i].a = 0;
else
gdpixels[i] = p;
}
return g;
}
void ImageRasterizer::load()
{
love::image::pixel *pixels = (love::image::pixel *) imageData->getData();
int imgw = imageData->getWidth();
int imgh = imageData->getHeight();
// We don't want another thread modifying our ImageData mid-parse.
love::thread::Lock lock(imageData->getMutex());
// Set the only metric that matters
metrics.height = imgh;
// Reading texture data begins
spacer = pixels[0];
int start = 0;
int end = 0;
for (int i = 0; i < numglyphs; ++i)
{
start = end;
// Finds out where the first character starts
while (start < imgw && equal(pixels[start], spacer))
++start;
// set previous glyph's spacing
if (i > 0 && imageGlyphs.size() > 0)
imageGlyphs[glyphs[i - 1]].spacing = (start > end) ? (start - end) : 0;
end = start;
// Find where glyph ends.
while (end < imgw && !equal(pixels[end], spacer))
++end;
if (start >= end)
break;
ImageGlyphData imageGlyph;
imageGlyph.x = start;
imageGlyph.width = end - start;
imageGlyphs[glyphs[i]] = imageGlyph;
}
// Find spacing of last glyph
if (numglyphs > 0)
{
start = end;
while (start < imgw && equal(pixels[start], spacer))
++start;
imageGlyphs[glyphs[numglyphs - 1]].spacing = (start > end) ? (start - end) : 0;
}
}
int ImageRasterizer::getGlyphCount() const
{
return numglyphs;
}
bool ImageRasterizer::hasGlyph(uint32 glyph) const
{
return imageGlyphs.find(glyph) != imageGlyphs.end();
}
} // font
} // love
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_IMAGE_RASTERIZER_H
#define LOVE_FONT_IMAGE_RASTERIZER_H
// LOVE
#include "filesystem/File.h"
#include "font/Rasterizer.h"
#include "image/ImageData.h"
#include <map>
namespace love
{
namespace font
{
/**
* Holds data for a font object.
**/
class ImageRasterizer : public Rasterizer
{
public:
ImageRasterizer(love::image::ImageData *imageData, uint32 *glyphs, int numglyphs);
virtual ~ImageRasterizer();
// Implement Rasterizer
virtual int getLineHeight() const;
virtual GlyphData *getGlyphData(uint32 glyph) const;
virtual int getGlyphCount() const;
virtual bool hasGlyph(uint32 glyph) const;
private:
// Load all the glyph positions into memory
void load();
// The image data
love::image::ImageData *imageData;
// The glyphs in the font
uint32 *glyphs;
// Number of glyphs in the font
int numglyphs;
// Information about a glyph in the ImageData
struct ImageGlyphData
{
int x;
int width;
int spacing;
};
std::map<uint32, ImageGlyphData> imageGlyphs;
// Color used to identify glyph separation in the source ImageData
love::image::pixel spacer;
}; // ImageRasterizer
} // font
} // love
#endif // LOVE_FONT_IMAGE_RASTERIZER_H
+99
View File
@@ -0,0 +1,99 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "Rasterizer.h"
// UTF-8
#include "libraries/utf8/utf8.h"
namespace love
{
namespace font
{
Rasterizer::~Rasterizer()
{
}
int Rasterizer::getHeight() const
{
return metrics.height;
}
int Rasterizer::getAdvance() const
{
return metrics.advance;
}
int Rasterizer::getAscent() const
{
return metrics.ascent;
}
int Rasterizer::getDescent() const
{
return metrics.descent;
}
GlyphData *Rasterizer::getGlyphData(const std::string &text) const
{
uint32 codepoint = 0;
try
{
codepoint = utf8::peek_next(text.begin(), text.end());
}
catch (utf8::exception &e)
{
throw love::Exception("Decoding error: %s", e.what());
}
return getGlyphData(codepoint);
}
bool Rasterizer::hasGlyphs(const std::string &text) const
{
if (text.size() == 0)
return false;
try
{
utf8::iterator<std::string::const_iterator> i(text.begin(), text.begin(), text.end());
utf8::iterator<std::string::const_iterator> end(text.end(), text.begin(), text.end());
while (i != end)
{
uint32 codepoint = *i++;
if (!hasGlyph(codepoint))
return false;
}
}
catch (utf8::exception &e)
{
throw love::Exception("Decoding error: %s", e.what());
}
return true;
}
} // font
} // love
+117
View File
@@ -0,0 +1,117 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_RASTERIZER_H
#define LOVE_FONT_RASTERIZER_H
// LOVE
#include "common/Object.h"
#include "common/int.h"
#include "GlyphData.h"
namespace love
{
namespace font
{
/**
* Holds the specific font metrics.
**/
struct FontMetrics
{
int advance;
int ascent;
int descent;
int height;
};
/**
* Holds data for a font object.
**/
class Rasterizer : public Object
{
public:
virtual ~Rasterizer();
/**
* Gets the max height of the glyphs.
**/
virtual int getHeight() const;
/**
* Gets the max advance of the glyphs.
**/
virtual int getAdvance() const;
/**
* Gets the max ascent (height above baseline) for the font.
**/
virtual int getAscent() const;
/**
* Gets the max descent (height below baseline) for the font.
**/
virtual int getDescent() const;
/**
* Gets the line height of the font.
**/
virtual int getLineHeight() const = 0;
/**
* Gets a specific glyph.
* @param glyph The (UNICODE) glyph codepoint to get data for.
**/
virtual GlyphData *getGlyphData(uint32 glyph) const = 0;
/**
* Gets a specific glyph.
* @param text The (UNICODE) glyph character to get the data for.
**/
virtual GlyphData *getGlyphData(const std::string &text) const;
/**
* Gets the number of glyphs the rasterizer has data for.
**/
virtual int getGlyphCount() const = 0;
/**
* Gets whether this Rasterizer has a specific glyph.
* @param glyph The (UNICODE) glyph codepoint.
**/
virtual bool hasGlyph(uint32 glyph) const = 0;
/**
* Gets whether this Rasterizer has all the glyphs in a string.
* @param text The (UTF-8) string.
**/
virtual bool hasGlyphs(const std::string &text) const;
protected:
FontMetrics metrics;
}; // Rasterizer
} // font
} // love
#endif // LOVE_FONT_RASTERIZER_H
+111
View File
@@ -0,0 +1,111 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Font.h"
#include "TrueTypeRasterizer.h"
#include "font/ImageRasterizer.h"
#include "libraries/utf8/utf8.h"
namespace love
{
namespace font
{
namespace freetype
{
Font::Font()
{
if (FT_Init_FreeType(&library))
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed\n");
}
Font::~Font()
{
FT_Done_FreeType(library);
}
Rasterizer *Font::newRasterizer(Data *data, int size)
{
return new TrueTypeRasterizer(library, data, size);
}
Rasterizer *Font::newRasterizer(love::image::ImageData *data, const std::string &text)
{
size_t strlen = text.size();
size_t numglyphs = 0;
uint32 *glyphs = new uint32[strlen];
try
{
utf8::iterator<std::string::const_iterator> i(text.begin(), text.begin(), text.end());
utf8::iterator<std::string::const_iterator> end(text.end(), text.begin(), text.end());
while (i != end)
glyphs[numglyphs++] = *i++;
}
catch (utf8::exception &e)
{
delete [] glyphs;
throw love::Exception("Decoding error: %s", e.what());
}
Rasterizer *r = newRasterizer(data, glyphs, numglyphs);
delete [] glyphs;
return r;
}
Rasterizer *Font::newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
{
return new ImageRasterizer(data, glyphs, numglyphs);
}
GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text)
{
uint32 codepoint = 0;
try
{
codepoint = utf8::peek_next(text.begin(), text.end());
}
catch (utf8::exception &e)
{
throw love::Exception("Decoding error: %s", e.what());
}
return r->getGlyphData(codepoint);
}
GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph)
{
return r->getGlyphData(glyph);
}
const char *Font::getName() const
{
return "love.font.freetype";
}
} // freetype
} // font
} // love
+76
View File
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_FREETYPE_FONT_H
#define LOVE_FONT_FREETYPE_FONT_H
// LOVE
#include "font/Font.h"
// FreeType2
#ifdef LOVE_MACOSX
#include <freetype/ft2build.h>
#else
#include <ft2build.h>
#endif
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>
#include <freetype/ftoutln.h>
#include <freetype/fttrigon.h>
namespace love
{
namespace font
{
namespace freetype
{
class Font : public love::font::Font
{
public:
Font();
/**
* Destructor.
**/
virtual ~Font();
// Implements Font
Rasterizer *newRasterizer(Data *data, int size);
Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &text);
Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs);
GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph);
GlyphData *newGlyphData(Rasterizer *r, uint32 glyph);
// Implement Module
const char *getName() const;
private:
// FreeType library
FT_Library library;
}; // Font
} // freetype
} // font
} // love
#endif // LOVE_FONT_FREETYPE_FONT_H
@@ -0,0 +1,121 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "TrueTypeRasterizer.h"
#include "common/Exception.h"
namespace love
{
namespace font
{
namespace freetype
{
TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, Data *data, int size)
: data(data)
{
if (FT_New_Memory_Face(library,
(const FT_Byte *)data->getData(), /* first byte in memory */
data->getSize(), /* size in bytes */
0, /* face_index */
&face))
throw love::Exception("TrueTypeFont Loading error: FT_New_Face failed (there is probably a problem with your font file)\n");
FT_Set_Pixel_Sizes(face, size, size);
// Set global metrics
FT_Size_Metrics s = face->size->metrics;
metrics.advance = s.max_advance >> 6;
metrics.ascent = s.ascender >> 6;
metrics.descent = s.descender >> 6;
metrics.height = s.height >> 6;
data->retain();
}
TrueTypeRasterizer::~TrueTypeRasterizer()
{
FT_Done_Face(face);
data->release();
}
int TrueTypeRasterizer::getLineHeight() const
{
return (int)(getHeight() * 1.25);
}
GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const
{
love::font::GlyphMetrics glyphMetrics = {};
FT_Glyph ftglyph;
// Initialize
if (FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_DEFAULT))
throw love::Exception("TrueTypeFont Loading vm->error: FT_Load_Glyph failed\n");
if (FT_Get_Glyph(face->glyph, &ftglyph))
throw love::Exception("TrueTypeFont Loading vm->error: FT_Get_Glyph failed\n");
FT_Glyph_To_Bitmap(&ftglyph, FT_RENDER_MODE_NORMAL, 0, 1);
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) ftglyph;
FT_Bitmap &bitmap = bitmap_glyph->bitmap; //just to make things easier
// Get metrics
glyphMetrics.bearingX = face->glyph->metrics.horiBearingX >> 6;
glyphMetrics.bearingY = face->glyph->metrics.horiBearingY >> 6;
glyphMetrics.height = bitmap.rows;
glyphMetrics.width = bitmap.width;
glyphMetrics.advance = face->glyph->metrics.horiAdvance >> 6;
GlyphData *glyphData = new GlyphData(glyph, glyphMetrics, GlyphData::FORMAT_LUMINANCE_ALPHA);
int size = bitmap.rows * bitmap.width;
unsigned char *dst = (unsigned char *) glyphData->getData();
// Note that bitmap.buffer contains only luminosity. We copy that single
// value to our luminosity-alpha format.
for (int i = 0; i < size; i++)
{
dst[2*i] = 255;
dst[2*i+1] = bitmap.buffer[i];
}
// Having copied the data over, we can destroy the glyph
FT_Done_Glyph(ftglyph);
// Return data
return glyphData;
}
int TrueTypeRasterizer::getGlyphCount() const
{
return face->num_glyphs;
}
bool TrueTypeRasterizer::hasGlyph(uint32 glyph) const
{
return FT_Get_Char_Index(face, glyph) != 0;
}
} // freetype
} // font
} // love
@@ -0,0 +1,70 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_FREETYPE_TRUE_TYPE_RASTERIZER_H
#define LOVE_FONT_FREETYPE_TRUE_TYPE_RASTERIZER_H
// LOVE
#include "filesystem/File.h"
#include "font/Rasterizer.h"
// TrueType2
#include <ft2build.h>
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>
#include <freetype/ftoutln.h>
#include <freetype/fttrigon.h>
namespace love
{
namespace font
{
namespace freetype
{
/**
* Holds data for a font object.
**/
class TrueTypeRasterizer : public Rasterizer
{
public:
TrueTypeRasterizer(FT_Library library, Data *data, int size);
virtual ~TrueTypeRasterizer();
// Implement Rasterizer
virtual int getLineHeight() const;
virtual GlyphData *getGlyphData(uint32 glyph) const;
virtual int getGlyphCount() const;
virtual bool hasGlyph(uint32 glyph) const;
private:
// TrueType face
FT_Face face;
// File data
Data *data;
}; // FreetypeRasterizer
} // freetype
} // font
} // love
#endif // LOVE_FONT_FREETYPE_TRUE_TYPE_RASTERIZER_H
@@ -0,0 +1,125 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_Font.h"
#include "Font.h"
#include "font/wrap_GlyphData.h"
#include "font/wrap_Rasterizer.h"
#include "TrueTypeRasterizer.h"
namespace love
{
namespace font
{
namespace freetype
{
static Font *instance = 0;
int w_newRasterizer(lua_State *L)
{
// Convert to FileData, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
Rasterizer *t = 0;
EXCEPT_GUARD(
if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T))
{
love::image::ImageData *d = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
const char *g = luaL_checkstring(L, 2);
std::string glyphs(g);
t = instance->newRasterizer(d, glyphs);
}
else if (luax_istype(L, 1, DATA_T))
{
Data *d = luax_checkdata(L, 1);
int size = luaL_checkint(L, 2);
t = instance->newRasterizer(d, size);
}
)
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
return 1;
}
int w_newGlyphData(lua_State *L)
{
Rasterizer *r = luax_checkrasterizer(L, 1);
GlyphData *t = 0;
// newGlyphData accepts a unicode character or a codepoint number.
if (lua_type(L, 2) == LUA_TSTRING)
{
std::string glyph = luax_checkstring(L, 2);
EXCEPT_GUARD(t = instance->newGlyphData(r, glyph);)
}
else
{
uint32 g = (uint32) luaL_checknumber(L, 2);
t = instance->newGlyphData(r, g);
}
luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, t);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "newRasterizer", w_newRasterizer },
{ "newGlyphData", w_newGlyphData },
{ 0, 0 }
};
static const lua_CFunction types[] =
{
luaopen_glyphdata,
luaopen_rasterizer,
0
};
extern "C" int luaopen_love_font(lua_State *L)
{
if (instance == 0)
{
EXCEPT_GUARD(instance = new Font();)
}
else
instance->retain();
WrappedModule w;
w.module = instance;
w.name = "font";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
return luax_register_module(L, w);
}
} // freetype
} // font
} // love
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_FREETYPE_WRAP_FONT_H
#define LOVE_FONT_FREETYPE_WRAP_FONT_H
// LOVE
#include "common/config.h"
#include "common/runtime.h"
namespace love
{
namespace font
{
namespace freetype
{
int w_newRasterizer(lua_State *L);
int w_newGlyphData(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_font(lua_State *L);
} // freetype
} // font
} // love
#endif // LOVE_FONT_FREETYPE_WRAP_FONT_H
@@ -0,0 +1,143 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_GlyphData.h"
namespace love
{
namespace font
{
GlyphData *luax_checkglyphdata(lua_State *L, int idx)
{
return luax_checktype<GlyphData>(L, idx, "GlyphData", FONT_GLYPH_DATA_T);
}
int w_GlyphData_getWidth(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
lua_pushinteger(L, t->getWidth());
return 1;
}
int w_GlyphData_getHeight(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
lua_pushinteger(L, t->getHeight());
return 1;
}
int w_GlyphData_getDimensions(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
lua_pushinteger(L, t->getWidth());
lua_pushinteger(L, t->getHeight());
return 2;
}
int w_GlyphData_getGlyph(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
uint32 glyph = t->getGlyph();
lua_pushnumber(L, (lua_Number) glyph);
return 1;
}
int w_GlyphData_getGlyphString(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
EXCEPT_GUARD(luax_pushstring(L, t->getGlyphString());)
return 1;
}
int w_GlyphData_getAdvance(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
lua_pushinteger(L, t->getAdvance());
return 1;
}
int w_GlyphData_getBearing(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
lua_pushinteger(L, t->getBearingX());
lua_pushinteger(L, t->getBearingY());
return 2;
}
int w_GlyphData_getBoundingBox(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
int minX = t->getMinX();
int minY = t->getMinY();
int maxX = t->getMaxX();
int maxY = t->getMaxY();
int width = maxX - minX;
int height = maxY - minY;
lua_pushinteger(L, minX);
lua_pushinteger(L, minY);
lua_pushinteger(L, width);
lua_pushinteger(L, height);
return 4;
}
int w_GlyphData_getFormat(lua_State *L)
{
GlyphData *t = luax_checkglyphdata(L, 1);
const char *str;
if (!GlyphData::getConstant(t->getFormat(), str))
return luaL_error(L, "unknown GlyphData format.");
lua_pushstring(L, str);
return 1;
}
static const luaL_Reg functions[] =
{
// Data
{ "getString", w_Data_getString },
{ "getPointer", w_Data_getPointer },
{ "getSize", w_Data_getSize },
{ "getWidth", w_GlyphData_getWidth },
{ "getHeight", w_GlyphData_getHeight },
{ "getDimensions", w_GlyphData_getDimensions },
{ "getGlyph", w_GlyphData_getGlyph },
{ "getGlyphString", w_GlyphData_getGlyphString },
{ "getAdvance", w_GlyphData_getAdvance },
{ "getBearing", w_GlyphData_getBearing },
{ "getBoundingBox", w_GlyphData_getBoundingBox },
{ "getFormat", w_GlyphData_getFormat },
{ 0, 0 }
};
extern "C" int luaopen_glyphdata(lua_State *L)
{
return luax_register_type(L, "GlyphData", functions);
}
} // font
} // love
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_WRAP_GLYPH_DATA_H
#define LOVE_FONT_WRAP_GLYPH_DATA_H
// LOVE
#include "common/runtime.h"
#include "common/wrap_Data.h"
#include "GlyphData.h"
namespace love
{
namespace font
{
GlyphData *luax_checkglyphdata(lua_State *L, int idx);
int w_GlyphData_getWidth(lua_State *L);
int w_GlyphData_getHeight(lua_State *L);
int w_GlyphData_getDimensions(lua_State *L);
int w_GlyphData_getGlyph(lua_State *L);
int w_GlyphData_getGlyphString(lua_State *L);
int w_GlyphData_getAdvance(lua_State *L);
int w_GlyphData_getBearing(lua_State *L);
int w_GlyphData_getBoundingBox(lua_State *L);
int w_GlyphData_getFormat(lua_State *L);
extern "C" int luaopen_glyphdata(lua_State *L);
} // font
} // love
#endif // LOVE_FONT_WRAP_GLYPH_DATA_H
@@ -0,0 +1,145 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_Rasterizer.h"
#include "common/wrap_Data.h"
namespace love
{
namespace font
{
Rasterizer *luax_checkrasterizer(lua_State *L, int idx)
{
return luax_checktype<Rasterizer>(L, idx, "Rasterizer", FONT_RASTERIZER_T);
}
int w_Rasterizer_getHeight(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
lua_pushinteger(L, t->getHeight());
return 1;
}
int w_Rasterizer_getAdvance(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
lua_pushinteger(L, t->getAdvance());
return 1;
}
int w_Rasterizer_getAscent(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
lua_pushinteger(L, t->getAscent());
return 1;
}
int w_Rasterizer_getDescent(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
lua_pushinteger(L, t->getDescent());
return 1;
}
int w_Rasterizer_getLineHeight(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
lua_pushinteger(L, t->getLineHeight());
return 1;
}
int w_Rasterizer_getGlyphData(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
GlyphData *g = 0;
EXCEPT_GUARD(
// getGlyphData accepts a unicode character or a codepoint number.
if (lua_type(L, 2) == LUA_TSTRING)
{
std::string glyph = luax_checkstring(L, 2);
g = t->getGlyphData(glyph);
}
else
{
uint32 glyph = (uint32) luaL_checknumber(L, 2);
g = t->getGlyphData(glyph);
}
)
luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, g);
return 1;
}
int w_Rasterizer_getGlyphCount(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
lua_pushinteger(L, t->getGlyphCount());
return 1;
}
int w_Rasterizer_hasGlyphs(lua_State *L)
{
Rasterizer *t = luax_checkrasterizer(L, 1);
bool hasglyph = false;
int count = lua_gettop(L) - 1;
count = count < 1 ? 1 : count;
EXCEPT_GUARD(
for (int i = 2; i < count + 2; i++)
{
if (lua_type(L, i) == LUA_TSTRING)
hasglyph = t->hasGlyphs(luax_checkstring(L, i));
else
hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, i));
if (!hasglyph)
break;
}
)
luax_pushboolean(L, hasglyph);
return 1;
}
static const luaL_Reg functions[] =
{
{ "getHeight", w_Rasterizer_getHeight },
{ "getAdvance", w_Rasterizer_getAdvance },
{ "getAscent", w_Rasterizer_getAscent },
{ "getDescent", w_Rasterizer_getDescent },
{ "getLineHeight", w_Rasterizer_getLineHeight },
{ "getGlyphData", w_Rasterizer_getGlyphData },
{ "getGlyphCount", w_Rasterizer_getGlyphCount },
{ "hasGlyphs", w_Rasterizer_hasGlyphs },
{ 0, 0 }
};
extern "C" int luaopen_rasterizer(lua_State *L)
{
return luax_register_type(L, "Rasterizer", functions);
}
} // font
} // love
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FONT_WRAP_RASTERIZER_H
#define LOVE_FONT_WRAP_RASTERIZER_H
// LOVE
#include "common/runtime.h"
#include "Rasterizer.h"
namespace love
{
namespace font
{
Rasterizer *luax_checkrasterizer(lua_State *L, int idx);
int w_Rasterizer_getHeight(lua_State *L);
int w_Rasterizer_getAdvance(lua_State *L);
int w_Rasterizer_getAscent(lua_State *L);
int w_Rasterizer_getDescent(lua_State *L);
int w_Rasterizer_getLineHeight(lua_State *L);
int w_Rasterizer_getGlyphData(lua_State *L);
int w_Rasterizer_getGlyphCount(lua_State *L);
int w_Rasterizer_hasGlyphs(lua_State *L);
extern "C" int luaopen_rasterizer(lua_State *L);
} // font
} // love
#endif // LOVE_FONT_WRAP_RASTERIZER_H