Pulled some Font module code out of the freetype implementation files, since it's not freetype-specific.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2015-02-16 01:47:20 -04:00
parent c2628fe4f9
commit e8286de1f3
6 changed files with 97 additions and 69 deletions
+86
View File
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2006-2015 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 "Font.h"
#include "BMFontRasterizer.h"
#include "ImageRasterizer.h"
#include "libraries/utf8/utf8.h"
namespace love
{
namespace font
{
Rasterizer *Font::newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images)
{
return new BMFontRasterizer(fontdef, images);
}
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text)
{
std::vector<uint32> glyphs;
glyphs.reserve(text.size());
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.push_back(*i++);
}
catch (utf8::exception &e)
{
throw love::Exception("UTF-8 decoding error: %s", e.what());
}
return newImageRasterizer(data, &glyphs[0], glyphs.size());
}
Rasterizer *Font::newImageRasterizer(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("UTF-8 decoding error: %s", e.what());
}
return r->getGlyphData(codepoint);
}
GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph)
{
return r->getGlyphData(glyph);
}
} // font
} // love
+5 -5
View File
@@ -49,13 +49,13 @@ public:
virtual Rasterizer *newTrueTypeRasterizer(int size) = 0;
virtual Rasterizer *newTrueTypeRasterizer(love::filesystem::FileData *data, int size) = 0;
virtual Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images) = 0;
virtual Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images);
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &glyphs);
virtual Rasterizer *newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int length);
virtual GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph) = 0;
virtual GlyphData *newGlyphData(Rasterizer *r, const std::string &glyph);
virtual GlyphData *newGlyphData(Rasterizer *r, uint32 glyph);
// Implement Module.
virtual ModuleType getModuleType() const { return M_FONT; }
+1 -56
View File
@@ -21,11 +21,8 @@
#include "Font.h"
#include "TrueTypeRasterizer.h"
#include "font/ImageRasterizer.h"
#include "font/BMFontRasterizer.h"
#include "libraries/utf8/utf8.h"
#include <string.h>
namespace love
@@ -54,7 +51,7 @@ Rasterizer *Font::newRasterizer(love::filesystem::FileData *data)
if (TrueTypeRasterizer::accepts(library, data))
return newTrueTypeRasterizer(data, 12);
else if (BMFontRasterizer::accepts(data))
return newBMFontRasterizer(data, std::vector<image::ImageData *>());
return newBMFontRasterizer(data, {});
throw love::Exception("Invalid font file: %s", data->getFilename().c_str());
}
@@ -74,58 +71,6 @@ Rasterizer *Font::newTrueTypeRasterizer(love::filesystem::FileData *data, int si
return new TrueTypeRasterizer(library, data, size);
}
Rasterizer *Font::newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images)
{
return new BMFontRasterizer(fontdef, images);
}
Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text)
{
std::vector<uint32> glyphs;
glyphs.reserve(text.size());
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.push_back(*i++);
}
catch (utf8::exception &e)
{
throw love::Exception("UTF-8 decoding error: %s", e.what());
}
return newImageRasterizer(data, &glyphs[0], glyphs.size());
}
Rasterizer *Font::newImageRasterizer(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("UTF-8 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";
-8
View File
@@ -49,14 +49,6 @@ public:
Rasterizer *newTrueTypeRasterizer(int size);
Rasterizer *newTrueTypeRasterizer(love::filesystem::FileData *data, int size);
Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images);
Rasterizer *newImageRasterizer(love::image::ImageData *data, const std::string &text);
Rasterizer *newImageRasterizer(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;