Added basic (unfinished and untested) font module.

This commit is contained in:
michaee
2009-07-28 20:22:48 +02:00
parent 121ed82bf7
commit 74190255ed
13 changed files with 905 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2006-2009 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"
namespace love
{
namespace font
{
Font::~Font()
{
}
const char * Font::getName() const
{
return "love.font";
}
} // font
} // love
+55
View File
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2006-2009 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 <common/Module.h>
#include <filesystem/File.h>
#include "Rasterizer.h"
namespace love
{
namespace font
{
/**
* The Font module is responsible for decoding font data. It is
* not responsible for drawing it.
**/
class Font : public Module
{
public:
/**
* Destructor.
**/
virtual ~Font();
// Implement Module
const char * getName() const;
}; // Font
} // font
} // love
#endif // LOVE_FONT_FONT_H
+94
View File
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2006-2009 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 "GlyphData.h"
namespace love
{
namespace font
{
GlyphData::GlyphData(wchar_t glyph, unsigned char * glyphData, GlyphMetrics glyphMetrics, int glyphBPP)
: glyph(glyph), data(glyphData), metrics(glyphMetrics), bpp(glyphBPP)
{
}
GlyphData::~GlyphData()
{
delete[] data;
}
void * GlyphData::getData() const
{
return (void *) data;
}
int GlyphData::getSize() const
{
return getWidth() * getHeight() * bpp;
}
int GlyphData::getHeight() const
{
return metrics.height;
}
int GlyphData::getWidth() const
{
return metrics.width;
}
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();
}
} // font
} // love
+119
View File
@@ -0,0 +1,119 @@
/**
* Copyright (c) 2006-2009 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/Data.h>
namespace love
{
namespace font
{
/**
* Holds the specific glyph data.
**/
struct GlyphMetrics
{
int height;
int width;
int advance;
int bearingX;
int bearingY;
};
/**
* Holds data for a specic glyph object.
**/
class GlyphData : public Data
{
private:
// The glyph itself
wchar_t glyph;
// Glyph metrics
GlyphMetrics metrics;
// Glyph texture data
unsigned char * data;
// Bits Per Pixel
int bpp;
public:
GlyphData(wchar_t glyph, unsigned char * glyphData, GlyphMetrics glyphMetrics, int glyphBPP);
virtual ~GlyphData();
// Implements Data.
void * getData() const;
int getSize() const;
/**
* Gets the height of the glyph.
**/
int getHeight() const;
/**
* Gets the width of the glyph.
**/
int getWidth() 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;
}; // GlyphData
} // font
} // love
#endif // LOVE_FONT_GLYPH_DATA_H
+52
View File
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2006-2009 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>
namespace love
{
namespace font
{
ImageRasterizer::ImageRasterizer(love::filesystem::File * file, wchar_t * glyphs)
: glyphs(glyphs)
{
//imageData = new ImageData(file);
}
ImageRasterizer::~ImageRasterizer()
{
delete imageData;
}
int ImageRasterizer::getLineHeight() const
{
return getHeight();
}
GlyphData * ImageRasterizer::getGlyphData(const wchar_t glyph) const
{
return 0;
}
} // font
} // love
+58
View File
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2006-2009 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>
namespace love
{
namespace font
{
/**
* Holds data for a font object.
**/
class ImageRasterizer : public Rasterizer
{
private:
// The image data
love::image::ImageData * imageData;
// Glyphs
wchar_t * glyphs;
public:
ImageRasterizer(love::filesystem::File * file, wchar_t * glyphs);
virtual ~ImageRasterizer();
// Implement FontData
virtual int getLineHeight() const;
virtual GlyphData * getGlyphData(const wchar_t glyph) const;
}; // ImageRasterizer
} // font
} // love
#endif // LOVE_FONT_IMAGE_RASTERIZER_H
+50
View File
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2006-2009 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"
namespace love
{
namespace font
{
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;
}
} // font
} // love
+89
View File
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2006-2009 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 "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
{
protected:
FontMetrics metrics;
public:
/**
* 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 to get data for
**/
virtual GlyphData * getGlyphData(const wchar_t glyph) const = 0;
}; // FontData
} // font
} // love
#endif // LOVE_FONT_RASTERIZER_H
@@ -0,0 +1,118 @@
/**
* Copyright (c) 2006-2009 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 "FreeTypeRasterizer.h"
#include <common/Exception.h>
namespace love
{
namespace font
{
namespace freetype
{
inline int next_p2(int num)
{
int powered = 2;
while(powered < num) powered <<= 1;
return powered;
}
FreeTypeRasterizer::FreeTypeRasterizer(love::filesystem::File * file, int size)
{
// Initialize
data = file->read();
if(FT_Init_FreeType(&library))
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed\n");
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
metrics.advance = face->max_advance_width;
metrics.ascent = face->ascender;
metrics.descent = face->descender;
metrics.height = face->height;
}
FreeTypeRasterizer::~FreeTypeRasterizer()
{
FT_Done_Face(face);
FT_Done_FreeType(library);
data->release();
}
int FreeTypeRasterizer::getLineHeight() const
{
return (int)(getHeight() * 1.25);
}
GlyphData * FreeTypeRasterizer::getGlyphData(const wchar_t glyph) const
{
unsigned char * textureData;
love::font::GlyphMetrics glyphMetrics;
int bpp = 2;
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;
glyphMetrics.bearingY = face->glyph->metrics.horiBearingY;
glyphMetrics.height = face->glyph->metrics.height;
glyphMetrics.width = face->glyph->metrics.width;
glyphMetrics.advance = face->glyph->advance.x >> 6;
// Get texture
int w = next_p2(bitmap.width);
int h = next_p2(bitmap.rows);
textureData = new unsigned char[bpp * w * h];
for(int j = 0; j < h; j++) for(int i = 0; i < w; i++)
{
textureData[bpp * (i + j * w)] = 255;
textureData[bpp * (i + j * w) + 1] = (i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width * j];
}
// Return data
GlyphData * glyphData = new GlyphData(glyph, textureData, glyphMetrics, bpp);
return glyphData;
}
} // freetype
} // font
} // love
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2006-2009 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_FREE_TYPE_RASTERIZER_H
#define LOVE_FONT_FREE_TYPE_RASTERIZER_H
// LOVE
#include <filesystem/File.h>
#include <font/Rasterizer.h>
// FreeType2
#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 FreeTypeRasterizer : public Rasterizer
{
private:
// FreeType library
FT_Library library;
// FreeType face
FT_Face face;
// File data
Data * data;
public:
FreeTypeRasterizer(love::filesystem::File * file, int size);
virtual ~FreeTypeRasterizer();
// Implement FontData
virtual int getLineHeight() const;
virtual GlyphData * getGlyphData(const wchar_t glyph) const;
}; // FreetypeRasterizer
} // freetype
} // font
} // love
#endif // LOVE_FONT_FREE_TYPE_RASTERIZER_H
+67
View File
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2006-2009 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"
namespace love
{
namespace font
{
static Font * instance = 0;
int _wrap_test(lua_State * L)
{
lua_pushinteger(L, 696);
return 1;
}
// List of functions to wrap.
static const luaL_Reg wrap_Font_functions[] = {
{ "test", _wrap_test },
{ 0, 0 }
};
static const lua_CFunction wrap_Font_types[] = {
_wrap_test,
0
};
int wrap_Font_open(lua_State * L)
{
if(instance == 0)
{
try
{
instance = new Font();
}
catch(Exception & e)
{
return luaL_error(L, e.what());
}
}
luax_register_gc(L, "love.font", instance);
return luax_register_module(L, wrap_Font_functions, wrap_Font_types);
}
} // sound
} // love
+38
View File
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2006-2009 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_FONT_H
#define LOVE_FONT_WRAP_FONT_H
// LOVE
#include "Font.h"
#include "wrap_Font.h"
namespace love
{
namespace font
{
int _wrap_test(lua_State * L);
int wrap_Font_open(lua_State * L);
} // font
} // love
#endif // LOVE_SOUND_WRAP_FONT_H