diff --git a/CMakeLists.txt b/CMakeLists.txt index 582a2fa34..446f7d6e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,6 +226,7 @@ source_group("modules\\filesystem\\physfs" FILES ${LOVE_SRC_MODULE_FILESYSTEM_PH set(LOVE_SRC_MODULE_FONT_ROOT src/modules/font/BMFontRasterizer.cpp src/modules/font/BMFontRasterizer.h + src/modules/font/Font.cpp src/modules/font/Font.h src/modules/font/GlyphData.cpp src/modules/font/GlyphData.h diff --git a/platform/macosx/love-framework.xcodeproj/project.pbxproj b/platform/macosx/love-framework.xcodeproj/project.pbxproj index ca6b625ee..288e82612 100644 --- a/platform/macosx/love-framework.xcodeproj/project.pbxproj +++ b/platform/macosx/love-framework.xcodeproj/project.pbxproj @@ -321,6 +321,7 @@ FAC86E6C1724555D00EED715 /* Quad.h in Headers */ = {isa = PBXBuildFile; fileRef = FAC86E681724555D00EED715 /* Quad.h */; }; FAD400A6196C5EBC004208B9 /* Text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAD400A5196C5EBC004208B9 /* Text.cpp */; }; FAD5970C196E174000E8EA36 /* wrap_Text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAD5970B196E174000E8EA36 /* wrap_Text.cpp */; }; + FAD661A21A91BA1800B64301 /* Font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAD661A11A91BA1800B64301 /* Font.cpp */; }; FAD8E84F1A7C949B0084E24D /* Vera.ttf.h in Headers */ = {isa = PBXBuildFile; fileRef = FAD8E84E1A7C949B0084E24D /* Vera.ttf.h */; }; FADD58DD18C30367005FC3BF /* FormatHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FADD58DC18C30367005FC3BF /* FormatHandler.cpp */; }; FADDCDAD1A91817800B32118 /* CoreAudioDecoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FADDCDAB1A91817800B32118 /* CoreAudioDecoder.cpp */; }; @@ -917,6 +918,7 @@ FAD400A7196C5ECC004208B9 /* Text.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Text.h; sourceTree = ""; }; FAD5970B196E174000E8EA36 /* wrap_Text.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_Text.cpp; sourceTree = ""; }; FAD5970D196E17C100E8EA36 /* wrap_Text.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = wrap_Text.h; sourceTree = ""; }; + FAD661A11A91BA1800B64301 /* Font.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Font.cpp; sourceTree = ""; }; FAD8E84E1A7C949B0084E24D /* Vera.ttf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Vera.ttf.h; sourceTree = ""; }; FADD58DC18C30367005FC3BF /* FormatHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FormatHandler.cpp; sourceTree = ""; }; FADDCDAB1A91817800B32118 /* CoreAudioDecoder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CoreAudioDecoder.cpp; sourceTree = ""; }; @@ -1226,6 +1228,7 @@ children = ( FA9556C919D3741A00F29A4C /* BMFontRasterizer.cpp */, FA9556CA19D3741A00F29A4C /* BMFontRasterizer.h */, + FAD661A11A91BA1800B64301 /* Font.cpp */, 2E9B5D9926034F9172215D5E /* Font.h */, 12BB56F127170B3709670896 /* freetype */, 74003CB27FA762A021183AD5 /* GlyphData.cpp */, @@ -2506,6 +2509,7 @@ FA5FDC8A1788D548002F0ED2 /* peer.c in Sources */, FA5FDC8B1788D548002F0ED2 /* protocol.c in Sources */, FA5FDC8D1788D548002F0ED2 /* unix.c in Sources */, + FAD661A21A91BA1800B64301 /* Font.cpp in Sources */, FA5FDC8E1788D548002F0ED2 /* win32.c in Sources */, FA34E7CD174B513F00E04D3F /* System.cpp in Sources */, FA34E7D2174B515D00E04D3F /* System.cpp in Sources */, diff --git a/src/modules/font/Font.cpp b/src/modules/font/Font.cpp new file mode 100644 index 000000000..a4419621a --- /dev/null +++ b/src/modules/font/Font.cpp @@ -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 &images) +{ + return new BMFontRasterizer(fontdef, images); +} + +Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text) +{ + std::vector glyphs; + glyphs.reserve(text.size()); + + try + { + utf8::iterator i(text.begin(), text.begin(), text.end()); + utf8::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 diff --git a/src/modules/font/Font.h b/src/modules/font/Font.h index fc95979cf..851621bd1 100644 --- a/src/modules/font/Font.h +++ b/src/modules/font/Font.h @@ -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 &images) = 0; + virtual Rasterizer *newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector &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; } diff --git a/src/modules/font/freetype/Font.cpp b/src/modules/font/freetype/Font.cpp index b25f3f7f5..0e9ba1031 100644 --- a/src/modules/font/freetype/Font.cpp +++ b/src/modules/font/freetype/Font.cpp @@ -21,11 +21,8 @@ #include "Font.h" #include "TrueTypeRasterizer.h" -#include "font/ImageRasterizer.h" #include "font/BMFontRasterizer.h" -#include "libraries/utf8/utf8.h" - #include 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()); + 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 &images) -{ - return new BMFontRasterizer(fontdef, images); -} - -Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text) -{ - std::vector glyphs; - glyphs.reserve(text.size()); - - try - { - utf8::iterator i(text.begin(), text.begin(), text.end()); - utf8::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"; diff --git a/src/modules/font/freetype/Font.h b/src/modules/font/freetype/Font.h index c15c01127..88c7fef89 100644 --- a/src/modules/font/freetype/Font.h +++ b/src/modules/font/freetype/Font.h @@ -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 &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;