1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2015-12-22 17:43:51 +01:00
commit edfc5298e1
252 changed files with 93965 additions and 0 deletions

View file

@ -0,0 +1,12 @@
#include "FontDriver.h"
FontDriver::FontDriver() :
m_hinting (true)
{
}
void FontDriver::setHinting(bool value)
{
m_hinting = value;
}

View file

@ -0,0 +1,31 @@
#ifndef SPECTRE_GRAPHICS_FONT_FONTDRIVER_H
#define SPECTRE_GRAPHICS_FONT_FONTDRIVER_H
#include <string>
#include <Spectre/Graphics/Image.h>
#include <Spectre/Graphics/Font.h>
class FontDriver
{
public :
FontDriver();
void setHinting(bool value);
virtual bool setCharacterSize(unsigned int size) = 0;
virtual bool loadFromFile(const std::string& filename) = 0;
virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img) = 0;
virtual std::string getName() = 0;
protected :
// True if hinting is enabled. false otherwise.
bool m_hinting;
};
#endif /* SPECTRE_GRAPHICS_FONT_FONTDRIVER_H */

View file

@ -0,0 +1,131 @@
#include <ft2build.h>
#include FT_MODULE_H
#include FT_LCD_FILTER_H
#include <Spectre/System/Log.h>
#include "FreeTypeError.h"
#include "FreeTypeDriver.h"
class LibWrapper
{
public :
static LibWrapper& getInstance()
{
static LibWrapper _inst;
return _inst;
};
// Do not implement.
LibWrapper(const LibWrapper&);
void operator=(const LibWrapper&);
FT_Library handle;
private :
LibWrapper();
~LibWrapper();
};
LibWrapper::LibWrapper()
{
FT_Error error = FT_Init_FreeType(&handle);
if (error) {
log("Could not initialize FreeType\n");
} else {
log("FreeType font driver was initialized.\n");
}
}
LibWrapper::~LibWrapper()
{
FT_Error error = FT_Done_FreeType(handle);
if (error) {
log("Could not close FreeType\n");
}
}
FreeTypeDriver::FreeTypeDriver() :
m_face (NULL)
{
}
FreeTypeDriver::~FreeTypeDriver()
{
if (m_face) {
FT_Done_Face(m_face);
}
}
bool FreeTypeDriver::setCharacterSize(unsigned int size)
{
FT_Error error = FT_Set_Pixel_Sizes(m_face, 0, size);
if (error) {
log("FreeType: failed to set character size\n");
return false;
}
return true;
}
bool FreeTypeDriver::loadFromFile(const std::string& filename)
{
FT_Face face;
FT_Error error;
error = FT_New_Face(LibWrapper::getInstance().handle, filename.c_str(), 0, &face);
if (error) {
log("FreeType: could not load file (%s): %s\n",
filename.c_str(), FT_GetErrorString(error));
return false;
}
error = FT_Select_Charmap(face, FT_ENCODING_UNICODE);
if (error) {
log("FreeType: (%s) failed to set unicode charmap\n", filename.c_str());
return false;
}
m_face = face;
return true;
}
Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img)
{
Font::Glyph glyph;
FT_Int32 flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL;
if (m_hinting) {
flags |= FT_LOAD_FORCE_AUTOHINT;
} else {
flags |= FT_LOAD_NO_AUTOHINT;
}
FT_Error error = FT_Load_Char(m_face, codepoint, flags);
if (!error) {
FT_GlyphSlot slot = m_face->glyph;
if (slot->bitmap.buffer) {
img.create(PixelFormat::PF_Alpha,
slot->bitmap.width,
slot->bitmap.rows,
slot->bitmap.buffer);
}
glyph.offset = vec2b(slot->bitmap_left, slot->bitmap_top);
glyph.size = vec2b(slot->bitmap.width, slot->bitmap.rows);
glyph.advance = static_cast<unsigned char>(slot->advance.x >> 6);
} else {
log("FreeType: failed to load glyph for character code '%c'\n", codepoint);
}
return glyph;
}
std::string FreeTypeDriver::getName()
{
return m_face->family_name;
}

View file

@ -0,0 +1,29 @@
#ifndef SPECTRE_GRAPHICS_FONT_FREETYPEDRIVER_H
#define SPECTRE_GRAPHICS_FONT_FREETYPEDRIVER_H
#include <ft2build.h>
#include FT_FREETYPE_H
#include "FontDriver.h"
class FreeTypeDriver : public FontDriver
{
public:
FreeTypeDriver();
~FreeTypeDriver();
virtual bool setCharacterSize(unsigned int size);
virtual bool loadFromFile(const std::string& filename);
virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img);
virtual std::string getName();
private :
FT_Face m_face;
};
#endif /* SPECTRE_GRAPHICS_FONT_FREETYPEDRIVER_H */

View file

@ -0,0 +1,28 @@
#include "FreeTypeError.h"
#include <ft2build.h>
#undef __FTERRORS_H__
#define FT_ERROR_START_LIST {
#define FT_ERRORDEF(e, v ,s) { e, s },
#define FT_ERROR_END_LIST { 0, NULL } };
const struct error_list {
int code;
const char* msg;
} ft_errors[] =
#include FT_ERRORS_H
const char* FT_GetErrorString(FT_Error error) {
const struct error_list* ptr = ft_errors;
while(ptr->msg) {
if (ptr->code == error)
return ptr->msg;
ptr++;
}
return "Unkown error";
}

View file

@ -0,0 +1,10 @@
#ifndef SPECTRE_GRAPHICS_FONT_FREETYPE_ERROR_H
#define SPECTRE_GRAPHICS_FONT_FREETYPE_ERROR_H
#include <ft2build.h>
#include FT_FREETYPE_H
const char* FT_GetErrorString(FT_Error error);
#endif /* SPECTRE_GRAPHICS_FONT_FREETYPE_ERROR_H */