update font_manager to bgfx coding style

This commit is contained in:
Jeremie Roy 2013-04-23 20:58:57 +02:00
parent 6b8b95acd8
commit 6c956ee0c9
2 changed files with 198 additions and 198 deletions

View file

@ -49,25 +49,25 @@ public:
/// Initialize from an external buffer
/// @remark The ownership of the buffer is external, and you must ensure it stays valid up to this object lifetime
/// @return true if the initialization succeed
bool init(const uint8_t* buffer, uint32_t bufferSize, int32_t fontIndex, uint32_t pixelHeight );
bool init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight );
/// return the font descriptor of the current font
FontInfo getFontInfo();
/// raster a glyph as 8bit alpha to a memory buffer
/// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.width * glyphInfo * height * sizeof(char)
bool bakeGlyphAlpha(CodePoint_t codePoint, GlyphInfo& outGlyphInfo, uint8_t* outBuffer);
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
bool bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
/// raster a glyph as 32bit subpixel rgba to a memory buffer
/// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.width * glyphInfo * height * sizeof(uint32_t)
bool bakeGlyphSubpixel(CodePoint_t codePoint, GlyphInfo& outGlyphInfo, uint8_t* outBuffer);
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(uint32_t)
bool bakeGlyphSubpixel(CodePoint_t _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
/// raster a glyph as 8bit signed distance to a memory buffer
/// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.width * glyphInfo * height * sizeof(char)
bool bakeGlyphDistance(CodePoint_t codePoint, GlyphInfo& outGlyphInfo, uint8_t* outBuffer);
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
bool bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
private:
void* m_font;
};
@ -75,8 +75,8 @@ private:
struct FTHolder
{
FT_Library library;
FT_Face face;
FT_Library m_library;
FT_Face m_face;
};
FontManager::TrueTypeFont::TrueTypeFont(): m_font(NULL)
{
@ -87,36 +87,36 @@ FontManager::TrueTypeFont::~TrueTypeFont()
if(m_font!=NULL)
{
FTHolder* holder = (FTHolder*) m_font;
FT_Done_Face( holder->face );
FT_Done_FreeType( holder->library );
FT_Done_Face( holder->m_face );
FT_Done_FreeType( holder->m_library );
delete m_font;
m_font = NULL;
}
}
bool FontManager::TrueTypeFont::init(const uint8_t* buffer, uint32_t bufferSize, int32_t fontIndex, uint32_t pixelHeight)
bool FontManager::TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight)
{
assert((bufferSize > 256 && bufferSize < 100000000) && "TrueType buffer size is suspicious");
assert((pixelHeight > 4 && pixelHeight < 128) && "TrueType buffer size is suspicious");
assert((_bufferSize > 256 && _bufferSize < 100000000) && "TrueType buffer size is suspicious");
assert((_pixelHeight > 4 && _pixelHeight < 128) && "TrueType buffer size is suspicious");
assert(m_font == NULL && "TrueTypeFont already initialized" );
FTHolder* holder = new FTHolder();
// Initialize Freetype library
FT_Error error = FT_Init_FreeType( &holder->library );
FT_Error error = FT_Init_FreeType( &holder->m_library );
if( error)
{
delete holder;
return false;
}
error = FT_New_Memory_Face( holder->library, buffer, bufferSize, fontIndex, &holder->face );
error = FT_New_Memory_Face( holder->m_library, _buffer, _bufferSize, _fontIndex, &holder->m_face );
if ( error == FT_Err_Unknown_File_Format )
{
// the font file could be opened and read, but it appears
//that its font format is unsupported
FT_Done_FreeType( holder->library );
FT_Done_FreeType( holder->m_library );
delete holder;
return false;
}
@ -124,25 +124,25 @@ bool FontManager::TrueTypeFont::init(const uint8_t* buffer, uint32_t bufferSize,
{
// another error code means that the font file could not
// be opened or read, or simply that it is broken...
FT_Done_FreeType( holder->library );
FT_Done_FreeType( holder->m_library );
delete holder;
return false;
}
// Select unicode charmap
error = FT_Select_Charmap( holder->face, FT_ENCODING_UNICODE );
error = FT_Select_Charmap( holder->m_face, FT_ENCODING_UNICODE );
if( error )
{
FT_Done_Face( holder->face );
FT_Done_FreeType( holder->library );
FT_Done_Face( holder->m_face );
FT_Done_FreeType( holder->m_library );
return false;
}
//set size in pixels
error = FT_Set_Pixel_Sizes( holder->face, 0, pixelHeight );
error = FT_Set_Pixel_Sizes( holder->m_face, 0, _pixelHeight );
if( error )
{
FT_Done_Face( holder->face );
FT_Done_FreeType( holder->library );
FT_Done_Face( holder->m_face );
FT_Done_FreeType( holder->m_library );
return false;
}
@ -155,31 +155,31 @@ FontInfo FontManager::TrueTypeFont::getFontInfo()
assert(m_font != NULL && "TrueTypeFont not initialized" );
FTHolder* holder = (FTHolder*) m_font;
assert(FT_IS_SCALABLE (holder->face));
assert(FT_IS_SCALABLE (holder->m_face));
FT_Size_Metrics metrics = holder->face->size->metrics;
FT_Size_Metrics metrics = holder->m_face->size->metrics;
//todo manage unscalable font
FontInfo outFontInfo;
outFontInfo.scale = 1.0f;
outFontInfo.ascender = metrics.ascender /64.0f;
outFontInfo.descender = metrics.descender /64.0f;
outFontInfo.lineGap = (metrics.height - metrics.ascender + metrics.descender) /64.0f;
outFontInfo.m_scale = 1.0f;
outFontInfo.m_ascender = metrics.ascender /64.0f;
outFontInfo.m_descender = metrics.descender /64.0f;
outFontInfo.m_lineGap = (metrics.height - metrics.ascender + metrics.descender) /64.0f;
outFontInfo.underline_position = FT_MulFix(holder->face->underline_position, metrics.y_scale) /64.0f;
outFontInfo.underline_thickness= FT_MulFix(holder->face->underline_thickness,metrics.y_scale) /64.0f;
outFontInfo.m_underline_position = FT_MulFix(holder->m_face->underline_position, metrics.y_scale) /64.0f;
outFontInfo.m_underline_thickness= FT_MulFix(holder->m_face->underline_thickness,metrics.y_scale) /64.0f;
return outFontInfo;
}
bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t codePoint, GlyphInfo& glyphInfo, uint8_t* outBuffer)
bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{
assert(m_font != NULL && "TrueTypeFont not initialized" );
FTHolder* holder = (FTHolder*) m_font;
glyphInfo.glyphIndex = FT_Get_Char_Index( holder->face, codePoint );
_glyphInfo.m_glyphIndex = FT_Get_Char_Index( holder->m_face, _codePoint );
FT_GlyphSlot slot = holder->face->glyph;
FT_Error error = FT_Load_Glyph( holder->face, glyphInfo.glyphIndex, FT_LOAD_DEFAULT );
FT_GlyphSlot slot = holder->m_face->glyph;
FT_Error error = FT_Load_Glyph( holder->m_face, _glyphInfo.m_glyphIndex, FT_LOAD_DEFAULT );
if(error) { return false; }
FT_Glyph glyph;
@ -196,34 +196,34 @@ bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t codePoint, GlyphInfo&
int w = bitmap->bitmap.width;
int h = bitmap->bitmap.rows;
glyphInfo.offset_x = (float) x;
glyphInfo.offset_y = (float) y;
glyphInfo.width = (float) w;
glyphInfo.height = (float) h;
glyphInfo.advance_x = (float)slot->advance.x /64.0f;
glyphInfo.advance_y = (float)slot->advance.y /64.0f;
_glyphInfo.m_offset_x = (float) x;
_glyphInfo.m_offset_y = (float) y;
_glyphInfo.m_width = (float) w;
_glyphInfo.m_height = (float) h;
_glyphInfo.m_advance_x = (float)slot->advance.x /64.0f;
_glyphInfo.m_advance_y = (float)slot->advance.y /64.0f;
int charsize = 1;
int depth=1;
int stride = bitmap->bitmap.pitch;
for( int i=0; i<h; ++i )
{
memcpy(outBuffer+(i*w) * charsize * depth,
memcpy(_outBuffer+(i*w) * charsize * depth,
bitmap->bitmap.buffer + (i*stride) * charsize, w * charsize * depth );
}
FT_Done_Glyph(glyph);
return true;
}
bool FontManager::TrueTypeFont::bakeGlyphSubpixel(CodePoint_t codePoint, GlyphInfo& glyphInfo, uint8_t* outBuffer)
bool FontManager::TrueTypeFont::bakeGlyphSubpixel(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{
assert(m_font != NULL && "TrueTypeFont not initialized" );
FTHolder* holder = (FTHolder*) m_font;
glyphInfo.glyphIndex = FT_Get_Char_Index( holder->face, codePoint );
_glyphInfo.m_glyphIndex = FT_Get_Char_Index( holder->m_face, _codePoint );
FT_GlyphSlot slot = holder->face->glyph;
FT_Error error = FT_Load_Glyph( holder->face, glyphInfo.glyphIndex, FT_LOAD_DEFAULT );
FT_GlyphSlot slot = holder->m_face->glyph;
FT_Error error = FT_Load_Glyph( holder->m_face, _glyphInfo.m_glyphIndex, FT_LOAD_DEFAULT );
if(error) { return false; }
FT_Glyph glyph;
@ -239,18 +239,18 @@ bool FontManager::TrueTypeFont::bakeGlyphSubpixel(CodePoint_t codePoint, GlyphIn
int w = bitmap->bitmap.width;
int h = bitmap->bitmap.rows;
glyphInfo.offset_x = (float) x;
glyphInfo.offset_y = (float) y;
glyphInfo.width = (float) w;
glyphInfo.height = (float) h;
glyphInfo.advance_x = (float)slot->advance.x /64.0f;
glyphInfo.advance_y = (float)slot->advance.y /64.0f;
_glyphInfo.m_offset_x = (float) x;
_glyphInfo.m_offset_y = (float) y;
_glyphInfo.m_width = (float) w;
_glyphInfo.m_height = (float) h;
_glyphInfo.m_advance_x = (float)slot->advance.x /64.0f;
_glyphInfo.m_advance_y = (float)slot->advance.y /64.0f;
int charsize = 1;
int depth=3;
int stride = bitmap->bitmap.pitch;
for( int i=0; i<h; ++i )
{
memcpy(outBuffer+(i*w) * charsize * depth,
memcpy(_outBuffer+(i*w) * charsize * depth,
bitmap->bitmap.buffer + (i*stride) * charsize, w * charsize * depth );
}
FT_Done_Glyph(glyph);
@ -335,18 +335,18 @@ void make_distance_map( unsigned char *img, unsigned char *outImg, unsigned int
}
bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t codePoint, GlyphInfo& glyphInfo, uint8_t* outBuffer)
bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{
assert(m_font != NULL && "TrueTypeFont not initialized" );
FTHolder* holder = (FTHolder*) m_font;
glyphInfo.glyphIndex = FT_Get_Char_Index( holder->face, codePoint );
_glyphInfo.m_glyphIndex = FT_Get_Char_Index( holder->m_face, _codePoint );
FT_Int32 loadMode = FT_LOAD_DEFAULT|FT_LOAD_NO_HINTING;
FT_Render_Mode renderMode = FT_RENDER_MODE_NORMAL;
FT_GlyphSlot slot = holder->face->glyph;
FT_Error error = FT_Load_Glyph( holder->face, glyphInfo.glyphIndex, loadMode );
FT_GlyphSlot slot = holder->m_face->glyph;
FT_Error error = FT_Load_Glyph( holder->m_face, _glyphInfo.m_glyphIndex, loadMode );
if(error) { return false; }
FT_Glyph glyph;
@ -363,12 +363,12 @@ bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t codePoint, GlyphIn
int w = bitmap->bitmap.width;
int h = bitmap->bitmap.rows;
glyphInfo.offset_x = (float) x;
glyphInfo.offset_y = (float) y;
glyphInfo.width = (float) w;
glyphInfo.height = (float) h;
glyphInfo.advance_x = (float)slot->advance.x /64.0f;
glyphInfo.advance_y = (float)slot->advance.y /64.0f;
_glyphInfo.m_offset_x = (float) x;
_glyphInfo.m_offset_y = (float) y;
_glyphInfo.m_width = (float) w;
_glyphInfo.m_height = (float) h;
_glyphInfo.m_advance_x = (float)slot->advance.x /64.0f;
_glyphInfo.m_advance_y = (float)slot->advance.y /64.0f;
int charsize = 1;
int depth=1;
@ -377,7 +377,7 @@ bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t codePoint, GlyphIn
for( int i=0; i<h; ++i )
{
memcpy(outBuffer+(i*w) * charsize * depth,
memcpy(_outBuffer+(i*w) * charsize * depth,
bitmap->bitmap.buffer + (i*stride) * charsize, w * charsize * depth );
}
FT_Done_Glyph(glyph);
@ -400,16 +400,16 @@ bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t codePoint, GlyphIn
//copy the original buffer to the temp one
for(uint32_t i= dh; i< nh-dh; ++i)
{
memcpy(alphaImg+i*nw+dw, outBuffer+(i-dh)*w, w);
memcpy(alphaImg+i*nw+dw, _outBuffer+(i-dh)*w, w);
}
make_distance_map(alphaImg, outBuffer, nw, nh);
make_distance_map(alphaImg, _outBuffer, nw, nh);
free(alphaImg);
glyphInfo.offset_x -= (float) dw;
glyphInfo.offset_y -= (float) dh;
glyphInfo.width = (float) nw ;
glyphInfo.height = (float) nh;
_glyphInfo.m_offset_x -= (float) dw;
_glyphInfo.m_offset_y -= (float) dh;
_glyphInfo.m_width = (float) nw ;
_glyphInfo.m_height = (float) nh;
}
return true;
@ -423,13 +423,13 @@ typedef stl::unordered_map<CodePoint_t, GlyphInfo> GlyphHash_t;
// cache font data
struct FontManager::CachedFont
{
CachedFont(){ trueTypeFont = NULL; masterFontHandle.idx = -1; }
FontInfo fontInfo;
GlyphHash_t cachedGlyphs;
FontManager::TrueTypeFont* trueTypeFont;
CachedFont(){ m_trueTypeFont = NULL; m_masterFontHandle.idx = -1; }
FontInfo m_fontInfo;
GlyphHash_t m_cachedGlyphs;
FontManager::TrueTypeFont* m_trueTypeFont;
// an handle to a master font in case of sub distance field font
FontHandle masterFontHandle;
int16_t __padding__;
FontHandle m_masterFontHandle;
int16_t m_padding;
};
@ -439,16 +439,16 @@ const uint16_t MAX_OPENED_FILES = 64;
const uint16_t MAX_OPENED_FONT = 64;
const uint32_t MAX_FONT_BUFFER_SIZE = 512*512*4;
FontManager::FontManager(Atlas* atlas):m_filesHandles(MAX_OPENED_FILES), m_fontHandles(MAX_OPENED_FONT)
FontManager::FontManager(Atlas* _atlas):m_filesHandles(MAX_OPENED_FILES), m_fontHandles(MAX_OPENED_FONT)
{
m_atlas = atlas;
m_atlas = _atlas;
m_ownAtlas = false;
init();
}
FontManager::FontManager(uint32_t textureSideWidth):m_filesHandles(MAX_OPENED_FILES), m_fontHandles(MAX_OPENED_FONT)
FontManager::FontManager(uint32_t _textureSideWidth):m_filesHandles(MAX_OPENED_FILES), m_fontHandles(MAX_OPENED_FONT)
{
m_atlas = new Atlas(textureSideWidth);
m_atlas = new Atlas(_textureSideWidth);
m_ownAtlas = true;
init();
}
@ -463,8 +463,8 @@ void FontManager::init()
uint8_t buffer[4*4*4];
memset( buffer, 255, 4 * 4 * 4);
m_blackGlyph.width=3;
m_blackGlyph.height=3;
m_blackGlyph.m_width=3;
m_blackGlyph.m_height=3;
assert( addBitmap(m_blackGlyph, buffer) );
//make sure the black glyph doesn't bleed
@ -494,10 +494,10 @@ FontManager::~FontManager()
TrueTypeHandle FontManager::loadTrueTypeFromFile(const char* fontPath)
TrueTypeHandle FontManager::loadTrueTypeFromFile(const char* _fontPath)
{
FILE * pFile;
pFile = fopen (fontPath, "rb");
pFile = fopen (_fontPath, "rb");
if (pFile==NULL)
{
TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
@ -544,34 +544,34 @@ TrueTypeHandle FontManager::loadTrueTypeFromFile(const char* fontPath)
return invalid;
}
TrueTypeHandle FontManager::loadTrueTypeFromMemory(const uint8_t* buffer, uint32_t size)
TrueTypeHandle FontManager::loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size)
{
uint16_t id = m_filesHandles.alloc();
assert(id != bx::HandleAlloc::invalid);
m_cachedFiles[id].buffer = new uint8_t[size];
m_cachedFiles[id].bufferSize = size;
memcpy(m_cachedFiles[id].buffer, buffer, size);
m_cachedFiles[id].buffer = new uint8_t[_size];
m_cachedFiles[id].bufferSize = _size;
memcpy(m_cachedFiles[id].buffer, _buffer, _size);
//TODO validate font
TrueTypeHandle ret = {id};
return ret;
}
void FontManager::unloadTrueType(TrueTypeHandle handle)
void FontManager::unloadTrueType(TrueTypeHandle _handle)
{
assert(bgfx::invalidHandle != handle.idx);
delete m_cachedFiles[handle.idx].buffer;
m_cachedFiles[handle.idx].bufferSize = 0;
m_cachedFiles[handle.idx].buffer = NULL;
m_filesHandles.free(handle.idx);
assert(bgfx::invalidHandle != _handle.idx);
delete m_cachedFiles[_handle.idx].buffer;
m_cachedFiles[_handle.idx].bufferSize = 0;
m_cachedFiles[_handle.idx].buffer = NULL;
m_filesHandles.free(_handle.idx);
}
FontHandle FontManager::createFontByPixelSize(TrueTypeHandle handle, uint32_t typefaceIndex, uint32_t pixelSize, FontType fontType)
FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _tt_handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType)
{
assert(bgfx::invalidHandle != handle.idx);
assert(bgfx::invalidHandle != _tt_handle.idx);
TrueTypeFont* ttf = new TrueTypeFont();
if(!ttf->init( m_cachedFiles[handle.idx].buffer, m_cachedFiles[handle.idx].bufferSize, typefaceIndex, pixelSize))
if(!ttf->init( m_cachedFiles[_tt_handle.idx].buffer, m_cachedFiles[_tt_handle.idx].bufferSize, _typefaceIndex, _pixelSize))
{
delete ttf;
FontHandle invalid = BGFX_INVALID_HANDLE;
@ -581,12 +581,12 @@ FontHandle FontManager::createFontByPixelSize(TrueTypeHandle handle, uint32_t ty
uint16_t fontIdx = m_fontHandles.alloc();
assert(fontIdx != bx::HandleAlloc::invalid);
m_cachedFonts[fontIdx].trueTypeFont = ttf;
m_cachedFonts[fontIdx].fontInfo = ttf->getFontInfo();
m_cachedFonts[fontIdx].fontInfo.fontType = fontType;
m_cachedFonts[fontIdx].fontInfo.pixelSize = pixelSize;
m_cachedFonts[fontIdx].cachedGlyphs.clear();
m_cachedFonts[fontIdx].masterFontHandle.idx = -1;
m_cachedFonts[fontIdx].m_trueTypeFont = ttf;
m_cachedFonts[fontIdx].m_fontInfo = ttf->getFontInfo();
m_cachedFonts[fontIdx].m_fontInfo.m_fontType = _fontType;
m_cachedFonts[fontIdx].m_fontInfo.m_pixelSize = _pixelSize;
m_cachedFonts[fontIdx].m_cachedGlyphs.clear();
m_cachedFonts[fontIdx].m_masterFontHandle.idx = -1;
FontHandle ret = {fontIdx};
return ret;
}
@ -595,24 +595,24 @@ FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle,
{
assert(bgfx::invalidHandle != _baseFontHandle.idx);
CachedFont& font = m_cachedFonts[_baseFontHandle.idx];
FontInfo& fontInfo = font.fontInfo;
FontInfo& fontInfo = font.m_fontInfo;
FontInfo newFontInfo = fontInfo;
newFontInfo.pixelSize = _pixelSize;
newFontInfo.scale = (float)_pixelSize / (float) fontInfo.pixelSize;
newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale);
newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale);
newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale);
newFontInfo.underline_thickness = (newFontInfo.underline_thickness * newFontInfo.scale);
newFontInfo.underline_position = (newFontInfo.underline_position * newFontInfo.scale);
newFontInfo.m_pixelSize = _pixelSize;
newFontInfo.m_scale = (float)_pixelSize / (float) fontInfo.m_pixelSize;
newFontInfo.m_ascender = (newFontInfo.m_ascender * newFontInfo.m_scale);
newFontInfo.m_descender = (newFontInfo.m_descender * newFontInfo.m_scale);
newFontInfo.m_lineGap = (newFontInfo.m_lineGap * newFontInfo.m_scale);
newFontInfo.m_underline_thickness = (newFontInfo.m_underline_thickness * newFontInfo.m_scale);
newFontInfo.m_underline_position = (newFontInfo.m_underline_position * newFontInfo.m_scale);
uint16_t fontIdx = m_fontHandles.alloc();
assert(fontIdx != bx::HandleAlloc::invalid);
m_cachedFonts[fontIdx].cachedGlyphs.clear();
m_cachedFonts[fontIdx].fontInfo = newFontInfo;
m_cachedFonts[fontIdx].trueTypeFont = NULL;
m_cachedFonts[fontIdx].masterFontHandle = _baseFontHandle;
m_cachedFonts[fontIdx].m_cachedGlyphs.clear();
m_cachedFonts[fontIdx].m_fontInfo = newFontInfo;
m_cachedFonts[fontIdx].m_trueTypeFont = NULL;
m_cachedFonts[fontIdx].m_masterFontHandle = _baseFontHandle;
FontHandle ret = {fontIdx};
return ret;
}
@ -635,29 +635,29 @@ void FontManager::destroyFont(FontHandle _handle)
{
assert(bgfx::invalidHandle != _handle.idx);
if(m_cachedFonts[_handle.idx].trueTypeFont != NULL)
if(m_cachedFonts[_handle.idx].m_trueTypeFont != NULL)
{
delete m_cachedFonts[_handle.idx].trueTypeFont;
m_cachedFonts[_handle.idx].trueTypeFont = NULL;
delete m_cachedFonts[_handle.idx].m_trueTypeFont;
m_cachedFonts[_handle.idx].m_trueTypeFont = NULL;
}
m_cachedFonts[_handle.idx].cachedGlyphs.clear();
m_cachedFonts[_handle.idx].m_cachedGlyphs.clear();
m_fontHandles.free(_handle.idx);
}
bool FontManager::preloadGlyph(FontHandle handle, const wchar_t* _string)
bool FontManager::preloadGlyph(FontHandle _handle, const wchar_t* _string)
{
assert(bgfx::invalidHandle != handle.idx);
CachedFont& font = m_cachedFonts[handle.idx];
assert(bgfx::invalidHandle != _handle.idx);
CachedFont& font = m_cachedFonts[_handle.idx];
//if truetype present
if(font.trueTypeFont != NULL)
if(font.m_trueTypeFont != NULL)
{
//parse string
for( size_t i=0, end = wcslen(_string) ; i < end; ++i )
{
//if glyph cached, continue
CodePoint_t codePoint = _string[i];
if(!preloadGlyph(handle, codePoint))
if(!preloadGlyph(_handle, codePoint))
{
return false;
}
@ -668,37 +668,37 @@ bool FontManager::preloadGlyph(FontHandle handle, const wchar_t* _string)
return false;
}
bool FontManager::preloadGlyph(FontHandle handle, CodePoint_t codePoint)
bool FontManager::preloadGlyph(FontHandle _handle, CodePoint_t _codePoint)
{
assert(bgfx::invalidHandle != handle.idx);
CachedFont& font = m_cachedFonts[handle.idx];
FontInfo& fontInfo = font.fontInfo;
assert(bgfx::invalidHandle != _handle.idx);
CachedFont& font = m_cachedFonts[_handle.idx];
FontInfo& fontInfo = font.m_fontInfo;
//check if glyph not already present
GlyphHash_t::iterator iter = font.cachedGlyphs.find(codePoint);
if(iter != font.cachedGlyphs.end())
GlyphHash_t::iterator iter = font.m_cachedGlyphs.find(_codePoint);
if(iter != font.m_cachedGlyphs.end())
{
return true;
}
//if truetype present
if(font.trueTypeFont != NULL)
if(font.m_trueTypeFont != NULL)
{
GlyphInfo glyphInfo;
//bake glyph as bitmap to buffer
switch(font.fontInfo.fontType)
switch(font.m_fontInfo.m_fontType)
{
case FONT_TYPE_ALPHA:
font.trueTypeFont->bakeGlyphAlpha(codePoint, glyphInfo, m_buffer);
font.m_trueTypeFont->bakeGlyphAlpha(_codePoint, glyphInfo, m_buffer);
break;
//case FONT_TYPE_LCD:
//font.trueTypeFont->bakeGlyphSubpixel(codePoint, glyphInfo, m_buffer);
//font.m_trueTypeFont->bakeGlyphSubpixel(codePoint, glyphInfo, m_buffer);
//break;
case FONT_TYPE_DISTANCE:
font.trueTypeFont->bakeGlyphDistance(codePoint, glyphInfo, m_buffer);
font.m_trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
break;
case FONT_TYPE_DISTANCE_SUBPIXEL:
font.trueTypeFont->bakeGlyphDistance(codePoint, glyphInfo, m_buffer);
font.m_trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
break;
default:
assert(false && "TextureType not supported yet");
@ -710,35 +710,35 @@ bool FontManager::preloadGlyph(FontHandle handle, CodePoint_t codePoint)
return false;
}
glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
glyphInfo.height = (glyphInfo.height * fontInfo.scale);
glyphInfo.width = (glyphInfo.width * fontInfo.scale);
glyphInfo.m_advance_x = (glyphInfo.m_advance_x * fontInfo.m_scale);
glyphInfo.m_advance_y = (glyphInfo.m_advance_y * fontInfo.m_scale);
glyphInfo.m_offset_x = (glyphInfo.m_offset_x * fontInfo.m_scale);
glyphInfo.m_offset_y = (glyphInfo.m_offset_y * fontInfo.m_scale);
glyphInfo.m_height = (glyphInfo.m_height * fontInfo.m_scale);
glyphInfo.m_width = (glyphInfo.m_width * fontInfo.m_scale);
// store cached glyph
font.cachedGlyphs[codePoint] = glyphInfo;
font.m_cachedGlyphs[_codePoint] = glyphInfo;
return true;
}else
{
//retrieve glyph from parent font if any
if(font.masterFontHandle.idx != bgfx::invalidHandle)
if(font.m_masterFontHandle.idx != bgfx::invalidHandle)
{
if(preloadGlyph(font.masterFontHandle, codePoint))
if(preloadGlyph(font.m_masterFontHandle, _codePoint))
{
GlyphInfo glyphInfo;
getGlyphInfo(font.masterFontHandle, codePoint, glyphInfo);
getGlyphInfo(font.m_masterFontHandle, _codePoint, glyphInfo);
glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
glyphInfo.height = (glyphInfo.height * fontInfo.scale);
glyphInfo.width = (glyphInfo.width * fontInfo.scale);
glyphInfo.m_advance_x = (glyphInfo.m_advance_x * fontInfo.m_scale);
glyphInfo.m_advance_y = (glyphInfo.m_advance_y * fontInfo.m_scale);
glyphInfo.m_offset_x = (glyphInfo.m_offset_x * fontInfo.m_scale);
glyphInfo.m_offset_y = (glyphInfo.m_offset_y * fontInfo.m_scale);
glyphInfo.m_height = (glyphInfo.m_height * fontInfo.m_scale);
glyphInfo.m_width = (glyphInfo.m_width * fontInfo.m_scale);
// store cached glyph
font.cachedGlyphs[codePoint] = glyphInfo;
font.m_cachedGlyphs[_codePoint] = glyphInfo;
return true;
}
}
@ -747,34 +747,34 @@ bool FontManager::preloadGlyph(FontHandle handle, CodePoint_t codePoint)
return false;
}
const FontInfo& FontManager::getFontInfo(FontHandle handle)
const FontInfo& FontManager::getFontInfo(FontHandle _handle)
{
assert(handle.idx != bgfx::invalidHandle);
return m_cachedFonts[handle.idx].fontInfo;
assert(_handle.idx != bgfx::invalidHandle);
return m_cachedFonts[_handle.idx].m_fontInfo;
}
bool FontManager::getGlyphInfo(FontHandle fontHandle, CodePoint_t codePoint, GlyphInfo& outInfo)
bool FontManager::getGlyphInfo(FontHandle _handle, CodePoint_t _codePoint, GlyphInfo& _outInfo)
{
GlyphHash_t::iterator iter = m_cachedFonts[fontHandle.idx].cachedGlyphs.find(codePoint);
if(iter == m_cachedFonts[fontHandle.idx].cachedGlyphs.end())
GlyphHash_t::iterator iter = m_cachedFonts[_handle.idx].m_cachedGlyphs.find(_codePoint);
if(iter == m_cachedFonts[_handle.idx].m_cachedGlyphs.end())
{
if(preloadGlyph(fontHandle, codePoint))
if(preloadGlyph(_handle, _codePoint))
{
iter = m_cachedFonts[fontHandle.idx].cachedGlyphs.find(codePoint);
iter = m_cachedFonts[_handle.idx].m_cachedGlyphs.find(_codePoint);
}else
{
return false;
}
}
outInfo = iter->second;
_outInfo = iter->second;
return true;
}
// ****************************************************************************
bool FontManager::addBitmap(GlyphInfo& glyphInfo, const uint8_t* data)
bool FontManager::addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data)
{
glyphInfo.regionIndex = m_atlas->addRegion((uint16_t) ceil(glyphInfo.width),(uint16_t) ceil(glyphInfo.height), data, AtlasRegion::TYPE_GRAY);
_glyphInfo.m_regionIndex = m_atlas->addRegion((uint16_t) ceil(_glyphInfo.m_width),(uint16_t) ceil(_glyphInfo.m_height), _data, AtlasRegion::TYPE_GRAY);
return true;
}

View file

@ -18,23 +18,23 @@ enum FontType
struct FontInfo
{
//the font height in pixel
uint16_t pixelSize;
uint16_t m_pixelSize;
/// Rendering type used for the font
int16_t fontType;
int16_t m_fontType;
/// The pixel extents above the baseline in pixels (typically positive)
float ascender;
float m_ascender;
/// The extents below the baseline in pixels (typically negative)
float descender;
float m_descender;
/// The spacing in pixels between one row's descent and the next row's ascent
float lineGap;
float m_lineGap;
/// The thickness of the under/hover/striketrough line in pixels
float underline_thickness;
float m_underline_thickness;
/// The position of the underline relatively to the baseline
float underline_position;
float m_underline_position;
//scale to apply to glyph data
float scale;
float m_scale;
};
// Glyph metrics:
@ -75,34 +75,34 @@ typedef int32_t CodePoint_t;
struct GlyphInfo
{
/// Index for faster retrieval
int32_t glyphIndex;
int32_t m_glyphIndex;
/// Glyph's width in pixels.
float width;
float m_width;
/// Glyph's height in pixels.
float height;
float m_height;
/// Glyph's left offset in pixels
float offset_x;
float m_offset_x;
/// Glyph's top offset in pixels
/// Remember that this is the distance from the baseline to the top-most
/// glyph scan line, upwards y coordinates being positive.
float offset_y;
float m_offset_y;
/// For horizontal text layouts, this is the unscaled horizontal distance in pixels
/// used to increment the pen position when the glyph is drawn as part of a string of text.
float advance_x;
float m_advance_x;
/// For vertical text layouts, this is the unscaled vertical distance in pixels
/// used to increment the pen position when the glyph is drawn as part of a string of text.
float advance_y;
float m_advance_y;
/// region index in the atlas storing textures
uint16_t regionIndex;
uint16_t m_regionIndex;
///32 bits alignment
int16_t padding;
int16_t m_padding;
};
BGFX_HANDLE(TrueTypeHandle);
@ -112,9 +112,9 @@ class FontManager
{
public:
/// create the font manager using an external cube atlas (doesn't take ownership of the atlas)
FontManager(Atlas* atlas);
FontManager(Atlas* _atlas);
/// create the font manager and create the texture cube as BGRA8 with linear filtering
FontManager(uint32_t textureSideWidth = 512);
FontManager(uint32_t _textureSideWidth = 512);
~FontManager();
@ -123,29 +123,29 @@ public:
/// load a TrueType font from a file path
/// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromFile(const char* fontPath);
TrueTypeHandle loadTrueTypeFromFile(const char* _fontPath);
/// load a TrueType font from a given buffer.
/// the buffer is copied and thus can be freed or reused after this call
/// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromMemory(const uint8_t* buffer, uint32_t size);
TrueTypeHandle loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size);
/// unload a TrueType font (free font memory) but keep loaded glyphs
void unloadTrueType(TrueTypeHandle handle);
void unloadTrueType(TrueTypeHandle _handle);
/// return a font whose height is a fixed pixel size
FontHandle createFontByPixelSize(TrueTypeHandle handle, uint32_t typefaceIndex, uint32_t pixelSize, FontType fontType = FONT_TYPE_ALPHA);
FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType = FONT_TYPE_ALPHA);
/// return a scaled child font whose height is a fixed pixel size
FontHandle createScaledFontToPixelSize(FontHandle baseFontHandle, uint32_t pixelSize);
FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
/// load a baked font (the set of glyph is fixed)
/// @return INVALID_HANDLE if the loading fail
FontHandle loadBakedFontFromFile(const char* imagePath, const char* descriptorPath);
FontHandle loadBakedFontFromFile(const char* _imagePath, const char* _descriptorPath);
/// load a baked font (the set of glyph is fixed)
/// @return INVALID_HANDLE if the loading fail
FontHandle loadBakedFontFromMemory(const uint8_t* imageBuffer, uint32_t imageSize, const uint8_t* descriptorBuffer, uint32_t descriptorSize);
FontHandle loadBakedFontFromMemory(const uint8_t* _imageBuffer, uint32_t _imageSize, const uint8_t* _descriptorBuffer, uint32_t _descriptorSize);
/// destroy a font (truetype or baked)
void destroyFont(FontHandle _handle);
@ -153,23 +153,23 @@ public:
/// Preload a set of glyphs from a TrueType file
/// @return true if every glyph could be preloaded, false otherwise
/// if the Font is a baked font, this only do validation on the characters
bool preloadGlyph(FontHandle handle, const wchar_t* _string);
bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
/// Preload a single glyph, return true on success
bool preloadGlyph(FontHandle handle, CodePoint_t character);
bool preloadGlyph(FontHandle _handle, CodePoint_t _character);
/// bake a font to disk (the set of preloaded glyph)
/// @return true if the baking succeed, false otherwise
bool saveBakedFont(FontHandle handle, const char* fontDirectory, const char* fontName );
bool saveBakedFont(FontHandle _handle, const char* _fontDirectory, const char* _fontName );
/// return the font descriptor of a font
/// @remark the handle is required to be valid
const FontInfo& getFontInfo(FontHandle handle);
const FontInfo& getFontInfo(FontHandle _handle);
/// Return the rendering informations about the glyph region
/// Load the glyph from a TrueType font if possible
/// @return true if the Glyph is available
bool getGlyphInfo(FontHandle fontHandle, CodePoint_t codePoint, GlyphInfo& outInfo);
bool getGlyphInfo(FontHandle _handle, CodePoint_t _codePoint, GlyphInfo& _outInfo);
GlyphInfo& getBlackGlyph(){ return m_blackGlyph; }
@ -184,10 +184,10 @@ private:
};
void init();
bool addBitmap(GlyphInfo& glyphInfo, const uint8_t* data);
bool addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data);
bool m_ownAtlas;
(Atlas* m_atlas;
Atlas* m_atlas;
bx::HandleAlloc m_fontHandles;
CachedFont* m_cachedFonts;