did a bunch of other stuff, sorry about the monolithic changeset but the diffs should be understandable enough

This commit is contained in:
Bill Meltsner
2010-04-15 10:19:01 -04:00
parent 17d7770eec
commit f8f812a719
18 changed files with 338 additions and 237 deletions
@@ -28,8 +28,6 @@
A946CE821172BCD7005E1462 /* wrap_EncodedImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A946CE811172BCD7005E1462 /* wrap_EncodedImageData.cpp */; }; A946CE821172BCD7005E1462 /* wrap_EncodedImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A946CE811172BCD7005E1462 /* wrap_EncodedImageData.cpp */; };
A946D3BB117681BD005E1462 /* FontData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A946D3BA117681BC005E1462 /* FontData.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 */; }; 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 */; }; A946D67F104248DA002BF36C /* Reference.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69EA10420ABF007D418B /* Reference.cpp */; };
A946D680104248DA002BF36C /* Matrix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69E310420ABF007D418B /* Matrix.cpp */; }; A946D680104248DA002BF36C /* Matrix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69E310420ABF007D418B /* Matrix.cpp */; };
A946D681104248DA002BF36C /* Object.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A93E69E810420ABF007D418B /* Object.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 = "<group>"; }; A946D3BA117681BC005E1462 /* FontData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FontData.cpp; sourceTree = "<group>"; };
A946D3C411768D69005E1462 /* wrap_FontData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_FontData.h; sourceTree = "<group>"; }; A946D3C411768D69005E1462 /* wrap_FontData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_FontData.h; sourceTree = "<group>"; };
A946D3C511768D69005E1462 /* wrap_FontData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_FontData.cpp; sourceTree = "<group>"; }; A946D3C511768D69005E1462 /* wrap_FontData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_FontData.cpp; sourceTree = "<group>"; };
A946D4421176AC42005E1462 /* Font.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Font.h; sourceTree = "<group>"; };
A968F0C61083A07B00A895AA /* EnumMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EnumMap.h; sourceTree = "<group>"; }; A968F0C61083A07B00A895AA /* EnumMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EnumMap.h; sourceTree = "<group>"; };
A968F0C71083A07C00A895AA /* StringMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringMap.h; sourceTree = "<group>"; }; A968F0C71083A07C00A895AA /* StringMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringMap.h; sourceTree = "<group>"; };
A968F0CE1083A9A900A895AA /* Event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Event.cpp; sourceTree = "<group>"; }; A968F0CE1083A9A900A895AA /* Event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Event.cpp; sourceTree = "<group>"; };
@@ -934,6 +933,7 @@
A93E6A6F10420AC1007D418B /* font */ = { A93E6A6F10420AC1007D418B /* font */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
A946D4421176AC42005E1462 /* Font.h */,
A946D3BA117681BC005E1462 /* FontData.cpp */, A946D3BA117681BC005E1462 /* FontData.cpp */,
A946D3B9117681BC005E1462 /* FontData.h */, A946D3B9117681BC005E1462 /* FontData.h */,
A93E6A7010420AC1007D418B /* freetype */, A93E6A7010420AC1007D418B /* freetype */,
@@ -1666,8 +1666,6 @@
A946CE821172BCD7005E1462 /* wrap_EncodedImageData.cpp in Sources */, A946CE821172BCD7005E1462 /* wrap_EncodedImageData.cpp in Sources */,
A946D3BB117681BD005E1462 /* FontData.cpp in Sources */, A946D3BB117681BD005E1462 /* FontData.cpp in Sources */,
A946D3C611768D69005E1462 /* wrap_FontData.cpp in Sources */, A946D3C611768D69005E1462 /* wrap_FontData.cpp in Sources */,
A946D4061176A711005E1462 /* ImageFont.cpp in Sources */,
A946D4071176A716005E1462 /* TrueTypeFont.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
+1 -1
View File
@@ -50,7 +50,7 @@ namespace font
return MAX_CHARS; return MAX_CHARS;
} }
GlyphData * FontData::getGlyph(unsigned short glyph) const GlyphData * FontData::getGlyphData(unsigned short glyph) const
{ {
return data[glyph]; return data[glyph];
} }
+1 -1
View File
@@ -41,7 +41,7 @@ namespace font
void * getData() const; void * getData() const;
int getSize() const; int getSize() const;
GlyphData * getGlyph(unsigned short glyph) const; GlyphData * getGlyphData(unsigned short glyph) const;
int getHeight() const; int getHeight() const;
static const unsigned int MAX_CHARS = 256; static const unsigned int MAX_CHARS = 256;
+28 -4
View File
@@ -25,10 +25,20 @@ namespace love
namespace font namespace font
{ {
GlyphData::GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics) GlyphData::GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics, GlyphData::Format f)
: glyph(glyph), metrics(glyphMetrics) : 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() GlyphData::~GlyphData()
@@ -43,7 +53,16 @@ namespace font
int GlyphData::getSize() const 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 int GlyphData::getHeight() const
@@ -91,5 +110,10 @@ namespace font
return this->getBearingY(); return this->getBearingY();
} }
GlyphData::Format GlyphData::getFormat() const
{
return format;
}
} // font } // font
} // love } // love
+26 -10
View File
@@ -39,6 +39,7 @@ namespace font
int advance; int advance;
int bearingX; int bearingX;
int bearingY; int bearingY;
int spacing;
}; };
/** /**
@@ -46,18 +47,15 @@ namespace font
**/ **/
class GlyphData : public Data class GlyphData : public Data
{ {
private:
// The glyph itself
unsigned short glyph;
// Glyph metrics
GlyphMetrics metrics;
// Glyph texture data
unsigned char * data;
public: public:
GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics); enum Format
{
FORMAT_LUMINOSITY_ALPHA,
FORMAT_RGBA
};
GlyphData(unsigned short glyph, GlyphMetrics glyphMetrics, Format f);
virtual ~GlyphData(); virtual ~GlyphData();
// Implements Data. // Implements Data.
@@ -109,6 +107,24 @@ namespace font
**/ **/
int getMaxY() const; 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 }; // GlyphData
} // font } // font
+15 -3
View File
@@ -38,8 +38,11 @@ namespace font
{ {
imageData->retain(); imageData->retain();
positions = new unsigned int[MAX_CHARS]; positions = new unsigned int[MAX_CHARS];
memset(positions, 0, MAX_CHARS*4);
widths = new unsigned int[MAX_CHARS]; widths = new unsigned int[MAX_CHARS];
memset(widths, 0, MAX_CHARS*4);
spacing = new unsigned int[MAX_CHARS]; spacing = new unsigned int[MAX_CHARS];
memset(spacing, 0, MAX_CHARS*4);
load(); load();
} }
@@ -61,11 +64,15 @@ namespace font
GlyphMetrics gm; GlyphMetrics gm;
gm.height = metrics.height; gm.height = metrics.height;
gm.width = widths[glyph]; 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(); unsigned char * gd = (unsigned char*)g->getData();
love::image::pixel * pixels = (love::image::pixel *)(imageData->getData()); love::image::pixel * pixels = (love::image::pixel *)(imageData->getData());
for (unsigned int i = positions[glyph]; i < positions[glyph] + widths[glyph]; i++) { for (unsigned int i = 0; i < widths[glyph]*getHeight(); i++) {
love::image::pixel p = pixels[i]; love::image::pixel p = pixels[ positions[glyph] + (i % widths[glyph]) + (imageData->getWidth() * (i / widths[glyph])) ];
gd[i*4] = p.r; gd[i*4] = p.r;
gd[i*4+1] = p.g; gd[i*4+1] = p.g;
gd[i*4+2] = p.b; gd[i*4+2] = p.b;
@@ -133,5 +140,10 @@ namespace font
} }
} }
int ImageRasterizer::getNumGlyphs() const
{
return length;
}
} // font } // font
} // love } // love
+1
View File
@@ -59,6 +59,7 @@ namespace font
// Implement Rasterizer // Implement Rasterizer
virtual int getLineHeight() const; virtual int getLineHeight() const;
virtual GlyphData * getGlyphData(unsigned short glyph) const; virtual GlyphData * getGlyphData(unsigned short glyph) const;
virtual int getNumGlyphs() const;
static const unsigned int MAX_CHARS = 256; static const unsigned int MAX_CHARS = 256;
+5
View File
@@ -83,6 +83,11 @@ namespace font
**/ **/
virtual GlyphData * getGlyphData(unsigned short glyph) const = 0; virtual GlyphData * getGlyphData(unsigned short glyph) const = 0;
/**
* Gets the number of glyphs the rasterizer has data for.
**/
virtual int getNumGlyphs() const = 0;
}; // Rasterizer }; // Rasterizer
@@ -46,10 +46,11 @@ namespace freetype
FT_Set_Pixel_Sizes(face, size, size); FT_Set_Pixel_Sizes(face, size, size);
// Set global metrics // Set global metrics
metrics.advance = face->max_advance_width; FT_Size_Metrics s = face->size->metrics;
metrics.ascent = face->ascender; metrics.advance = s.max_advance >> 6;
metrics.descent = face->descender; metrics.ascent = s.ascender >> 6;
metrics.height = face->height; metrics.descent = s.descender >> 6;
metrics.height = s.height >> 6;
} }
TrueTypeRasterizer::~TrueTypeRasterizer() TrueTypeRasterizer::~TrueTypeRasterizer()
@@ -80,23 +81,24 @@ namespace freetype
FT_Bitmap& bitmap = bitmap_glyph->bitmap; //just to make things easier FT_Bitmap& bitmap = bitmap_glyph->bitmap; //just to make things easier
// Get metrics // Get metrics
glyphMetrics.bearingX = face->glyph->metrics.horiBearingX; glyphMetrics.bearingX = face->glyph->metrics.horiBearingX >> 6;
glyphMetrics.bearingY = face->glyph->metrics.horiBearingY; glyphMetrics.bearingY = face->glyph->metrics.horiBearingY >> 6;
glyphMetrics.height = bitmap.rows; glyphMetrics.height = bitmap.rows;
glyphMetrics.width = bitmap.width; 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; int size = bitmap.rows*bitmap.width;
unsigned char * dst = (unsigned char *)glyphData->getData(); unsigned char * dst = (unsigned char *)glyphData->getData();
// Note that bitmap.buffer contains only luminosity. We copy that single value to // 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<size; i++) for(int i = 0; i<size; i++)
{ {
dst[4*i] = dst[4*i+1] = dst[4*i+2] = dst[4*i+3] = bitmap.buffer[i]; dst[4*i] = dst[4*i+1] = dst[4*i+2] = 255;
dst[4*i+3] = bitmap.buffer[i];
} }
} }
@@ -107,6 +109,11 @@ namespace freetype
return glyphData; return glyphData;
} }
int TrueTypeRasterizer::getNumGlyphs() const
{
return 256;
}
} // freetype } // freetype
} // font } // font
} // love } // love
@@ -63,6 +63,7 @@ namespace freetype
// Implement Rasterizer // Implement Rasterizer
virtual int getLineHeight() const; virtual int getLineHeight() const;
virtual GlyphData * getGlyphData(unsigned short glyph) const; virtual GlyphData * getGlyphData(unsigned short glyph) const;
virtual int getNumGlyphs() const;
}; // FreetypeRasterizer }; // FreetypeRasterizer
+41 -5
View File
@@ -20,29 +20,65 @@
#include "Font.h" #include "Font.h"
#include <common/math.h>
#include <math.h>
namespace love namespace love
{ {
namespace graphics namespace graphics
{ {
namespace opengl 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++) for(unsigned int i = 0; i < MAX_CHARS; i++)
{ {
widths[i] = 0; glyphs[i] = new Glyph(data->getGlyphData(i));
spacing[i] = 0; 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() Font::~Font()
{ {
delete[] glyphs;
} }
float Font::getHeight() const 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 float Font::getWidth(const std::string & line) const
+21 -18
View File
@@ -26,7 +26,8 @@
// LOVE // LOVE
#include <common/Object.h> #include <common/Object.h>
#include <graphics/Volatile.h> #include <font/FontData.h>
#include "Glyph.h"
namespace love namespace love
{ {
@@ -34,33 +35,35 @@ namespace graphics
{ {
namespace opengl namespace opengl
{ {
class Font : public Object, public Volatile class Font : public Object
{ {
protected: private:
int size; int height;
float lineHeight; float lineHeight;
float mSpacing; // modifies the spacing by multiplying it with this value float mSpacing; // modifies the spacing by multiplying it with this value
Glyph ** glyphs;
public: public:
static const unsigned int MAX_CHARS = 256; static const unsigned int MAX_CHARS = 256;
// The widths of each character. // The widths of each character.
int widths[MAX_CHARS]; int widths[MAX_CHARS];
// The spacing of each character.
int spacing[MAX_CHARS]; 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. * Default constructor.
* *
* @param file The file containing the OpenGLFont data. * @param data The font data to construct from.
* @param size The size of the OpenGLFont.
**/ **/
Font(int size); Font(love::font::FontData * data);
virtual ~Font(); virtual ~Font();
virtual bool load() = 0;
virtual void unload() = 0;
/** /**
* Prints the text at the designated position. * Prints the text at the designated position.
* *
@@ -68,7 +71,7 @@ namespace opengl
* @param x The x-coordinate. * @param x The x-coordinate.
* @param y The y-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. * Prints the text at the designated position with rotation and scaling.
@@ -78,7 +81,7 @@ namespace opengl
* @param y The y-coordinate. * @param y The y-coordinate.
* @param angle The amount of rotation. * @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. * Prints the character at the designated position.
@@ -87,27 +90,27 @@ namespace opengl
* @param x The x-coordinate. * @param x The x-coordinate.
* @param y The y-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. * Returns the height of the font.
**/ **/
virtual float getHeight() const; float getHeight() const;
/** /**
* Returns the width of the passed string. * Returns the width of the passed string.
* *
* @param line A line of text. * @param line A line of text.
**/ **/
virtual float getWidth(const std::string & line) const; float getWidth(const std::string & line) const;
virtual float getWidth(const char * line) const; float getWidth(const char * line) const;
/** /**
* Returns the width of the passed character. * Returns the width of the passed character.
* *
* @param character A 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, * 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. * Returns the line height.
**/ **/
virtual float getLineHeight() const; float getLineHeight() const;
/** /**
* Sets the spacing modifier (changes the spacing between the characters the * Sets the spacing modifier (changes the spacing between the characters the
+14 -2
View File
@@ -34,7 +34,6 @@ namespace opengl
: data(data), width((float)data->getWidth()), height((float)data->getHeight()), texture(0) : data(data), width((float)data->getWidth()), height((float)data->getHeight()), texture(0)
{ {
data->retain(); data->retain();
data->getWidth();
memset(vertices, 255, sizeof(vertex)*4); memset(vertices, 255, sizeof(vertex)*4);
@@ -94,6 +93,9 @@ namespace opengl
bool Glyph::loadVolatile() bool Glyph::loadVolatile()
{ {
GLint format = GL_RGBA;
if (data->getFormat() == love::font::GlyphData::FORMAT_LUMINOSITY_ALPHA) format = GL_LUMINANCE_ALPHA;
glGenTextures(1,&texture); glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -108,7 +110,7 @@ namespace opengl
(GLsizei)width, (GLsizei)width,
(GLsizei)height, (GLsizei)height,
0, 0,
GL_RGBA, format,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
data->getData()); data->getData());
@@ -125,6 +127,16 @@ namespace opengl
} }
} }
float Glyph::getWidth() const
{
return width;
}
float Glyph::getHeight() const
{
return height;
}
} // opengl } // opengl
} // graphics } // graphics
} // love } // love
+3
View File
@@ -64,6 +64,9 @@ namespace opengl
bool loadVolatile(); 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; void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
}; // Glyph }; // Glyph
+3 -26
View File
@@ -410,26 +410,12 @@ namespace opengl
return new Quad(v, sw, sh); 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. // Load it and check for errors.
if(!font->load()) if(!font)
{
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())
{ {
delete font; delete font;
return 0; return 0;
@@ -497,15 +483,6 @@ namespace opengl
currentFont->retain(); currentFont->retain();
} }
void Graphics::setFont( Data * data, int size )
{
if(currentFont != 0)
currentFont->release();
currentFont = new TrueTypeFont(data, size);
currentFont->load();
}
Font * Graphics::getFont() Font * Graphics::getFont()
{ {
return currentFont; return currentFont;
+4 -18
View File
@@ -33,12 +33,13 @@
// LOVE // LOVE
#include <graphics/Graphics.h> #include <graphics/Graphics.h>
#include <font/FontData.h>
#include <image/Image.h> #include <image/Image.h>
#include <image/ImageData.h> #include <image/ImageData.h>
#include "Font.h"
#include "Image.h" #include "Image.h"
#include "TrueTypeFont.h"
#include "ImageFont.h"
#include "Quad.h" #include "Quad.h"
#include "SpriteBatch.h" #include "SpriteBatch.h"
#include "ParticleSystem.h" #include "ParticleSystem.h"
@@ -248,12 +249,7 @@ namespace opengl
/** /**
* Creates a Font object. * Creates a Font object.
**/ **/
Font * newFont(Data * data, int size); Font * newFont(love::font::FontData * data);
/**
* Creates an ImageFont object.
**/
Font * newImageFont(Image * image, const char * glyphs, float spacing = 1);
SpriteBatch * newSpriteBatch(Image * image, int size, int usage); SpriteBatch * newSpriteBatch(Image * image, int size, int usage);
@@ -285,16 +281,6 @@ namespace opengl
* @parm font A Font object. * @parm font A Font object.
**/ **/
void setFont(Font * font); 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. * Gets the current Font, or nil if none.
**/ **/
+1 -1
View File
@@ -160,7 +160,7 @@ namespace opengl
glPushMatrix(); glPushMatrix();
glTranslatef(ceil(x), ceil(y), 0.0f); 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); glScalef(sx, sy, 1.0f);
GLuint TrueTypeFont = list; GLuint TrueTypeFont = list;
+74 -54
View File
@@ -21,6 +21,8 @@
#include "wrap_Graphics.h" #include "wrap_Graphics.h"
#include <image/ImageData.h> #include <image/ImageData.h>
#include <font/Rasterizer.h>
#include <font/FontData.h>
#include <scripts/graphics.lua.h> #include <scripts/graphics.lua.h>
@@ -197,42 +199,37 @@ namespace opengl
int w_newFont1(lua_State * L) int w_newFont1(lua_State * L)
{ {
Data * d = 0;
// Convert to File, if necessary. // Convert to File, if necessary.
if(lua_isstring(L, 1)) if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile"); luax_convobj(L, 1, "filesystem", "newFile");
if(luax_istype(L, 1, FILESYSTEM_FILE_T)) // Convert to Data, if necessary.
{ if(luax_istype(L, 1, FILESYSTEM_FILE_T)) {
// Check the value. love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); Data * d = f->read();
try { lua_pop(L, 1); // get rid of the file
d = file->read(); luax_newtype(L, "Data", DATA_T, (void*)d);
} catch (Exception & e) {
return luaL_error(L, e.what());
}
}
else if(luax_istype(L, 1, DATA_T))
{
d = luax_checktype<Data>(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");
} }
// Second optional parameter can be a number: // Convert to Rasterizer, if necessary.
int size = luaL_optint(L, 2, 12); if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) {
int idxs[] = {1, 2};
Font * font; luax_convobj(L, idxs, 2, "font", "newRasterizer");
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<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T);
// Create the font.
Font * font = instance->newFont(data);
if(font == 0) 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); luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
return 1; return 1;
@@ -240,24 +237,29 @@ namespace opengl
int w_newImageFont(lua_State * L) int w_newImageFont(lua_State * L)
{ {
// Convert to File, if necessary. // Convert to ImageData if necessary.
if(lua_isstring(L, 1)) if(lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, DATA_T))
luax_convobj(L, 1, "filesystem", "newFile"); luax_convobj(L, 1, "image", "newImageData");
// Convert to Image, if necessary. // Convert to Rasterizer if necessary.
if(luax_istype(L, 1, FILESYSTEM_FILE_T)) if(luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) {
luax_convobj(L, 1, "graphics", "newImage"); int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newRasterizer");
}
// Check the value. // Convert to FontData, if necessary.
Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T); if(luax_istype(L, 1, FONT_RASTERIZER_T))
luax_convobj(L, 1, "font", "newFontData");
const char * glyphs = luaL_checkstring(L, 2); love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T);
Font * font = instance->newImageFont(image, glyphs); // Create the font.
Font * font = instance->newFont(data);
if(font == 0) 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); luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
return 1; return 1;
@@ -329,25 +331,43 @@ namespace opengl
// The second parameter is an optional int. // The second parameter is an optional int.
int size = luaL_optint(L, 2, 12); int size = luaL_optint(L, 2, 12);
// If the first parameter is a string, convert it to a file. Font * font;
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
// If the first parameter is a File, use another setFont function. // If the first parameter isn't a Font, create a new one
if(luax_istype(L, 1, FILESYSTEM_FILE_T)) if (!luax_istype(L, 1, GRAPHICS_FONT_T)) {
{ lua_pushinteger(L, size); // push the size
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); lua_insert(L, 2); // move it to its proper place
instance->setFont(file->read(), size); // Convert to File, if necessary.
return 0; if(lua_isstring(L, 1))
} luax_convobj(L, 1, "filesystem", "newFile");
else if(luax_istype(L, 1, DATA_T))
{
Data * data = luax_checktype<Data>(L, 1, "Data", DATA_T);
instance->setFont(data, size);
return 0;
}
Font * font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T); // Convert to Data, if necessary.
if(luax_istype(L, 1, FILESYSTEM_FILE_T)) {
love::filesystem::File * f = luax_checktype<love::filesystem::File>(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<love::font::FontData>(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 font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
instance->setFont(font); instance->setFont(font);
return 0; return 0;
} }