From f8f812a71913716f349752ba29159fdadeda5a0a Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Thu, 15 Apr 2010 10:19:01 -0400 Subject: [PATCH] did a bunch of other stuff, sorry about the monolithic changeset but the diffs should be understandable enough --- .../macosx/love.xcodeproj/project.pbxproj | 6 +- src/modules/font/FontData.cpp | 2 +- src/modules/font/FontData.h | 2 +- src/modules/font/GlyphData.cpp | 32 +++- src/modules/font/GlyphData.h | 96 ++++++----- src/modules/font/ImageRasterizer.cpp | 18 +- src/modules/font/ImageRasterizer.h | 1 + src/modules/font/Rasterizer.h | 5 + .../font/freetype/TrueTypeRasterizer.cpp | 27 +-- .../font/freetype/TrueTypeRasterizer.h | 1 + src/modules/graphics/opengl/Font.cpp | 82 +++++++--- src/modules/graphics/opengl/Font.h | 39 +++-- src/modules/graphics/opengl/Glyph.cpp | 16 +- src/modules/graphics/opengl/Glyph.h | 41 ++--- src/modules/graphics/opengl/Graphics.cpp | 29 +--- src/modules/graphics/opengl/Graphics.h | 22 +-- src/modules/graphics/opengl/TrueTypeFont.cpp | 2 +- src/modules/graphics/opengl/wrap_Graphics.cpp | 154 ++++++++++-------- 18 files changed, 338 insertions(+), 237 deletions(-) diff --git a/platform/macosx/love.xcodeproj/project.pbxproj b/platform/macosx/love.xcodeproj/project.pbxproj index 615ee9acb..bae574559 100644 --- a/platform/macosx/love.xcodeproj/project.pbxproj +++ b/platform/macosx/love.xcodeproj/project.pbxproj @@ -28,8 +28,6 @@ A946CE821172BCD7005E1462 /* wrap_EncodedImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A946CE811172BCD7005E1462 /* wrap_EncodedImageData.cpp */; }; A946D3BB117681BD005E1462 /* FontData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A946D3BA117681BC005E1462 /* FontData.cpp */; }; A946D3C611768D69005E1462 /* wrap_FontData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A946D3C511768D69005E1462 /* wrap_FontData.cpp */; }; - A946D4061176A711005E1462 /* ImageFont.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E6A9110420AC2007D418B /* ImageFont.cpp */; }; - A946D4071176A716005E1462 /* TrueTypeFont.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E6A9710420AC2007D418B /* TrueTypeFont.cpp */; }; A946D67F104248DA002BF36C /* Reference.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69EA10420ABF007D418B /* Reference.cpp */; }; A946D680104248DA002BF36C /* Matrix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69E310420ABF007D418B /* Matrix.cpp */; }; A946D681104248DA002BF36C /* Object.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69E810420ABF007D418B /* Object.cpp */; }; @@ -582,6 +580,7 @@ A946D3BA117681BC005E1462 /* FontData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FontData.cpp; sourceTree = ""; }; A946D3C411768D69005E1462 /* wrap_FontData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_FontData.h; sourceTree = ""; }; A946D3C511768D69005E1462 /* wrap_FontData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_FontData.cpp; sourceTree = ""; }; + A946D4421176AC42005E1462 /* Font.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Font.h; sourceTree = ""; }; A968F0C61083A07B00A895AA /* EnumMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EnumMap.h; sourceTree = ""; }; A968F0C71083A07C00A895AA /* StringMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringMap.h; sourceTree = ""; }; A968F0CE1083A9A900A895AA /* Event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Event.cpp; sourceTree = ""; }; @@ -934,6 +933,7 @@ A93E6A6F10420AC1007D418B /* font */ = { isa = PBXGroup; children = ( + A946D4421176AC42005E1462 /* Font.h */, A946D3BA117681BC005E1462 /* FontData.cpp */, A946D3B9117681BC005E1462 /* FontData.h */, A93E6A7010420AC1007D418B /* freetype */, @@ -1666,8 +1666,6 @@ A946CE821172BCD7005E1462 /* wrap_EncodedImageData.cpp in Sources */, A946D3BB117681BD005E1462 /* FontData.cpp in Sources */, A946D3C611768D69005E1462 /* wrap_FontData.cpp in Sources */, - A946D4061176A711005E1462 /* ImageFont.cpp in Sources */, - A946D4071176A716005E1462 /* TrueTypeFont.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/modules/font/FontData.cpp b/src/modules/font/FontData.cpp index af740e32e..e5f9eeb14 100644 --- a/src/modules/font/FontData.cpp +++ b/src/modules/font/FontData.cpp @@ -50,7 +50,7 @@ namespace font return MAX_CHARS; } - GlyphData * FontData::getGlyph(unsigned short glyph) const + GlyphData * FontData::getGlyphData(unsigned short glyph) const { return data[glyph]; } diff --git a/src/modules/font/FontData.h b/src/modules/font/FontData.h index 0be063b2d..23d238dbd 100644 --- a/src/modules/font/FontData.h +++ b/src/modules/font/FontData.h @@ -41,7 +41,7 @@ namespace font void * getData() const; int getSize() const; - GlyphData * getGlyph(unsigned short glyph) const; + GlyphData * getGlyphData(unsigned short glyph) const; int getHeight() const; static const unsigned int MAX_CHARS = 256; diff --git a/src/modules/font/GlyphData.cpp b/src/modules/font/GlyphData.cpp index bf816074f..249f40cce 100644 --- a/src/modules/font/GlyphData.cpp +++ b/src/modules/font/GlyphData.cpp @@ -25,10 +25,20 @@ namespace love namespace font { - GlyphData::GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics) - : glyph(glyph), metrics(glyphMetrics) + GlyphData::GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics, GlyphData::Format f) + : glyph(glyph), metrics(glyphMetrics), format(f) { - data = new unsigned char[getWidth() * getHeight() * 4]; + if (getWidth() && getHeight()) { + switch (f) { + case GlyphData::FORMAT_LUMINOSITY_ALPHA: + data = new unsigned char[getWidth() * getHeight() * 2]; + break; + case GlyphData::FORMAT_RGBA: + default: + data = new unsigned char[getWidth() * getHeight() * 4]; + break; + } + } } GlyphData::~GlyphData() @@ -43,7 +53,16 @@ namespace font int GlyphData::getSize() const { - return getWidth() * getHeight() * 4; + switch(format) { + case GlyphData::FORMAT_LUMINOSITY_ALPHA: + return getWidth() * getHeight() * 2; + break; + case GlyphData::FORMAT_RGBA: + default: + return getWidth() * getHeight() * 4; + break; + } + } int GlyphData::getHeight() const @@ -90,6 +109,11 @@ namespace font { return this->getBearingY(); } + + GlyphData::Format GlyphData::getFormat() const + { + return format; + } } // font } // love diff --git a/src/modules/font/GlyphData.h b/src/modules/font/GlyphData.h index b6a0b8993..c92ce1be2 100644 --- a/src/modules/font/GlyphData.h +++ b/src/modules/font/GlyphData.h @@ -1,21 +1,21 @@ -/** -* Copyright (c) 2006-2010 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. +/** +* Copyright (c) 2006-2010 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 @@ -29,16 +29,17 @@ namespace love { namespace font { - /** - * Holds the specific glyph data. - **/ - struct GlyphMetrics - { - int height; - int width; - int advance; - int bearingX; - int bearingY; + /** + * Holds the specific glyph data. + **/ + struct GlyphMetrics + { + int height; + int width; + int advance; + int bearingX; + int bearingY; + int spacing; }; /** @@ -46,22 +47,19 @@ namespace font **/ class GlyphData : public Data { - private: - // The glyph itself - unsigned short glyph; - - // Glyph metrics - GlyphMetrics metrics; - - // Glyph texture data - unsigned char * data; public: - GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics); + enum Format + { + FORMAT_LUMINOSITY_ALPHA, + FORMAT_RGBA + }; + + GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics, Format f); virtual ~GlyphData(); - // Implements Data. - void * getData() const; + // Implements Data. + void * getData() const; int getSize() const; /** @@ -108,6 +106,24 @@ namespace font * Gets the max y value of the glyph. **/ int getMaxY() const; + + /** + * Gets the format of the glyph data. + **/ + Format getFormat() const; + + private: + // The glyph itself + unsigned short glyph; + + // Glyph metrics + GlyphMetrics metrics; + + // Glyph texture data + unsigned char * data; + + // The format the data's in + Format format; }; // GlyphData diff --git a/src/modules/font/ImageRasterizer.cpp b/src/modules/font/ImageRasterizer.cpp index db35b4e49..40ea6da81 100644 --- a/src/modules/font/ImageRasterizer.cpp +++ b/src/modules/font/ImageRasterizer.cpp @@ -38,8 +38,11 @@ namespace font { imageData->retain(); positions = new unsigned int[MAX_CHARS]; + memset(positions, 0, MAX_CHARS*4); widths = new unsigned int[MAX_CHARS]; + memset(widths, 0, MAX_CHARS*4); spacing = new unsigned int[MAX_CHARS]; + memset(spacing, 0, MAX_CHARS*4); load(); } @@ -61,11 +64,15 @@ namespace font GlyphMetrics gm; gm.height = metrics.height; gm.width = widths[glyph]; - GlyphData * g = new GlyphData(glyph, gm); + gm.advance = spacing[glyph] + widths[glyph]; + gm.bearingX = 0; + gm.bearingY = 0; + GlyphData * g = new GlyphData(glyph, gm, GlyphData::FORMAT_RGBA); + if (gm.width == 0) return g; unsigned char * gd = (unsigned char*)g->getData(); love::image::pixel * pixels = (love::image::pixel *)(imageData->getData()); - for (unsigned int i = positions[glyph]; i < positions[glyph] + widths[glyph]; i++) { - love::image::pixel p = pixels[i]; + for (unsigned int i = 0; i < widths[glyph]*getHeight(); i++) { + love::image::pixel p = pixels[ positions[glyph] + (i % widths[glyph]) + (imageData->getWidth() * (i / widths[glyph])) ]; gd[i*4] = p.r; gd[i*4+1] = p.g; gd[i*4+2] = p.b; @@ -132,6 +139,11 @@ namespace font } } } + + int ImageRasterizer::getNumGlyphs() const + { + return length; + } } // font } // love \ No newline at end of file diff --git a/src/modules/font/ImageRasterizer.h b/src/modules/font/ImageRasterizer.h index c5ea3d132..6639f9422 100644 --- a/src/modules/font/ImageRasterizer.h +++ b/src/modules/font/ImageRasterizer.h @@ -59,6 +59,7 @@ namespace font // Implement Rasterizer virtual int getLineHeight() const; virtual GlyphData * getGlyphData(unsigned short glyph) const; + virtual int getNumGlyphs() const; static const unsigned int MAX_CHARS = 256; diff --git a/src/modules/font/Rasterizer.h b/src/modules/font/Rasterizer.h index b0134d0d0..aced727ce 100644 --- a/src/modules/font/Rasterizer.h +++ b/src/modules/font/Rasterizer.h @@ -82,6 +82,11 @@ namespace font * @param glyph The (UNICODE) glyph to get data for **/ virtual GlyphData * getGlyphData(unsigned short glyph) const = 0; + + /** + * Gets the number of glyphs the rasterizer has data for. + **/ + virtual int getNumGlyphs() const = 0; }; // Rasterizer diff --git a/src/modules/font/freetype/TrueTypeRasterizer.cpp b/src/modules/font/freetype/TrueTypeRasterizer.cpp index 2bb31cb90..7f175112e 100644 --- a/src/modules/font/freetype/TrueTypeRasterizer.cpp +++ b/src/modules/font/freetype/TrueTypeRasterizer.cpp @@ -46,10 +46,11 @@ namespace freetype FT_Set_Pixel_Sizes(face, size, size); // Set global metrics - metrics.advance = face->max_advance_width; - metrics.ascent = face->ascender; - metrics.descent = face->descender; - metrics.height = face->height; + 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; } TrueTypeRasterizer::~TrueTypeRasterizer() @@ -80,23 +81,24 @@ namespace freetype FT_Bitmap& bitmap = bitmap_glyph->bitmap; //just to make things easier // Get metrics - glyphMetrics.bearingX = face->glyph->metrics.horiBearingX; - glyphMetrics.bearingY = face->glyph->metrics.horiBearingY; + 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->advance.x >> 6; + glyphMetrics.advance = face->glyph->metrics.horiAdvance >> 6; - GlyphData * glyphData = new GlyphData(glyph, glyphMetrics); + GlyphData * glyphData = new GlyphData(glyph, glyphMetrics, GlyphData::FORMAT_RGBA); { 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 rgba format. + // our luminosity-alpha format. for(int i = 0; i +#include + namespace love { namespace graphics { namespace opengl { - Font::Font(int size) - : size(size), lineHeight(1), mSpacing(1) + + Font::Font(love::font::FontData * data) + : height(data->getHeight()), lineHeight(1.25) { + glyphs = new Glyph*[MAX_CHARS]; for(unsigned int i = 0; i < MAX_CHARS; i++) { - widths[i] = 0; - spacing[i] = 0; + glyphs[i] = new Glyph(data->getGlyphData(i)); + glyphs[i]->load(); + widths[i] = data->getGlyphData(i)->getWidth(); + spacing[i] = data->getGlyphData(i)->getAdvance(); + bearingX[i] = data->getGlyphData(i)->getBearingX(); + bearingY[i] = data->getGlyphData(i)->getBearingY(); } } Font::~Font() { + delete[] glyphs; } float Font::getHeight() const { - return (float)size; + return height; + } + + void Font::print(std::string text, float x, float y) const + { + print(text, x, y, 0.0f, 1.0f, 1.0f); + } + + void Font::print(std::string text, float x, float y, float angle, float sx, float sy) const + { + glPushMatrix(); + + glTranslatef(ceil(x), ceil(y+getHeight()), 0.0f); + glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f); + glScalef(sx, sy, 1.0f); + int s = 0; + for (unsigned int i = 0; i < text.size(); i++) { + int g = (int)text[i]; + glyphs[g]->draw(bearingX[g] + s, -bearingY[g], 0, 1, 1, 0, 0); + s += spacing[g]; + } + glPopMatrix(); + } + + void Font::print(char character, float x, float y) const + { + glyphs[character]->draw(x, y+getHeight(), 0, 1, 1, 0, 0); } float Font::getWidth(const std::string & line) const diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index 127e2f5ca..736e99740 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/src/modules/graphics/opengl/Font.h @@ -26,7 +26,8 @@ // LOVE #include -#include +#include +#include "Glyph.h" namespace love { @@ -34,33 +35,35 @@ namespace graphics { namespace opengl { - class Font : public Object, public Volatile + class Font : public Object { - protected: + private: - int size; + int height; float lineHeight; float mSpacing; // modifies the spacing by multiplying it with this value + Glyph ** glyphs; public: static const unsigned int MAX_CHARS = 256; // The widths of each character. int widths[MAX_CHARS]; + // The spacing of each character. int spacing[MAX_CHARS]; + // The X-bearing of each character. + int bearingX[MAX_CHARS]; + // The Y-bearing of each character. + int bearingY[MAX_CHARS]; /** * Default constructor. * - * @param file The file containing the OpenGLFont data. - * @param size The size of the OpenGLFont. + * @param data The font data to construct from. **/ - Font(int size); + Font(love::font::FontData * data); virtual ~Font(); - virtual bool load() = 0; - virtual void unload() = 0; - /** * Prints the text at the designated position. * @@ -68,7 +71,7 @@ namespace opengl * @param x The x-coordinate. * @param y The y-coordinate. **/ - virtual void print(std::string text, float x, float y) const = 0; + void print(std::string text, float x, float y) const; /** * Prints the text at the designated position with rotation and scaling. @@ -78,7 +81,7 @@ namespace opengl * @param y The y-coordinate. * @param angle The amount of rotation. **/ - virtual void print(std::string text, float x, float y, float angle, float sx, float sy) const = 0; + void print(std::string text, float x, float y, float angle, float sx, float sy) const; /** * Prints the character at the designated position. @@ -87,27 +90,27 @@ namespace opengl * @param x The x-coordinate. * @param y The y-coordinate. **/ - virtual void print(char character, float x, float y) const = 0; + void print(char character, float x, float y) const; /** * Returns the height of the font. **/ - virtual float getHeight() const; + float getHeight() const; /** * Returns the width of the passed string. * * @param line A line of text. **/ - virtual float getWidth(const std::string & line) const; - virtual float getWidth(const char * line) const; + float getWidth(const std::string & line) const; + float getWidth(const char * line) const; /** * Returns the width of the passed character. * * @param character A character. **/ - virtual float getWidth(const char character) const; + float getWidth(const char character) const; /** * Sets the line height (which should be a number to multiply the font size by, @@ -119,7 +122,7 @@ namespace opengl /** * Returns the line height. **/ - virtual float getLineHeight() const; + float getLineHeight() const; /** * Sets the spacing modifier (changes the spacing between the characters the diff --git a/src/modules/graphics/opengl/Glyph.cpp b/src/modules/graphics/opengl/Glyph.cpp index ee43a6652..06f431456 100644 --- a/src/modules/graphics/opengl/Glyph.cpp +++ b/src/modules/graphics/opengl/Glyph.cpp @@ -34,7 +34,6 @@ namespace opengl : data(data), width((float)data->getWidth()), height((float)data->getHeight()), texture(0) { data->retain(); - data->getWidth(); memset(vertices, 255, sizeof(vertex)*4); @@ -94,6 +93,9 @@ namespace opengl bool Glyph::loadVolatile() { + GLint format = GL_RGBA; + if (data->getFormat() == love::font::GlyphData::FORMAT_LUMINOSITY_ALPHA) format = GL_LUMINANCE_ALPHA; + glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -108,7 +110,7 @@ namespace opengl (GLsizei)width, (GLsizei)height, 0, - GL_RGBA, + format, GL_UNSIGNED_BYTE, data->getData()); @@ -124,6 +126,16 @@ namespace opengl texture = 0; } } + + float Glyph::getWidth() const + { + return width; + } + + float Glyph::getHeight() const + { + return height; + } } // opengl } // graphics diff --git a/src/modules/graphics/opengl/Glyph.h b/src/modules/graphics/opengl/Glyph.h index 5a03f94e5..e89cc94ee 100644 --- a/src/modules/graphics/opengl/Glyph.h +++ b/src/modules/graphics/opengl/Glyph.h @@ -1,21 +1,21 @@ -/** -* Copyright (c) 2006-2010 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. +/** +* Copyright (c) 2006-2010 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_GRAPHICS_OPENGL_GLYPH_H @@ -62,7 +62,10 @@ namespace opengl // Implements Volatile. bool loadVolatile(); - void unloadVolatile(); + void unloadVolatile(); + + float getWidth() const; + float getHeight() const; void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const; diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index c304e849a..96d3596a0 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -410,26 +410,12 @@ namespace opengl return new Quad(v, sw, sh); } - Font * Graphics::newFont(Data * data, int size) + Font * Graphics::newFont(love::font::FontData * data) { - Font * font = new TrueTypeFont(data, size); + Font * font = new Font(data); // Load it and check for errors. - if(!font->load()) - { - delete font; - return 0; - } - - return font; - } - - Font * Graphics::newImageFont(Image * image, const char * glyphs, float) - { - Font * font = new ImageFont(image, std::string(glyphs)); - - // Load it and check for errors. - if(!font->load()) + if(!font) { delete font; return 0; @@ -497,15 +483,6 @@ namespace opengl currentFont->retain(); } - void Graphics::setFont( Data * data, int size ) - { - if(currentFont != 0) - currentFont->release(); - - currentFont = new TrueTypeFont(data, size); - currentFont->load(); - } - Font * Graphics::getFont() { return currentFont; diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 86ef64566..e865b1dae 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -33,12 +33,13 @@ // LOVE #include +#include + #include #include +#include "Font.h" #include "Image.h" -#include "TrueTypeFont.h" -#include "ImageFont.h" #include "Quad.h" #include "SpriteBatch.h" #include "ParticleSystem.h" @@ -248,12 +249,7 @@ namespace opengl /** * Creates a Font object. **/ - Font * newFont(Data * data, int size); - - /** - * Creates an ImageFont object. - **/ - Font * newImageFont(Image * image, const char * glyphs, float spacing = 1); + Font * newFont(love::font::FontData * data); SpriteBatch * newSpriteBatch(Image * image, int size, int usage); @@ -285,16 +281,6 @@ namespace opengl * @parm font A Font object. **/ void setFont(Font * font); - - /** - * Sets a default font. The font is - * loaded and sent to the GPU every time this is called, - * so no over-using. - * @param data Data - * @param size The size of the font. - **/ - void setFont(Data * data, int size = 12); - /** * Gets the current Font, or nil if none. **/ diff --git a/src/modules/graphics/opengl/TrueTypeFont.cpp b/src/modules/graphics/opengl/TrueTypeFont.cpp index 77fb2f686..69da64336 100644 --- a/src/modules/graphics/opengl/TrueTypeFont.cpp +++ b/src/modules/graphics/opengl/TrueTypeFont.cpp @@ -160,7 +160,7 @@ namespace opengl glPushMatrix(); glTranslatef(ceil(x), ceil(y), 0.0f); - glRotatef(LOVE_TORAD(angle), 0, 0, 1.0f); + glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f); glScalef(sx, sy, 1.0f); GLuint TrueTypeFont = list; diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 28472948e..4851bbd94 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -21,6 +21,8 @@ #include "wrap_Graphics.h" #include +#include +#include #include @@ -197,69 +199,69 @@ namespace opengl int w_newFont1(lua_State * L) { - Data * d = 0; - // Convert to File, if necessary. if(lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); - - if(luax_istype(L, 1, FILESYSTEM_FILE_T)) - { - // Check the value. - love::filesystem::File * file = luax_checktype(L, 1, "File", FILESYSTEM_FILE_T); - try { - d = file->read(); - } catch (Exception & e) { - return luaL_error(L, e.what()); - } + + // Convert to Data, if necessary. + if(luax_istype(L, 1, FILESYSTEM_FILE_T)) { + love::filesystem::File * f = luax_checktype(L, 1, "File", FILESYSTEM_FILE_T); + Data * d = f->read(); + lua_pop(L, 1); // get rid of the file + luax_newtype(L, "Data", DATA_T, (void*)d); } - else if(luax_istype(L, 1, DATA_T)) - { - d = luax_checktype(L, 1, "Data", DATA_T); - } else { // This is not the type you're looking for. - return luaL_error(L, "love.graphics.newFont() requires a string, File, or font data as argument #1"); + + // Convert to Rasterizer, if necessary. + if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) { + int idxs[] = {1, 2}; + luax_convobj(L, idxs, 2, "font", "newRasterizer"); } - - // Second optional parameter can be a number: - int size = luaL_optint(L, 2, 12); - - Font * font; - try { - font = instance->newFont(d, size); - } catch (Exception & e) { - return luaL_error(L, e.what()); - } - + + // Convert to FontData, if necessary. + if(luax_istype(L, 1, FONT_RASTERIZER_T)) + luax_convobj(L, 1, "font", "newFontData"); + + love::font::FontData * data = luax_checktype(L, 1, "FontData", FONT_FONT_DATA_T); + + // Create the font. + Font * font = instance->newFont(data); + if(font == 0) - return luaL_error(L, "Could not load the font"); - + return luaL_error(L, "Could not load font."); + + // Push the type. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font); - + return 1; } int w_newImageFont(lua_State * L) { - // Convert to File, if necessary. - if(lua_isstring(L, 1)) - luax_convobj(L, 1, "filesystem", "newFile"); - - // Convert to Image, if necessary. - if(luax_istype(L, 1, FILESYSTEM_FILE_T)) - luax_convobj(L, 1, "graphics", "newImage"); - - // Check the value. - Image * image = luax_checktype(L, 1, "Image", GRAPHICS_IMAGE_T); - - const char * glyphs = luaL_checkstring(L, 2); - - Font * font = instance->newImageFont(image, glyphs); - + // Convert to ImageData if necessary. + if(lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, DATA_T)) + luax_convobj(L, 1, "image", "newImageData"); + + // Convert to Rasterizer if necessary. + if(luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) { + int idxs[] = {1, 2}; + luax_convobj(L, idxs, 2, "font", "newRasterizer"); + } + + // Convert to FontData, if necessary. + if(luax_istype(L, 1, FONT_RASTERIZER_T)) + luax_convobj(L, 1, "font", "newFontData"); + + love::font::FontData * data = luax_checktype(L, 1, "FontData", FONT_FONT_DATA_T); + + // Create the font. + Font * font = instance->newFont(data); + if(font == 0) - return luaL_error(L, "Could not load the font"); - + return luaL_error(L, "Could not load font."); + + // Push the type. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font); - + return 1; } @@ -329,25 +331,43 @@ namespace opengl // The second parameter is an optional int. int size = luaL_optint(L, 2, 12); - // If the first parameter is a string, convert it to a file. - if(lua_isstring(L, 1)) - luax_convobj(L, 1, "filesystem", "newFile"); - - // If the first parameter is a File, use another setFont function. - if(luax_istype(L, 1, FILESYSTEM_FILE_T)) - { - love::filesystem::File * file = luax_checktype(L, 1, "File", FILESYSTEM_FILE_T); - instance->setFont(file->read(), size); - return 0; + Font * font; + + // If the first parameter isn't a Font, create a new one + if (!luax_istype(L, 1, GRAPHICS_FONT_T)) { + lua_pushinteger(L, size); // push the size + lua_insert(L, 2); // move it to its proper place + // Convert to File, if necessary. + if(lua_isstring(L, 1)) + luax_convobj(L, 1, "filesystem", "newFile"); + + // Convert to Data, if necessary. + if(luax_istype(L, 1, FILESYSTEM_FILE_T)) { + love::filesystem::File * f = luax_checktype(L, 1, "File", FILESYSTEM_FILE_T); + Data * d = f->read(); + lua_pop(L, 1); // get rid of the file + luax_newtype(L, "Data", DATA_T, (void*)d); + } + + // Convert to Rasterizer, if necessary. + if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) { + int idxs[] = {1, 2}; + luax_convobj(L, idxs, 2, "font", "newRasterizer"); + } + + // Convert to FontData, if necessary. + if(luax_istype(L, 1, FONT_RASTERIZER_T)) + luax_convobj(L, 1, "font", "newFontData"); + + love::font::FontData * data = luax_checktype(L, 1, "FontData", FONT_FONT_DATA_T); + + // Create the font. + font = instance->newFont(data); + + if(font == 0) + return luaL_error(L, "Could not load font."); } - else if(luax_istype(L, 1, DATA_T)) - { - Data * data = luax_checktype(L, 1, "Data", DATA_T); - instance->setFont(data, size); - return 0; - } - - Font * font = luax_checktype(L, 1, "Font", GRAPHICS_FONT_T); + else font = luax_checktype(L, 1, "Font", GRAPHICS_FONT_T); instance->setFont(font); return 0; }