1
0
Fork 0

source/Graphics/Font/FontDriver.h: Add support for outlines.

This commit is contained in:
Henrik Hautakoski 2016-04-17 22:18:24 +02:00
parent 308e0dff6b
commit 6306c1e647
3 changed files with 42 additions and 10 deletions

View file

@ -18,7 +18,7 @@ public :
virtual bool loadFromFile(const std::string& filename) = 0; virtual bool loadFromFile(const std::string& filename) = 0;
virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img) = 0; virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img, unsigned int outlineSize = 0) = 0;
virtual std::string getName() = 0; virtual std::string getName() = 0;

View file

@ -3,6 +3,7 @@
#include FT_MODULE_H #include FT_MODULE_H
#include FT_LCD_FILTER_H #include FT_LCD_FILTER_H
#include FT_STROKER_H
#include <Spectre/System/Log.h> #include <Spectre/System/Log.h>
#include "FreeTypeError.h" #include "FreeTypeError.h"
@ -23,6 +24,8 @@ public :
FT_Library handle; FT_Library handle;
FT_Stroker stroker;
private : private :
LibWrapper(); LibWrapper();
@ -34,14 +37,21 @@ LibWrapper::LibWrapper()
FT_Error error = FT_Init_FreeType(&handle); FT_Error error = FT_Init_FreeType(&handle);
if (error) { if (error) {
log("Could not initialize FreeType\n"); log("Could not initialize FreeType\n");
return;
} else { } else {
log("FreeType font driver was initialized.\n"); log("FreeType font driver was initialized.\n");
} }
error = FT_Stroker_New(handle, &stroker);
} }
LibWrapper::~LibWrapper() LibWrapper::~LibWrapper()
{ {
FT_Error error = FT_Done_FreeType(handle); FT_Error error;
FT_Stroker_Done(stroker);
error = FT_Done_FreeType(handle);
if (error) { if (error) {
log("Could not close FreeType\n"); log("Could not close FreeType\n");
} }
@ -92,10 +102,15 @@ bool FreeTypeDriver::loadFromFile(const std::string& filename)
return true; return true;
} }
Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img) Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img, unsigned int outlineSize)
{ {
Font::Glyph glyph; Font::Glyph glyph;
FT_Int32 flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL; FT_Glyph glyph_info;
FT_Int32 flags = FT_LOAD_TARGET_NORMAL;
if (outlineSize > 0) {
flags |= FT_LOAD_NO_BITMAP;
}
if (m_hinting) { if (m_hinting) {
flags |= FT_LOAD_FORCE_AUTOHINT; flags |= FT_LOAD_FORCE_AUTOHINT;
@ -103,13 +118,30 @@ Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img)
flags |= FT_LOAD_NO_AUTOHINT; flags |= FT_LOAD_NO_AUTOHINT;
} }
FT_Error error = FT_Load_Char(m_face, codepoint, flags); if (FT_Load_Char(m_face, codepoint, flags) != 0) {
log("FreeType: failed to load glyph for character code '%c'\n", codepoint);
}
FT_Error error = FT_Get_Glyph(m_face->glyph, &glyph_info);
if (!error) { if (!error) {
FT_Glyph_Metrics& metrics = m_face->glyph->metrics; 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) { if (metrics.width > 0 && metrics.height > 0) {
FT_Bitmap& bmp = m_face->glyph->bitmap; 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, img.create(PixelFormat::PF_Alpha,
bmp.width, bmp.width,
@ -118,8 +150,8 @@ Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img)
glyph.offset.x = metrics.horiBearingX / (1 << 6); glyph.offset.x = metrics.horiBearingX / (1 << 6);
glyph.offset.y = metrics.horiBearingY / (1 << 6); glyph.offset.y = metrics.horiBearingY / (1 << 6);
glyph.size.x = metrics.width / (1 << 6); glyph.size.x = (metrics.width / (1 << 6)) + (outlineSize * 2);
glyph.size.y = metrics.height / (1 << 6); glyph.size.y = (metrics.height / (1 << 6)) + (outlineSize * 2);
} }
glyph.advance = static_cast<unsigned char>(metrics.horiAdvance >> 6); glyph.advance = static_cast<unsigned char>(metrics.horiAdvance >> 6);

View file

@ -17,7 +17,7 @@ public:
virtual bool loadFromFile(const std::string& filename); virtual bool loadFromFile(const std::string& filename);
virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img); virtual Font::Glyph loadGlyph(unsigned int codepoint, Image& img, unsigned int outlineSize = 0);
virtual std::string getName(); virtual std::string getName();