Font: rename Driver stuff to Engine and move subdirectory
This commit is contained in:
parent
8b96338e24
commit
5c4eea4ae1
11 changed files with 57 additions and 57 deletions
16
source/Graphics/Font/Engine/FontEngine.cpp
Normal file
16
source/Graphics/Font/Engine/FontEngine.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
#include "FontEngine.h"
|
||||
|
||||
namespace sp {
|
||||
|
||||
FontEngine::FontEngine() :
|
||||
m_hinting (true)
|
||||
{
|
||||
}
|
||||
|
||||
void FontEngine::setHinting(bool value)
|
||||
{
|
||||
m_hinting = value;
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
35
source/Graphics/Font/Engine/FontEngine.h
Normal file
35
source/Graphics/Font/Engine/FontEngine.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
|
||||
#ifndef SPECTRE_GRAPHICS_FONT_ENGINE_FONTENGINE_H
|
||||
#define SPECTRE_GRAPHICS_FONT_ENGINE_FONTENGINE_H
|
||||
|
||||
#include <string>
|
||||
#include <Spectre/Graphics/Image.h>
|
||||
#include <Spectre/Graphics/Font.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
class FontEngine
|
||||
{
|
||||
public :
|
||||
|
||||
FontEngine();
|
||||
|
||||
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, unsigned int outlineSize = 0) = 0;
|
||||
|
||||
virtual std::string getName() = 0;
|
||||
|
||||
protected :
|
||||
|
||||
// True if hinting is enabled. false otherwise.
|
||||
bool m_hinting;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
#endif /* SPECTRE_GRAPHICS_FONT_ENGINE_FONTENGINE_H */
|
||||
171
source/Graphics/Font/Engine/FreeTypeEngine.cpp
Normal file
171
source/Graphics/Font/Engine/FreeTypeEngine.cpp
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
|
||||
#include <ft2build.h>
|
||||
|
||||
#include FT_MODULE_H
|
||||
#include FT_LCD_FILTER_H
|
||||
#include FT_STROKER_H
|
||||
|
||||
#include <Spectre/System/Log.h>
|
||||
#include "FreeTypeError.h"
|
||||
#include "FreeTypeEngine.h"
|
||||
|
||||
namespace sp {
|
||||
|
||||
class LibWrapper
|
||||
{
|
||||
public :
|
||||
static LibWrapper& getInstance()
|
||||
{
|
||||
static LibWrapper _inst;
|
||||
return _inst;
|
||||
};
|
||||
|
||||
// Do not implement.
|
||||
LibWrapper(const LibWrapper&);
|
||||
void operator=(const LibWrapper&);
|
||||
|
||||
FT_Library handle;
|
||||
|
||||
FT_Stroker stroker;
|
||||
|
||||
private :
|
||||
|
||||
LibWrapper();
|
||||
~LibWrapper();
|
||||
};
|
||||
|
||||
LibWrapper::LibWrapper()
|
||||
{
|
||||
FT_Error error = FT_Init_FreeType(&handle);
|
||||
if (error) {
|
||||
Log::error("Could not initialize FreeType");
|
||||
return;
|
||||
}
|
||||
|
||||
Log::info("FreeType font driver was initialized.");
|
||||
error = FT_Stroker_New(handle, &stroker);
|
||||
}
|
||||
|
||||
LibWrapper::~LibWrapper()
|
||||
{
|
||||
FT_Error error;
|
||||
|
||||
FT_Stroker_Done(stroker);
|
||||
|
||||
error = FT_Done_FreeType(handle);
|
||||
if (error) {
|
||||
Log::error("Could not close FreeType");
|
||||
}
|
||||
}
|
||||
|
||||
FreeTypeEngine::FreeTypeEngine() :
|
||||
m_face (NULL)
|
||||
{
|
||||
}
|
||||
|
||||
FreeTypeEngine::~FreeTypeEngine()
|
||||
{
|
||||
if (m_face) {
|
||||
FT_Done_Face(m_face);
|
||||
}
|
||||
}
|
||||
|
||||
bool FreeTypeEngine::setCharacterSize(unsigned int size)
|
||||
{
|
||||
FT_Error error = FT_Set_Pixel_Sizes(m_face, 0, size);
|
||||
if (error) {
|
||||
Log::warn("FreeType: failed to set character size");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FreeTypeEngine::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::warn("FreeType: could not load file (%s): %s",
|
||||
filename.c_str(), FT_GetErrorString(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
error = FT_Select_Charmap(face, FT_ENCODING_UNICODE);
|
||||
if (error) {
|
||||
Log::warn("FreeType: (%s) failed to set unicode charmap", filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_face = face;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Font::Glyph FreeTypeEngine::loadGlyph(unsigned int codepoint, Image& img, unsigned int outlineSize)
|
||||
{
|
||||
Font::Glyph glyph;
|
||||
FT_Glyph glyph_info;
|
||||
FT_Int32 flags = FT_LOAD_TARGET_NORMAL;
|
||||
|
||||
if (outlineSize > 0) {
|
||||
flags |= FT_LOAD_NO_BITMAP;
|
||||
}
|
||||
|
||||
if (m_hinting) {
|
||||
flags |= FT_LOAD_FORCE_AUTOHINT;
|
||||
} else {
|
||||
flags |= FT_LOAD_NO_AUTOHINT;
|
||||
}
|
||||
|
||||
if (FT_Load_Char(m_face, codepoint, flags) != 0) {
|
||||
Log::warn("FreeType: failed to load glyph for character code '%c'", codepoint);
|
||||
}
|
||||
|
||||
FT_Error error = FT_Get_Glyph(m_face->glyph, &glyph_info);
|
||||
if (!error) {
|
||||
|
||||
FT_Glyph_Metrics& metrics = m_face->glyph->metrics;
|
||||
|
||||
if (outlineSize) {
|
||||
FT_Stroker stroker = LibWrapper::getInstance().stroker;
|
||||
|
||||
FT_Stroker_Set(stroker, outlineSize * 64,
|
||||
FT_STROKER_LINECAP_ROUND,
|
||||
FT_STROKER_LINEJOIN_ROUND,
|
||||
0);
|
||||
|
||||
FT_Glyph_Stroke(&glyph_info, stroker, false);
|
||||
}
|
||||
|
||||
if (metrics.width > 0 && metrics.height > 0) {
|
||||
|
||||
FT_Glyph_To_Bitmap(&glyph_info, FT_RENDER_MODE_NORMAL, 0, true);
|
||||
FT_Bitmap& bmp = reinterpret_cast<FT_BitmapGlyph>(glyph_info)->bitmap;
|
||||
|
||||
img.create(PixelFormat::PF_Alpha,
|
||||
bmp.width,
|
||||
bmp.rows,
|
||||
bmp.buffer);
|
||||
|
||||
glyph.offset.x = static_cast<unsigned char>(metrics.horiBearingX / (1 << 6));
|
||||
glyph.offset.y = static_cast<unsigned char>(metrics.horiBearingY / (1 << 6));
|
||||
glyph.size.x = static_cast<unsigned char>(metrics.width / (1 << 6)) + (outlineSize * 2);
|
||||
glyph.size.y = static_cast<unsigned char>(metrics.height / (1 << 6)) + (outlineSize * 2);
|
||||
}
|
||||
|
||||
glyph.advance = static_cast<unsigned char>(metrics.horiAdvance >> 6) + (outlineSize * 2);
|
||||
} else {
|
||||
Log::warn("FreeType: failed to load glyph for character code '%c'", codepoint);
|
||||
}
|
||||
|
||||
return glyph;
|
||||
}
|
||||
|
||||
std::string FreeTypeEngine::getName()
|
||||
{
|
||||
return m_face->family_name;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
34
source/Graphics/Font/Engine/FreeTypeEngine.h
Normal file
34
source/Graphics/Font/Engine/FreeTypeEngine.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
|
||||
#ifndef SPECTRE_GRAPHICS_FONT_ENGINE_FREETYPEENGINE_H
|
||||
#define SPECTRE_GRAPHICS_FONT_ENGINE_FREETYPEENGINE_H
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
#include <string>
|
||||
#include "FontEngine.h"
|
||||
|
||||
namespace sp {
|
||||
|
||||
class FreeTypeEngine : public FontEngine
|
||||
{
|
||||
public:
|
||||
FreeTypeEngine();
|
||||
~FreeTypeEngine();
|
||||
|
||||
virtual bool setCharacterSize(unsigned int size);
|
||||
|
||||
virtual bool loadFromFile(const std::string& filename);
|
||||
|
||||
virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img, unsigned int outlineSize = 0);
|
||||
|
||||
virtual std::string getName();
|
||||
|
||||
private :
|
||||
|
||||
FT_Face m_face;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
#endif /* SPECTRE_GRAPHICS_FONT_ENGINE_FREETYPEENGINE_H */
|
||||
28
source/Graphics/Font/Engine/FreeTypeError.cpp
Normal file
28
source/Graphics/Font/Engine/FreeTypeError.cpp
Normal 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";
|
||||
}
|
||||
10
source/Graphics/Font/Engine/FreeTypeError.h
Normal file
10
source/Graphics/Font/Engine/FreeTypeError.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
#ifndef SPECTRE_GRAPHICS_FONT_ENGINE_FREETYPE_ERROR_H
|
||||
#define SPECTRE_GRAPHICS_FONT_ENGINE_FREETYPE_ERROR_H
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
const char* FT_GetErrorString(FT_Error error);
|
||||
|
||||
#endif /* SPECTRE_GRAPHICS_FONT_ENGINE_FREETYPE_ERROR_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue