[freetype] Correct error handling

This commit is contained in:
Alex Zolotarev 2013-11-26 13:57:40 +03:00 committed by Alex Zolotarev
parent 69a9e988af
commit 0de2a8c5f1
7 changed files with 65 additions and 83 deletions

8
graphics/freetype.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "freetype.hpp"
#undef __FTERRORS_H__
#define FT_ERRORDEF(e, v, s) {e, s},
#define FT_ERROR_START_LIST {
#define FT_ERROR_END_LIST {0, 0}};
FreetypeError g_FT_Errors[] =
#include FT_ERRORS_H

38
graphics/freetype.hpp Normal file
View file

@ -0,0 +1,38 @@
#pragma once
#include "../base/logging.hpp"
// Put all needed FT includes in one place
#include <ft2build.h>
#include FT_TYPES_H
#include FT_SYSTEM_H
#include FT_FREETYPE_H
#include FT_STROKER_H
#include FT_CACHE_H
struct FreetypeError
{
int m_code;
char const * m_message;
};
extern FreetypeError g_FT_Errors[];
#define FREETYPE_CHECK(x) \
do \
{ \
FT_Error const err = (x); \
if (err) \
LOG(LWARNING, ("Freetype:", g_FT_Errors[err].m_code, g_FT_Errors[err].m_message)); \
} while (false)
#define FREETYPE_CHECK_RETURN(x, msg) \
do \
{ \
FT_Error const err = (x); \
if (err) \
{ \
LOG(LWARNING, ("Freetype", g_FT_Errors[err].m_code, g_FT_Errors[err].m_message, msg)); \
return; \
} \
} while (false)

View file

@ -1,42 +0,0 @@
#include "../base/SRC_FIRST.hpp"
#include "ft2_debug.hpp"
#undef __FTERRORS_H__
#define FT_ERRORDEF( e, v, s ) { e, s },
#define FT_ERROR_START_LIST {
#define FT_ERROR_END_LIST { 0, 0 } };
const struct
{
int err_code;
const char* err_msg;
} ft_errors[] =
#include FT_ERRORS_H
namespace ft2_impl
{
char const * FT_Error_Description(FT_Error error)
{
int i = 1;
while (ft_errors[i].err_code != 0)
{
if (ft_errors[i].err_code == error)
break;
else
++i;
}
return ft_errors[i].err_msg;
}
void CheckError(FT_Error error, char const * msg)
{
if (error != 0)
{
if (msg == 0) msg = "";
LOG(LWARNING, ("FT_Error:", FT_Error_Description(error), msg));
}
}
}

View file

@ -1,22 +0,0 @@
#pragma once
#include <ft2build.h>
#include <freetype/ftgzip.h>
#include <freetype/ftcache.h>
#include "../base/logging.hpp"
#include FT_FREETYPE_H
#include FT_STROKER_H
#include FT_GLYPH_H
namespace ft2_impl
{
void CheckError(FT_Error error, char const * msg = 0);
}
#define FTCHECK(x) do { FT_Error e = (x); ft2_impl::CheckError(e); } while (false)
#define FTCHECKRETURN(x, msg) \
do { FT_Error e = (x); \
if (e != 0) { ft2_impl::CheckError(e, msg); return; } } \
while (false)

View file

@ -1,4 +1,5 @@
#include "glyph_cache_impl.hpp"
#include "freetype.hpp"
#include "../platform/platform.hpp"
@ -248,7 +249,7 @@ namespace graphics
// from routine, so add font to fonts array only in the end.
FT_Face face;
FTCHECKRETURN(pFont->CreateFaceID(m_lib, &face), fileName);
FREETYPE_CHECK_RETURN(pFont->CreateFaceID(m_lib, &face), fileName);
vector<FT_ULong> charcodes;
@ -260,7 +261,7 @@ namespace graphics
sort(charcodes.begin(), charcodes.end());
charcodes.erase(unique(charcodes.begin(), charcodes.end()), charcodes.end());
FTCHECKRETURN(FT_Done_Face(face), fileName);
FREETYPE_CHECK_RETURN(FT_Done_Face(face), fileName);
m_fonts.push_back(pFont);
@ -419,22 +420,22 @@ namespace graphics
if (!m_isDebugging)
{
FTCHECK(FT_Init_FreeType(&m_lib));
FREETYPE_CHECK(FT_Init_FreeType(&m_lib));
/// Initializing caches
FTCHECK(FTC_Manager_New(m_lib, 3, 10, params.m_maxSize, &RequestFace, 0, &m_manager));
FREETYPE_CHECK(FTC_Manager_New(m_lib, 3, 10, params.m_maxSize, &RequestFace, 0, &m_manager));
FTCHECK(FTC_ImageCache_New(m_manager, &m_normalGlyphCache));
FTCHECK(FTC_StrokedImageCache_New(m_manager, &m_strokedGlyphCache));
FREETYPE_CHECK(FTC_ImageCache_New(m_manager, &m_normalGlyphCache));
FREETYPE_CHECK(FTC_StrokedImageCache_New(m_manager, &m_strokedGlyphCache));
FTCHECK(FTC_ImageCache_New(m_manager, &m_normalMetricsCache));
FTCHECK(FTC_StrokedImageCache_New(m_manager, &m_strokedMetricsCache));
FREETYPE_CHECK(FTC_ImageCache_New(m_manager, &m_normalMetricsCache));
FREETYPE_CHECK(FTC_StrokedImageCache_New(m_manager, &m_strokedMetricsCache));
/// Initializing stroker
FTCHECK(FT_Stroker_New(m_lib, &m_stroker));
FREETYPE_CHECK(FT_Stroker_New(m_lib, &m_stroker));
FT_Stroker_Set(m_stroker, FT_Fixed(graphics::visualScale(params.m_density) * 2 * 64), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
FTCHECK(FTC_CMapCache_New(m_manager, &m_charMapCache));
FREETYPE_CHECK(FTC_CMapCache_New(m_manager, &m_charMapCache));
}
else
{
@ -527,7 +528,7 @@ namespace graphics
if (key.m_isMask)
{
FTCHECK(FTC_StrokedImageCache_LookupScaler(
FREETYPE_CHECK(FTC_StrokedImageCache_LookupScaler(
m_strokedMetricsCache,
&fontScaler,
m_stroker,
@ -538,7 +539,7 @@ namespace graphics
}
else
{
FTCHECK(FTC_ImageCache_LookupScaler(
FREETYPE_CHECK(FTC_ImageCache_LookupScaler(
m_normalMetricsCache,
&fontScaler,
FT_LOAD_DEFAULT,
@ -580,7 +581,7 @@ namespace graphics
if (key.m_isMask)
{
FTCHECK(FTC_StrokedImageCache_LookupScaler(
FREETYPE_CHECK(FTC_StrokedImageCache_LookupScaler(
m_strokedGlyphCache,
&fontScaler,
m_stroker,
@ -592,7 +593,7 @@ namespace graphics
}
else
{
FTCHECK(FTC_ImageCache_LookupScaler(
FREETYPE_CHECK(FTC_ImageCache_LookupScaler(
m_normalGlyphCache,
&fontScaler,
FT_LOAD_DEFAULT | FT_LOAD_RENDER,
@ -610,7 +611,7 @@ namespace graphics
bitmap->m_height = bitmapGlyph ? bitmapGlyph->bitmap.rows : 0;
bitmap->m_pitch = bitmapGlyph ? bitmapGlyph->bitmap.pitch : 0;
if (bitmap->m_width * bitmap->m_height != 0)
if (bitmap->m_width && bitmap->m_height)
{
bitmap->m_data.resize(bitmap->m_pitch * bitmap->m_height);
memcpy(&bitmap->m_data[0],

View file

@ -1,7 +1,7 @@
#pragma once
#include "glyph_cache.hpp"
#include "ft2_debug.hpp"
#include "freetype.hpp"
#include "../base/string_utils.hpp"
@ -11,7 +11,6 @@
#include "../std/vector.hpp"
#include "../std/shared_ptr.hpp"
#include <freetype/ftsystem.h>
namespace graphics
{

View file

@ -39,8 +39,8 @@ SOURCES += \
skin_loader.cpp \
resource_cache.cpp \
glyph_cache.cpp \
freetype.cpp \
glyph_cache_impl.cpp \
ft2_debug.cpp \
geometry_batcher.cpp \
text_renderer.cpp \
path_renderer.cpp \
@ -108,7 +108,7 @@ HEADERS += \
glyph_cache.hpp \
data_formats.hpp \
glyph_cache_impl.hpp \
ft2_debug.hpp \
freetype.hpp \
text_renderer.hpp \
geometry_batcher.hpp \
screen.hpp \